Add qdf24xx base tuning support.
[official-gcc.git] / gcc / cp / parser.c
blob3e8270e1e6acb0629a494864f1d36a5f0cd1acc3
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2016 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 "omp-low.h"
39 #include "gomp-constants.h"
40 #include "c-family/c-indentation.h"
41 #include "context.h"
42 #include "cp-cilkplus.h"
45 /* The lexer. */
47 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
48 and c-lex.c) and the C++ parser. */
50 static cp_token eof_token =
52 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
55 /* The various kinds of non integral constant we encounter. */
56 enum non_integral_constant {
57 NIC_NONE,
58 /* floating-point literal */
59 NIC_FLOAT,
60 /* %<this%> */
61 NIC_THIS,
62 /* %<__FUNCTION__%> */
63 NIC_FUNC_NAME,
64 /* %<__PRETTY_FUNCTION__%> */
65 NIC_PRETTY_FUNC,
66 /* %<__func__%> */
67 NIC_C99_FUNC,
68 /* "%<va_arg%> */
69 NIC_VA_ARG,
70 /* a cast */
71 NIC_CAST,
72 /* %<typeid%> operator */
73 NIC_TYPEID,
74 /* non-constant compound literals */
75 NIC_NCC,
76 /* a function call */
77 NIC_FUNC_CALL,
78 /* an increment */
79 NIC_INC,
80 /* an decrement */
81 NIC_DEC,
82 /* an array reference */
83 NIC_ARRAY_REF,
84 /* %<->%> */
85 NIC_ARROW,
86 /* %<.%> */
87 NIC_POINT,
88 /* the address of a label */
89 NIC_ADDR_LABEL,
90 /* %<*%> */
91 NIC_STAR,
92 /* %<&%> */
93 NIC_ADDR,
94 /* %<++%> */
95 NIC_PREINCREMENT,
96 /* %<--%> */
97 NIC_PREDECREMENT,
98 /* %<new%> */
99 NIC_NEW,
100 /* %<delete%> */
101 NIC_DEL,
102 /* calls to overloaded operators */
103 NIC_OVERLOADED,
104 /* an assignment */
105 NIC_ASSIGNMENT,
106 /* a comma operator */
107 NIC_COMMA,
108 /* a call to a constructor */
109 NIC_CONSTRUCTOR,
110 /* a transaction expression */
111 NIC_TRANSACTION
114 /* The various kinds of errors about name-lookup failing. */
115 enum name_lookup_error {
116 /* NULL */
117 NLE_NULL,
118 /* is not a type */
119 NLE_TYPE,
120 /* is not a class or namespace */
121 NLE_CXX98,
122 /* is not a class, namespace, or enumeration */
123 NLE_NOT_CXX98
126 /* The various kinds of required token */
127 enum required_token {
128 RT_NONE,
129 RT_SEMICOLON, /* ';' */
130 RT_OPEN_PAREN, /* '(' */
131 RT_CLOSE_BRACE, /* '}' */
132 RT_OPEN_BRACE, /* '{' */
133 RT_CLOSE_SQUARE, /* ']' */
134 RT_OPEN_SQUARE, /* '[' */
135 RT_COMMA, /* ',' */
136 RT_SCOPE, /* '::' */
137 RT_LESS, /* '<' */
138 RT_GREATER, /* '>' */
139 RT_EQ, /* '=' */
140 RT_ELLIPSIS, /* '...' */
141 RT_MULT, /* '*' */
142 RT_COMPL, /* '~' */
143 RT_COLON, /* ':' */
144 RT_COLON_SCOPE, /* ':' or '::' */
145 RT_CLOSE_PAREN, /* ')' */
146 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
147 RT_PRAGMA_EOL, /* end of line */
148 RT_NAME, /* identifier */
150 /* The type is CPP_KEYWORD */
151 RT_NEW, /* new */
152 RT_DELETE, /* delete */
153 RT_RETURN, /* return */
154 RT_WHILE, /* while */
155 RT_EXTERN, /* extern */
156 RT_STATIC_ASSERT, /* static_assert */
157 RT_DECLTYPE, /* decltype */
158 RT_OPERATOR, /* operator */
159 RT_CLASS, /* class */
160 RT_TEMPLATE, /* template */
161 RT_NAMESPACE, /* namespace */
162 RT_USING, /* using */
163 RT_ASM, /* asm */
164 RT_TRY, /* try */
165 RT_CATCH, /* catch */
166 RT_THROW, /* throw */
167 RT_LABEL, /* __label__ */
168 RT_AT_TRY, /* @try */
169 RT_AT_SYNCHRONIZED, /* @synchronized */
170 RT_AT_THROW, /* @throw */
172 RT_SELECT, /* selection-statement */
173 RT_INTERATION, /* iteration-statement */
174 RT_JUMP, /* jump-statement */
175 RT_CLASS_KEY, /* class-key */
176 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
177 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
178 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
179 RT_TRANSACTION_CANCEL /* __transaction_cancel */
182 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
183 reverting it on destruction. */
185 class type_id_in_expr_sentinel
187 cp_parser *parser;
188 bool saved;
189 public:
190 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
191 : parser (parser),
192 saved (parser->in_type_id_in_expr_p)
193 { parser->in_type_id_in_expr_p = set; }
194 ~type_id_in_expr_sentinel ()
195 { parser->in_type_id_in_expr_p = saved; }
198 /* Prototypes. */
200 static cp_lexer *cp_lexer_new_main
201 (void);
202 static cp_lexer *cp_lexer_new_from_tokens
203 (cp_token_cache *tokens);
204 static void cp_lexer_destroy
205 (cp_lexer *);
206 static int cp_lexer_saving_tokens
207 (const cp_lexer *);
208 static cp_token *cp_lexer_token_at
209 (cp_lexer *, cp_token_position);
210 static void cp_lexer_get_preprocessor_token
211 (cp_lexer *, cp_token *);
212 static inline cp_token *cp_lexer_peek_token
213 (cp_lexer *);
214 static cp_token *cp_lexer_peek_nth_token
215 (cp_lexer *, size_t);
216 static inline bool cp_lexer_next_token_is
217 (cp_lexer *, enum cpp_ttype);
218 static bool cp_lexer_next_token_is_not
219 (cp_lexer *, enum cpp_ttype);
220 static bool cp_lexer_next_token_is_keyword
221 (cp_lexer *, enum rid);
222 static cp_token *cp_lexer_consume_token
223 (cp_lexer *);
224 static void cp_lexer_purge_token
225 (cp_lexer *);
226 static void cp_lexer_purge_tokens_after
227 (cp_lexer *, cp_token_position);
228 static void cp_lexer_save_tokens
229 (cp_lexer *);
230 static void cp_lexer_commit_tokens
231 (cp_lexer *);
232 static void cp_lexer_rollback_tokens
233 (cp_lexer *);
234 static void cp_lexer_print_token
235 (FILE *, cp_token *);
236 static inline bool cp_lexer_debugging_p
237 (cp_lexer *);
238 static void cp_lexer_start_debugging
239 (cp_lexer *) ATTRIBUTE_UNUSED;
240 static void cp_lexer_stop_debugging
241 (cp_lexer *) ATTRIBUTE_UNUSED;
243 static cp_token_cache *cp_token_cache_new
244 (cp_token *, cp_token *);
246 static void cp_parser_initial_pragma
247 (cp_token *);
249 static tree cp_literal_operator_id
250 (const char *);
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 != lexer->buffer->address ());
767 tp--;
770 return cp_lexer_token_at (lexer, tp);
773 /* nonzero if we are presently saving tokens. */
775 static inline int
776 cp_lexer_saving_tokens (const cp_lexer* lexer)
778 return lexer->saved_tokens.length () != 0;
781 /* Store the next token from the preprocessor in *TOKEN. Return true
782 if we reach EOF. If LEXER is NULL, assume we are handling an
783 initial #pragma pch_preprocess, and thus want the lexer to return
784 processed strings. */
786 static void
787 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
789 static int is_extern_c = 0;
791 /* Get a new token from the preprocessor. */
792 token->type
793 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
794 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
795 token->keyword = RID_MAX;
796 token->purged_p = false;
797 token->error_reported = false;
799 /* On some systems, some header files are surrounded by an
800 implicit extern "C" block. Set a flag in the token if it
801 comes from such a header. */
802 is_extern_c += pending_lang_change;
803 pending_lang_change = 0;
804 token->implicit_extern_c = is_extern_c > 0;
806 /* Check to see if this token is a keyword. */
807 if (token->type == CPP_NAME)
809 if (C_IS_RESERVED_WORD (token->u.value))
811 /* Mark this token as a keyword. */
812 token->type = CPP_KEYWORD;
813 /* Record which keyword. */
814 token->keyword = C_RID_CODE (token->u.value);
816 else
818 if (warn_cxx11_compat
819 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
820 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
822 /* Warn about the C++0x keyword (but still treat it as
823 an identifier). */
824 warning (OPT_Wc__11_compat,
825 "identifier %qE is a keyword in C++11",
826 token->u.value);
828 /* Clear out the C_RID_CODE so we don't warn about this
829 particular identifier-turned-keyword again. */
830 C_SET_RID_CODE (token->u.value, RID_MAX);
833 token->keyword = RID_MAX;
836 else if (token->type == CPP_AT_NAME)
838 /* This only happens in Objective-C++; it must be a keyword. */
839 token->type = CPP_KEYWORD;
840 switch (C_RID_CODE (token->u.value))
842 /* Replace 'class' with '@class', 'private' with '@private',
843 etc. This prevents confusion with the C++ keyword
844 'class', and makes the tokens consistent with other
845 Objective-C 'AT' keywords. For example '@class' is
846 reported as RID_AT_CLASS which is consistent with
847 '@synchronized', which is reported as
848 RID_AT_SYNCHRONIZED.
850 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
851 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
852 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
853 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
854 case RID_THROW: token->keyword = RID_AT_THROW; break;
855 case RID_TRY: token->keyword = RID_AT_TRY; break;
856 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
857 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
858 default: token->keyword = C_RID_CODE (token->u.value);
863 /* Update the globals input_location and the input file stack from TOKEN. */
864 static inline void
865 cp_lexer_set_source_position_from_token (cp_token *token)
867 if (token->type != CPP_EOF)
869 input_location = token->location;
873 /* Update the globals input_location and the input file stack from LEXER. */
874 static inline void
875 cp_lexer_set_source_position (cp_lexer *lexer)
877 cp_token *token = cp_lexer_peek_token (lexer);
878 cp_lexer_set_source_position_from_token (token);
881 /* Return a pointer to the next token in the token stream, but do not
882 consume it. */
884 static inline cp_token *
885 cp_lexer_peek_token (cp_lexer *lexer)
887 if (cp_lexer_debugging_p (lexer))
889 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
890 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
891 putc ('\n', cp_lexer_debug_stream);
893 return lexer->next_token;
896 /* Return true if the next token has the indicated TYPE. */
898 static inline bool
899 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
901 return cp_lexer_peek_token (lexer)->type == type;
904 /* Return true if the next token does not have the indicated TYPE. */
906 static inline bool
907 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
909 return !cp_lexer_next_token_is (lexer, type);
912 /* Return true if the next token is the indicated KEYWORD. */
914 static inline bool
915 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
917 return cp_lexer_peek_token (lexer)->keyword == keyword;
920 static inline bool
921 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
923 return cp_lexer_peek_nth_token (lexer, n)->type == type;
926 static inline bool
927 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
929 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
932 /* Return true if the next token is not the indicated KEYWORD. */
934 static inline bool
935 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
937 return cp_lexer_peek_token (lexer)->keyword != keyword;
940 /* Return true if the next token is a keyword for a decl-specifier. */
942 static bool
943 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
945 cp_token *token;
947 token = cp_lexer_peek_token (lexer);
948 switch (token->keyword)
950 /* auto specifier: storage-class-specifier in C++,
951 simple-type-specifier in C++0x. */
952 case RID_AUTO:
953 /* Storage classes. */
954 case RID_REGISTER:
955 case RID_STATIC:
956 case RID_EXTERN:
957 case RID_MUTABLE:
958 case RID_THREAD:
959 /* Elaborated type specifiers. */
960 case RID_ENUM:
961 case RID_CLASS:
962 case RID_STRUCT:
963 case RID_UNION:
964 case RID_TYPENAME:
965 /* Simple type specifiers. */
966 case RID_CHAR:
967 case RID_CHAR16:
968 case RID_CHAR32:
969 case RID_WCHAR:
970 case RID_BOOL:
971 case RID_SHORT:
972 case RID_INT:
973 case RID_LONG:
974 case RID_SIGNED:
975 case RID_UNSIGNED:
976 case RID_FLOAT:
977 case RID_DOUBLE:
978 case RID_VOID:
979 /* GNU extensions. */
980 case RID_ATTRIBUTE:
981 case RID_TYPEOF:
982 /* C++0x extensions. */
983 case RID_DECLTYPE:
984 case RID_UNDERLYING_TYPE:
985 return true;
987 default:
988 if (token->keyword >= RID_FIRST_INT_N
989 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
990 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
991 return true;
992 return false;
996 /* Returns TRUE iff the token T begins a decltype type. */
998 static bool
999 token_is_decltype (cp_token *t)
1001 return (t->keyword == RID_DECLTYPE
1002 || t->type == CPP_DECLTYPE);
1005 /* Returns TRUE iff the next token begins a decltype type. */
1007 static bool
1008 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1010 cp_token *t = cp_lexer_peek_token (lexer);
1011 return token_is_decltype (t);
1014 /* Called when processing a token with tree_check_value; perform or defer the
1015 associated checks and return the value. */
1017 static tree
1018 saved_checks_value (struct tree_check *check_value)
1020 /* Perform any access checks that were deferred. */
1021 vec<deferred_access_check, va_gc> *checks;
1022 deferred_access_check *chk;
1023 checks = check_value->checks;
1024 if (checks)
1026 int i;
1027 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1028 perform_or_defer_access_check (chk->binfo,
1029 chk->decl,
1030 chk->diag_decl, tf_warning_or_error);
1032 /* Return the stored value. */
1033 return check_value->value;
1036 /* Return a pointer to the Nth token in the token stream. If N is 1,
1037 then this is precisely equivalent to cp_lexer_peek_token (except
1038 that it is not inline). One would like to disallow that case, but
1039 there is one case (cp_parser_nth_token_starts_template_id) where
1040 the caller passes a variable for N and it might be 1. */
1042 static cp_token *
1043 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1045 cp_token *token;
1047 /* N is 1-based, not zero-based. */
1048 gcc_assert (n > 0);
1050 if (cp_lexer_debugging_p (lexer))
1051 fprintf (cp_lexer_debug_stream,
1052 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1054 --n;
1055 token = lexer->next_token;
1056 gcc_assert (!n || token != &eof_token);
1057 while (n != 0)
1059 ++token;
1060 if (token == lexer->last_token)
1062 token = &eof_token;
1063 break;
1066 if (!token->purged_p)
1067 --n;
1070 if (cp_lexer_debugging_p (lexer))
1072 cp_lexer_print_token (cp_lexer_debug_stream, token);
1073 putc ('\n', cp_lexer_debug_stream);
1076 return token;
1079 /* Return the next token, and advance the lexer's next_token pointer
1080 to point to the next non-purged token. */
1082 static cp_token *
1083 cp_lexer_consume_token (cp_lexer* lexer)
1085 cp_token *token = lexer->next_token;
1087 gcc_assert (token != &eof_token);
1088 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1092 lexer->next_token++;
1093 if (lexer->next_token == lexer->last_token)
1095 lexer->next_token = &eof_token;
1096 break;
1100 while (lexer->next_token->purged_p);
1102 cp_lexer_set_source_position_from_token (token);
1104 /* Provide debugging output. */
1105 if (cp_lexer_debugging_p (lexer))
1107 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1108 cp_lexer_print_token (cp_lexer_debug_stream, token);
1109 putc ('\n', cp_lexer_debug_stream);
1112 return token;
1115 /* Permanently remove the next token from the token stream, and
1116 advance the next_token pointer to refer to the next non-purged
1117 token. */
1119 static void
1120 cp_lexer_purge_token (cp_lexer *lexer)
1122 cp_token *tok = lexer->next_token;
1124 gcc_assert (tok != &eof_token);
1125 tok->purged_p = true;
1126 tok->location = UNKNOWN_LOCATION;
1127 tok->u.value = NULL_TREE;
1128 tok->keyword = RID_MAX;
1132 tok++;
1133 if (tok == lexer->last_token)
1135 tok = &eof_token;
1136 break;
1139 while (tok->purged_p);
1140 lexer->next_token = tok;
1143 /* Permanently remove all tokens after TOK, up to, but not
1144 including, the token that will be returned next by
1145 cp_lexer_peek_token. */
1147 static void
1148 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1150 cp_token *peek = lexer->next_token;
1152 if (peek == &eof_token)
1153 peek = lexer->last_token;
1155 gcc_assert (tok < peek);
1157 for ( tok += 1; tok != peek; tok += 1)
1159 tok->purged_p = true;
1160 tok->location = UNKNOWN_LOCATION;
1161 tok->u.value = NULL_TREE;
1162 tok->keyword = RID_MAX;
1166 /* Begin saving tokens. All tokens consumed after this point will be
1167 preserved. */
1169 static void
1170 cp_lexer_save_tokens (cp_lexer* lexer)
1172 /* Provide debugging output. */
1173 if (cp_lexer_debugging_p (lexer))
1174 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1176 lexer->saved_tokens.safe_push (lexer->next_token);
1179 /* Commit to the portion of the token stream most recently saved. */
1181 static void
1182 cp_lexer_commit_tokens (cp_lexer* lexer)
1184 /* Provide debugging output. */
1185 if (cp_lexer_debugging_p (lexer))
1186 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1188 lexer->saved_tokens.pop ();
1191 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1192 to the token stream. Stop saving tokens. */
1194 static void
1195 cp_lexer_rollback_tokens (cp_lexer* lexer)
1197 /* Provide debugging output. */
1198 if (cp_lexer_debugging_p (lexer))
1199 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1201 lexer->next_token = lexer->saved_tokens.pop ();
1204 /* RAII wrapper around the above functions, with sanity checking. Creating
1205 a variable saves tokens, which are committed when the variable is
1206 destroyed unless they are explicitly rolled back by calling the rollback
1207 member function. */
1209 struct saved_token_sentinel
1211 cp_lexer *lexer;
1212 unsigned len;
1213 bool commit;
1214 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1216 len = lexer->saved_tokens.length ();
1217 cp_lexer_save_tokens (lexer);
1219 void rollback ()
1221 cp_lexer_rollback_tokens (lexer);
1222 commit = false;
1224 ~saved_token_sentinel()
1226 if (commit)
1227 cp_lexer_commit_tokens (lexer);
1228 gcc_assert (lexer->saved_tokens.length () == len);
1232 /* Print a representation of the TOKEN on the STREAM. */
1234 static void
1235 cp_lexer_print_token (FILE * stream, cp_token *token)
1237 /* We don't use cpp_type2name here because the parser defines
1238 a few tokens of its own. */
1239 static const char *const token_names[] = {
1240 /* cpplib-defined token types */
1241 #define OP(e, s) #e,
1242 #define TK(e, s) #e,
1243 TTYPE_TABLE
1244 #undef OP
1245 #undef TK
1246 /* C++ parser token types - see "Manifest constants", above. */
1247 "KEYWORD",
1248 "TEMPLATE_ID",
1249 "NESTED_NAME_SPECIFIER",
1252 /* For some tokens, print the associated data. */
1253 switch (token->type)
1255 case CPP_KEYWORD:
1256 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1257 For example, `struct' is mapped to an INTEGER_CST. */
1258 if (!identifier_p (token->u.value))
1259 break;
1260 /* else fall through */
1261 case CPP_NAME:
1262 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1263 break;
1265 case CPP_STRING:
1266 case CPP_STRING16:
1267 case CPP_STRING32:
1268 case CPP_WSTRING:
1269 case CPP_UTF8STRING:
1270 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1271 break;
1273 case CPP_NUMBER:
1274 print_generic_expr (stream, token->u.value, 0);
1275 break;
1277 default:
1278 /* If we have a name for the token, print it out. Otherwise, we
1279 simply give the numeric code. */
1280 if (token->type < ARRAY_SIZE(token_names))
1281 fputs (token_names[token->type], stream);
1282 else
1283 fprintf (stream, "[%d]", token->type);
1284 break;
1288 DEBUG_FUNCTION void
1289 debug (cp_token &ref)
1291 cp_lexer_print_token (stderr, &ref);
1292 fprintf (stderr, "\n");
1295 DEBUG_FUNCTION void
1296 debug (cp_token *ptr)
1298 if (ptr)
1299 debug (*ptr);
1300 else
1301 fprintf (stderr, "<nil>\n");
1305 /* Start emitting debugging information. */
1307 static void
1308 cp_lexer_start_debugging (cp_lexer* lexer)
1310 if (!LEXER_DEBUGGING_ENABLED_P)
1311 fatal_error (input_location,
1312 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1314 lexer->debugging_p = true;
1315 cp_lexer_debug_stream = stderr;
1318 /* Stop emitting debugging information. */
1320 static void
1321 cp_lexer_stop_debugging (cp_lexer* lexer)
1323 if (!LEXER_DEBUGGING_ENABLED_P)
1324 fatal_error (input_location,
1325 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1327 lexer->debugging_p = false;
1328 cp_lexer_debug_stream = NULL;
1331 /* Create a new cp_token_cache, representing a range of tokens. */
1333 static cp_token_cache *
1334 cp_token_cache_new (cp_token *first, cp_token *last)
1336 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1337 cache->first = first;
1338 cache->last = last;
1339 return cache;
1342 /* Diagnose if #pragma omp declare simd isn't followed immediately
1343 by function declaration or definition. */
1345 static inline void
1346 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1348 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1350 error ("%<#pragma omp declare simd%> not immediately followed by "
1351 "function declaration or definition");
1352 parser->omp_declare_simd = NULL;
1356 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1357 and put that into "omp declare simd" attribute. */
1359 static inline void
1360 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1362 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1364 if (fndecl == error_mark_node)
1366 parser->omp_declare_simd = NULL;
1367 return;
1369 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1371 cp_ensure_no_omp_declare_simd (parser);
1372 return;
1377 /* Diagnose if #pragma acc routine isn't followed immediately by function
1378 declaration or definition. */
1380 static inline void
1381 cp_ensure_no_oacc_routine (cp_parser *parser)
1383 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1385 tree clauses = parser->oacc_routine->clauses;
1386 location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE (clauses));
1388 error_at (loc, "%<#pragma acc routine%> not followed by a function "
1389 "declaration or definition");
1390 parser->oacc_routine = NULL;
1394 /* Decl-specifiers. */
1396 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1398 static void
1399 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1401 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1404 /* Declarators. */
1406 /* Nothing other than the parser should be creating declarators;
1407 declarators are a semi-syntactic representation of C++ entities.
1408 Other parts of the front end that need to create entities (like
1409 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1411 static cp_declarator *make_call_declarator
1412 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1413 static cp_declarator *make_array_declarator
1414 (cp_declarator *, tree);
1415 static cp_declarator *make_pointer_declarator
1416 (cp_cv_quals, cp_declarator *, tree);
1417 static cp_declarator *make_reference_declarator
1418 (cp_cv_quals, cp_declarator *, bool, tree);
1419 static cp_declarator *make_ptrmem_declarator
1420 (cp_cv_quals, tree, cp_declarator *, tree);
1422 /* An erroneous declarator. */
1423 static cp_declarator *cp_error_declarator;
1425 /* The obstack on which declarators and related data structures are
1426 allocated. */
1427 static struct obstack declarator_obstack;
1429 /* Alloc BYTES from the declarator memory pool. */
1431 static inline void *
1432 alloc_declarator (size_t bytes)
1434 return obstack_alloc (&declarator_obstack, bytes);
1437 /* Allocate a declarator of the indicated KIND. Clear fields that are
1438 common to all declarators. */
1440 static cp_declarator *
1441 make_declarator (cp_declarator_kind kind)
1443 cp_declarator *declarator;
1445 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1446 declarator->kind = kind;
1447 declarator->attributes = NULL_TREE;
1448 declarator->std_attributes = NULL_TREE;
1449 declarator->declarator = NULL;
1450 declarator->parameter_pack_p = false;
1451 declarator->id_loc = UNKNOWN_LOCATION;
1453 return declarator;
1456 /* Make a declarator for a generalized identifier. If
1457 QUALIFYING_SCOPE is non-NULL, the identifier is
1458 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1459 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1460 is, if any. */
1462 static cp_declarator *
1463 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1464 special_function_kind sfk)
1466 cp_declarator *declarator;
1468 /* It is valid to write:
1470 class C { void f(); };
1471 typedef C D;
1472 void D::f();
1474 The standard is not clear about whether `typedef const C D' is
1475 legal; as of 2002-09-15 the committee is considering that
1476 question. EDG 3.0 allows that syntax. Therefore, we do as
1477 well. */
1478 if (qualifying_scope && TYPE_P (qualifying_scope))
1479 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1481 gcc_assert (identifier_p (unqualified_name)
1482 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1483 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1485 declarator = make_declarator (cdk_id);
1486 declarator->u.id.qualifying_scope = qualifying_scope;
1487 declarator->u.id.unqualified_name = unqualified_name;
1488 declarator->u.id.sfk = sfk;
1490 return declarator;
1493 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1494 of modifiers such as const or volatile to apply to the pointer
1495 type, represented as identifiers. ATTRIBUTES represent the attributes that
1496 appertain to the pointer or reference. */
1498 cp_declarator *
1499 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1500 tree attributes)
1502 cp_declarator *declarator;
1504 declarator = make_declarator (cdk_pointer);
1505 declarator->declarator = target;
1506 declarator->u.pointer.qualifiers = cv_qualifiers;
1507 declarator->u.pointer.class_type = NULL_TREE;
1508 if (target)
1510 declarator->id_loc = target->id_loc;
1511 declarator->parameter_pack_p = target->parameter_pack_p;
1512 target->parameter_pack_p = false;
1514 else
1515 declarator->parameter_pack_p = false;
1517 declarator->std_attributes = attributes;
1519 return declarator;
1522 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1523 represent the attributes that appertain to the pointer or
1524 reference. */
1526 cp_declarator *
1527 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1528 bool rvalue_ref, tree attributes)
1530 cp_declarator *declarator;
1532 declarator = make_declarator (cdk_reference);
1533 declarator->declarator = target;
1534 declarator->u.reference.qualifiers = cv_qualifiers;
1535 declarator->u.reference.rvalue_ref = rvalue_ref;
1536 if (target)
1538 declarator->id_loc = target->id_loc;
1539 declarator->parameter_pack_p = target->parameter_pack_p;
1540 target->parameter_pack_p = false;
1542 else
1543 declarator->parameter_pack_p = false;
1545 declarator->std_attributes = attributes;
1547 return declarator;
1550 /* Like make_pointer_declarator -- but for a pointer to a non-static
1551 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1552 appertain to the pointer or reference. */
1554 cp_declarator *
1555 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1556 cp_declarator *pointee,
1557 tree attributes)
1559 cp_declarator *declarator;
1561 declarator = make_declarator (cdk_ptrmem);
1562 declarator->declarator = pointee;
1563 declarator->u.pointer.qualifiers = cv_qualifiers;
1564 declarator->u.pointer.class_type = class_type;
1566 if (pointee)
1568 declarator->parameter_pack_p = pointee->parameter_pack_p;
1569 pointee->parameter_pack_p = false;
1571 else
1572 declarator->parameter_pack_p = false;
1574 declarator->std_attributes = attributes;
1576 return declarator;
1579 /* Make a declarator for the function given by TARGET, with the
1580 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1581 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1582 indicates what exceptions can be thrown. */
1584 cp_declarator *
1585 make_call_declarator (cp_declarator *target,
1586 tree parms,
1587 cp_cv_quals cv_qualifiers,
1588 cp_virt_specifiers virt_specifiers,
1589 cp_ref_qualifier ref_qualifier,
1590 tree tx_qualifier,
1591 tree exception_specification,
1592 tree late_return_type,
1593 tree requires_clause)
1595 cp_declarator *declarator;
1597 declarator = make_declarator (cdk_function);
1598 declarator->declarator = target;
1599 declarator->u.function.parameters = parms;
1600 declarator->u.function.qualifiers = cv_qualifiers;
1601 declarator->u.function.virt_specifiers = virt_specifiers;
1602 declarator->u.function.ref_qualifier = ref_qualifier;
1603 declarator->u.function.tx_qualifier = tx_qualifier;
1604 declarator->u.function.exception_specification = exception_specification;
1605 declarator->u.function.late_return_type = late_return_type;
1606 declarator->u.function.requires_clause = requires_clause;
1607 if (target)
1609 declarator->id_loc = target->id_loc;
1610 declarator->parameter_pack_p = target->parameter_pack_p;
1611 target->parameter_pack_p = false;
1613 else
1614 declarator->parameter_pack_p = false;
1616 return declarator;
1619 /* Make a declarator for an array of BOUNDS elements, each of which is
1620 defined by ELEMENT. */
1622 cp_declarator *
1623 make_array_declarator (cp_declarator *element, tree bounds)
1625 cp_declarator *declarator;
1627 declarator = make_declarator (cdk_array);
1628 declarator->declarator = element;
1629 declarator->u.array.bounds = bounds;
1630 if (element)
1632 declarator->id_loc = element->id_loc;
1633 declarator->parameter_pack_p = element->parameter_pack_p;
1634 element->parameter_pack_p = false;
1636 else
1637 declarator->parameter_pack_p = false;
1639 return declarator;
1642 /* Determine whether the declarator we've seen so far can be a
1643 parameter pack, when followed by an ellipsis. */
1644 static bool
1645 declarator_can_be_parameter_pack (cp_declarator *declarator)
1647 if (declarator && declarator->parameter_pack_p)
1648 /* We already saw an ellipsis. */
1649 return false;
1651 /* Search for a declarator name, or any other declarator that goes
1652 after the point where the ellipsis could appear in a parameter
1653 pack. If we find any of these, then this declarator can not be
1654 made into a parameter pack. */
1655 bool found = false;
1656 while (declarator && !found)
1658 switch ((int)declarator->kind)
1660 case cdk_id:
1661 case cdk_array:
1662 found = true;
1663 break;
1665 case cdk_error:
1666 return true;
1668 default:
1669 declarator = declarator->declarator;
1670 break;
1674 return !found;
1677 cp_parameter_declarator *no_parameters;
1679 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1680 DECLARATOR and DEFAULT_ARGUMENT. */
1682 cp_parameter_declarator *
1683 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1684 cp_declarator *declarator,
1685 tree default_argument,
1686 bool template_parameter_pack_p = false)
1688 cp_parameter_declarator *parameter;
1690 parameter = ((cp_parameter_declarator *)
1691 alloc_declarator (sizeof (cp_parameter_declarator)));
1692 parameter->next = NULL;
1693 if (decl_specifiers)
1694 parameter->decl_specifiers = *decl_specifiers;
1695 else
1696 clear_decl_specs (&parameter->decl_specifiers);
1697 parameter->declarator = declarator;
1698 parameter->default_argument = default_argument;
1699 parameter->template_parameter_pack_p = template_parameter_pack_p;
1701 return parameter;
1704 /* Returns true iff DECLARATOR is a declaration for a function. */
1706 static bool
1707 function_declarator_p (const cp_declarator *declarator)
1709 while (declarator)
1711 if (declarator->kind == cdk_function
1712 && declarator->declarator->kind == cdk_id)
1713 return true;
1714 if (declarator->kind == cdk_id
1715 || declarator->kind == cdk_error)
1716 return false;
1717 declarator = declarator->declarator;
1719 return false;
1722 /* The parser. */
1724 /* Overview
1725 --------
1727 A cp_parser parses the token stream as specified by the C++
1728 grammar. Its job is purely parsing, not semantic analysis. For
1729 example, the parser breaks the token stream into declarators,
1730 expressions, statements, and other similar syntactic constructs.
1731 It does not check that the types of the expressions on either side
1732 of an assignment-statement are compatible, or that a function is
1733 not declared with a parameter of type `void'.
1735 The parser invokes routines elsewhere in the compiler to perform
1736 semantic analysis and to build up the abstract syntax tree for the
1737 code processed.
1739 The parser (and the template instantiation code, which is, in a
1740 way, a close relative of parsing) are the only parts of the
1741 compiler that should be calling push_scope and pop_scope, or
1742 related functions. The parser (and template instantiation code)
1743 keeps track of what scope is presently active; everything else
1744 should simply honor that. (The code that generates static
1745 initializers may also need to set the scope, in order to check
1746 access control correctly when emitting the initializers.)
1748 Methodology
1749 -----------
1751 The parser is of the standard recursive-descent variety. Upcoming
1752 tokens in the token stream are examined in order to determine which
1753 production to use when parsing a non-terminal. Some C++ constructs
1754 require arbitrary look ahead to disambiguate. For example, it is
1755 impossible, in the general case, to tell whether a statement is an
1756 expression or declaration without scanning the entire statement.
1757 Therefore, the parser is capable of "parsing tentatively." When the
1758 parser is not sure what construct comes next, it enters this mode.
1759 Then, while we attempt to parse the construct, the parser queues up
1760 error messages, rather than issuing them immediately, and saves the
1761 tokens it consumes. If the construct is parsed successfully, the
1762 parser "commits", i.e., it issues any queued error messages and
1763 the tokens that were being preserved are permanently discarded.
1764 If, however, the construct is not parsed successfully, the parser
1765 rolls back its state completely so that it can resume parsing using
1766 a different alternative.
1768 Future Improvements
1769 -------------------
1771 The performance of the parser could probably be improved substantially.
1772 We could often eliminate the need to parse tentatively by looking ahead
1773 a little bit. In some places, this approach might not entirely eliminate
1774 the need to parse tentatively, but it might still speed up the average
1775 case. */
1777 /* Flags that are passed to some parsing functions. These values can
1778 be bitwise-ored together. */
1780 enum
1782 /* No flags. */
1783 CP_PARSER_FLAGS_NONE = 0x0,
1784 /* The construct is optional. If it is not present, then no error
1785 should be issued. */
1786 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1787 /* When parsing a type-specifier, treat user-defined type-names
1788 as non-type identifiers. */
1789 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1790 /* When parsing a type-specifier, do not try to parse a class-specifier
1791 or enum-specifier. */
1792 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1793 /* When parsing a decl-specifier-seq, only allow type-specifier or
1794 constexpr. */
1795 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1798 /* This type is used for parameters and variables which hold
1799 combinations of the above flags. */
1800 typedef int cp_parser_flags;
1802 /* The different kinds of declarators we want to parse. */
1804 enum cp_parser_declarator_kind
1806 /* We want an abstract declarator. */
1807 CP_PARSER_DECLARATOR_ABSTRACT,
1808 /* We want a named declarator. */
1809 CP_PARSER_DECLARATOR_NAMED,
1810 /* We don't mind, but the name must be an unqualified-id. */
1811 CP_PARSER_DECLARATOR_EITHER
1814 /* The precedence values used to parse binary expressions. The minimum value
1815 of PREC must be 1, because zero is reserved to quickly discriminate
1816 binary operators from other tokens. */
1818 enum cp_parser_prec
1820 PREC_NOT_OPERATOR,
1821 PREC_LOGICAL_OR_EXPRESSION,
1822 PREC_LOGICAL_AND_EXPRESSION,
1823 PREC_INCLUSIVE_OR_EXPRESSION,
1824 PREC_EXCLUSIVE_OR_EXPRESSION,
1825 PREC_AND_EXPRESSION,
1826 PREC_EQUALITY_EXPRESSION,
1827 PREC_RELATIONAL_EXPRESSION,
1828 PREC_SHIFT_EXPRESSION,
1829 PREC_ADDITIVE_EXPRESSION,
1830 PREC_MULTIPLICATIVE_EXPRESSION,
1831 PREC_PM_EXPRESSION,
1832 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1835 /* A mapping from a token type to a corresponding tree node type, with a
1836 precedence value. */
1838 struct cp_parser_binary_operations_map_node
1840 /* The token type. */
1841 enum cpp_ttype token_type;
1842 /* The corresponding tree code. */
1843 enum tree_code tree_type;
1844 /* The precedence of this operator. */
1845 enum cp_parser_prec prec;
1848 struct cp_parser_expression_stack_entry
1850 /* Left hand side of the binary operation we are currently
1851 parsing. */
1852 cp_expr lhs;
1853 /* Original tree code for left hand side, if it was a binary
1854 expression itself (used for -Wparentheses). */
1855 enum tree_code lhs_type;
1856 /* Tree code for the binary operation we are parsing. */
1857 enum tree_code tree_type;
1858 /* Precedence of the binary operation we are parsing. */
1859 enum cp_parser_prec prec;
1860 /* Location of the binary operation we are parsing. */
1861 location_t loc;
1864 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1865 entries because precedence levels on the stack are monotonically
1866 increasing. */
1867 typedef struct cp_parser_expression_stack_entry
1868 cp_parser_expression_stack[NUM_PREC_VALUES];
1870 /* Prototypes. */
1872 /* Constructors and destructors. */
1874 static cp_parser_context *cp_parser_context_new
1875 (cp_parser_context *);
1877 /* Class variables. */
1879 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1881 /* The operator-precedence table used by cp_parser_binary_expression.
1882 Transformed into an associative array (binops_by_token) by
1883 cp_parser_new. */
1885 static const cp_parser_binary_operations_map_node binops[] = {
1886 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1887 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1889 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1890 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1891 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1893 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1894 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1896 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1897 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1899 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1900 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1901 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1902 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1904 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1905 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1907 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1909 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1911 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1913 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1915 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1918 /* The same as binops, but initialized by cp_parser_new so that
1919 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1920 for speed. */
1921 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1923 /* Constructors and destructors. */
1925 /* Construct a new context. The context below this one on the stack
1926 is given by NEXT. */
1928 static cp_parser_context *
1929 cp_parser_context_new (cp_parser_context* next)
1931 cp_parser_context *context;
1933 /* Allocate the storage. */
1934 if (cp_parser_context_free_list != NULL)
1936 /* Pull the first entry from the free list. */
1937 context = cp_parser_context_free_list;
1938 cp_parser_context_free_list = context->next;
1939 memset (context, 0, sizeof (*context));
1941 else
1942 context = ggc_cleared_alloc<cp_parser_context> ();
1944 /* No errors have occurred yet in this context. */
1945 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1946 /* If this is not the bottommost context, copy information that we
1947 need from the previous context. */
1948 if (next)
1950 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1951 expression, then we are parsing one in this context, too. */
1952 context->object_type = next->object_type;
1953 /* Thread the stack. */
1954 context->next = next;
1957 return context;
1960 /* Managing the unparsed function queues. */
1962 #define unparsed_funs_with_default_args \
1963 parser->unparsed_queues->last ().funs_with_default_args
1964 #define unparsed_funs_with_definitions \
1965 parser->unparsed_queues->last ().funs_with_definitions
1966 #define unparsed_nsdmis \
1967 parser->unparsed_queues->last ().nsdmis
1968 #define unparsed_classes \
1969 parser->unparsed_queues->last ().classes
1971 static void
1972 push_unparsed_function_queues (cp_parser *parser)
1974 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1975 vec_safe_push (parser->unparsed_queues, e);
1978 static void
1979 pop_unparsed_function_queues (cp_parser *parser)
1981 release_tree_vector (unparsed_funs_with_definitions);
1982 parser->unparsed_queues->pop ();
1985 /* Prototypes. */
1987 /* Constructors and destructors. */
1989 static cp_parser *cp_parser_new
1990 (void);
1992 /* Routines to parse various constructs.
1994 Those that return `tree' will return the error_mark_node (rather
1995 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1996 Sometimes, they will return an ordinary node if error-recovery was
1997 attempted, even though a parse error occurred. So, to check
1998 whether or not a parse error occurred, you should always use
1999 cp_parser_error_occurred. If the construct is optional (indicated
2000 either by an `_opt' in the name of the function that does the
2001 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2002 the construct is not present. */
2004 /* Lexical conventions [gram.lex] */
2006 static cp_expr cp_parser_identifier
2007 (cp_parser *);
2008 static cp_expr cp_parser_string_literal
2009 (cp_parser *, bool, bool, bool);
2010 static cp_expr cp_parser_userdef_char_literal
2011 (cp_parser *);
2012 static tree cp_parser_userdef_string_literal
2013 (tree);
2014 static cp_expr cp_parser_userdef_numeric_literal
2015 (cp_parser *);
2017 /* Basic concepts [gram.basic] */
2019 static bool cp_parser_translation_unit
2020 (cp_parser *);
2022 /* Expressions [gram.expr] */
2024 static cp_expr cp_parser_primary_expression
2025 (cp_parser *, bool, bool, bool, cp_id_kind *);
2026 static cp_expr cp_parser_id_expression
2027 (cp_parser *, bool, bool, bool *, bool, bool);
2028 static cp_expr cp_parser_unqualified_id
2029 (cp_parser *, bool, bool, bool, bool);
2030 static tree cp_parser_nested_name_specifier_opt
2031 (cp_parser *, bool, bool, bool, bool);
2032 static tree cp_parser_nested_name_specifier
2033 (cp_parser *, bool, bool, bool, bool);
2034 static tree cp_parser_qualifying_entity
2035 (cp_parser *, bool, bool, bool, bool, bool);
2036 static cp_expr cp_parser_postfix_expression
2037 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2038 static tree cp_parser_postfix_open_square_expression
2039 (cp_parser *, tree, bool, bool);
2040 static tree cp_parser_postfix_dot_deref_expression
2041 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2042 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2043 (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2044 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2045 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2046 static void cp_parser_pseudo_destructor_name
2047 (cp_parser *, tree, tree *, tree *);
2048 static cp_expr cp_parser_unary_expression
2049 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2050 static enum tree_code cp_parser_unary_operator
2051 (cp_token *);
2052 static tree cp_parser_new_expression
2053 (cp_parser *);
2054 static vec<tree, va_gc> *cp_parser_new_placement
2055 (cp_parser *);
2056 static tree cp_parser_new_type_id
2057 (cp_parser *, tree *);
2058 static cp_declarator *cp_parser_new_declarator_opt
2059 (cp_parser *);
2060 static cp_declarator *cp_parser_direct_new_declarator
2061 (cp_parser *);
2062 static vec<tree, va_gc> *cp_parser_new_initializer
2063 (cp_parser *);
2064 static tree cp_parser_delete_expression
2065 (cp_parser *);
2066 static cp_expr cp_parser_cast_expression
2067 (cp_parser *, bool, bool, bool, cp_id_kind *);
2068 static cp_expr cp_parser_binary_expression
2069 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2070 static tree cp_parser_question_colon_clause
2071 (cp_parser *, cp_expr);
2072 static cp_expr cp_parser_assignment_expression
2073 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2074 static enum tree_code cp_parser_assignment_operator_opt
2075 (cp_parser *);
2076 static cp_expr cp_parser_expression
2077 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2078 static cp_expr cp_parser_constant_expression
2079 (cp_parser *, bool = false, bool * = NULL);
2080 static cp_expr cp_parser_builtin_offsetof
2081 (cp_parser *);
2082 static cp_expr cp_parser_lambda_expression
2083 (cp_parser *);
2084 static void cp_parser_lambda_introducer
2085 (cp_parser *, tree);
2086 static bool cp_parser_lambda_declarator_opt
2087 (cp_parser *, tree);
2088 static void cp_parser_lambda_body
2089 (cp_parser *, tree);
2091 /* Statements [gram.stmt.stmt] */
2093 static void cp_parser_statement
2094 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL);
2095 static void cp_parser_label_for_labeled_statement
2096 (cp_parser *, tree);
2097 static tree cp_parser_expression_statement
2098 (cp_parser *, tree);
2099 static tree cp_parser_compound_statement
2100 (cp_parser *, tree, int, bool);
2101 static void cp_parser_statement_seq_opt
2102 (cp_parser *, tree);
2103 static tree cp_parser_selection_statement
2104 (cp_parser *, bool *, vec<tree> *);
2105 static tree cp_parser_condition
2106 (cp_parser *);
2107 static tree cp_parser_iteration_statement
2108 (cp_parser *, bool *, bool);
2109 static bool cp_parser_for_init_statement
2110 (cp_parser *, tree *decl);
2111 static tree cp_parser_for
2112 (cp_parser *, bool);
2113 static tree cp_parser_c_for
2114 (cp_parser *, tree, tree, bool);
2115 static tree cp_parser_range_for
2116 (cp_parser *, tree, tree, tree, bool);
2117 static void do_range_for_auto_deduction
2118 (tree, tree);
2119 static tree cp_parser_perform_range_for_lookup
2120 (tree, tree *, tree *);
2121 static tree cp_parser_range_for_member_function
2122 (tree, tree);
2123 static tree cp_parser_jump_statement
2124 (cp_parser *);
2125 static void cp_parser_declaration_statement
2126 (cp_parser *);
2128 static tree cp_parser_implicitly_scoped_statement
2129 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2130 static void cp_parser_already_scoped_statement
2131 (cp_parser *, bool *, const token_indent_info &);
2133 /* Declarations [gram.dcl.dcl] */
2135 static void cp_parser_declaration_seq_opt
2136 (cp_parser *);
2137 static void cp_parser_declaration
2138 (cp_parser *);
2139 static void cp_parser_block_declaration
2140 (cp_parser *, bool);
2141 static void cp_parser_simple_declaration
2142 (cp_parser *, bool, tree *);
2143 static void cp_parser_decl_specifier_seq
2144 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2145 static tree cp_parser_storage_class_specifier_opt
2146 (cp_parser *);
2147 static tree cp_parser_function_specifier_opt
2148 (cp_parser *, cp_decl_specifier_seq *);
2149 static tree cp_parser_type_specifier
2150 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2151 int *, bool *);
2152 static tree cp_parser_simple_type_specifier
2153 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2154 static tree cp_parser_type_name
2155 (cp_parser *, bool);
2156 static tree cp_parser_type_name
2157 (cp_parser *);
2158 static tree cp_parser_nonclass_name
2159 (cp_parser* parser);
2160 static tree cp_parser_elaborated_type_specifier
2161 (cp_parser *, bool, bool);
2162 static tree cp_parser_enum_specifier
2163 (cp_parser *);
2164 static void cp_parser_enumerator_list
2165 (cp_parser *, tree);
2166 static void cp_parser_enumerator_definition
2167 (cp_parser *, tree);
2168 static tree cp_parser_namespace_name
2169 (cp_parser *);
2170 static void cp_parser_namespace_definition
2171 (cp_parser *);
2172 static void cp_parser_namespace_body
2173 (cp_parser *);
2174 static tree cp_parser_qualified_namespace_specifier
2175 (cp_parser *);
2176 static void cp_parser_namespace_alias_definition
2177 (cp_parser *);
2178 static bool cp_parser_using_declaration
2179 (cp_parser *, bool);
2180 static void cp_parser_using_directive
2181 (cp_parser *);
2182 static tree cp_parser_alias_declaration
2183 (cp_parser *);
2184 static void cp_parser_asm_definition
2185 (cp_parser *);
2186 static void cp_parser_linkage_specification
2187 (cp_parser *);
2188 static void cp_parser_static_assert
2189 (cp_parser *, bool);
2190 static tree cp_parser_decltype
2191 (cp_parser *);
2193 /* Declarators [gram.dcl.decl] */
2195 static tree cp_parser_init_declarator
2196 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2197 bool, bool, int, bool *, tree *, location_t *, tree *);
2198 static cp_declarator *cp_parser_declarator
2199 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2200 static cp_declarator *cp_parser_direct_declarator
2201 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2202 static enum tree_code cp_parser_ptr_operator
2203 (cp_parser *, tree *, cp_cv_quals *, tree *);
2204 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2205 (cp_parser *);
2206 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2207 (cp_parser *);
2208 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2209 (cp_parser *);
2210 static tree cp_parser_tx_qualifier_opt
2211 (cp_parser *);
2212 static tree cp_parser_late_return_type_opt
2213 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2214 static tree cp_parser_declarator_id
2215 (cp_parser *, bool);
2216 static tree cp_parser_type_id
2217 (cp_parser *);
2218 static tree cp_parser_template_type_arg
2219 (cp_parser *);
2220 static tree cp_parser_trailing_type_id (cp_parser *);
2221 static tree cp_parser_type_id_1
2222 (cp_parser *, bool, bool);
2223 static void cp_parser_type_specifier_seq
2224 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2225 static tree cp_parser_parameter_declaration_clause
2226 (cp_parser *);
2227 static tree cp_parser_parameter_declaration_list
2228 (cp_parser *, bool *);
2229 static cp_parameter_declarator *cp_parser_parameter_declaration
2230 (cp_parser *, bool, bool *);
2231 static tree cp_parser_default_argument
2232 (cp_parser *, bool);
2233 static void cp_parser_function_body
2234 (cp_parser *, bool);
2235 static tree cp_parser_initializer
2236 (cp_parser *, bool *, bool *);
2237 static cp_expr cp_parser_initializer_clause
2238 (cp_parser *, bool *);
2239 static cp_expr cp_parser_braced_list
2240 (cp_parser*, bool*);
2241 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2242 (cp_parser *, bool *);
2244 static bool cp_parser_ctor_initializer_opt_and_function_body
2245 (cp_parser *, bool);
2247 static tree cp_parser_late_parsing_omp_declare_simd
2248 (cp_parser *, tree);
2250 static tree cp_parser_late_parsing_cilk_simd_fn_info
2251 (cp_parser *, tree);
2253 static tree cp_parser_late_parsing_oacc_routine
2254 (cp_parser *, tree);
2256 static tree synthesize_implicit_template_parm
2257 (cp_parser *, tree);
2258 static tree finish_fully_implicit_template
2259 (cp_parser *, tree);
2261 /* Classes [gram.class] */
2263 static tree cp_parser_class_name
2264 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2265 static tree cp_parser_class_specifier
2266 (cp_parser *);
2267 static tree cp_parser_class_head
2268 (cp_parser *, bool *);
2269 static enum tag_types cp_parser_class_key
2270 (cp_parser *);
2271 static void cp_parser_type_parameter_key
2272 (cp_parser* parser);
2273 static void cp_parser_member_specification_opt
2274 (cp_parser *);
2275 static void cp_parser_member_declaration
2276 (cp_parser *);
2277 static tree cp_parser_pure_specifier
2278 (cp_parser *);
2279 static tree cp_parser_constant_initializer
2280 (cp_parser *);
2282 /* Derived classes [gram.class.derived] */
2284 static tree cp_parser_base_clause
2285 (cp_parser *);
2286 static tree cp_parser_base_specifier
2287 (cp_parser *);
2289 /* Special member functions [gram.special] */
2291 static tree cp_parser_conversion_function_id
2292 (cp_parser *);
2293 static tree cp_parser_conversion_type_id
2294 (cp_parser *);
2295 static cp_declarator *cp_parser_conversion_declarator_opt
2296 (cp_parser *);
2297 static bool cp_parser_ctor_initializer_opt
2298 (cp_parser *);
2299 static void cp_parser_mem_initializer_list
2300 (cp_parser *);
2301 static tree cp_parser_mem_initializer
2302 (cp_parser *);
2303 static tree cp_parser_mem_initializer_id
2304 (cp_parser *);
2306 /* Overloading [gram.over] */
2308 static cp_expr cp_parser_operator_function_id
2309 (cp_parser *);
2310 static cp_expr cp_parser_operator
2311 (cp_parser *);
2313 /* Templates [gram.temp] */
2315 static void cp_parser_template_declaration
2316 (cp_parser *, bool);
2317 static tree cp_parser_template_parameter_list
2318 (cp_parser *);
2319 static tree cp_parser_template_parameter
2320 (cp_parser *, bool *, bool *);
2321 static tree cp_parser_type_parameter
2322 (cp_parser *, bool *);
2323 static tree cp_parser_template_id
2324 (cp_parser *, bool, bool, enum tag_types, bool);
2325 static tree cp_parser_template_name
2326 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2327 static tree cp_parser_template_argument_list
2328 (cp_parser *);
2329 static tree cp_parser_template_argument
2330 (cp_parser *);
2331 static void cp_parser_explicit_instantiation
2332 (cp_parser *);
2333 static void cp_parser_explicit_specialization
2334 (cp_parser *);
2336 /* Exception handling [gram.exception] */
2338 static tree cp_parser_try_block
2339 (cp_parser *);
2340 static bool cp_parser_function_try_block
2341 (cp_parser *);
2342 static void cp_parser_handler_seq
2343 (cp_parser *);
2344 static void cp_parser_handler
2345 (cp_parser *);
2346 static tree cp_parser_exception_declaration
2347 (cp_parser *);
2348 static tree cp_parser_throw_expression
2349 (cp_parser *);
2350 static tree cp_parser_exception_specification_opt
2351 (cp_parser *);
2352 static tree cp_parser_type_id_list
2353 (cp_parser *);
2355 /* GNU Extensions */
2357 static tree cp_parser_asm_specification_opt
2358 (cp_parser *);
2359 static tree cp_parser_asm_operand_list
2360 (cp_parser *);
2361 static tree cp_parser_asm_clobber_list
2362 (cp_parser *);
2363 static tree cp_parser_asm_label_list
2364 (cp_parser *);
2365 static bool cp_next_tokens_can_be_attribute_p
2366 (cp_parser *);
2367 static bool cp_next_tokens_can_be_gnu_attribute_p
2368 (cp_parser *);
2369 static bool cp_next_tokens_can_be_std_attribute_p
2370 (cp_parser *);
2371 static bool cp_nth_tokens_can_be_std_attribute_p
2372 (cp_parser *, size_t);
2373 static bool cp_nth_tokens_can_be_gnu_attribute_p
2374 (cp_parser *, size_t);
2375 static bool cp_nth_tokens_can_be_attribute_p
2376 (cp_parser *, size_t);
2377 static tree cp_parser_attributes_opt
2378 (cp_parser *);
2379 static tree cp_parser_gnu_attributes_opt
2380 (cp_parser *);
2381 static tree cp_parser_gnu_attribute_list
2382 (cp_parser *);
2383 static tree cp_parser_std_attribute
2384 (cp_parser *);
2385 static tree cp_parser_std_attribute_spec
2386 (cp_parser *);
2387 static tree cp_parser_std_attribute_spec_seq
2388 (cp_parser *);
2389 static bool cp_parser_extension_opt
2390 (cp_parser *, int *);
2391 static void cp_parser_label_declaration
2392 (cp_parser *);
2394 /* Concept Extensions */
2396 static tree cp_parser_requires_clause
2397 (cp_parser *);
2398 static tree cp_parser_requires_clause_opt
2399 (cp_parser *);
2400 static tree cp_parser_requires_expression
2401 (cp_parser *);
2402 static tree cp_parser_requirement_parameter_list
2403 (cp_parser *);
2404 static tree cp_parser_requirement_body
2405 (cp_parser *);
2406 static tree cp_parser_requirement_list
2407 (cp_parser *);
2408 static tree cp_parser_requirement
2409 (cp_parser *);
2410 static tree cp_parser_simple_requirement
2411 (cp_parser *);
2412 static tree cp_parser_compound_requirement
2413 (cp_parser *);
2414 static tree cp_parser_type_requirement
2415 (cp_parser *);
2416 static tree cp_parser_nested_requirement
2417 (cp_parser *);
2419 /* Transactional Memory Extensions */
2421 static tree cp_parser_transaction
2422 (cp_parser *, cp_token *);
2423 static tree cp_parser_transaction_expression
2424 (cp_parser *, enum rid);
2425 static bool cp_parser_function_transaction
2426 (cp_parser *, enum rid);
2427 static tree cp_parser_transaction_cancel
2428 (cp_parser *);
2430 enum pragma_context {
2431 pragma_external,
2432 pragma_member,
2433 pragma_objc_icode,
2434 pragma_stmt,
2435 pragma_compound
2437 static bool cp_parser_pragma
2438 (cp_parser *, enum pragma_context, bool *);
2440 /* Objective-C++ Productions */
2442 static tree cp_parser_objc_message_receiver
2443 (cp_parser *);
2444 static tree cp_parser_objc_message_args
2445 (cp_parser *);
2446 static tree cp_parser_objc_message_expression
2447 (cp_parser *);
2448 static cp_expr cp_parser_objc_encode_expression
2449 (cp_parser *);
2450 static tree cp_parser_objc_defs_expression
2451 (cp_parser *);
2452 static tree cp_parser_objc_protocol_expression
2453 (cp_parser *);
2454 static tree cp_parser_objc_selector_expression
2455 (cp_parser *);
2456 static cp_expr cp_parser_objc_expression
2457 (cp_parser *);
2458 static bool cp_parser_objc_selector_p
2459 (enum cpp_ttype);
2460 static tree cp_parser_objc_selector
2461 (cp_parser *);
2462 static tree cp_parser_objc_protocol_refs_opt
2463 (cp_parser *);
2464 static void cp_parser_objc_declaration
2465 (cp_parser *, tree);
2466 static tree cp_parser_objc_statement
2467 (cp_parser *);
2468 static bool cp_parser_objc_valid_prefix_attributes
2469 (cp_parser *, tree *);
2470 static void cp_parser_objc_at_property_declaration
2471 (cp_parser *) ;
2472 static void cp_parser_objc_at_synthesize_declaration
2473 (cp_parser *) ;
2474 static void cp_parser_objc_at_dynamic_declaration
2475 (cp_parser *) ;
2476 static tree cp_parser_objc_struct_declaration
2477 (cp_parser *) ;
2479 /* Utility Routines */
2481 static cp_expr cp_parser_lookup_name
2482 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2483 static tree cp_parser_lookup_name_simple
2484 (cp_parser *, tree, location_t);
2485 static tree cp_parser_maybe_treat_template_as_class
2486 (tree, bool);
2487 static bool cp_parser_check_declarator_template_parameters
2488 (cp_parser *, cp_declarator *, location_t);
2489 static bool cp_parser_check_template_parameters
2490 (cp_parser *, unsigned, location_t, cp_declarator *);
2491 static cp_expr cp_parser_simple_cast_expression
2492 (cp_parser *);
2493 static tree cp_parser_global_scope_opt
2494 (cp_parser *, bool);
2495 static bool cp_parser_constructor_declarator_p
2496 (cp_parser *, bool);
2497 static tree cp_parser_function_definition_from_specifiers_and_declarator
2498 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2499 static tree cp_parser_function_definition_after_declarator
2500 (cp_parser *, bool);
2501 static bool cp_parser_template_declaration_after_export
2502 (cp_parser *, bool);
2503 static void cp_parser_perform_template_parameter_access_checks
2504 (vec<deferred_access_check, va_gc> *);
2505 static tree cp_parser_single_declaration
2506 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2507 static cp_expr cp_parser_functional_cast
2508 (cp_parser *, tree);
2509 static tree cp_parser_save_member_function_body
2510 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2511 static tree cp_parser_save_nsdmi
2512 (cp_parser *);
2513 static tree cp_parser_enclosed_template_argument_list
2514 (cp_parser *);
2515 static void cp_parser_save_default_args
2516 (cp_parser *, tree);
2517 static void cp_parser_late_parsing_for_member
2518 (cp_parser *, tree);
2519 static tree cp_parser_late_parse_one_default_arg
2520 (cp_parser *, tree, tree, tree);
2521 static void cp_parser_late_parsing_nsdmi
2522 (cp_parser *, tree);
2523 static void cp_parser_late_parsing_default_args
2524 (cp_parser *, tree);
2525 static tree cp_parser_sizeof_operand
2526 (cp_parser *, enum rid);
2527 static tree cp_parser_trait_expr
2528 (cp_parser *, enum rid);
2529 static bool cp_parser_declares_only_class_p
2530 (cp_parser *);
2531 static void cp_parser_set_storage_class
2532 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2533 static void cp_parser_set_decl_spec_type
2534 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2535 static void set_and_check_decl_spec_loc
2536 (cp_decl_specifier_seq *decl_specs,
2537 cp_decl_spec ds, cp_token *);
2538 static bool cp_parser_friend_p
2539 (const cp_decl_specifier_seq *);
2540 static void cp_parser_required_error
2541 (cp_parser *, required_token, bool);
2542 static cp_token *cp_parser_require
2543 (cp_parser *, enum cpp_ttype, required_token);
2544 static cp_token *cp_parser_require_keyword
2545 (cp_parser *, enum rid, required_token);
2546 static bool cp_parser_token_starts_function_definition_p
2547 (cp_token *);
2548 static bool cp_parser_next_token_starts_class_definition_p
2549 (cp_parser *);
2550 static bool cp_parser_next_token_ends_template_argument_p
2551 (cp_parser *);
2552 static bool cp_parser_nth_token_starts_template_argument_list_p
2553 (cp_parser *, size_t);
2554 static enum tag_types cp_parser_token_is_class_key
2555 (cp_token *);
2556 static enum tag_types cp_parser_token_is_type_parameter_key
2557 (cp_token *);
2558 static void cp_parser_check_class_key
2559 (enum tag_types, tree type);
2560 static void cp_parser_check_access_in_redeclaration
2561 (tree type, location_t location);
2562 static bool cp_parser_optional_template_keyword
2563 (cp_parser *);
2564 static void cp_parser_pre_parsed_nested_name_specifier
2565 (cp_parser *);
2566 static bool cp_parser_cache_group
2567 (cp_parser *, enum cpp_ttype, unsigned);
2568 static tree cp_parser_cache_defarg
2569 (cp_parser *parser, bool nsdmi);
2570 static void cp_parser_parse_tentatively
2571 (cp_parser *);
2572 static void cp_parser_commit_to_tentative_parse
2573 (cp_parser *);
2574 static void cp_parser_commit_to_topmost_tentative_parse
2575 (cp_parser *);
2576 static void cp_parser_abort_tentative_parse
2577 (cp_parser *);
2578 static bool cp_parser_parse_definitely
2579 (cp_parser *);
2580 static inline bool cp_parser_parsing_tentatively
2581 (cp_parser *);
2582 static bool cp_parser_uncommitted_to_tentative_parse_p
2583 (cp_parser *);
2584 static void cp_parser_error
2585 (cp_parser *, const char *);
2586 static void cp_parser_name_lookup_error
2587 (cp_parser *, tree, tree, name_lookup_error, location_t);
2588 static bool cp_parser_simulate_error
2589 (cp_parser *);
2590 static bool cp_parser_check_type_definition
2591 (cp_parser *);
2592 static void cp_parser_check_for_definition_in_return_type
2593 (cp_declarator *, tree, location_t type_location);
2594 static void cp_parser_check_for_invalid_template_id
2595 (cp_parser *, tree, enum tag_types, location_t location);
2596 static bool cp_parser_non_integral_constant_expression
2597 (cp_parser *, non_integral_constant);
2598 static void cp_parser_diagnose_invalid_type_name
2599 (cp_parser *, tree, location_t);
2600 static bool cp_parser_parse_and_diagnose_invalid_type_name
2601 (cp_parser *);
2602 static int cp_parser_skip_to_closing_parenthesis
2603 (cp_parser *, bool, bool, bool);
2604 static void cp_parser_skip_to_end_of_statement
2605 (cp_parser *);
2606 static void cp_parser_consume_semicolon_at_end_of_statement
2607 (cp_parser *);
2608 static void cp_parser_skip_to_end_of_block_or_statement
2609 (cp_parser *);
2610 static bool cp_parser_skip_to_closing_brace
2611 (cp_parser *);
2612 static void cp_parser_skip_to_end_of_template_parameter_list
2613 (cp_parser *);
2614 static void cp_parser_skip_to_pragma_eol
2615 (cp_parser*, cp_token *);
2616 static bool cp_parser_error_occurred
2617 (cp_parser *);
2618 static bool cp_parser_allow_gnu_extensions_p
2619 (cp_parser *);
2620 static bool cp_parser_is_pure_string_literal
2621 (cp_token *);
2622 static bool cp_parser_is_string_literal
2623 (cp_token *);
2624 static bool cp_parser_is_keyword
2625 (cp_token *, enum rid);
2626 static tree cp_parser_make_typename_type
2627 (cp_parser *, tree, location_t location);
2628 static cp_declarator * cp_parser_make_indirect_declarator
2629 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2630 static bool cp_parser_compound_literal_p
2631 (cp_parser *);
2632 static bool cp_parser_array_designator_p
2633 (cp_parser *);
2634 static bool cp_parser_skip_to_closing_square_bracket
2635 (cp_parser *);
2637 /* Concept-related syntactic transformations */
2639 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2640 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2642 // -------------------------------------------------------------------------- //
2643 // Unevaluated Operand Guard
2645 // Implementation of an RAII helper for unevaluated operand parsing.
2646 cp_unevaluated::cp_unevaluated ()
2648 ++cp_unevaluated_operand;
2649 ++c_inhibit_evaluation_warnings;
2652 cp_unevaluated::~cp_unevaluated ()
2654 --c_inhibit_evaluation_warnings;
2655 --cp_unevaluated_operand;
2658 // -------------------------------------------------------------------------- //
2659 // Tentative Parsing
2661 /* Returns nonzero if we are parsing tentatively. */
2663 static inline bool
2664 cp_parser_parsing_tentatively (cp_parser* parser)
2666 return parser->context->next != NULL;
2669 /* Returns nonzero if TOKEN is a string literal. */
2671 static bool
2672 cp_parser_is_pure_string_literal (cp_token* token)
2674 return (token->type == CPP_STRING ||
2675 token->type == CPP_STRING16 ||
2676 token->type == CPP_STRING32 ||
2677 token->type == CPP_WSTRING ||
2678 token->type == CPP_UTF8STRING);
2681 /* Returns nonzero if TOKEN is a string literal
2682 of a user-defined string literal. */
2684 static bool
2685 cp_parser_is_string_literal (cp_token* token)
2687 return (cp_parser_is_pure_string_literal (token) ||
2688 token->type == CPP_STRING_USERDEF ||
2689 token->type == CPP_STRING16_USERDEF ||
2690 token->type == CPP_STRING32_USERDEF ||
2691 token->type == CPP_WSTRING_USERDEF ||
2692 token->type == CPP_UTF8STRING_USERDEF);
2695 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2697 static bool
2698 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2700 return token->keyword == keyword;
2703 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2704 PRAGMA_NONE. */
2706 static enum pragma_kind
2707 cp_parser_pragma_kind (cp_token *token)
2709 if (token->type != CPP_PRAGMA)
2710 return PRAGMA_NONE;
2711 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2712 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2715 /* Helper function for cp_parser_error.
2716 Having peeked a token of kind TOK1_KIND that might signify
2717 a conflict marker, peek successor tokens to determine
2718 if we actually do have a conflict marker.
2719 Specifically, we consider a run of 7 '<', '=' or '>' characters
2720 at the start of a line as a conflict marker.
2721 These come through the lexer as three pairs and a single,
2722 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2723 If it returns true, *OUT_LOC is written to with the location/range
2724 of the marker. */
2726 static bool
2727 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2728 location_t *out_loc)
2730 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2731 if (token2->type != tok1_kind)
2732 return false;
2733 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2734 if (token3->type != tok1_kind)
2735 return false;
2736 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2737 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2738 return false;
2740 /* It must be at the start of the line. */
2741 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2742 if (LOCATION_COLUMN (start_loc) != 1)
2743 return false;
2745 /* We have a conflict marker. Construct a location of the form:
2746 <<<<<<<
2747 ^~~~~~~
2748 with start == caret, finishing at the end of the marker. */
2749 location_t finish_loc = get_finish (token4->location);
2750 *out_loc = make_location (start_loc, start_loc, finish_loc);
2752 return true;
2755 /* If not parsing tentatively, issue a diagnostic of the form
2756 FILE:LINE: MESSAGE before TOKEN
2757 where TOKEN is the next token in the input stream. MESSAGE
2758 (specified by the caller) is usually of the form "expected
2759 OTHER-TOKEN". */
2761 static void
2762 cp_parser_error (cp_parser* parser, const char* gmsgid)
2764 if (!cp_parser_simulate_error (parser))
2766 cp_token *token = cp_lexer_peek_token (parser->lexer);
2767 /* This diagnostic makes more sense if it is tagged to the line
2768 of the token we just peeked at. */
2769 cp_lexer_set_source_position_from_token (token);
2771 if (token->type == CPP_PRAGMA)
2773 error_at (token->location,
2774 "%<#pragma%> is not allowed here");
2775 cp_parser_skip_to_pragma_eol (parser, token);
2776 return;
2779 /* If this is actually a conflict marker, report it as such. */
2780 if (token->type == CPP_LSHIFT
2781 || token->type == CPP_RSHIFT
2782 || token->type == CPP_EQ_EQ)
2784 location_t loc;
2785 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2787 error_at (loc, "version control conflict marker in file");
2788 return;
2792 c_parse_error (gmsgid,
2793 /* Because c_parser_error does not understand
2794 CPP_KEYWORD, keywords are treated like
2795 identifiers. */
2796 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2797 token->u.value, token->flags);
2801 /* Issue an error about name-lookup failing. NAME is the
2802 IDENTIFIER_NODE DECL is the result of
2803 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2804 the thing that we hoped to find. */
2806 static void
2807 cp_parser_name_lookup_error (cp_parser* parser,
2808 tree name,
2809 tree decl,
2810 name_lookup_error desired,
2811 location_t location)
2813 /* If name lookup completely failed, tell the user that NAME was not
2814 declared. */
2815 if (decl == error_mark_node)
2817 if (parser->scope && parser->scope != global_namespace)
2818 error_at (location, "%<%E::%E%> has not been declared",
2819 parser->scope, name);
2820 else if (parser->scope == global_namespace)
2821 error_at (location, "%<::%E%> has not been declared", name);
2822 else if (parser->object_scope
2823 && !CLASS_TYPE_P (parser->object_scope))
2824 error_at (location, "request for member %qE in non-class type %qT",
2825 name, parser->object_scope);
2826 else if (parser->object_scope)
2827 error_at (location, "%<%T::%E%> has not been declared",
2828 parser->object_scope, name);
2829 else
2830 error_at (location, "%qE has not been declared", name);
2832 else if (parser->scope && parser->scope != global_namespace)
2834 switch (desired)
2836 case NLE_TYPE:
2837 error_at (location, "%<%E::%E%> is not a type",
2838 parser->scope, name);
2839 break;
2840 case NLE_CXX98:
2841 error_at (location, "%<%E::%E%> is not a class or namespace",
2842 parser->scope, name);
2843 break;
2844 case NLE_NOT_CXX98:
2845 error_at (location,
2846 "%<%E::%E%> is not a class, namespace, or enumeration",
2847 parser->scope, name);
2848 break;
2849 default:
2850 gcc_unreachable ();
2854 else if (parser->scope == global_namespace)
2856 switch (desired)
2858 case NLE_TYPE:
2859 error_at (location, "%<::%E%> is not a type", name);
2860 break;
2861 case NLE_CXX98:
2862 error_at (location, "%<::%E%> is not a class or namespace", name);
2863 break;
2864 case NLE_NOT_CXX98:
2865 error_at (location,
2866 "%<::%E%> is not a class, namespace, or enumeration",
2867 name);
2868 break;
2869 default:
2870 gcc_unreachable ();
2873 else
2875 switch (desired)
2877 case NLE_TYPE:
2878 error_at (location, "%qE is not a type", name);
2879 break;
2880 case NLE_CXX98:
2881 error_at (location, "%qE is not a class or namespace", name);
2882 break;
2883 case NLE_NOT_CXX98:
2884 error_at (location,
2885 "%qE is not a class, namespace, or enumeration", name);
2886 break;
2887 default:
2888 gcc_unreachable ();
2893 /* If we are parsing tentatively, remember that an error has occurred
2894 during this tentative parse. Returns true if the error was
2895 simulated; false if a message should be issued by the caller. */
2897 static bool
2898 cp_parser_simulate_error (cp_parser* parser)
2900 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2902 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2903 return true;
2905 return false;
2908 /* This function is called when a type is defined. If type
2909 definitions are forbidden at this point, an error message is
2910 issued. */
2912 static bool
2913 cp_parser_check_type_definition (cp_parser* parser)
2915 /* If types are forbidden here, issue a message. */
2916 if (parser->type_definition_forbidden_message)
2918 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2919 in the message need to be interpreted. */
2920 error (parser->type_definition_forbidden_message);
2921 return false;
2923 return true;
2926 /* This function is called when the DECLARATOR is processed. The TYPE
2927 was a type defined in the decl-specifiers. If it is invalid to
2928 define a type in the decl-specifiers for DECLARATOR, an error is
2929 issued. TYPE_LOCATION is the location of TYPE and is used
2930 for error reporting. */
2932 static void
2933 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2934 tree type, location_t type_location)
2936 /* [dcl.fct] forbids type definitions in return types.
2937 Unfortunately, it's not easy to know whether or not we are
2938 processing a return type until after the fact. */
2939 while (declarator
2940 && (declarator->kind == cdk_pointer
2941 || declarator->kind == cdk_reference
2942 || declarator->kind == cdk_ptrmem))
2943 declarator = declarator->declarator;
2944 if (declarator
2945 && declarator->kind == cdk_function)
2947 error_at (type_location,
2948 "new types may not be defined in a return type");
2949 inform (type_location,
2950 "(perhaps a semicolon is missing after the definition of %qT)",
2951 type);
2955 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2956 "<" in any valid C++ program. If the next token is indeed "<",
2957 issue a message warning the user about what appears to be an
2958 invalid attempt to form a template-id. LOCATION is the location
2959 of the type-specifier (TYPE) */
2961 static void
2962 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2963 tree type,
2964 enum tag_types tag_type,
2965 location_t location)
2967 cp_token_position start = 0;
2969 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2971 if (TYPE_P (type))
2972 error_at (location, "%qT is not a template", type);
2973 else if (identifier_p (type))
2975 if (tag_type != none_type)
2976 error_at (location, "%qE is not a class template", type);
2977 else
2978 error_at (location, "%qE is not a template", type);
2980 else
2981 error_at (location, "invalid template-id");
2982 /* Remember the location of the invalid "<". */
2983 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2984 start = cp_lexer_token_position (parser->lexer, true);
2985 /* Consume the "<". */
2986 cp_lexer_consume_token (parser->lexer);
2987 /* Parse the template arguments. */
2988 cp_parser_enclosed_template_argument_list (parser);
2989 /* Permanently remove the invalid template arguments so that
2990 this error message is not issued again. */
2991 if (start)
2992 cp_lexer_purge_tokens_after (parser->lexer, start);
2996 /* If parsing an integral constant-expression, issue an error message
2997 about the fact that THING appeared and return true. Otherwise,
2998 return false. In either case, set
2999 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3001 static bool
3002 cp_parser_non_integral_constant_expression (cp_parser *parser,
3003 non_integral_constant thing)
3005 parser->non_integral_constant_expression_p = true;
3006 if (parser->integral_constant_expression_p)
3008 if (!parser->allow_non_integral_constant_expression_p)
3010 const char *msg = NULL;
3011 switch (thing)
3013 case NIC_FLOAT:
3014 error ("floating-point literal "
3015 "cannot appear in a constant-expression");
3016 return true;
3017 case NIC_CAST:
3018 error ("a cast to a type other than an integral or "
3019 "enumeration type cannot appear in a "
3020 "constant-expression");
3021 return true;
3022 case NIC_TYPEID:
3023 error ("%<typeid%> operator "
3024 "cannot appear in a constant-expression");
3025 return true;
3026 case NIC_NCC:
3027 error ("non-constant compound literals "
3028 "cannot appear in a constant-expression");
3029 return true;
3030 case NIC_FUNC_CALL:
3031 error ("a function call "
3032 "cannot appear in a constant-expression");
3033 return true;
3034 case NIC_INC:
3035 error ("an increment "
3036 "cannot appear in a constant-expression");
3037 return true;
3038 case NIC_DEC:
3039 error ("an decrement "
3040 "cannot appear in a constant-expression");
3041 return true;
3042 case NIC_ARRAY_REF:
3043 error ("an array reference "
3044 "cannot appear in a constant-expression");
3045 return true;
3046 case NIC_ADDR_LABEL:
3047 error ("the address of a label "
3048 "cannot appear in a constant-expression");
3049 return true;
3050 case NIC_OVERLOADED:
3051 error ("calls to overloaded operators "
3052 "cannot appear in a constant-expression");
3053 return true;
3054 case NIC_ASSIGNMENT:
3055 error ("an assignment cannot appear in a constant-expression");
3056 return true;
3057 case NIC_COMMA:
3058 error ("a comma operator "
3059 "cannot appear in a constant-expression");
3060 return true;
3061 case NIC_CONSTRUCTOR:
3062 error ("a call to a constructor "
3063 "cannot appear in a constant-expression");
3064 return true;
3065 case NIC_TRANSACTION:
3066 error ("a transaction expression "
3067 "cannot appear in a constant-expression");
3068 return true;
3069 case NIC_THIS:
3070 msg = "this";
3071 break;
3072 case NIC_FUNC_NAME:
3073 msg = "__FUNCTION__";
3074 break;
3075 case NIC_PRETTY_FUNC:
3076 msg = "__PRETTY_FUNCTION__";
3077 break;
3078 case NIC_C99_FUNC:
3079 msg = "__func__";
3080 break;
3081 case NIC_VA_ARG:
3082 msg = "va_arg";
3083 break;
3084 case NIC_ARROW:
3085 msg = "->";
3086 break;
3087 case NIC_POINT:
3088 msg = ".";
3089 break;
3090 case NIC_STAR:
3091 msg = "*";
3092 break;
3093 case NIC_ADDR:
3094 msg = "&";
3095 break;
3096 case NIC_PREINCREMENT:
3097 msg = "++";
3098 break;
3099 case NIC_PREDECREMENT:
3100 msg = "--";
3101 break;
3102 case NIC_NEW:
3103 msg = "new";
3104 break;
3105 case NIC_DEL:
3106 msg = "delete";
3107 break;
3108 default:
3109 gcc_unreachable ();
3111 if (msg)
3112 error ("%qs cannot appear in a constant-expression", msg);
3113 return true;
3116 return false;
3119 /* Emit a diagnostic for an invalid type name. This function commits
3120 to the current active tentative parse, if any. (Otherwise, the
3121 problematic construct might be encountered again later, resulting
3122 in duplicate error messages.) LOCATION is the location of ID. */
3124 static void
3125 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3126 location_t location)
3128 tree decl, ambiguous_decls;
3129 cp_parser_commit_to_tentative_parse (parser);
3130 /* Try to lookup the identifier. */
3131 decl = cp_parser_lookup_name (parser, id, none_type,
3132 /*is_template=*/false,
3133 /*is_namespace=*/false,
3134 /*check_dependency=*/true,
3135 &ambiguous_decls, location);
3136 if (ambiguous_decls)
3137 /* If the lookup was ambiguous, an error will already have
3138 been issued. */
3139 return;
3140 /* If the lookup found a template-name, it means that the user forgot
3141 to specify an argument list. Emit a useful error message. */
3142 if (DECL_TYPE_TEMPLATE_P (decl))
3144 error_at (location,
3145 "invalid use of template-name %qE without an argument list",
3146 decl);
3147 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3149 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3150 error_at (location, "invalid use of destructor %qD as a type", id);
3151 else if (TREE_CODE (decl) == TYPE_DECL)
3152 /* Something like 'unsigned A a;' */
3153 error_at (location, "invalid combination of multiple type-specifiers");
3154 else if (!parser->scope)
3156 /* Issue an error message. */
3157 error_at (location, "%qE does not name a type", id);
3158 /* If we're in a template class, it's possible that the user was
3159 referring to a type from a base class. For example:
3161 template <typename T> struct A { typedef T X; };
3162 template <typename T> struct B : public A<T> { X x; };
3164 The user should have said "typename A<T>::X". */
3165 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3166 inform (location, "C++11 %<constexpr%> only available with "
3167 "-std=c++11 or -std=gnu++11");
3168 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3169 inform (location, "C++11 %<noexcept%> only available with "
3170 "-std=c++11 or -std=gnu++11");
3171 else if (cxx_dialect < cxx11
3172 && TREE_CODE (id) == IDENTIFIER_NODE
3173 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
3174 inform (location, "C++11 %<thread_local%> only available with "
3175 "-std=c++11 or -std=gnu++11");
3176 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3177 inform (location, "%<concept%> only available with -fconcepts");
3178 else if (processing_template_decl && current_class_type
3179 && TYPE_BINFO (current_class_type))
3181 tree b;
3183 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3185 b = TREE_CHAIN (b))
3187 tree base_type = BINFO_TYPE (b);
3188 if (CLASS_TYPE_P (base_type)
3189 && dependent_type_p (base_type))
3191 tree field;
3192 /* Go from a particular instantiation of the
3193 template (which will have an empty TYPE_FIELDs),
3194 to the main version. */
3195 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3196 for (field = TYPE_FIELDS (base_type);
3197 field;
3198 field = DECL_CHAIN (field))
3199 if (TREE_CODE (field) == TYPE_DECL
3200 && DECL_NAME (field) == id)
3202 inform (location,
3203 "(perhaps %<typename %T::%E%> was intended)",
3204 BINFO_TYPE (b), id);
3205 break;
3207 if (field)
3208 break;
3213 /* Here we diagnose qualified-ids where the scope is actually correct,
3214 but the identifier does not resolve to a valid type name. */
3215 else if (parser->scope != error_mark_node)
3217 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3219 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3220 error_at (location_of (id),
3221 "%qE in namespace %qE does not name a template type",
3222 id, parser->scope);
3223 else
3224 error_at (location_of (id),
3225 "%qE in namespace %qE does not name a type",
3226 id, parser->scope);
3227 if (DECL_P (decl))
3228 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3230 else if (CLASS_TYPE_P (parser->scope)
3231 && constructor_name_p (id, parser->scope))
3233 /* A<T>::A<T>() */
3234 error_at (location, "%<%T::%E%> names the constructor, not"
3235 " the type", parser->scope, id);
3236 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3237 error_at (location, "and %qT has no template constructors",
3238 parser->scope);
3240 else if (TYPE_P (parser->scope)
3241 && dependent_scope_p (parser->scope))
3242 error_at (location, "need %<typename%> before %<%T::%E%> because "
3243 "%qT is a dependent scope",
3244 parser->scope, id, parser->scope);
3245 else if (TYPE_P (parser->scope))
3247 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3248 error_at (location_of (id),
3249 "%qE in %q#T does not name a template type",
3250 id, parser->scope);
3251 else
3252 error_at (location_of (id),
3253 "%qE in %q#T does not name a type",
3254 id, parser->scope);
3255 if (DECL_P (decl))
3256 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3258 else
3259 gcc_unreachable ();
3263 /* Check for a common situation where a type-name should be present,
3264 but is not, and issue a sensible error message. Returns true if an
3265 invalid type-name was detected.
3267 The situation handled by this function are variable declarations of the
3268 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3269 Usually, `ID' should name a type, but if we got here it means that it
3270 does not. We try to emit the best possible error message depending on
3271 how exactly the id-expression looks like. */
3273 static bool
3274 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3276 tree id;
3277 cp_token *token = cp_lexer_peek_token (parser->lexer);
3279 /* Avoid duplicate error about ambiguous lookup. */
3280 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3282 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3283 if (next->type == CPP_NAME && next->error_reported)
3284 goto out;
3287 cp_parser_parse_tentatively (parser);
3288 id = cp_parser_id_expression (parser,
3289 /*template_keyword_p=*/false,
3290 /*check_dependency_p=*/true,
3291 /*template_p=*/NULL,
3292 /*declarator_p=*/true,
3293 /*optional_p=*/false);
3294 /* If the next token is a (, this is a function with no explicit return
3295 type, i.e. constructor, destructor or conversion op. */
3296 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3297 || TREE_CODE (id) == TYPE_DECL)
3299 cp_parser_abort_tentative_parse (parser);
3300 return false;
3302 if (!cp_parser_parse_definitely (parser))
3303 return false;
3305 /* Emit a diagnostic for the invalid type. */
3306 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3307 out:
3308 /* If we aren't in the middle of a declarator (i.e. in a
3309 parameter-declaration-clause), skip to the end of the declaration;
3310 there's no point in trying to process it. */
3311 if (!parser->in_declarator_p)
3312 cp_parser_skip_to_end_of_block_or_statement (parser);
3313 return true;
3316 /* Consume tokens up to, and including, the next non-nested closing `)'.
3317 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3318 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3319 found an unnested token of that type. */
3321 static int
3322 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3323 bool recovering,
3324 cpp_ttype or_ttype,
3325 bool consume_paren)
3327 unsigned paren_depth = 0;
3328 unsigned brace_depth = 0;
3329 unsigned square_depth = 0;
3331 if (recovering && or_ttype == CPP_EOF
3332 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3333 return 0;
3335 while (true)
3337 cp_token * token = cp_lexer_peek_token (parser->lexer);
3339 /* Have we found what we're looking for before the closing paren? */
3340 if (token->type == or_ttype && or_ttype != CPP_EOF
3341 && !brace_depth && !paren_depth && !square_depth)
3342 return -1;
3344 switch (token->type)
3346 case CPP_EOF:
3347 case CPP_PRAGMA_EOL:
3348 /* If we've run out of tokens, then there is no closing `)'. */
3349 return 0;
3351 /* This is good for lambda expression capture-lists. */
3352 case CPP_OPEN_SQUARE:
3353 ++square_depth;
3354 break;
3355 case CPP_CLOSE_SQUARE:
3356 if (!square_depth--)
3357 return 0;
3358 break;
3360 case CPP_SEMICOLON:
3361 /* This matches the processing in skip_to_end_of_statement. */
3362 if (!brace_depth)
3363 return 0;
3364 break;
3366 case CPP_OPEN_BRACE:
3367 ++brace_depth;
3368 break;
3369 case CPP_CLOSE_BRACE:
3370 if (!brace_depth--)
3371 return 0;
3372 break;
3374 case CPP_OPEN_PAREN:
3375 if (!brace_depth)
3376 ++paren_depth;
3377 break;
3379 case CPP_CLOSE_PAREN:
3380 if (!brace_depth && !paren_depth--)
3382 if (consume_paren)
3383 cp_lexer_consume_token (parser->lexer);
3384 return 1;
3386 break;
3388 default:
3389 break;
3392 /* Consume the token. */
3393 cp_lexer_consume_token (parser->lexer);
3397 /* Consume tokens up to, and including, the next non-nested closing `)'.
3398 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3399 are doing error recovery. Returns -1 if OR_COMMA is true and we
3400 found an unnested token of that type. */
3402 static int
3403 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3404 bool recovering,
3405 bool or_comma,
3406 bool consume_paren)
3408 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3409 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3410 ttype, consume_paren);
3413 /* Consume tokens until we reach the end of the current statement.
3414 Normally, that will be just before consuming a `;'. However, if a
3415 non-nested `}' comes first, then we stop before consuming that. */
3417 static void
3418 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3420 unsigned nesting_depth = 0;
3422 /* Unwind generic function template scope if necessary. */
3423 if (parser->fully_implicit_function_template_p)
3424 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3426 while (true)
3428 cp_token *token = cp_lexer_peek_token (parser->lexer);
3430 switch (token->type)
3432 case CPP_EOF:
3433 case CPP_PRAGMA_EOL:
3434 /* If we've run out of tokens, stop. */
3435 return;
3437 case CPP_SEMICOLON:
3438 /* If the next token is a `;', we have reached the end of the
3439 statement. */
3440 if (!nesting_depth)
3441 return;
3442 break;
3444 case CPP_CLOSE_BRACE:
3445 /* If this is a non-nested '}', stop before consuming it.
3446 That way, when confronted with something like:
3448 { 3 + }
3450 we stop before consuming the closing '}', even though we
3451 have not yet reached a `;'. */
3452 if (nesting_depth == 0)
3453 return;
3455 /* If it is the closing '}' for a block that we have
3456 scanned, stop -- but only after consuming the token.
3457 That way given:
3459 void f g () { ... }
3460 typedef int I;
3462 we will stop after the body of the erroneously declared
3463 function, but before consuming the following `typedef'
3464 declaration. */
3465 if (--nesting_depth == 0)
3467 cp_lexer_consume_token (parser->lexer);
3468 return;
3471 case CPP_OPEN_BRACE:
3472 ++nesting_depth;
3473 break;
3475 default:
3476 break;
3479 /* Consume the token. */
3480 cp_lexer_consume_token (parser->lexer);
3484 /* This function is called at the end of a statement or declaration.
3485 If the next token is a semicolon, it is consumed; otherwise, error
3486 recovery is attempted. */
3488 static void
3489 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3491 /* Look for the trailing `;'. */
3492 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3494 /* If there is additional (erroneous) input, skip to the end of
3495 the statement. */
3496 cp_parser_skip_to_end_of_statement (parser);
3497 /* If the next token is now a `;', consume it. */
3498 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3499 cp_lexer_consume_token (parser->lexer);
3503 /* Skip tokens until we have consumed an entire block, or until we
3504 have consumed a non-nested `;'. */
3506 static void
3507 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3509 int nesting_depth = 0;
3511 /* Unwind generic function template scope if necessary. */
3512 if (parser->fully_implicit_function_template_p)
3513 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3515 while (nesting_depth >= 0)
3517 cp_token *token = cp_lexer_peek_token (parser->lexer);
3519 switch (token->type)
3521 case CPP_EOF:
3522 case CPP_PRAGMA_EOL:
3523 /* If we've run out of tokens, stop. */
3524 return;
3526 case CPP_SEMICOLON:
3527 /* Stop if this is an unnested ';'. */
3528 if (!nesting_depth)
3529 nesting_depth = -1;
3530 break;
3532 case CPP_CLOSE_BRACE:
3533 /* Stop if this is an unnested '}', or closes the outermost
3534 nesting level. */
3535 nesting_depth--;
3536 if (nesting_depth < 0)
3537 return;
3538 if (!nesting_depth)
3539 nesting_depth = -1;
3540 break;
3542 case CPP_OPEN_BRACE:
3543 /* Nest. */
3544 nesting_depth++;
3545 break;
3547 default:
3548 break;
3551 /* Consume the token. */
3552 cp_lexer_consume_token (parser->lexer);
3556 /* Skip tokens until a non-nested closing curly brace is the next
3557 token, or there are no more tokens. Return true in the first case,
3558 false otherwise. */
3560 static bool
3561 cp_parser_skip_to_closing_brace (cp_parser *parser)
3563 unsigned nesting_depth = 0;
3565 while (true)
3567 cp_token *token = cp_lexer_peek_token (parser->lexer);
3569 switch (token->type)
3571 case CPP_EOF:
3572 case CPP_PRAGMA_EOL:
3573 /* If we've run out of tokens, stop. */
3574 return false;
3576 case CPP_CLOSE_BRACE:
3577 /* If the next token is a non-nested `}', then we have reached
3578 the end of the current block. */
3579 if (nesting_depth-- == 0)
3580 return true;
3581 break;
3583 case CPP_OPEN_BRACE:
3584 /* If it the next token is a `{', then we are entering a new
3585 block. Consume the entire block. */
3586 ++nesting_depth;
3587 break;
3589 default:
3590 break;
3593 /* Consume the token. */
3594 cp_lexer_consume_token (parser->lexer);
3598 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3599 parameter is the PRAGMA token, allowing us to purge the entire pragma
3600 sequence. */
3602 static void
3603 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3605 cp_token *token;
3607 parser->lexer->in_pragma = false;
3610 token = cp_lexer_consume_token (parser->lexer);
3611 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3613 /* Ensure that the pragma is not parsed again. */
3614 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3617 /* Require pragma end of line, resyncing with it as necessary. The
3618 arguments are as for cp_parser_skip_to_pragma_eol. */
3620 static void
3621 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3623 parser->lexer->in_pragma = false;
3624 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3625 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3628 /* This is a simple wrapper around make_typename_type. When the id is
3629 an unresolved identifier node, we can provide a superior diagnostic
3630 using cp_parser_diagnose_invalid_type_name. */
3632 static tree
3633 cp_parser_make_typename_type (cp_parser *parser, tree id,
3634 location_t id_location)
3636 tree result;
3637 if (identifier_p (id))
3639 result = make_typename_type (parser->scope, id, typename_type,
3640 /*complain=*/tf_none);
3641 if (result == error_mark_node)
3642 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3643 return result;
3645 return make_typename_type (parser->scope, id, typename_type, tf_error);
3648 /* This is a wrapper around the
3649 make_{pointer,ptrmem,reference}_declarator functions that decides
3650 which one to call based on the CODE and CLASS_TYPE arguments. The
3651 CODE argument should be one of the values returned by
3652 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3653 appertain to the pointer or reference. */
3655 static cp_declarator *
3656 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3657 cp_cv_quals cv_qualifiers,
3658 cp_declarator *target,
3659 tree attributes)
3661 if (code == ERROR_MARK)
3662 return cp_error_declarator;
3664 if (code == INDIRECT_REF)
3665 if (class_type == NULL_TREE)
3666 return make_pointer_declarator (cv_qualifiers, target, attributes);
3667 else
3668 return make_ptrmem_declarator (cv_qualifiers, class_type,
3669 target, attributes);
3670 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3671 return make_reference_declarator (cv_qualifiers, target,
3672 false, attributes);
3673 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3674 return make_reference_declarator (cv_qualifiers, target,
3675 true, attributes);
3676 gcc_unreachable ();
3679 /* Create a new C++ parser. */
3681 static cp_parser *
3682 cp_parser_new (void)
3684 cp_parser *parser;
3685 cp_lexer *lexer;
3686 unsigned i;
3688 /* cp_lexer_new_main is called before doing GC allocation because
3689 cp_lexer_new_main might load a PCH file. */
3690 lexer = cp_lexer_new_main ();
3692 /* Initialize the binops_by_token so that we can get the tree
3693 directly from the token. */
3694 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3695 binops_by_token[binops[i].token_type] = binops[i];
3697 parser = ggc_cleared_alloc<cp_parser> ();
3698 parser->lexer = lexer;
3699 parser->context = cp_parser_context_new (NULL);
3701 /* For now, we always accept GNU extensions. */
3702 parser->allow_gnu_extensions_p = 1;
3704 /* The `>' token is a greater-than operator, not the end of a
3705 template-id. */
3706 parser->greater_than_is_operator_p = true;
3708 parser->default_arg_ok_p = true;
3710 /* We are not parsing a constant-expression. */
3711 parser->integral_constant_expression_p = false;
3712 parser->allow_non_integral_constant_expression_p = false;
3713 parser->non_integral_constant_expression_p = false;
3715 /* Local variable names are not forbidden. */
3716 parser->local_variables_forbidden_p = false;
3718 /* We are not processing an `extern "C"' declaration. */
3719 parser->in_unbraced_linkage_specification_p = false;
3721 /* We are not processing a declarator. */
3722 parser->in_declarator_p = false;
3724 /* We are not processing a template-argument-list. */
3725 parser->in_template_argument_list_p = false;
3727 /* We are not in an iteration statement. */
3728 parser->in_statement = 0;
3730 /* We are not in a switch statement. */
3731 parser->in_switch_statement_p = false;
3733 /* We are not parsing a type-id inside an expression. */
3734 parser->in_type_id_in_expr_p = false;
3736 /* Declarations aren't implicitly extern "C". */
3737 parser->implicit_extern_c = false;
3739 /* String literals should be translated to the execution character set. */
3740 parser->translate_strings_p = true;
3742 /* We are not parsing a function body. */
3743 parser->in_function_body = false;
3745 /* We can correct until told otherwise. */
3746 parser->colon_corrects_to_scope_p = true;
3748 /* The unparsed function queue is empty. */
3749 push_unparsed_function_queues (parser);
3751 /* There are no classes being defined. */
3752 parser->num_classes_being_defined = 0;
3754 /* No template parameters apply. */
3755 parser->num_template_parameter_lists = 0;
3757 /* Not declaring an implicit function template. */
3758 parser->auto_is_implicit_function_template_parm_p = false;
3759 parser->fully_implicit_function_template_p = false;
3760 parser->implicit_template_parms = 0;
3761 parser->implicit_template_scope = 0;
3763 /* Active OpenACC routine clauses. */
3764 parser->oacc_routine = NULL;
3766 /* Allow constrained-type-specifiers. */
3767 parser->prevent_constrained_type_specifiers = 0;
3769 return parser;
3772 /* Create a cp_lexer structure which will emit the tokens in CACHE
3773 and push it onto the parser's lexer stack. This is used for delayed
3774 parsing of in-class method bodies and default arguments, and should
3775 not be confused with tentative parsing. */
3776 static void
3777 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3779 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3780 lexer->next = parser->lexer;
3781 parser->lexer = lexer;
3783 /* Move the current source position to that of the first token in the
3784 new lexer. */
3785 cp_lexer_set_source_position_from_token (lexer->next_token);
3788 /* Pop the top lexer off the parser stack. This is never used for the
3789 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3790 static void
3791 cp_parser_pop_lexer (cp_parser *parser)
3793 cp_lexer *lexer = parser->lexer;
3794 parser->lexer = lexer->next;
3795 cp_lexer_destroy (lexer);
3797 /* Put the current source position back where it was before this
3798 lexer was pushed. */
3799 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3802 /* Lexical conventions [gram.lex] */
3804 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3805 identifier. */
3807 static cp_expr
3808 cp_parser_identifier (cp_parser* parser)
3810 cp_token *token;
3812 /* Look for the identifier. */
3813 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3814 /* Return the value. */
3815 if (token)
3816 return cp_expr (token->u.value, token->location);
3817 else
3818 return error_mark_node;
3821 /* Parse a sequence of adjacent string constants. Returns a
3822 TREE_STRING representing the combined, nul-terminated string
3823 constant. If TRANSLATE is true, translate the string to the
3824 execution character set. If WIDE_OK is true, a wide string is
3825 invalid here.
3827 C++98 [lex.string] says that if a narrow string literal token is
3828 adjacent to a wide string literal token, the behavior is undefined.
3829 However, C99 6.4.5p4 says that this results in a wide string literal.
3830 We follow C99 here, for consistency with the C front end.
3832 This code is largely lifted from lex_string() in c-lex.c.
3834 FUTURE: ObjC++ will need to handle @-strings here. */
3835 static cp_expr
3836 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3837 bool lookup_udlit = true)
3839 tree value;
3840 size_t count;
3841 struct obstack str_ob;
3842 cpp_string str, istr, *strs;
3843 cp_token *tok;
3844 enum cpp_ttype type, curr_type;
3845 int have_suffix_p = 0;
3846 tree string_tree;
3847 tree suffix_id = NULL_TREE;
3848 bool curr_tok_is_userdef_p = false;
3850 tok = cp_lexer_peek_token (parser->lexer);
3851 if (!cp_parser_is_string_literal (tok))
3853 cp_parser_error (parser, "expected string-literal");
3854 return error_mark_node;
3857 location_t loc = tok->location;
3859 if (cpp_userdef_string_p (tok->type))
3861 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3862 curr_type = cpp_userdef_string_remove_type (tok->type);
3863 curr_tok_is_userdef_p = true;
3865 else
3867 string_tree = tok->u.value;
3868 curr_type = tok->type;
3870 type = curr_type;
3872 /* Try to avoid the overhead of creating and destroying an obstack
3873 for the common case of just one string. */
3874 if (!cp_parser_is_string_literal
3875 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3877 cp_lexer_consume_token (parser->lexer);
3879 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3880 str.len = TREE_STRING_LENGTH (string_tree);
3881 count = 1;
3883 if (curr_tok_is_userdef_p)
3885 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3886 have_suffix_p = 1;
3887 curr_type = cpp_userdef_string_remove_type (tok->type);
3889 else
3890 curr_type = tok->type;
3892 strs = &str;
3894 else
3896 location_t last_tok_loc = tok->location;
3897 gcc_obstack_init (&str_ob);
3898 count = 0;
3902 cp_lexer_consume_token (parser->lexer);
3903 count++;
3904 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3905 str.len = TREE_STRING_LENGTH (string_tree);
3907 if (curr_tok_is_userdef_p)
3909 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3910 if (have_suffix_p == 0)
3912 suffix_id = curr_suffix_id;
3913 have_suffix_p = 1;
3915 else if (have_suffix_p == 1
3916 && curr_suffix_id != suffix_id)
3918 error ("inconsistent user-defined literal suffixes"
3919 " %qD and %qD in string literal",
3920 suffix_id, curr_suffix_id);
3921 have_suffix_p = -1;
3923 curr_type = cpp_userdef_string_remove_type (tok->type);
3925 else
3926 curr_type = tok->type;
3928 if (type != curr_type)
3930 if (type == CPP_STRING)
3931 type = curr_type;
3932 else if (curr_type != CPP_STRING)
3934 rich_location rich_loc (line_table, tok->location);
3935 rich_loc.add_range (last_tok_loc, false);
3936 error_at_rich_loc (&rich_loc,
3937 "unsupported non-standard concatenation "
3938 "of string literals");
3942 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3944 last_tok_loc = tok->location;
3946 tok = cp_lexer_peek_token (parser->lexer);
3947 if (cpp_userdef_string_p (tok->type))
3949 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3950 curr_type = cpp_userdef_string_remove_type (tok->type);
3951 curr_tok_is_userdef_p = true;
3953 else
3955 string_tree = tok->u.value;
3956 curr_type = tok->type;
3957 curr_tok_is_userdef_p = false;
3960 while (cp_parser_is_string_literal (tok));
3962 /* A string literal built by concatenation has its caret=start at
3963 the start of the initial string, and its finish at the finish of
3964 the final string literal. */
3965 loc = make_location (loc, loc, get_finish (last_tok_loc));
3967 strs = (cpp_string *) obstack_finish (&str_ob);
3970 if (type != CPP_STRING && !wide_ok)
3972 cp_parser_error (parser, "a wide string is invalid in this context");
3973 type = CPP_STRING;
3976 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3977 (parse_in, strs, count, &istr, type))
3979 value = build_string (istr.len, (const char *)istr.text);
3980 free (CONST_CAST (unsigned char *, istr.text));
3982 switch (type)
3984 default:
3985 case CPP_STRING:
3986 case CPP_UTF8STRING:
3987 TREE_TYPE (value) = char_array_type_node;
3988 break;
3989 case CPP_STRING16:
3990 TREE_TYPE (value) = char16_array_type_node;
3991 break;
3992 case CPP_STRING32:
3993 TREE_TYPE (value) = char32_array_type_node;
3994 break;
3995 case CPP_WSTRING:
3996 TREE_TYPE (value) = wchar_array_type_node;
3997 break;
4000 value = fix_string_type (value);
4002 if (have_suffix_p)
4004 tree literal = build_userdef_literal (suffix_id, value,
4005 OT_NONE, NULL_TREE);
4006 if (lookup_udlit)
4007 value = cp_parser_userdef_string_literal (literal);
4008 else
4009 value = literal;
4012 else
4013 /* cpp_interpret_string has issued an error. */
4014 value = error_mark_node;
4016 if (count > 1)
4017 obstack_free (&str_ob, 0);
4019 return cp_expr (value, loc);
4022 /* Look up a literal operator with the name and the exact arguments. */
4024 static tree
4025 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4027 tree decl, fns;
4028 decl = lookup_name (name);
4029 if (!decl || !is_overloaded_fn (decl))
4030 return error_mark_node;
4032 for (fns = decl; fns; fns = OVL_NEXT (fns))
4034 unsigned int ix;
4035 bool found = true;
4036 tree fn = OVL_CURRENT (fns);
4037 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4038 if (parmtypes != NULL_TREE)
4040 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4041 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4043 tree tparm = TREE_VALUE (parmtypes);
4044 tree targ = TREE_TYPE ((*args)[ix]);
4045 bool ptr = TYPE_PTR_P (tparm);
4046 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4047 if ((ptr || arr || !same_type_p (tparm, targ))
4048 && (!ptr || !arr
4049 || !same_type_p (TREE_TYPE (tparm),
4050 TREE_TYPE (targ))))
4051 found = false;
4053 if (found
4054 && ix == vec_safe_length (args)
4055 /* May be this should be sufficient_parms_p instead,
4056 depending on how exactly should user-defined literals
4057 work in presence of default arguments on the literal
4058 operator parameters. */
4059 && parmtypes == void_list_node)
4060 return decl;
4064 return error_mark_node;
4067 /* Parse a user-defined char constant. Returns a call to a user-defined
4068 literal operator taking the character as an argument. */
4070 static cp_expr
4071 cp_parser_userdef_char_literal (cp_parser *parser)
4073 cp_token *token = cp_lexer_consume_token (parser->lexer);
4074 tree literal = token->u.value;
4075 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4076 tree value = USERDEF_LITERAL_VALUE (literal);
4077 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4078 tree decl, result;
4080 /* Build up a call to the user-defined operator */
4081 /* Lookup the name we got back from the id-expression. */
4082 vec<tree, va_gc> *args = make_tree_vector ();
4083 vec_safe_push (args, value);
4084 decl = lookup_literal_operator (name, args);
4085 if (!decl || decl == error_mark_node)
4087 error ("unable to find character literal operator %qD with %qT argument",
4088 name, TREE_TYPE (value));
4089 release_tree_vector (args);
4090 return error_mark_node;
4092 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4093 release_tree_vector (args);
4094 return result;
4097 /* A subroutine of cp_parser_userdef_numeric_literal to
4098 create a char... template parameter pack from a string node. */
4100 static tree
4101 make_char_string_pack (tree value)
4103 tree charvec;
4104 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4105 const char *str = TREE_STRING_POINTER (value);
4106 int i, len = TREE_STRING_LENGTH (value) - 1;
4107 tree argvec = make_tree_vec (1);
4109 /* Fill in CHARVEC with all of the parameters. */
4110 charvec = make_tree_vec (len);
4111 for (i = 0; i < len; ++i)
4112 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4114 /* Build the argument packs. */
4115 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4116 TREE_TYPE (argpack) = char_type_node;
4118 TREE_VEC_ELT (argvec, 0) = argpack;
4120 return argvec;
4123 /* A subroutine of cp_parser_userdef_numeric_literal to
4124 create a char... template parameter pack from a string node. */
4126 static tree
4127 make_string_pack (tree value)
4129 tree charvec;
4130 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4131 const unsigned char *str
4132 = (const unsigned char *) TREE_STRING_POINTER (value);
4133 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4134 int len = TREE_STRING_LENGTH (value) / sz - 1;
4135 tree argvec = make_tree_vec (2);
4137 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4138 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4140 /* First template parm is character type. */
4141 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4143 /* Fill in CHARVEC with all of the parameters. */
4144 charvec = make_tree_vec (len);
4145 for (int i = 0; i < len; ++i)
4146 TREE_VEC_ELT (charvec, i)
4147 = double_int_to_tree (str_char_type_node,
4148 double_int::from_buffer (str + i * sz, sz));
4150 /* Build the argument packs. */
4151 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4152 TREE_TYPE (argpack) = str_char_type_node;
4154 TREE_VEC_ELT (argvec, 1) = argpack;
4156 return argvec;
4159 /* Parse a user-defined numeric constant. returns a call to a user-defined
4160 literal operator. */
4162 static cp_expr
4163 cp_parser_userdef_numeric_literal (cp_parser *parser)
4165 cp_token *token = cp_lexer_consume_token (parser->lexer);
4166 tree literal = token->u.value;
4167 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4168 tree value = USERDEF_LITERAL_VALUE (literal);
4169 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4170 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4171 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4172 tree decl, result;
4173 vec<tree, va_gc> *args;
4175 /* Look for a literal operator taking the exact type of numeric argument
4176 as the literal value. */
4177 args = make_tree_vector ();
4178 vec_safe_push (args, value);
4179 decl = lookup_literal_operator (name, args);
4180 if (decl && decl != error_mark_node)
4182 result = finish_call_expr (decl, &args, false, true,
4183 tf_warning_or_error);
4185 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4187 warning_at (token->location, OPT_Woverflow,
4188 "integer literal exceeds range of %qT type",
4189 long_long_unsigned_type_node);
4191 else
4193 if (overflow > 0)
4194 warning_at (token->location, OPT_Woverflow,
4195 "floating literal exceeds range of %qT type",
4196 long_double_type_node);
4197 else if (overflow < 0)
4198 warning_at (token->location, OPT_Woverflow,
4199 "floating literal truncated to zero");
4202 release_tree_vector (args);
4203 return result;
4205 release_tree_vector (args);
4207 /* If the numeric argument didn't work, look for a raw literal
4208 operator taking a const char* argument consisting of the number
4209 in string format. */
4210 args = make_tree_vector ();
4211 vec_safe_push (args, num_string);
4212 decl = lookup_literal_operator (name, args);
4213 if (decl && decl != error_mark_node)
4215 result = finish_call_expr (decl, &args, false, true,
4216 tf_warning_or_error);
4217 release_tree_vector (args);
4218 return result;
4220 release_tree_vector (args);
4222 /* If the raw literal didn't work, look for a non-type template
4223 function with parameter pack char.... Call the function with
4224 template parameter characters representing the number. */
4225 args = make_tree_vector ();
4226 decl = lookup_literal_operator (name, args);
4227 if (decl && decl != error_mark_node)
4229 tree tmpl_args = make_char_string_pack (num_string);
4230 decl = lookup_template_function (decl, tmpl_args);
4231 result = finish_call_expr (decl, &args, false, true,
4232 tf_warning_or_error);
4233 release_tree_vector (args);
4234 return result;
4237 release_tree_vector (args);
4239 error ("unable to find numeric literal operator %qD", name);
4240 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4241 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4242 "to enable more built-in suffixes");
4243 return error_mark_node;
4246 /* Parse a user-defined string constant. Returns a call to a user-defined
4247 literal operator taking a character pointer and the length of the string
4248 as arguments. */
4250 static tree
4251 cp_parser_userdef_string_literal (tree literal)
4253 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4254 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4255 tree value = USERDEF_LITERAL_VALUE (literal);
4256 int len = TREE_STRING_LENGTH (value)
4257 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4258 tree decl, result;
4259 vec<tree, va_gc> *args;
4261 /* Build up a call to the user-defined operator. */
4262 /* Lookup the name we got back from the id-expression. */
4263 args = make_tree_vector ();
4264 vec_safe_push (args, value);
4265 vec_safe_push (args, build_int_cst (size_type_node, len));
4266 decl = lookup_literal_operator (name, args);
4268 if (decl && decl != error_mark_node)
4270 result = finish_call_expr (decl, &args, false, true,
4271 tf_warning_or_error);
4272 release_tree_vector (args);
4273 return result;
4275 release_tree_vector (args);
4277 /* Look for a template function with typename parameter CharT
4278 and parameter pack CharT... Call the function with
4279 template parameter characters representing the string. */
4280 args = make_tree_vector ();
4281 decl = lookup_literal_operator (name, args);
4282 if (decl && decl != error_mark_node)
4284 tree tmpl_args = make_string_pack (value);
4285 decl = lookup_template_function (decl, tmpl_args);
4286 result = finish_call_expr (decl, &args, false, true,
4287 tf_warning_or_error);
4288 release_tree_vector (args);
4289 return result;
4291 release_tree_vector (args);
4293 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4294 name, TREE_TYPE (value), size_type_node);
4295 return error_mark_node;
4299 /* Basic concepts [gram.basic] */
4301 /* Parse a translation-unit.
4303 translation-unit:
4304 declaration-seq [opt]
4306 Returns TRUE if all went well. */
4308 static bool
4309 cp_parser_translation_unit (cp_parser* parser)
4311 /* The address of the first non-permanent object on the declarator
4312 obstack. */
4313 static void *declarator_obstack_base;
4315 bool success;
4317 /* Create the declarator obstack, if necessary. */
4318 if (!cp_error_declarator)
4320 gcc_obstack_init (&declarator_obstack);
4321 /* Create the error declarator. */
4322 cp_error_declarator = make_declarator (cdk_error);
4323 /* Create the empty parameter list. */
4324 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4325 /* Remember where the base of the declarator obstack lies. */
4326 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4329 cp_parser_declaration_seq_opt (parser);
4331 /* If there are no tokens left then all went well. */
4332 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4334 /* Get rid of the token array; we don't need it any more. */
4335 cp_lexer_destroy (parser->lexer);
4336 parser->lexer = NULL;
4338 /* This file might have been a context that's implicitly extern
4339 "C". If so, pop the lang context. (Only relevant for PCH.) */
4340 if (parser->implicit_extern_c)
4342 pop_lang_context ();
4343 parser->implicit_extern_c = false;
4346 /* Finish up. */
4347 finish_translation_unit ();
4349 success = true;
4351 else
4353 cp_parser_error (parser, "expected declaration");
4354 success = false;
4357 /* Make sure the declarator obstack was fully cleaned up. */
4358 gcc_assert (obstack_next_free (&declarator_obstack)
4359 == declarator_obstack_base);
4361 /* All went well. */
4362 return success;
4365 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4366 decltype context. */
4368 static inline tsubst_flags_t
4369 complain_flags (bool decltype_p)
4371 tsubst_flags_t complain = tf_warning_or_error;
4372 if (decltype_p)
4373 complain |= tf_decltype;
4374 return complain;
4377 /* We're about to parse a collection of statements. If we're currently
4378 parsing tentatively, set up a firewall so that any nested
4379 cp_parser_commit_to_tentative_parse won't affect the current context. */
4381 static cp_token_position
4382 cp_parser_start_tentative_firewall (cp_parser *parser)
4384 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4385 return 0;
4387 cp_parser_parse_tentatively (parser);
4388 cp_parser_commit_to_topmost_tentative_parse (parser);
4389 return cp_lexer_token_position (parser->lexer, false);
4392 /* We've finished parsing the collection of statements. Wrap up the
4393 firewall and replace the relevant tokens with the parsed form. */
4395 static void
4396 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4397 tree expr)
4399 if (!start)
4400 return;
4402 /* Finish the firewall level. */
4403 cp_parser_parse_definitely (parser);
4404 /* And remember the result of the parse for when we try again. */
4405 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4406 token->type = CPP_PREPARSED_EXPR;
4407 token->u.value = expr;
4408 token->keyword = RID_MAX;
4409 cp_lexer_purge_tokens_after (parser->lexer, start);
4412 /* Like the above functions, but let the user modify the tokens. Used by
4413 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4414 later parses, so it makes sense to localize the effects of
4415 cp_parser_commit_to_tentative_parse. */
4417 struct tentative_firewall
4419 cp_parser *parser;
4420 bool set;
4422 tentative_firewall (cp_parser *p): parser(p)
4424 /* If we're currently parsing tentatively, start a committed level as a
4425 firewall and then an inner tentative parse. */
4426 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4428 cp_parser_parse_tentatively (parser);
4429 cp_parser_commit_to_topmost_tentative_parse (parser);
4430 cp_parser_parse_tentatively (parser);
4434 ~tentative_firewall()
4436 if (set)
4438 /* Finish the inner tentative parse and the firewall, propagating any
4439 uncommitted error state to the outer tentative parse. */
4440 bool err = cp_parser_error_occurred (parser);
4441 cp_parser_parse_definitely (parser);
4442 cp_parser_parse_definitely (parser);
4443 if (err)
4444 cp_parser_simulate_error (parser);
4449 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4450 enclosing parentheses. */
4452 static cp_expr
4453 cp_parser_statement_expr (cp_parser *parser)
4455 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4457 /* Consume the '('. */
4458 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4459 cp_lexer_consume_token (parser->lexer);
4460 /* Start the statement-expression. */
4461 tree expr = begin_stmt_expr ();
4462 /* Parse the compound-statement. */
4463 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4464 /* Finish up. */
4465 expr = finish_stmt_expr (expr, false);
4466 /* Consume the ')'. */
4467 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4468 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4469 cp_parser_skip_to_end_of_statement (parser);
4471 cp_parser_end_tentative_firewall (parser, start, expr);
4472 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4473 return cp_expr (expr, combined_loc);
4476 /* Expressions [gram.expr] */
4478 /* Parse a fold-operator.
4480 fold-operator:
4481 - * / % ^ & | = < > << >>
4482 = -= *= /= %= ^= &= |= <<= >>=
4483 == != <= >= && || , .* ->*
4485 This returns the tree code corresponding to the matched operator
4486 as an int. When the current token matches a compound assignment
4487 opertor, the resulting tree code is the negative value of the
4488 non-assignment operator. */
4490 static int
4491 cp_parser_fold_operator (cp_token *token)
4493 switch (token->type)
4495 case CPP_PLUS: return PLUS_EXPR;
4496 case CPP_MINUS: return MINUS_EXPR;
4497 case CPP_MULT: return MULT_EXPR;
4498 case CPP_DIV: return TRUNC_DIV_EXPR;
4499 case CPP_MOD: return TRUNC_MOD_EXPR;
4500 case CPP_XOR: return BIT_XOR_EXPR;
4501 case CPP_AND: return BIT_AND_EXPR;
4502 case CPP_OR: return BIT_IOR_EXPR;
4503 case CPP_LSHIFT: return LSHIFT_EXPR;
4504 case CPP_RSHIFT: return RSHIFT_EXPR;
4506 case CPP_EQ: return -NOP_EXPR;
4507 case CPP_PLUS_EQ: return -PLUS_EXPR;
4508 case CPP_MINUS_EQ: return -MINUS_EXPR;
4509 case CPP_MULT_EQ: return -MULT_EXPR;
4510 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4511 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4512 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4513 case CPP_AND_EQ: return -BIT_AND_EXPR;
4514 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4515 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4516 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4518 case CPP_EQ_EQ: return EQ_EXPR;
4519 case CPP_NOT_EQ: return NE_EXPR;
4520 case CPP_LESS: return LT_EXPR;
4521 case CPP_GREATER: return GT_EXPR;
4522 case CPP_LESS_EQ: return LE_EXPR;
4523 case CPP_GREATER_EQ: return GE_EXPR;
4525 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4526 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4528 case CPP_COMMA: return COMPOUND_EXPR;
4530 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4531 case CPP_DEREF_STAR: return MEMBER_REF;
4533 default: return ERROR_MARK;
4537 /* Returns true if CODE indicates a binary expression, which is not allowed in
4538 the LHS of a fold-expression. More codes will need to be added to use this
4539 function in other contexts. */
4541 static bool
4542 is_binary_op (tree_code code)
4544 switch (code)
4546 case PLUS_EXPR:
4547 case POINTER_PLUS_EXPR:
4548 case MINUS_EXPR:
4549 case MULT_EXPR:
4550 case TRUNC_DIV_EXPR:
4551 case TRUNC_MOD_EXPR:
4552 case BIT_XOR_EXPR:
4553 case BIT_AND_EXPR:
4554 case BIT_IOR_EXPR:
4555 case LSHIFT_EXPR:
4556 case RSHIFT_EXPR:
4558 case MODOP_EXPR:
4560 case EQ_EXPR:
4561 case NE_EXPR:
4562 case LE_EXPR:
4563 case GE_EXPR:
4564 case LT_EXPR:
4565 case GT_EXPR:
4567 case TRUTH_ANDIF_EXPR:
4568 case TRUTH_ORIF_EXPR:
4570 case COMPOUND_EXPR:
4572 case DOTSTAR_EXPR:
4573 case MEMBER_REF:
4574 return true;
4576 default:
4577 return false;
4581 /* If the next token is a suitable fold operator, consume it and return as
4582 the function above. */
4584 static int
4585 cp_parser_fold_operator (cp_parser *parser)
4587 cp_token* token = cp_lexer_peek_token (parser->lexer);
4588 int code = cp_parser_fold_operator (token);
4589 if (code != ERROR_MARK)
4590 cp_lexer_consume_token (parser->lexer);
4591 return code;
4594 /* Parse a fold-expression.
4596 fold-expression:
4597 ( ... folding-operator cast-expression)
4598 ( cast-expression folding-operator ... )
4599 ( cast-expression folding operator ... folding-operator cast-expression)
4601 Note that the '(' and ')' are matched in primary expression. */
4603 static cp_expr
4604 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4606 cp_id_kind pidk;
4608 // Left fold.
4609 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4611 cp_lexer_consume_token (parser->lexer);
4612 int op = cp_parser_fold_operator (parser);
4613 if (op == ERROR_MARK)
4615 cp_parser_error (parser, "expected binary operator");
4616 return error_mark_node;
4619 tree expr = cp_parser_cast_expression (parser, false, false,
4620 false, &pidk);
4621 if (expr == error_mark_node)
4622 return error_mark_node;
4623 return finish_left_unary_fold_expr (expr, op);
4626 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4627 int op = cp_parser_fold_operator (parser);
4628 if (op == ERROR_MARK)
4630 cp_parser_error (parser, "expected binary operator");
4631 return error_mark_node;
4634 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4636 cp_parser_error (parser, "expected ...");
4637 return error_mark_node;
4639 cp_lexer_consume_token (parser->lexer);
4641 /* The operands of a fold-expression are cast-expressions, so binary or
4642 conditional expressions are not allowed. We check this here to avoid
4643 tentative parsing. */
4644 if (is_binary_op (TREE_CODE (expr1)))
4645 error_at (location_of (expr1),
4646 "binary expression in operand of fold-expression");
4647 else if (TREE_CODE (expr1) == COND_EXPR)
4648 error_at (location_of (expr1),
4649 "conditional expression in operand of fold-expression");
4651 // Right fold.
4652 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4653 return finish_right_unary_fold_expr (expr1, op);
4655 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4657 cp_parser_error (parser, "mismatched operator in fold-expression");
4658 return error_mark_node;
4660 cp_lexer_consume_token (parser->lexer);
4662 // Binary left or right fold.
4663 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4664 if (expr2 == error_mark_node)
4665 return error_mark_node;
4666 return finish_binary_fold_expr (expr1, expr2, op);
4669 /* Parse a primary-expression.
4671 primary-expression:
4672 literal
4673 this
4674 ( expression )
4675 id-expression
4676 lambda-expression (C++11)
4678 GNU Extensions:
4680 primary-expression:
4681 ( compound-statement )
4682 __builtin_va_arg ( assignment-expression , type-id )
4683 __builtin_offsetof ( type-id , offsetof-expression )
4685 C++ Extensions:
4686 __has_nothrow_assign ( type-id )
4687 __has_nothrow_constructor ( type-id )
4688 __has_nothrow_copy ( type-id )
4689 __has_trivial_assign ( type-id )
4690 __has_trivial_constructor ( type-id )
4691 __has_trivial_copy ( type-id )
4692 __has_trivial_destructor ( type-id )
4693 __has_virtual_destructor ( type-id )
4694 __is_abstract ( type-id )
4695 __is_base_of ( type-id , type-id )
4696 __is_class ( type-id )
4697 __is_empty ( type-id )
4698 __is_enum ( type-id )
4699 __is_final ( type-id )
4700 __is_literal_type ( type-id )
4701 __is_pod ( type-id )
4702 __is_polymorphic ( type-id )
4703 __is_std_layout ( type-id )
4704 __is_trivial ( type-id )
4705 __is_union ( type-id )
4707 Objective-C++ Extension:
4709 primary-expression:
4710 objc-expression
4712 literal:
4713 __null
4715 ADDRESS_P is true iff this expression was immediately preceded by
4716 "&" and therefore might denote a pointer-to-member. CAST_P is true
4717 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4718 true iff this expression is a template argument.
4720 Returns a representation of the expression. Upon return, *IDK
4721 indicates what kind of id-expression (if any) was present. */
4723 static cp_expr
4724 cp_parser_primary_expression (cp_parser *parser,
4725 bool address_p,
4726 bool cast_p,
4727 bool template_arg_p,
4728 bool decltype_p,
4729 cp_id_kind *idk)
4731 cp_token *token = NULL;
4733 /* Assume the primary expression is not an id-expression. */
4734 *idk = CP_ID_KIND_NONE;
4736 /* Peek at the next token. */
4737 token = cp_lexer_peek_token (parser->lexer);
4738 switch ((int) token->type)
4740 /* literal:
4741 integer-literal
4742 character-literal
4743 floating-literal
4744 string-literal
4745 boolean-literal
4746 pointer-literal
4747 user-defined-literal */
4748 case CPP_CHAR:
4749 case CPP_CHAR16:
4750 case CPP_CHAR32:
4751 case CPP_WCHAR:
4752 case CPP_UTF8CHAR:
4753 case CPP_NUMBER:
4754 case CPP_PREPARSED_EXPR:
4755 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4756 return cp_parser_userdef_numeric_literal (parser);
4757 token = cp_lexer_consume_token (parser->lexer);
4758 if (TREE_CODE (token->u.value) == FIXED_CST)
4760 error_at (token->location,
4761 "fixed-point types not supported in C++");
4762 return error_mark_node;
4764 /* Floating-point literals are only allowed in an integral
4765 constant expression if they are cast to an integral or
4766 enumeration type. */
4767 if (TREE_CODE (token->u.value) == REAL_CST
4768 && parser->integral_constant_expression_p
4769 && pedantic)
4771 /* CAST_P will be set even in invalid code like "int(2.7 +
4772 ...)". Therefore, we have to check that the next token
4773 is sure to end the cast. */
4774 if (cast_p)
4776 cp_token *next_token;
4778 next_token = cp_lexer_peek_token (parser->lexer);
4779 if (/* The comma at the end of an
4780 enumerator-definition. */
4781 next_token->type != CPP_COMMA
4782 /* The curly brace at the end of an enum-specifier. */
4783 && next_token->type != CPP_CLOSE_BRACE
4784 /* The end of a statement. */
4785 && next_token->type != CPP_SEMICOLON
4786 /* The end of the cast-expression. */
4787 && next_token->type != CPP_CLOSE_PAREN
4788 /* The end of an array bound. */
4789 && next_token->type != CPP_CLOSE_SQUARE
4790 /* The closing ">" in a template-argument-list. */
4791 && (next_token->type != CPP_GREATER
4792 || parser->greater_than_is_operator_p)
4793 /* C++0x only: A ">>" treated like two ">" tokens,
4794 in a template-argument-list. */
4795 && (next_token->type != CPP_RSHIFT
4796 || (cxx_dialect == cxx98)
4797 || parser->greater_than_is_operator_p))
4798 cast_p = false;
4801 /* If we are within a cast, then the constraint that the
4802 cast is to an integral or enumeration type will be
4803 checked at that point. If we are not within a cast, then
4804 this code is invalid. */
4805 if (!cast_p)
4806 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4808 return cp_expr (token->u.value, token->location);
4810 case CPP_CHAR_USERDEF:
4811 case CPP_CHAR16_USERDEF:
4812 case CPP_CHAR32_USERDEF:
4813 case CPP_WCHAR_USERDEF:
4814 case CPP_UTF8CHAR_USERDEF:
4815 return cp_parser_userdef_char_literal (parser);
4817 case CPP_STRING:
4818 case CPP_STRING16:
4819 case CPP_STRING32:
4820 case CPP_WSTRING:
4821 case CPP_UTF8STRING:
4822 case CPP_STRING_USERDEF:
4823 case CPP_STRING16_USERDEF:
4824 case CPP_STRING32_USERDEF:
4825 case CPP_WSTRING_USERDEF:
4826 case CPP_UTF8STRING_USERDEF:
4827 /* ??? Should wide strings be allowed when parser->translate_strings_p
4828 is false (i.e. in attributes)? If not, we can kill the third
4829 argument to cp_parser_string_literal. */
4830 return cp_parser_string_literal (parser,
4831 parser->translate_strings_p,
4832 true);
4834 case CPP_OPEN_PAREN:
4835 /* If we see `( { ' then we are looking at the beginning of
4836 a GNU statement-expression. */
4837 if (cp_parser_allow_gnu_extensions_p (parser)
4838 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4840 /* Statement-expressions are not allowed by the standard. */
4841 pedwarn (token->location, OPT_Wpedantic,
4842 "ISO C++ forbids braced-groups within expressions");
4844 /* And they're not allowed outside of a function-body; you
4845 cannot, for example, write:
4847 int i = ({ int j = 3; j + 1; });
4849 at class or namespace scope. */
4850 if (!parser->in_function_body
4851 || parser->in_template_argument_list_p)
4853 error_at (token->location,
4854 "statement-expressions are not allowed outside "
4855 "functions nor in template-argument lists");
4856 cp_parser_skip_to_end_of_block_or_statement (parser);
4857 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4858 cp_lexer_consume_token (parser->lexer);
4859 return error_mark_node;
4861 else
4862 return cp_parser_statement_expr (parser);
4864 /* Otherwise it's a normal parenthesized expression. */
4866 cp_expr expr;
4867 bool saved_greater_than_is_operator_p;
4869 location_t open_paren_loc = token->location;
4871 /* Consume the `('. */
4872 cp_lexer_consume_token (parser->lexer);
4873 /* Within a parenthesized expression, a `>' token is always
4874 the greater-than operator. */
4875 saved_greater_than_is_operator_p
4876 = parser->greater_than_is_operator_p;
4877 parser->greater_than_is_operator_p = true;
4879 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4880 /* Left fold expression. */
4881 expr = NULL_TREE;
4882 else
4883 /* Parse the parenthesized expression. */
4884 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4886 token = cp_lexer_peek_token (parser->lexer);
4887 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
4889 expr = cp_parser_fold_expression (parser, expr);
4890 if (expr != error_mark_node
4891 && cxx_dialect < cxx1z
4892 && !in_system_header_at (input_location))
4893 pedwarn (input_location, 0, "fold-expressions only available "
4894 "with -std=c++1z or -std=gnu++1z");
4896 else
4897 /* Let the front end know that this expression was
4898 enclosed in parentheses. This matters in case, for
4899 example, the expression is of the form `A::B', since
4900 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4901 not. */
4902 expr = finish_parenthesized_expr (expr);
4904 /* DR 705: Wrapping an unqualified name in parentheses
4905 suppresses arg-dependent lookup. We want to pass back
4906 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4907 (c++/37862), but none of the others. */
4908 if (*idk != CP_ID_KIND_QUALIFIED)
4909 *idk = CP_ID_KIND_NONE;
4911 /* The `>' token might be the end of a template-id or
4912 template-parameter-list now. */
4913 parser->greater_than_is_operator_p
4914 = saved_greater_than_is_operator_p;
4916 /* Consume the `)'. */
4917 token = cp_lexer_peek_token (parser->lexer);
4918 location_t close_paren_loc = token->location;
4919 expr.set_range (open_paren_loc, close_paren_loc);
4920 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)
4921 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4922 cp_parser_skip_to_end_of_statement (parser);
4924 return expr;
4927 case CPP_OPEN_SQUARE:
4929 if (c_dialect_objc ())
4931 /* We might have an Objective-C++ message. */
4932 cp_parser_parse_tentatively (parser);
4933 tree msg = cp_parser_objc_message_expression (parser);
4934 /* If that works out, we're done ... */
4935 if (cp_parser_parse_definitely (parser))
4936 return msg;
4937 /* ... else, fall though to see if it's a lambda. */
4939 cp_expr lam = cp_parser_lambda_expression (parser);
4940 /* Don't warn about a failed tentative parse. */
4941 if (cp_parser_error_occurred (parser))
4942 return error_mark_node;
4943 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4944 return lam;
4947 case CPP_OBJC_STRING:
4948 if (c_dialect_objc ())
4949 /* We have an Objective-C++ string literal. */
4950 return cp_parser_objc_expression (parser);
4951 cp_parser_error (parser, "expected primary-expression");
4952 return error_mark_node;
4954 case CPP_KEYWORD:
4955 switch (token->keyword)
4957 /* These two are the boolean literals. */
4958 case RID_TRUE:
4959 cp_lexer_consume_token (parser->lexer);
4960 return cp_expr (boolean_true_node, token->location);
4961 case RID_FALSE:
4962 cp_lexer_consume_token (parser->lexer);
4963 return cp_expr (boolean_false_node, token->location);
4965 /* The `__null' literal. */
4966 case RID_NULL:
4967 cp_lexer_consume_token (parser->lexer);
4968 return cp_expr (null_node, token->location);
4970 /* The `nullptr' literal. */
4971 case RID_NULLPTR:
4972 cp_lexer_consume_token (parser->lexer);
4973 return cp_expr (nullptr_node, token->location);
4975 /* Recognize the `this' keyword. */
4976 case RID_THIS:
4977 cp_lexer_consume_token (parser->lexer);
4978 if (parser->local_variables_forbidden_p)
4980 error_at (token->location,
4981 "%<this%> may not be used in this context");
4982 return error_mark_node;
4984 /* Pointers cannot appear in constant-expressions. */
4985 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4986 return error_mark_node;
4987 return cp_expr (finish_this_expr (), token->location);
4989 /* The `operator' keyword can be the beginning of an
4990 id-expression. */
4991 case RID_OPERATOR:
4992 goto id_expression;
4994 case RID_FUNCTION_NAME:
4995 case RID_PRETTY_FUNCTION_NAME:
4996 case RID_C99_FUNCTION_NAME:
4998 non_integral_constant name;
5000 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5001 __func__ are the names of variables -- but they are
5002 treated specially. Therefore, they are handled here,
5003 rather than relying on the generic id-expression logic
5004 below. Grammatically, these names are id-expressions.
5006 Consume the token. */
5007 token = cp_lexer_consume_token (parser->lexer);
5009 switch (token->keyword)
5011 case RID_FUNCTION_NAME:
5012 name = NIC_FUNC_NAME;
5013 break;
5014 case RID_PRETTY_FUNCTION_NAME:
5015 name = NIC_PRETTY_FUNC;
5016 break;
5017 case RID_C99_FUNCTION_NAME:
5018 name = NIC_C99_FUNC;
5019 break;
5020 default:
5021 gcc_unreachable ();
5024 if (cp_parser_non_integral_constant_expression (parser, name))
5025 return error_mark_node;
5027 /* Look up the name. */
5028 return finish_fname (token->u.value);
5031 case RID_VA_ARG:
5033 tree expression;
5034 tree type;
5035 source_location type_location;
5036 location_t start_loc
5037 = cp_lexer_peek_token (parser->lexer)->location;
5038 /* The `__builtin_va_arg' construct is used to handle
5039 `va_arg'. Consume the `__builtin_va_arg' token. */
5040 cp_lexer_consume_token (parser->lexer);
5041 /* Look for the opening `('. */
5042 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5043 /* Now, parse the assignment-expression. */
5044 expression = cp_parser_assignment_expression (parser);
5045 /* Look for the `,'. */
5046 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5047 type_location = cp_lexer_peek_token (parser->lexer)->location;
5048 /* Parse the type-id. */
5050 type_id_in_expr_sentinel s (parser);
5051 type = cp_parser_type_id (parser);
5053 /* Look for the closing `)'. */
5054 location_t finish_loc
5055 = cp_lexer_peek_token (parser->lexer)->location;
5056 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5057 /* Using `va_arg' in a constant-expression is not
5058 allowed. */
5059 if (cp_parser_non_integral_constant_expression (parser,
5060 NIC_VA_ARG))
5061 return error_mark_node;
5062 /* Construct a location of the form:
5063 __builtin_va_arg (v, int)
5064 ~~~~~~~~~~~~~~~~~~~~~^~~~
5065 with the caret at the type, ranging from the start of the
5066 "__builtin_va_arg" token to the close paren. */
5067 location_t combined_loc
5068 = make_location (type_location, start_loc, finish_loc);
5069 return build_x_va_arg (combined_loc, expression, type);
5072 case RID_OFFSETOF:
5073 return cp_parser_builtin_offsetof (parser);
5075 case RID_HAS_NOTHROW_ASSIGN:
5076 case RID_HAS_NOTHROW_CONSTRUCTOR:
5077 case RID_HAS_NOTHROW_COPY:
5078 case RID_HAS_TRIVIAL_ASSIGN:
5079 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5080 case RID_HAS_TRIVIAL_COPY:
5081 case RID_HAS_TRIVIAL_DESTRUCTOR:
5082 case RID_HAS_VIRTUAL_DESTRUCTOR:
5083 case RID_IS_ABSTRACT:
5084 case RID_IS_BASE_OF:
5085 case RID_IS_CLASS:
5086 case RID_IS_EMPTY:
5087 case RID_IS_ENUM:
5088 case RID_IS_FINAL:
5089 case RID_IS_LITERAL_TYPE:
5090 case RID_IS_POD:
5091 case RID_IS_POLYMORPHIC:
5092 case RID_IS_SAME_AS:
5093 case RID_IS_STD_LAYOUT:
5094 case RID_IS_TRIVIAL:
5095 case RID_IS_TRIVIALLY_ASSIGNABLE:
5096 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5097 case RID_IS_TRIVIALLY_COPYABLE:
5098 case RID_IS_UNION:
5099 return cp_parser_trait_expr (parser, token->keyword);
5101 // C++ concepts
5102 case RID_REQUIRES:
5103 return cp_parser_requires_expression (parser);
5105 /* Objective-C++ expressions. */
5106 case RID_AT_ENCODE:
5107 case RID_AT_PROTOCOL:
5108 case RID_AT_SELECTOR:
5109 return cp_parser_objc_expression (parser);
5111 case RID_TEMPLATE:
5112 if (parser->in_function_body
5113 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5114 == CPP_LESS))
5116 error_at (token->location,
5117 "a template declaration cannot appear at block scope");
5118 cp_parser_skip_to_end_of_block_or_statement (parser);
5119 return error_mark_node;
5121 default:
5122 cp_parser_error (parser, "expected primary-expression");
5123 return error_mark_node;
5126 /* An id-expression can start with either an identifier, a
5127 `::' as the beginning of a qualified-id, or the "operator"
5128 keyword. */
5129 case CPP_NAME:
5130 case CPP_SCOPE:
5131 case CPP_TEMPLATE_ID:
5132 case CPP_NESTED_NAME_SPECIFIER:
5134 id_expression:
5135 cp_expr id_expression;
5136 cp_expr decl;
5137 const char *error_msg;
5138 bool template_p;
5139 bool done;
5140 cp_token *id_expr_token;
5142 /* Parse the id-expression. */
5143 id_expression
5144 = cp_parser_id_expression (parser,
5145 /*template_keyword_p=*/false,
5146 /*check_dependency_p=*/true,
5147 &template_p,
5148 /*declarator_p=*/false,
5149 /*optional_p=*/false);
5150 if (id_expression == error_mark_node)
5151 return error_mark_node;
5152 id_expr_token = token;
5153 token = cp_lexer_peek_token (parser->lexer);
5154 done = (token->type != CPP_OPEN_SQUARE
5155 && token->type != CPP_OPEN_PAREN
5156 && token->type != CPP_DOT
5157 && token->type != CPP_DEREF
5158 && token->type != CPP_PLUS_PLUS
5159 && token->type != CPP_MINUS_MINUS);
5160 /* If we have a template-id, then no further lookup is
5161 required. If the template-id was for a template-class, we
5162 will sometimes have a TYPE_DECL at this point. */
5163 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5164 || TREE_CODE (id_expression) == TYPE_DECL)
5165 decl = id_expression;
5166 /* Look up the name. */
5167 else
5169 tree ambiguous_decls;
5171 /* If we already know that this lookup is ambiguous, then
5172 we've already issued an error message; there's no reason
5173 to check again. */
5174 if (id_expr_token->type == CPP_NAME
5175 && id_expr_token->error_reported)
5177 cp_parser_simulate_error (parser);
5178 return error_mark_node;
5181 decl = cp_parser_lookup_name (parser, id_expression,
5182 none_type,
5183 template_p,
5184 /*is_namespace=*/false,
5185 /*check_dependency=*/true,
5186 &ambiguous_decls,
5187 id_expr_token->location);
5188 /* If the lookup was ambiguous, an error will already have
5189 been issued. */
5190 if (ambiguous_decls)
5191 return error_mark_node;
5193 /* In Objective-C++, we may have an Objective-C 2.0
5194 dot-syntax for classes here. */
5195 if (c_dialect_objc ()
5196 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5197 && TREE_CODE (decl) == TYPE_DECL
5198 && objc_is_class_name (decl))
5200 tree component;
5201 cp_lexer_consume_token (parser->lexer);
5202 component = cp_parser_identifier (parser);
5203 if (component == error_mark_node)
5204 return error_mark_node;
5206 tree result = objc_build_class_component_ref (id_expression,
5207 component);
5208 /* Build a location of the form:
5209 expr.component
5210 ~~~~~^~~~~~~~~
5211 with caret at the start of the component name (at
5212 input_location), ranging from the start of the id_expression
5213 to the end of the component name. */
5214 location_t combined_loc
5215 = make_location (input_location, id_expression.get_start (),
5216 get_finish (input_location));
5217 protected_set_expr_location (result, combined_loc);
5218 return result;
5221 /* In Objective-C++, an instance variable (ivar) may be preferred
5222 to whatever cp_parser_lookup_name() found.
5223 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5224 rest of c-family, we have to do a little extra work to preserve
5225 any location information in cp_expr "decl". Given that
5226 objc_lookup_ivar is implemented in "c-family" and "objc", we
5227 have a trip through the pure "tree" type, rather than cp_expr.
5228 Naively copying it back to "decl" would implicitly give the
5229 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5230 store an EXPR_LOCATION. Hence we only update "decl" (and
5231 hence its location_t) if we get back a different tree node. */
5232 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5233 id_expression);
5234 if (decl_tree != decl.get_value ())
5235 decl = cp_expr (decl_tree);
5237 /* If name lookup gives us a SCOPE_REF, then the
5238 qualifying scope was dependent. */
5239 if (TREE_CODE (decl) == SCOPE_REF)
5241 /* At this point, we do not know if DECL is a valid
5242 integral constant expression. We assume that it is
5243 in fact such an expression, so that code like:
5245 template <int N> struct A {
5246 int a[B<N>::i];
5249 is accepted. At template-instantiation time, we
5250 will check that B<N>::i is actually a constant. */
5251 return decl;
5253 /* Check to see if DECL is a local variable in a context
5254 where that is forbidden. */
5255 if (parser->local_variables_forbidden_p
5256 && local_variable_p (decl))
5258 /* It might be that we only found DECL because we are
5259 trying to be generous with pre-ISO scoping rules.
5260 For example, consider:
5262 int i;
5263 void g() {
5264 for (int i = 0; i < 10; ++i) {}
5265 extern void f(int j = i);
5268 Here, name look up will originally find the out
5269 of scope `i'. We need to issue a warning message,
5270 but then use the global `i'. */
5271 decl = check_for_out_of_scope_variable (decl);
5272 if (local_variable_p (decl))
5274 error_at (id_expr_token->location,
5275 "local variable %qD may not appear in this context",
5276 decl.get_value ());
5277 return error_mark_node;
5282 decl = (finish_id_expression
5283 (id_expression, decl, parser->scope,
5284 idk,
5285 parser->integral_constant_expression_p,
5286 parser->allow_non_integral_constant_expression_p,
5287 &parser->non_integral_constant_expression_p,
5288 template_p, done, address_p,
5289 template_arg_p,
5290 &error_msg,
5291 id_expr_token->location));
5292 if (error_msg)
5293 cp_parser_error (parser, error_msg);
5294 decl.set_location (id_expr_token->location);
5295 return decl;
5298 /* Anything else is an error. */
5299 default:
5300 cp_parser_error (parser, "expected primary-expression");
5301 return error_mark_node;
5305 static inline cp_expr
5306 cp_parser_primary_expression (cp_parser *parser,
5307 bool address_p,
5308 bool cast_p,
5309 bool template_arg_p,
5310 cp_id_kind *idk)
5312 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5313 /*decltype*/false, idk);
5316 /* Parse an id-expression.
5318 id-expression:
5319 unqualified-id
5320 qualified-id
5322 qualified-id:
5323 :: [opt] nested-name-specifier template [opt] unqualified-id
5324 :: identifier
5325 :: operator-function-id
5326 :: template-id
5328 Return a representation of the unqualified portion of the
5329 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5330 a `::' or nested-name-specifier.
5332 Often, if the id-expression was a qualified-id, the caller will
5333 want to make a SCOPE_REF to represent the qualified-id. This
5334 function does not do this in order to avoid wastefully creating
5335 SCOPE_REFs when they are not required.
5337 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5338 `template' keyword.
5340 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5341 uninstantiated templates.
5343 If *TEMPLATE_P is non-NULL, it is set to true iff the
5344 `template' keyword is used to explicitly indicate that the entity
5345 named is a template.
5347 If DECLARATOR_P is true, the id-expression is appearing as part of
5348 a declarator, rather than as part of an expression. */
5350 static cp_expr
5351 cp_parser_id_expression (cp_parser *parser,
5352 bool template_keyword_p,
5353 bool check_dependency_p,
5354 bool *template_p,
5355 bool declarator_p,
5356 bool optional_p)
5358 bool global_scope_p;
5359 bool nested_name_specifier_p;
5361 /* Assume the `template' keyword was not used. */
5362 if (template_p)
5363 *template_p = template_keyword_p;
5365 /* Look for the optional `::' operator. */
5366 global_scope_p
5367 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
5368 != NULL_TREE);
5369 /* Look for the optional nested-name-specifier. */
5370 nested_name_specifier_p
5371 = (cp_parser_nested_name_specifier_opt (parser,
5372 /*typename_keyword_p=*/false,
5373 check_dependency_p,
5374 /*type_p=*/false,
5375 declarator_p)
5376 != NULL_TREE);
5377 /* If there is a nested-name-specifier, then we are looking at
5378 the first qualified-id production. */
5379 if (nested_name_specifier_p)
5381 tree saved_scope;
5382 tree saved_object_scope;
5383 tree saved_qualifying_scope;
5384 tree unqualified_id;
5385 bool is_template;
5387 /* See if the next token is the `template' keyword. */
5388 if (!template_p)
5389 template_p = &is_template;
5390 *template_p = cp_parser_optional_template_keyword (parser);
5391 /* Name lookup we do during the processing of the
5392 unqualified-id might obliterate SCOPE. */
5393 saved_scope = parser->scope;
5394 saved_object_scope = parser->object_scope;
5395 saved_qualifying_scope = parser->qualifying_scope;
5396 /* Process the final unqualified-id. */
5397 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5398 check_dependency_p,
5399 declarator_p,
5400 /*optional_p=*/false);
5401 /* Restore the SAVED_SCOPE for our caller. */
5402 parser->scope = saved_scope;
5403 parser->object_scope = saved_object_scope;
5404 parser->qualifying_scope = saved_qualifying_scope;
5406 return unqualified_id;
5408 /* Otherwise, if we are in global scope, then we are looking at one
5409 of the other qualified-id productions. */
5410 else if (global_scope_p)
5412 cp_token *token;
5413 tree id;
5415 /* Peek at the next token. */
5416 token = cp_lexer_peek_token (parser->lexer);
5418 /* If it's an identifier, and the next token is not a "<", then
5419 we can avoid the template-id case. This is an optimization
5420 for this common case. */
5421 if (token->type == CPP_NAME
5422 && !cp_parser_nth_token_starts_template_argument_list_p
5423 (parser, 2))
5424 return cp_parser_identifier (parser);
5426 cp_parser_parse_tentatively (parser);
5427 /* Try a template-id. */
5428 id = cp_parser_template_id (parser,
5429 /*template_keyword_p=*/false,
5430 /*check_dependency_p=*/true,
5431 none_type,
5432 declarator_p);
5433 /* If that worked, we're done. */
5434 if (cp_parser_parse_definitely (parser))
5435 return id;
5437 /* Peek at the next token. (Changes in the token buffer may
5438 have invalidated the pointer obtained above.) */
5439 token = cp_lexer_peek_token (parser->lexer);
5441 switch (token->type)
5443 case CPP_NAME:
5444 return cp_parser_identifier (parser);
5446 case CPP_KEYWORD:
5447 if (token->keyword == RID_OPERATOR)
5448 return cp_parser_operator_function_id (parser);
5449 /* Fall through. */
5451 default:
5452 cp_parser_error (parser, "expected id-expression");
5453 return error_mark_node;
5456 else
5457 return cp_parser_unqualified_id (parser, template_keyword_p,
5458 /*check_dependency_p=*/true,
5459 declarator_p,
5460 optional_p);
5463 /* Parse an unqualified-id.
5465 unqualified-id:
5466 identifier
5467 operator-function-id
5468 conversion-function-id
5469 ~ class-name
5470 template-id
5472 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5473 keyword, in a construct like `A::template ...'.
5475 Returns a representation of unqualified-id. For the `identifier'
5476 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5477 production a BIT_NOT_EXPR is returned; the operand of the
5478 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5479 other productions, see the documentation accompanying the
5480 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5481 names are looked up in uninstantiated templates. If DECLARATOR_P
5482 is true, the unqualified-id is appearing as part of a declarator,
5483 rather than as part of an expression. */
5485 static cp_expr
5486 cp_parser_unqualified_id (cp_parser* parser,
5487 bool template_keyword_p,
5488 bool check_dependency_p,
5489 bool declarator_p,
5490 bool optional_p)
5492 cp_token *token;
5494 /* Peek at the next token. */
5495 token = cp_lexer_peek_token (parser->lexer);
5497 switch ((int) token->type)
5499 case CPP_NAME:
5501 tree id;
5503 /* We don't know yet whether or not this will be a
5504 template-id. */
5505 cp_parser_parse_tentatively (parser);
5506 /* Try a template-id. */
5507 id = cp_parser_template_id (parser, template_keyword_p,
5508 check_dependency_p,
5509 none_type,
5510 declarator_p);
5511 /* If it worked, we're done. */
5512 if (cp_parser_parse_definitely (parser))
5513 return id;
5514 /* Otherwise, it's an ordinary identifier. */
5515 return cp_parser_identifier (parser);
5518 case CPP_TEMPLATE_ID:
5519 return cp_parser_template_id (parser, template_keyword_p,
5520 check_dependency_p,
5521 none_type,
5522 declarator_p);
5524 case CPP_COMPL:
5526 tree type_decl;
5527 tree qualifying_scope;
5528 tree object_scope;
5529 tree scope;
5530 bool done;
5532 /* Consume the `~' token. */
5533 cp_lexer_consume_token (parser->lexer);
5534 /* Parse the class-name. The standard, as written, seems to
5535 say that:
5537 template <typename T> struct S { ~S (); };
5538 template <typename T> S<T>::~S() {}
5540 is invalid, since `~' must be followed by a class-name, but
5541 `S<T>' is dependent, and so not known to be a class.
5542 That's not right; we need to look in uninstantiated
5543 templates. A further complication arises from:
5545 template <typename T> void f(T t) {
5546 t.T::~T();
5549 Here, it is not possible to look up `T' in the scope of `T'
5550 itself. We must look in both the current scope, and the
5551 scope of the containing complete expression.
5553 Yet another issue is:
5555 struct S {
5556 int S;
5557 ~S();
5560 S::~S() {}
5562 The standard does not seem to say that the `S' in `~S'
5563 should refer to the type `S' and not the data member
5564 `S::S'. */
5566 /* DR 244 says that we look up the name after the "~" in the
5567 same scope as we looked up the qualifying name. That idea
5568 isn't fully worked out; it's more complicated than that. */
5569 scope = parser->scope;
5570 object_scope = parser->object_scope;
5571 qualifying_scope = parser->qualifying_scope;
5573 /* Check for invalid scopes. */
5574 if (scope == error_mark_node)
5576 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5577 cp_lexer_consume_token (parser->lexer);
5578 return error_mark_node;
5580 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5582 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5583 error_at (token->location,
5584 "scope %qT before %<~%> is not a class-name",
5585 scope);
5586 cp_parser_simulate_error (parser);
5587 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5588 cp_lexer_consume_token (parser->lexer);
5589 return error_mark_node;
5591 gcc_assert (!scope || TYPE_P (scope));
5593 /* If the name is of the form "X::~X" it's OK even if X is a
5594 typedef. */
5595 token = cp_lexer_peek_token (parser->lexer);
5596 if (scope
5597 && token->type == CPP_NAME
5598 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5599 != CPP_LESS)
5600 && (token->u.value == TYPE_IDENTIFIER (scope)
5601 || (CLASS_TYPE_P (scope)
5602 && constructor_name_p (token->u.value, scope))))
5604 cp_lexer_consume_token (parser->lexer);
5605 return build_nt (BIT_NOT_EXPR, scope);
5608 /* ~auto means the destructor of whatever the object is. */
5609 if (cp_parser_is_keyword (token, RID_AUTO))
5611 if (cxx_dialect < cxx14)
5612 pedwarn (input_location, 0,
5613 "%<~auto%> only available with "
5614 "-std=c++14 or -std=gnu++14");
5615 cp_lexer_consume_token (parser->lexer);
5616 return build_nt (BIT_NOT_EXPR, make_auto ());
5619 /* If there was an explicit qualification (S::~T), first look
5620 in the scope given by the qualification (i.e., S).
5622 Note: in the calls to cp_parser_class_name below we pass
5623 typename_type so that lookup finds the injected-class-name
5624 rather than the constructor. */
5625 done = false;
5626 type_decl = NULL_TREE;
5627 if (scope)
5629 cp_parser_parse_tentatively (parser);
5630 type_decl = cp_parser_class_name (parser,
5631 /*typename_keyword_p=*/false,
5632 /*template_keyword_p=*/false,
5633 typename_type,
5634 /*check_dependency=*/false,
5635 /*class_head_p=*/false,
5636 declarator_p);
5637 if (cp_parser_parse_definitely (parser))
5638 done = true;
5640 /* In "N::S::~S", look in "N" as well. */
5641 if (!done && scope && qualifying_scope)
5643 cp_parser_parse_tentatively (parser);
5644 parser->scope = qualifying_scope;
5645 parser->object_scope = NULL_TREE;
5646 parser->qualifying_scope = NULL_TREE;
5647 type_decl
5648 = cp_parser_class_name (parser,
5649 /*typename_keyword_p=*/false,
5650 /*template_keyword_p=*/false,
5651 typename_type,
5652 /*check_dependency=*/false,
5653 /*class_head_p=*/false,
5654 declarator_p);
5655 if (cp_parser_parse_definitely (parser))
5656 done = true;
5658 /* In "p->S::~T", look in the scope given by "*p" as well. */
5659 else if (!done && object_scope)
5661 cp_parser_parse_tentatively (parser);
5662 parser->scope = object_scope;
5663 parser->object_scope = NULL_TREE;
5664 parser->qualifying_scope = NULL_TREE;
5665 type_decl
5666 = cp_parser_class_name (parser,
5667 /*typename_keyword_p=*/false,
5668 /*template_keyword_p=*/false,
5669 typename_type,
5670 /*check_dependency=*/false,
5671 /*class_head_p=*/false,
5672 declarator_p);
5673 if (cp_parser_parse_definitely (parser))
5674 done = true;
5676 /* Look in the surrounding context. */
5677 if (!done)
5679 parser->scope = NULL_TREE;
5680 parser->object_scope = NULL_TREE;
5681 parser->qualifying_scope = NULL_TREE;
5682 if (processing_template_decl)
5683 cp_parser_parse_tentatively (parser);
5684 type_decl
5685 = cp_parser_class_name (parser,
5686 /*typename_keyword_p=*/false,
5687 /*template_keyword_p=*/false,
5688 typename_type,
5689 /*check_dependency=*/false,
5690 /*class_head_p=*/false,
5691 declarator_p);
5692 if (processing_template_decl
5693 && ! cp_parser_parse_definitely (parser))
5695 /* We couldn't find a type with this name. If we're parsing
5696 tentatively, fail and try something else. */
5697 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5699 cp_parser_simulate_error (parser);
5700 return error_mark_node;
5702 /* Otherwise, accept it and check for a match at instantiation
5703 time. */
5704 type_decl = cp_parser_identifier (parser);
5705 if (type_decl != error_mark_node)
5706 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5707 return type_decl;
5710 /* If an error occurred, assume that the name of the
5711 destructor is the same as the name of the qualifying
5712 class. That allows us to keep parsing after running
5713 into ill-formed destructor names. */
5714 if (type_decl == error_mark_node && scope)
5715 return build_nt (BIT_NOT_EXPR, scope);
5716 else if (type_decl == error_mark_node)
5717 return error_mark_node;
5719 /* Check that destructor name and scope match. */
5720 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5722 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5723 error_at (token->location,
5724 "declaration of %<~%T%> as member of %qT",
5725 type_decl, scope);
5726 cp_parser_simulate_error (parser);
5727 return error_mark_node;
5730 /* [class.dtor]
5732 A typedef-name that names a class shall not be used as the
5733 identifier in the declarator for a destructor declaration. */
5734 if (declarator_p
5735 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5736 && !DECL_SELF_REFERENCE_P (type_decl)
5737 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5738 error_at (token->location,
5739 "typedef-name %qD used as destructor declarator",
5740 type_decl);
5742 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5745 case CPP_KEYWORD:
5746 if (token->keyword == RID_OPERATOR)
5748 cp_expr id;
5750 /* This could be a template-id, so we try that first. */
5751 cp_parser_parse_tentatively (parser);
5752 /* Try a template-id. */
5753 id = cp_parser_template_id (parser, template_keyword_p,
5754 /*check_dependency_p=*/true,
5755 none_type,
5756 declarator_p);
5757 /* If that worked, we're done. */
5758 if (cp_parser_parse_definitely (parser))
5759 return id;
5760 /* We still don't know whether we're looking at an
5761 operator-function-id or a conversion-function-id. */
5762 cp_parser_parse_tentatively (parser);
5763 /* Try an operator-function-id. */
5764 id = cp_parser_operator_function_id (parser);
5765 /* If that didn't work, try a conversion-function-id. */
5766 if (!cp_parser_parse_definitely (parser))
5767 id = cp_parser_conversion_function_id (parser);
5768 else if (UDLIT_OPER_P (id))
5770 /* 17.6.3.3.5 */
5771 const char *name = UDLIT_OP_SUFFIX (id);
5772 if (name[0] != '_' && !in_system_header_at (input_location)
5773 && declarator_p)
5774 warning (0, "literal operator suffixes not preceded by %<_%>"
5775 " are reserved for future standardization");
5778 return id;
5780 /* Fall through. */
5782 default:
5783 if (optional_p)
5784 return NULL_TREE;
5785 cp_parser_error (parser, "expected unqualified-id");
5786 return error_mark_node;
5790 /* Parse an (optional) nested-name-specifier.
5792 nested-name-specifier: [C++98]
5793 class-or-namespace-name :: nested-name-specifier [opt]
5794 class-or-namespace-name :: template nested-name-specifier [opt]
5796 nested-name-specifier: [C++0x]
5797 type-name ::
5798 namespace-name ::
5799 nested-name-specifier identifier ::
5800 nested-name-specifier template [opt] simple-template-id ::
5802 PARSER->SCOPE should be set appropriately before this function is
5803 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5804 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5805 in name lookups.
5807 Sets PARSER->SCOPE to the class (TYPE) or namespace
5808 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5809 it unchanged if there is no nested-name-specifier. Returns the new
5810 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5812 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5813 part of a declaration and/or decl-specifier. */
5815 static tree
5816 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5817 bool typename_keyword_p,
5818 bool check_dependency_p,
5819 bool type_p,
5820 bool is_declaration)
5822 bool success = false;
5823 cp_token_position start = 0;
5824 cp_token *token;
5826 /* Remember where the nested-name-specifier starts. */
5827 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5829 start = cp_lexer_token_position (parser->lexer, false);
5830 push_deferring_access_checks (dk_deferred);
5833 while (true)
5835 tree new_scope;
5836 tree old_scope;
5837 tree saved_qualifying_scope;
5838 bool template_keyword_p;
5840 /* Spot cases that cannot be the beginning of a
5841 nested-name-specifier. */
5842 token = cp_lexer_peek_token (parser->lexer);
5844 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5845 the already parsed nested-name-specifier. */
5846 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5848 /* Grab the nested-name-specifier and continue the loop. */
5849 cp_parser_pre_parsed_nested_name_specifier (parser);
5850 /* If we originally encountered this nested-name-specifier
5851 with IS_DECLARATION set to false, we will not have
5852 resolved TYPENAME_TYPEs, so we must do so here. */
5853 if (is_declaration
5854 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5856 new_scope = resolve_typename_type (parser->scope,
5857 /*only_current_p=*/false);
5858 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5859 parser->scope = new_scope;
5861 success = true;
5862 continue;
5865 /* Spot cases that cannot be the beginning of a
5866 nested-name-specifier. On the second and subsequent times
5867 through the loop, we look for the `template' keyword. */
5868 if (success && token->keyword == RID_TEMPLATE)
5870 /* A template-id can start a nested-name-specifier. */
5871 else if (token->type == CPP_TEMPLATE_ID)
5873 /* DR 743: decltype can be used in a nested-name-specifier. */
5874 else if (token_is_decltype (token))
5876 else
5878 /* If the next token is not an identifier, then it is
5879 definitely not a type-name or namespace-name. */
5880 if (token->type != CPP_NAME)
5881 break;
5882 /* If the following token is neither a `<' (to begin a
5883 template-id), nor a `::', then we are not looking at a
5884 nested-name-specifier. */
5885 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5887 if (token->type == CPP_COLON
5888 && parser->colon_corrects_to_scope_p
5889 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5891 error_at (token->location,
5892 "found %<:%> in nested-name-specifier, expected %<::%>");
5893 token->type = CPP_SCOPE;
5896 if (token->type != CPP_SCOPE
5897 && !cp_parser_nth_token_starts_template_argument_list_p
5898 (parser, 2))
5899 break;
5902 /* The nested-name-specifier is optional, so we parse
5903 tentatively. */
5904 cp_parser_parse_tentatively (parser);
5906 /* Look for the optional `template' keyword, if this isn't the
5907 first time through the loop. */
5908 if (success)
5909 template_keyword_p = cp_parser_optional_template_keyword (parser);
5910 else
5911 template_keyword_p = false;
5913 /* Save the old scope since the name lookup we are about to do
5914 might destroy it. */
5915 old_scope = parser->scope;
5916 saved_qualifying_scope = parser->qualifying_scope;
5917 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5918 look up names in "X<T>::I" in order to determine that "Y" is
5919 a template. So, if we have a typename at this point, we make
5920 an effort to look through it. */
5921 if (is_declaration
5922 && !typename_keyword_p
5923 && parser->scope
5924 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5925 parser->scope = resolve_typename_type (parser->scope,
5926 /*only_current_p=*/false);
5927 /* Parse the qualifying entity. */
5928 new_scope
5929 = cp_parser_qualifying_entity (parser,
5930 typename_keyword_p,
5931 template_keyword_p,
5932 check_dependency_p,
5933 type_p,
5934 is_declaration);
5935 /* Look for the `::' token. */
5936 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5938 /* If we found what we wanted, we keep going; otherwise, we're
5939 done. */
5940 if (!cp_parser_parse_definitely (parser))
5942 bool error_p = false;
5944 /* Restore the OLD_SCOPE since it was valid before the
5945 failed attempt at finding the last
5946 class-or-namespace-name. */
5947 parser->scope = old_scope;
5948 parser->qualifying_scope = saved_qualifying_scope;
5950 /* If the next token is a decltype, and the one after that is a
5951 `::', then the decltype has failed to resolve to a class or
5952 enumeration type. Give this error even when parsing
5953 tentatively since it can't possibly be valid--and we're going
5954 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5955 won't get another chance.*/
5956 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5957 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5958 == CPP_SCOPE))
5960 token = cp_lexer_consume_token (parser->lexer);
5961 error_at (token->location, "decltype evaluates to %qT, "
5962 "which is not a class or enumeration type",
5963 token->u.tree_check_value->value);
5964 parser->scope = error_mark_node;
5965 error_p = true;
5966 /* As below. */
5967 success = true;
5968 cp_lexer_consume_token (parser->lexer);
5971 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
5972 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
5974 /* If we have a non-type template-id followed by ::, it can't
5975 possibly be valid. */
5976 token = cp_lexer_peek_token (parser->lexer);
5977 tree tid = token->u.tree_check_value->value;
5978 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
5979 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
5981 tree tmpl = NULL_TREE;
5982 if (is_overloaded_fn (tid))
5984 tree fns = get_fns (tid);
5985 if (!OVL_CHAIN (fns))
5986 tmpl = OVL_CURRENT (fns);
5987 error_at (token->location, "function template-id %qD "
5988 "in nested-name-specifier", tid);
5990 else
5992 /* Variable template. */
5993 tmpl = TREE_OPERAND (tid, 0);
5994 gcc_assert (variable_template_p (tmpl));
5995 error_at (token->location, "variable template-id %qD "
5996 "in nested-name-specifier", tid);
5998 if (tmpl)
5999 inform (DECL_SOURCE_LOCATION (tmpl),
6000 "%qD declared here", tmpl);
6002 parser->scope = error_mark_node;
6003 error_p = true;
6004 /* As below. */
6005 success = true;
6006 cp_lexer_consume_token (parser->lexer);
6007 cp_lexer_consume_token (parser->lexer);
6011 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6012 break;
6013 /* If the next token is an identifier, and the one after
6014 that is a `::', then any valid interpretation would have
6015 found a class-or-namespace-name. */
6016 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6017 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6018 == CPP_SCOPE)
6019 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6020 != CPP_COMPL))
6022 token = cp_lexer_consume_token (parser->lexer);
6023 if (!error_p)
6025 if (!token->error_reported)
6027 tree decl;
6028 tree ambiguous_decls;
6030 decl = cp_parser_lookup_name (parser, token->u.value,
6031 none_type,
6032 /*is_template=*/false,
6033 /*is_namespace=*/false,
6034 /*check_dependency=*/true,
6035 &ambiguous_decls,
6036 token->location);
6037 if (TREE_CODE (decl) == TEMPLATE_DECL)
6038 error_at (token->location,
6039 "%qD used without template parameters",
6040 decl);
6041 else if (ambiguous_decls)
6043 // cp_parser_lookup_name has the same diagnostic,
6044 // thus make sure to emit it at most once.
6045 if (cp_parser_uncommitted_to_tentative_parse_p
6046 (parser))
6048 error_at (token->location,
6049 "reference to %qD is ambiguous",
6050 token->u.value);
6051 print_candidates (ambiguous_decls);
6053 decl = error_mark_node;
6055 else
6057 if (cxx_dialect != cxx98)
6058 cp_parser_name_lookup_error
6059 (parser, token->u.value, decl, NLE_NOT_CXX98,
6060 token->location);
6061 else
6062 cp_parser_name_lookup_error
6063 (parser, token->u.value, decl, NLE_CXX98,
6064 token->location);
6067 parser->scope = error_mark_node;
6068 error_p = true;
6069 /* Treat this as a successful nested-name-specifier
6070 due to:
6072 [basic.lookup.qual]
6074 If the name found is not a class-name (clause
6075 _class_) or namespace-name (_namespace.def_), the
6076 program is ill-formed. */
6077 success = true;
6079 cp_lexer_consume_token (parser->lexer);
6081 break;
6083 /* We've found one valid nested-name-specifier. */
6084 success = true;
6085 /* Name lookup always gives us a DECL. */
6086 if (TREE_CODE (new_scope) == TYPE_DECL)
6087 new_scope = TREE_TYPE (new_scope);
6088 /* Uses of "template" must be followed by actual templates. */
6089 if (template_keyword_p
6090 && !(CLASS_TYPE_P (new_scope)
6091 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6092 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6093 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6094 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6095 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6096 == TEMPLATE_ID_EXPR)))
6097 permerror (input_location, TYPE_P (new_scope)
6098 ? G_("%qT is not a template")
6099 : G_("%qD is not a template"),
6100 new_scope);
6101 /* If it is a class scope, try to complete it; we are about to
6102 be looking up names inside the class. */
6103 if (TYPE_P (new_scope)
6104 /* Since checking types for dependency can be expensive,
6105 avoid doing it if the type is already complete. */
6106 && !COMPLETE_TYPE_P (new_scope)
6107 /* Do not try to complete dependent types. */
6108 && !dependent_type_p (new_scope))
6110 new_scope = complete_type (new_scope);
6111 /* If it is a typedef to current class, use the current
6112 class instead, as the typedef won't have any names inside
6113 it yet. */
6114 if (!COMPLETE_TYPE_P (new_scope)
6115 && currently_open_class (new_scope))
6116 new_scope = TYPE_MAIN_VARIANT (new_scope);
6118 /* Make sure we look in the right scope the next time through
6119 the loop. */
6120 parser->scope = new_scope;
6123 /* If parsing tentatively, replace the sequence of tokens that makes
6124 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6125 token. That way, should we re-parse the token stream, we will
6126 not have to repeat the effort required to do the parse, nor will
6127 we issue duplicate error messages. */
6128 if (success && start)
6130 cp_token *token;
6132 token = cp_lexer_token_at (parser->lexer, start);
6133 /* Reset the contents of the START token. */
6134 token->type = CPP_NESTED_NAME_SPECIFIER;
6135 /* Retrieve any deferred checks. Do not pop this access checks yet
6136 so the memory will not be reclaimed during token replacing below. */
6137 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6138 token->u.tree_check_value->value = parser->scope;
6139 token->u.tree_check_value->checks = get_deferred_access_checks ();
6140 token->u.tree_check_value->qualifying_scope =
6141 parser->qualifying_scope;
6142 token->keyword = RID_MAX;
6144 /* Purge all subsequent tokens. */
6145 cp_lexer_purge_tokens_after (parser->lexer, start);
6148 if (start)
6149 pop_to_parent_deferring_access_checks ();
6151 return success ? parser->scope : NULL_TREE;
6154 /* Parse a nested-name-specifier. See
6155 cp_parser_nested_name_specifier_opt for details. This function
6156 behaves identically, except that it will an issue an error if no
6157 nested-name-specifier is present. */
6159 static tree
6160 cp_parser_nested_name_specifier (cp_parser *parser,
6161 bool typename_keyword_p,
6162 bool check_dependency_p,
6163 bool type_p,
6164 bool is_declaration)
6166 tree scope;
6168 /* Look for the nested-name-specifier. */
6169 scope = cp_parser_nested_name_specifier_opt (parser,
6170 typename_keyword_p,
6171 check_dependency_p,
6172 type_p,
6173 is_declaration);
6174 /* If it was not present, issue an error message. */
6175 if (!scope)
6177 cp_parser_error (parser, "expected nested-name-specifier");
6178 parser->scope = NULL_TREE;
6181 return scope;
6184 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6185 this is either a class-name or a namespace-name (which corresponds
6186 to the class-or-namespace-name production in the grammar). For
6187 C++0x, it can also be a type-name that refers to an enumeration
6188 type or a simple-template-id.
6190 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6191 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6192 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6193 TYPE_P is TRUE iff the next name should be taken as a class-name,
6194 even the same name is declared to be another entity in the same
6195 scope.
6197 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6198 specified by the class-or-namespace-name. If neither is found the
6199 ERROR_MARK_NODE is returned. */
6201 static tree
6202 cp_parser_qualifying_entity (cp_parser *parser,
6203 bool typename_keyword_p,
6204 bool template_keyword_p,
6205 bool check_dependency_p,
6206 bool type_p,
6207 bool is_declaration)
6209 tree saved_scope;
6210 tree saved_qualifying_scope;
6211 tree saved_object_scope;
6212 tree scope;
6213 bool only_class_p;
6214 bool successful_parse_p;
6216 /* DR 743: decltype can appear in a nested-name-specifier. */
6217 if (cp_lexer_next_token_is_decltype (parser->lexer))
6219 scope = cp_parser_decltype (parser);
6220 if (TREE_CODE (scope) != ENUMERAL_TYPE
6221 && !MAYBE_CLASS_TYPE_P (scope))
6223 cp_parser_simulate_error (parser);
6224 return error_mark_node;
6226 if (TYPE_NAME (scope))
6227 scope = TYPE_NAME (scope);
6228 return scope;
6231 /* Before we try to parse the class-name, we must save away the
6232 current PARSER->SCOPE since cp_parser_class_name will destroy
6233 it. */
6234 saved_scope = parser->scope;
6235 saved_qualifying_scope = parser->qualifying_scope;
6236 saved_object_scope = parser->object_scope;
6237 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6238 there is no need to look for a namespace-name. */
6239 only_class_p = template_keyword_p
6240 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6241 if (!only_class_p)
6242 cp_parser_parse_tentatively (parser);
6243 scope = cp_parser_class_name (parser,
6244 typename_keyword_p,
6245 template_keyword_p,
6246 type_p ? class_type : none_type,
6247 check_dependency_p,
6248 /*class_head_p=*/false,
6249 is_declaration,
6250 /*enum_ok=*/cxx_dialect > cxx98);
6251 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6252 /* If that didn't work, try for a namespace-name. */
6253 if (!only_class_p && !successful_parse_p)
6255 /* Restore the saved scope. */
6256 parser->scope = saved_scope;
6257 parser->qualifying_scope = saved_qualifying_scope;
6258 parser->object_scope = saved_object_scope;
6259 /* If we are not looking at an identifier followed by the scope
6260 resolution operator, then this is not part of a
6261 nested-name-specifier. (Note that this function is only used
6262 to parse the components of a nested-name-specifier.) */
6263 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6264 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6265 return error_mark_node;
6266 scope = cp_parser_namespace_name (parser);
6269 return scope;
6272 /* Return true if we are looking at a compound-literal, false otherwise. */
6274 static bool
6275 cp_parser_compound_literal_p (cp_parser *parser)
6277 /* Consume the `('. */
6278 cp_lexer_consume_token (parser->lexer);
6280 cp_lexer_save_tokens (parser->lexer);
6282 /* Skip tokens until the next token is a closing parenthesis.
6283 If we find the closing `)', and the next token is a `{', then
6284 we are looking at a compound-literal. */
6285 bool compound_literal_p
6286 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6287 /*consume_paren=*/true)
6288 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6290 /* Roll back the tokens we skipped. */
6291 cp_lexer_rollback_tokens (parser->lexer);
6293 return compound_literal_p;
6296 /* Parse a postfix-expression.
6298 postfix-expression:
6299 primary-expression
6300 postfix-expression [ expression ]
6301 postfix-expression ( expression-list [opt] )
6302 simple-type-specifier ( expression-list [opt] )
6303 typename :: [opt] nested-name-specifier identifier
6304 ( expression-list [opt] )
6305 typename :: [opt] nested-name-specifier template [opt] template-id
6306 ( expression-list [opt] )
6307 postfix-expression . template [opt] id-expression
6308 postfix-expression -> template [opt] id-expression
6309 postfix-expression . pseudo-destructor-name
6310 postfix-expression -> pseudo-destructor-name
6311 postfix-expression ++
6312 postfix-expression --
6313 dynamic_cast < type-id > ( expression )
6314 static_cast < type-id > ( expression )
6315 reinterpret_cast < type-id > ( expression )
6316 const_cast < type-id > ( expression )
6317 typeid ( expression )
6318 typeid ( type-id )
6320 GNU Extension:
6322 postfix-expression:
6323 ( type-id ) { initializer-list , [opt] }
6325 This extension is a GNU version of the C99 compound-literal
6326 construct. (The C99 grammar uses `type-name' instead of `type-id',
6327 but they are essentially the same concept.)
6329 If ADDRESS_P is true, the postfix expression is the operand of the
6330 `&' operator. CAST_P is true if this expression is the target of a
6331 cast.
6333 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6334 class member access expressions [expr.ref].
6336 Returns a representation of the expression. */
6338 static cp_expr
6339 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6340 bool member_access_only_p, bool decltype_p,
6341 cp_id_kind * pidk_return)
6343 cp_token *token;
6344 location_t loc;
6345 enum rid keyword;
6346 cp_id_kind idk = CP_ID_KIND_NONE;
6347 cp_expr postfix_expression = NULL_TREE;
6348 bool is_member_access = false;
6349 int saved_in_statement = -1;
6351 /* Peek at the next token. */
6352 token = cp_lexer_peek_token (parser->lexer);
6353 loc = token->location;
6354 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6356 /* Some of the productions are determined by keywords. */
6357 keyword = token->keyword;
6358 switch (keyword)
6360 case RID_DYNCAST:
6361 case RID_STATCAST:
6362 case RID_REINTCAST:
6363 case RID_CONSTCAST:
6365 tree type;
6366 cp_expr expression;
6367 const char *saved_message;
6368 bool saved_in_type_id_in_expr_p;
6370 /* All of these can be handled in the same way from the point
6371 of view of parsing. Begin by consuming the token
6372 identifying the cast. */
6373 cp_lexer_consume_token (parser->lexer);
6375 /* New types cannot be defined in the cast. */
6376 saved_message = parser->type_definition_forbidden_message;
6377 parser->type_definition_forbidden_message
6378 = G_("types may not be defined in casts");
6380 /* Look for the opening `<'. */
6381 cp_parser_require (parser, CPP_LESS, RT_LESS);
6382 /* Parse the type to which we are casting. */
6383 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6384 parser->in_type_id_in_expr_p = true;
6385 type = cp_parser_type_id (parser);
6386 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6387 /* Look for the closing `>'. */
6388 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6389 /* Restore the old message. */
6390 parser->type_definition_forbidden_message = saved_message;
6392 bool saved_greater_than_is_operator_p
6393 = parser->greater_than_is_operator_p;
6394 parser->greater_than_is_operator_p = true;
6396 /* And the expression which is being cast. */
6397 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6398 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6399 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6400 RT_CLOSE_PAREN);
6401 location_t end_loc = close_paren ?
6402 close_paren->location : UNKNOWN_LOCATION;
6404 parser->greater_than_is_operator_p
6405 = saved_greater_than_is_operator_p;
6407 /* Only type conversions to integral or enumeration types
6408 can be used in constant-expressions. */
6409 if (!cast_valid_in_integral_constant_expression_p (type)
6410 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6411 return error_mark_node;
6413 switch (keyword)
6415 case RID_DYNCAST:
6416 postfix_expression
6417 = build_dynamic_cast (type, expression, tf_warning_or_error);
6418 break;
6419 case RID_STATCAST:
6420 postfix_expression
6421 = build_static_cast (type, expression, tf_warning_or_error);
6422 break;
6423 case RID_REINTCAST:
6424 postfix_expression
6425 = build_reinterpret_cast (type, expression,
6426 tf_warning_or_error);
6427 break;
6428 case RID_CONSTCAST:
6429 postfix_expression
6430 = build_const_cast (type, expression, tf_warning_or_error);
6431 break;
6432 default:
6433 gcc_unreachable ();
6436 /* Construct a location e.g. :
6437 reinterpret_cast <int *> (expr)
6438 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6439 ranging from the start of the "*_cast" token to the final closing
6440 paren, with the caret at the start. */
6441 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6442 postfix_expression.set_location (cp_cast_loc);
6444 break;
6446 case RID_TYPEID:
6448 tree type;
6449 const char *saved_message;
6450 bool saved_in_type_id_in_expr_p;
6452 /* Consume the `typeid' token. */
6453 cp_lexer_consume_token (parser->lexer);
6454 /* Look for the `(' token. */
6455 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6456 /* Types cannot be defined in a `typeid' expression. */
6457 saved_message = parser->type_definition_forbidden_message;
6458 parser->type_definition_forbidden_message
6459 = G_("types may not be defined in a %<typeid%> expression");
6460 /* We can't be sure yet whether we're looking at a type-id or an
6461 expression. */
6462 cp_parser_parse_tentatively (parser);
6463 /* Try a type-id first. */
6464 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6465 parser->in_type_id_in_expr_p = true;
6466 type = cp_parser_type_id (parser);
6467 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6468 /* Look for the `)' token. Otherwise, we can't be sure that
6469 we're not looking at an expression: consider `typeid (int
6470 (3))', for example. */
6471 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6472 /* If all went well, simply lookup the type-id. */
6473 if (cp_parser_parse_definitely (parser))
6474 postfix_expression = get_typeid (type, tf_warning_or_error);
6475 /* Otherwise, fall back to the expression variant. */
6476 else
6478 tree expression;
6480 /* Look for an expression. */
6481 expression = cp_parser_expression (parser, & idk);
6482 /* Compute its typeid. */
6483 postfix_expression = build_typeid (expression, tf_warning_or_error);
6484 /* Look for the `)' token. */
6485 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6487 /* Restore the saved message. */
6488 parser->type_definition_forbidden_message = saved_message;
6489 /* `typeid' may not appear in an integral constant expression. */
6490 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6491 return error_mark_node;
6493 break;
6495 case RID_TYPENAME:
6497 tree type;
6498 /* The syntax permitted here is the same permitted for an
6499 elaborated-type-specifier. */
6500 ++parser->prevent_constrained_type_specifiers;
6501 type = cp_parser_elaborated_type_specifier (parser,
6502 /*is_friend=*/false,
6503 /*is_declaration=*/false);
6504 --parser->prevent_constrained_type_specifiers;
6505 postfix_expression = cp_parser_functional_cast (parser, type);
6507 break;
6509 case RID_CILK_SPAWN:
6511 location_t cilk_spawn_loc
6512 = cp_lexer_peek_token (parser->lexer)->location;
6513 cp_lexer_consume_token (parser->lexer);
6514 token = cp_lexer_peek_token (parser->lexer);
6515 if (token->type == CPP_SEMICOLON)
6517 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6518 "an expression");
6519 postfix_expression = error_mark_node;
6520 break;
6522 else if (!current_function_decl)
6524 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6525 "inside a function");
6526 postfix_expression = error_mark_node;
6527 break;
6529 else
6531 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6532 saved_in_statement = parser->in_statement;
6533 parser->in_statement |= IN_CILK_SPAWN;
6535 cfun->calls_cilk_spawn = 1;
6536 postfix_expression =
6537 cp_parser_postfix_expression (parser, false, false,
6538 false, false, &idk);
6539 if (!flag_cilkplus)
6541 error_at (token->location, "-fcilkplus must be enabled to use"
6542 " %<_Cilk_spawn%>");
6543 cfun->calls_cilk_spawn = 0;
6545 else if (saved_in_statement & IN_CILK_SPAWN)
6547 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6548 "are not permitted");
6549 postfix_expression = error_mark_node;
6550 cfun->calls_cilk_spawn = 0;
6552 else
6554 location_t loc = postfix_expression.get_location ();
6555 postfix_expression = build_cilk_spawn (token->location,
6556 postfix_expression);
6557 /* Build a location of the form:
6558 _Cilk_spawn expr
6559 ~~~~~~~~~~~~^~~~
6560 with caret at the expr, ranging from the start of the
6561 _Cilk_spawn token to the end of the expression. */
6562 location_t combined_loc =
6563 make_location (loc, cilk_spawn_loc, get_finish (loc));
6564 postfix_expression.set_location (combined_loc);
6565 if (postfix_expression != error_mark_node)
6566 SET_EXPR_LOCATION (postfix_expression, input_location);
6567 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6569 break;
6572 case RID_BUILTIN_SHUFFLE:
6574 vec<tree, va_gc> *vec;
6575 unsigned int i;
6576 tree p;
6578 cp_lexer_consume_token (parser->lexer);
6579 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6580 /*cast_p=*/false, /*allow_expansion_p=*/true,
6581 /*non_constant_p=*/NULL);
6582 if (vec == NULL)
6583 return error_mark_node;
6585 FOR_EACH_VEC_ELT (*vec, i, p)
6586 mark_exp_read (p);
6588 if (vec->length () == 2)
6589 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6590 tf_warning_or_error);
6591 else if (vec->length () == 3)
6592 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6593 tf_warning_or_error);
6594 else
6596 error_at (loc, "wrong number of arguments to "
6597 "%<__builtin_shuffle%>");
6598 return error_mark_node;
6600 break;
6603 default:
6605 tree type;
6607 /* If the next thing is a simple-type-specifier, we may be
6608 looking at a functional cast. We could also be looking at
6609 an id-expression. So, we try the functional cast, and if
6610 that doesn't work we fall back to the primary-expression. */
6611 cp_parser_parse_tentatively (parser);
6612 /* Look for the simple-type-specifier. */
6613 ++parser->prevent_constrained_type_specifiers;
6614 type = cp_parser_simple_type_specifier (parser,
6615 /*decl_specs=*/NULL,
6616 CP_PARSER_FLAGS_NONE);
6617 --parser->prevent_constrained_type_specifiers;
6618 /* Parse the cast itself. */
6619 if (!cp_parser_error_occurred (parser))
6620 postfix_expression
6621 = cp_parser_functional_cast (parser, type);
6622 /* If that worked, we're done. */
6623 if (cp_parser_parse_definitely (parser))
6624 break;
6626 /* If the functional-cast didn't work out, try a
6627 compound-literal. */
6628 if (cp_parser_allow_gnu_extensions_p (parser)
6629 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6631 cp_expr initializer = NULL_TREE;
6633 cp_parser_parse_tentatively (parser);
6635 /* Avoid calling cp_parser_type_id pointlessly, see comment
6636 in cp_parser_cast_expression about c++/29234. */
6637 if (!cp_parser_compound_literal_p (parser))
6638 cp_parser_simulate_error (parser);
6639 else
6641 /* Parse the type. */
6642 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6643 parser->in_type_id_in_expr_p = true;
6644 type = cp_parser_type_id (parser);
6645 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6646 /* Look for the `)'. */
6647 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6650 /* If things aren't going well, there's no need to
6651 keep going. */
6652 if (!cp_parser_error_occurred (parser))
6654 bool non_constant_p;
6655 /* Parse the brace-enclosed initializer list. */
6656 initializer = cp_parser_braced_list (parser,
6657 &non_constant_p);
6659 /* If that worked, we're definitely looking at a
6660 compound-literal expression. */
6661 if (cp_parser_parse_definitely (parser))
6663 /* Warn the user that a compound literal is not
6664 allowed in standard C++. */
6665 pedwarn (input_location, OPT_Wpedantic,
6666 "ISO C++ forbids compound-literals");
6667 /* For simplicity, we disallow compound literals in
6668 constant-expressions. We could
6669 allow compound literals of integer type, whose
6670 initializer was a constant, in constant
6671 expressions. Permitting that usage, as a further
6672 extension, would not change the meaning of any
6673 currently accepted programs. (Of course, as
6674 compound literals are not part of ISO C++, the
6675 standard has nothing to say.) */
6676 if (cp_parser_non_integral_constant_expression (parser,
6677 NIC_NCC))
6679 postfix_expression = error_mark_node;
6680 break;
6682 /* Form the representation of the compound-literal. */
6683 postfix_expression
6684 = finish_compound_literal (type, initializer,
6685 tf_warning_or_error);
6686 postfix_expression.set_location (initializer.get_location ());
6687 break;
6691 /* It must be a primary-expression. */
6692 postfix_expression
6693 = cp_parser_primary_expression (parser, address_p, cast_p,
6694 /*template_arg_p=*/false,
6695 decltype_p,
6696 &idk);
6698 break;
6701 /* Note that we don't need to worry about calling build_cplus_new on a
6702 class-valued CALL_EXPR in decltype when it isn't the end of the
6703 postfix-expression; unary_complex_lvalue will take care of that for
6704 all these cases. */
6706 /* Keep looping until the postfix-expression is complete. */
6707 while (true)
6709 if (idk == CP_ID_KIND_UNQUALIFIED
6710 && identifier_p (postfix_expression)
6711 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6712 /* It is not a Koenig lookup function call. */
6713 postfix_expression
6714 = unqualified_name_lookup_error (postfix_expression);
6716 /* Peek at the next token. */
6717 token = cp_lexer_peek_token (parser->lexer);
6719 switch (token->type)
6721 case CPP_OPEN_SQUARE:
6722 if (cp_next_tokens_can_be_std_attribute_p (parser))
6724 cp_parser_error (parser,
6725 "two consecutive %<[%> shall "
6726 "only introduce an attribute");
6727 return error_mark_node;
6729 postfix_expression
6730 = cp_parser_postfix_open_square_expression (parser,
6731 postfix_expression,
6732 false,
6733 decltype_p);
6734 postfix_expression.set_range (start_loc,
6735 postfix_expression.get_location ());
6737 idk = CP_ID_KIND_NONE;
6738 is_member_access = false;
6739 break;
6741 case CPP_OPEN_PAREN:
6742 /* postfix-expression ( expression-list [opt] ) */
6744 bool koenig_p;
6745 bool is_builtin_constant_p;
6746 bool saved_integral_constant_expression_p = false;
6747 bool saved_non_integral_constant_expression_p = false;
6748 tsubst_flags_t complain = complain_flags (decltype_p);
6749 vec<tree, va_gc> *args;
6750 location_t close_paren_loc = UNKNOWN_LOCATION;
6752 is_member_access = false;
6754 is_builtin_constant_p
6755 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6756 if (is_builtin_constant_p)
6758 /* The whole point of __builtin_constant_p is to allow
6759 non-constant expressions to appear as arguments. */
6760 saved_integral_constant_expression_p
6761 = parser->integral_constant_expression_p;
6762 saved_non_integral_constant_expression_p
6763 = parser->non_integral_constant_expression_p;
6764 parser->integral_constant_expression_p = false;
6766 args = (cp_parser_parenthesized_expression_list
6767 (parser, non_attr,
6768 /*cast_p=*/false, /*allow_expansion_p=*/true,
6769 /*non_constant_p=*/NULL,
6770 /*close_paren_loc=*/&close_paren_loc));
6771 if (is_builtin_constant_p)
6773 parser->integral_constant_expression_p
6774 = saved_integral_constant_expression_p;
6775 parser->non_integral_constant_expression_p
6776 = saved_non_integral_constant_expression_p;
6779 if (args == NULL)
6781 postfix_expression = error_mark_node;
6782 break;
6785 /* Function calls are not permitted in
6786 constant-expressions. */
6787 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6788 && cp_parser_non_integral_constant_expression (parser,
6789 NIC_FUNC_CALL))
6791 postfix_expression = error_mark_node;
6792 release_tree_vector (args);
6793 break;
6796 koenig_p = false;
6797 if (idk == CP_ID_KIND_UNQUALIFIED
6798 || idk == CP_ID_KIND_TEMPLATE_ID)
6800 if (identifier_p (postfix_expression))
6802 if (!args->is_empty ())
6804 koenig_p = true;
6805 if (!any_type_dependent_arguments_p (args))
6806 postfix_expression
6807 = perform_koenig_lookup (postfix_expression, args,
6808 complain);
6810 else
6811 postfix_expression
6812 = unqualified_fn_lookup_error (postfix_expression);
6814 /* We do not perform argument-dependent lookup if
6815 normal lookup finds a non-function, in accordance
6816 with the expected resolution of DR 218. */
6817 else if (!args->is_empty ()
6818 && is_overloaded_fn (postfix_expression))
6820 tree fn = get_first_fn (postfix_expression);
6821 fn = STRIP_TEMPLATE (fn);
6823 /* Do not do argument dependent lookup if regular
6824 lookup finds a member function or a block-scope
6825 function declaration. [basic.lookup.argdep]/3 */
6826 if (!DECL_FUNCTION_MEMBER_P (fn)
6827 && !DECL_LOCAL_FUNCTION_P (fn))
6829 koenig_p = true;
6830 if (!any_type_dependent_arguments_p (args))
6831 postfix_expression
6832 = perform_koenig_lookup (postfix_expression, args,
6833 complain);
6838 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6839 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6840 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6841 && vec_safe_length (args) == 3)
6843 tree arg0 = (*args)[0];
6844 tree arg1 = (*args)[1];
6845 tree arg2 = (*args)[2];
6846 int literal_mask = ((!!integer_zerop (arg1) << 1)
6847 | (!!integer_zerop (arg2) << 2));
6848 if (TREE_CODE (arg2) == CONST_DECL)
6849 arg2 = DECL_INITIAL (arg2);
6850 warn_for_memset (input_location, arg0, arg2, literal_mask);
6853 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6855 tree instance = TREE_OPERAND (postfix_expression, 0);
6856 tree fn = TREE_OPERAND (postfix_expression, 1);
6858 if (processing_template_decl
6859 && (type_dependent_object_expression_p (instance)
6860 || (!BASELINK_P (fn)
6861 && TREE_CODE (fn) != FIELD_DECL)
6862 || type_dependent_expression_p (fn)
6863 || any_type_dependent_arguments_p (args)))
6865 postfix_expression
6866 = build_nt_call_vec (postfix_expression, args);
6867 release_tree_vector (args);
6868 break;
6871 if (BASELINK_P (fn))
6873 postfix_expression
6874 = (build_new_method_call
6875 (instance, fn, &args, NULL_TREE,
6876 (idk == CP_ID_KIND_QUALIFIED
6877 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6878 : LOOKUP_NORMAL),
6879 /*fn_p=*/NULL,
6880 complain));
6882 else
6883 postfix_expression
6884 = finish_call_expr (postfix_expression, &args,
6885 /*disallow_virtual=*/false,
6886 /*koenig_p=*/false,
6887 complain);
6889 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6890 || TREE_CODE (postfix_expression) == MEMBER_REF
6891 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6892 postfix_expression = (build_offset_ref_call_from_tree
6893 (postfix_expression, &args,
6894 complain));
6895 else if (idk == CP_ID_KIND_QUALIFIED)
6896 /* A call to a static class member, or a namespace-scope
6897 function. */
6898 postfix_expression
6899 = finish_call_expr (postfix_expression, &args,
6900 /*disallow_virtual=*/true,
6901 koenig_p,
6902 complain);
6903 else
6904 /* All other function calls. */
6905 postfix_expression
6906 = finish_call_expr (postfix_expression, &args,
6907 /*disallow_virtual=*/false,
6908 koenig_p,
6909 complain);
6911 if (close_paren_loc != UNKNOWN_LOCATION)
6913 location_t combined_loc = make_location (token->location,
6914 start_loc,
6915 close_paren_loc);
6916 postfix_expression.set_location (combined_loc);
6919 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6920 idk = CP_ID_KIND_NONE;
6922 release_tree_vector (args);
6924 break;
6926 case CPP_DOT:
6927 case CPP_DEREF:
6928 /* postfix-expression . template [opt] id-expression
6929 postfix-expression . pseudo-destructor-name
6930 postfix-expression -> template [opt] id-expression
6931 postfix-expression -> pseudo-destructor-name */
6933 /* Consume the `.' or `->' operator. */
6934 cp_lexer_consume_token (parser->lexer);
6936 postfix_expression
6937 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6938 postfix_expression,
6939 false, &idk, loc);
6941 is_member_access = true;
6942 break;
6944 case CPP_PLUS_PLUS:
6945 /* postfix-expression ++ */
6946 /* Consume the `++' token. */
6947 cp_lexer_consume_token (parser->lexer);
6948 /* Generate a representation for the complete expression. */
6949 postfix_expression
6950 = finish_increment_expr (postfix_expression,
6951 POSTINCREMENT_EXPR);
6952 /* Increments may not appear in constant-expressions. */
6953 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6954 postfix_expression = error_mark_node;
6955 idk = CP_ID_KIND_NONE;
6956 is_member_access = false;
6957 break;
6959 case CPP_MINUS_MINUS:
6960 /* postfix-expression -- */
6961 /* Consume the `--' token. */
6962 cp_lexer_consume_token (parser->lexer);
6963 /* Generate a representation for the complete expression. */
6964 postfix_expression
6965 = finish_increment_expr (postfix_expression,
6966 POSTDECREMENT_EXPR);
6967 /* Decrements may not appear in constant-expressions. */
6968 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6969 postfix_expression = error_mark_node;
6970 idk = CP_ID_KIND_NONE;
6971 is_member_access = false;
6972 break;
6974 default:
6975 if (pidk_return != NULL)
6976 * pidk_return = idk;
6977 if (member_access_only_p)
6978 return is_member_access
6979 ? postfix_expression
6980 : cp_expr (error_mark_node);
6981 else
6982 return postfix_expression;
6986 /* We should never get here. */
6987 gcc_unreachable ();
6988 return error_mark_node;
6991 /* This function parses Cilk Plus array notations. If a normal array expr. is
6992 parsed then the array index is passed back to the caller through *INIT_INDEX
6993 and the function returns a NULL_TREE. If array notation expr. is parsed,
6994 then *INIT_INDEX is ignored by the caller and the function returns
6995 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6996 error_mark_node. */
6998 static tree
6999 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
7000 tree array_value)
7002 cp_token *token = NULL;
7003 tree length_index, stride = NULL_TREE, value_tree, array_type;
7004 if (!array_value || array_value == error_mark_node)
7006 cp_parser_skip_to_end_of_statement (parser);
7007 return error_mark_node;
7010 array_type = TREE_TYPE (array_value);
7012 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7013 parser->colon_corrects_to_scope_p = false;
7014 token = cp_lexer_peek_token (parser->lexer);
7016 if (!token)
7018 cp_parser_error (parser, "expected %<:%> or numeral");
7019 return error_mark_node;
7021 else if (token->type == CPP_COLON)
7023 /* Consume the ':'. */
7024 cp_lexer_consume_token (parser->lexer);
7026 /* If we are here, then we have a case like this A[:]. */
7027 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7029 cp_parser_error (parser, "expected %<]%>");
7030 cp_parser_skip_to_end_of_statement (parser);
7031 return error_mark_node;
7033 *init_index = NULL_TREE;
7034 stride = NULL_TREE;
7035 length_index = NULL_TREE;
7037 else
7039 /* If we are here, then there are three valid possibilities:
7040 1. ARRAY [ EXP ]
7041 2. ARRAY [ EXP : EXP ]
7042 3. ARRAY [ EXP : EXP : EXP ] */
7044 *init_index = cp_parser_expression (parser);
7045 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7047 /* This indicates that we have a normal array expression. */
7048 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7049 return NULL_TREE;
7052 /* Consume the ':'. */
7053 cp_lexer_consume_token (parser->lexer);
7054 length_index = cp_parser_expression (parser);
7055 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7057 cp_lexer_consume_token (parser->lexer);
7058 stride = cp_parser_expression (parser);
7061 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7063 if (*init_index == error_mark_node || length_index == error_mark_node
7064 || stride == error_mark_node || array_type == error_mark_node)
7066 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7067 cp_lexer_consume_token (parser->lexer);
7068 return error_mark_node;
7070 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7072 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7073 length_index, stride, array_type);
7074 return value_tree;
7077 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7078 by cp_parser_builtin_offsetof. We're looking for
7080 postfix-expression [ expression ]
7081 postfix-expression [ braced-init-list ] (C++11)
7083 FOR_OFFSETOF is set if we're being called in that context, which
7084 changes how we deal with integer constant expressions. */
7086 static tree
7087 cp_parser_postfix_open_square_expression (cp_parser *parser,
7088 tree postfix_expression,
7089 bool for_offsetof,
7090 bool decltype_p)
7092 tree index = NULL_TREE;
7093 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7094 bool saved_greater_than_is_operator_p;
7096 /* Consume the `[' token. */
7097 cp_lexer_consume_token (parser->lexer);
7099 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7100 parser->greater_than_is_operator_p = true;
7102 /* Parse the index expression. */
7103 /* ??? For offsetof, there is a question of what to allow here. If
7104 offsetof is not being used in an integral constant expression context,
7105 then we *could* get the right answer by computing the value at runtime.
7106 If we are in an integral constant expression context, then we might
7107 could accept any constant expression; hard to say without analysis.
7108 Rather than open the barn door too wide right away, allow only integer
7109 constant expressions here. */
7110 if (for_offsetof)
7111 index = cp_parser_constant_expression (parser);
7112 else
7114 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7116 bool expr_nonconst_p;
7117 cp_lexer_set_source_position (parser->lexer);
7118 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7119 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7120 if (flag_cilkplus
7121 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7123 error_at (cp_lexer_peek_token (parser->lexer)->location,
7124 "braced list index is not allowed with array "
7125 "notation");
7126 cp_parser_skip_to_end_of_statement (parser);
7127 return error_mark_node;
7130 else if (flag_cilkplus)
7132 /* Here are have these two options:
7133 ARRAY[EXP : EXP] - Array notation expr with default
7134 stride of 1.
7135 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7136 stride. */
7137 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7138 postfix_expression);
7139 if (an_exp)
7140 return an_exp;
7142 else
7143 index = cp_parser_expression (parser);
7146 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7148 /* Look for the closing `]'. */
7149 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7151 /* Build the ARRAY_REF. */
7152 postfix_expression = grok_array_decl (loc, postfix_expression,
7153 index, decltype_p);
7155 /* When not doing offsetof, array references are not permitted in
7156 constant-expressions. */
7157 if (!for_offsetof
7158 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7159 postfix_expression = error_mark_node;
7161 return postfix_expression;
7164 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7165 by cp_parser_builtin_offsetof. We're looking for
7167 postfix-expression . template [opt] id-expression
7168 postfix-expression . pseudo-destructor-name
7169 postfix-expression -> template [opt] id-expression
7170 postfix-expression -> pseudo-destructor-name
7172 FOR_OFFSETOF is set if we're being called in that context. That sorta
7173 limits what of the above we'll actually accept, but nevermind.
7174 TOKEN_TYPE is the "." or "->" token, which will already have been
7175 removed from the stream. */
7177 static tree
7178 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7179 enum cpp_ttype token_type,
7180 cp_expr postfix_expression,
7181 bool for_offsetof, cp_id_kind *idk,
7182 location_t location)
7184 tree name;
7185 bool dependent_p;
7186 bool pseudo_destructor_p;
7187 tree scope = NULL_TREE;
7188 location_t start_loc = postfix_expression.get_start ();
7190 /* If this is a `->' operator, dereference the pointer. */
7191 if (token_type == CPP_DEREF)
7192 postfix_expression = build_x_arrow (location, postfix_expression,
7193 tf_warning_or_error);
7194 /* Check to see whether or not the expression is type-dependent and
7195 not the current instantiation. */
7196 dependent_p = type_dependent_object_expression_p (postfix_expression);
7197 /* The identifier following the `->' or `.' is not qualified. */
7198 parser->scope = NULL_TREE;
7199 parser->qualifying_scope = NULL_TREE;
7200 parser->object_scope = NULL_TREE;
7201 *idk = CP_ID_KIND_NONE;
7203 /* Enter the scope corresponding to the type of the object
7204 given by the POSTFIX_EXPRESSION. */
7205 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
7207 scope = TREE_TYPE (postfix_expression);
7208 /* According to the standard, no expression should ever have
7209 reference type. Unfortunately, we do not currently match
7210 the standard in this respect in that our internal representation
7211 of an expression may have reference type even when the standard
7212 says it does not. Therefore, we have to manually obtain the
7213 underlying type here. */
7214 scope = non_reference (scope);
7215 /* The type of the POSTFIX_EXPRESSION must be complete. */
7216 /* Unlike the object expression in other contexts, *this is not
7217 required to be of complete type for purposes of class member
7218 access (5.2.5) outside the member function body. */
7219 if (postfix_expression != current_class_ref
7220 && !(processing_template_decl
7221 && current_class_type
7222 && (same_type_ignoring_top_level_qualifiers_p
7223 (scope, current_class_type))))
7224 scope = complete_type_or_else (scope, postfix_expression);
7225 /* Let the name lookup machinery know that we are processing a
7226 class member access expression. */
7227 parser->context->object_type = scope;
7228 /* If something went wrong, we want to be able to discern that case,
7229 as opposed to the case where there was no SCOPE due to the type
7230 of expression being dependent. */
7231 if (!scope)
7232 scope = error_mark_node;
7233 /* If the SCOPE was erroneous, make the various semantic analysis
7234 functions exit quickly -- and without issuing additional error
7235 messages. */
7236 if (scope == error_mark_node)
7237 postfix_expression = error_mark_node;
7239 else
7240 /* Tell cp_parser_lookup_name that there was an object, even though it's
7241 type-dependent. */
7242 parser->context->object_type = unknown_type_node;
7244 /* Assume this expression is not a pseudo-destructor access. */
7245 pseudo_destructor_p = false;
7247 /* If the SCOPE is a scalar type, then, if this is a valid program,
7248 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7249 is type dependent, it can be pseudo-destructor-name or something else.
7250 Try to parse it as pseudo-destructor-name first. */
7251 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7253 tree s;
7254 tree type;
7256 cp_parser_parse_tentatively (parser);
7257 /* Parse the pseudo-destructor-name. */
7258 s = NULL_TREE;
7259 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7260 &s, &type);
7261 if (dependent_p
7262 && (cp_parser_error_occurred (parser)
7263 || !SCALAR_TYPE_P (type)))
7264 cp_parser_abort_tentative_parse (parser);
7265 else if (cp_parser_parse_definitely (parser))
7267 pseudo_destructor_p = true;
7268 postfix_expression
7269 = finish_pseudo_destructor_expr (postfix_expression,
7270 s, type, location);
7274 if (!pseudo_destructor_p)
7276 /* If the SCOPE is not a scalar type, we are looking at an
7277 ordinary class member access expression, rather than a
7278 pseudo-destructor-name. */
7279 bool template_p;
7280 cp_token *token = cp_lexer_peek_token (parser->lexer);
7281 /* Parse the id-expression. */
7282 name = (cp_parser_id_expression
7283 (parser,
7284 cp_parser_optional_template_keyword (parser),
7285 /*check_dependency_p=*/true,
7286 &template_p,
7287 /*declarator_p=*/false,
7288 /*optional_p=*/false));
7289 /* In general, build a SCOPE_REF if the member name is qualified.
7290 However, if the name was not dependent and has already been
7291 resolved; there is no need to build the SCOPE_REF. For example;
7293 struct X { void f(); };
7294 template <typename T> void f(T* t) { t->X::f(); }
7296 Even though "t" is dependent, "X::f" is not and has been resolved
7297 to a BASELINK; there is no need to include scope information. */
7299 /* But we do need to remember that there was an explicit scope for
7300 virtual function calls. */
7301 if (parser->scope)
7302 *idk = CP_ID_KIND_QUALIFIED;
7304 /* If the name is a template-id that names a type, we will get a
7305 TYPE_DECL here. That is invalid code. */
7306 if (TREE_CODE (name) == TYPE_DECL)
7308 error_at (token->location, "invalid use of %qD", name);
7309 postfix_expression = error_mark_node;
7311 else
7313 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7315 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7317 error_at (token->location, "%<%D::%D%> is not a class member",
7318 parser->scope, name);
7319 postfix_expression = error_mark_node;
7321 else
7322 name = build_qualified_name (/*type=*/NULL_TREE,
7323 parser->scope,
7324 name,
7325 template_p);
7326 parser->scope = NULL_TREE;
7327 parser->qualifying_scope = NULL_TREE;
7328 parser->object_scope = NULL_TREE;
7330 if (parser->scope && name && BASELINK_P (name))
7331 adjust_result_of_qualified_name_lookup
7332 (name, parser->scope, scope);
7333 postfix_expression
7334 = finish_class_member_access_expr (postfix_expression, name,
7335 template_p,
7336 tf_warning_or_error);
7337 /* Build a location e.g.:
7338 ptr->access_expr
7339 ~~~^~~~~~~~~~~~~
7340 where the caret is at the deref token, ranging from
7341 the start of postfix_expression to the end of the access expr. */
7342 location_t end_loc
7343 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7344 location_t combined_loc
7345 = make_location (input_location, start_loc, end_loc);
7346 protected_set_expr_location (postfix_expression, combined_loc);
7350 /* We no longer need to look up names in the scope of the object on
7351 the left-hand side of the `.' or `->' operator. */
7352 parser->context->object_type = NULL_TREE;
7354 /* Outside of offsetof, these operators may not appear in
7355 constant-expressions. */
7356 if (!for_offsetof
7357 && (cp_parser_non_integral_constant_expression
7358 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7359 postfix_expression = error_mark_node;
7361 return postfix_expression;
7364 /* Parse a parenthesized expression-list.
7366 expression-list:
7367 assignment-expression
7368 expression-list, assignment-expression
7370 attribute-list:
7371 expression-list
7372 identifier
7373 identifier, expression-list
7375 CAST_P is true if this expression is the target of a cast.
7377 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7378 argument pack.
7380 Returns a vector of trees. Each element is a representation of an
7381 assignment-expression. NULL is returned if the ( and or ) are
7382 missing. An empty, but allocated, vector is returned on no
7383 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7384 if we are parsing an attribute list for an attribute that wants a
7385 plain identifier argument, normal_attr for an attribute that wants
7386 an expression, or non_attr if we aren't parsing an attribute list. If
7387 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7388 not all of the expressions in the list were constant.
7389 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7390 will be written to with the location of the closing parenthesis. If
7391 an error occurs, it may or may not be written to. */
7393 static vec<tree, va_gc> *
7394 cp_parser_parenthesized_expression_list (cp_parser* parser,
7395 int is_attribute_list,
7396 bool cast_p,
7397 bool allow_expansion_p,
7398 bool *non_constant_p,
7399 location_t *close_paren_loc)
7401 vec<tree, va_gc> *expression_list;
7402 bool fold_expr_p = is_attribute_list != non_attr;
7403 tree identifier = NULL_TREE;
7404 bool saved_greater_than_is_operator_p;
7406 /* Assume all the expressions will be constant. */
7407 if (non_constant_p)
7408 *non_constant_p = false;
7410 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7411 return NULL;
7413 expression_list = make_tree_vector ();
7415 /* Within a parenthesized expression, a `>' token is always
7416 the greater-than operator. */
7417 saved_greater_than_is_operator_p
7418 = parser->greater_than_is_operator_p;
7419 parser->greater_than_is_operator_p = true;
7421 /* Consume expressions until there are no more. */
7422 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7423 while (true)
7425 tree expr;
7427 /* At the beginning of attribute lists, check to see if the
7428 next token is an identifier. */
7429 if (is_attribute_list == id_attr
7430 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7432 cp_token *token;
7434 /* Consume the identifier. */
7435 token = cp_lexer_consume_token (parser->lexer);
7436 /* Save the identifier. */
7437 identifier = token->u.value;
7439 else
7441 bool expr_non_constant_p;
7443 /* Parse the next assignment-expression. */
7444 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7446 /* A braced-init-list. */
7447 cp_lexer_set_source_position (parser->lexer);
7448 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7449 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7450 if (non_constant_p && expr_non_constant_p)
7451 *non_constant_p = true;
7453 else if (non_constant_p)
7455 expr = (cp_parser_constant_expression
7456 (parser, /*allow_non_constant_p=*/true,
7457 &expr_non_constant_p));
7458 if (expr_non_constant_p)
7459 *non_constant_p = true;
7461 else
7462 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7463 cast_p);
7465 if (fold_expr_p)
7466 expr = instantiate_non_dependent_expr (expr);
7468 /* If we have an ellipsis, then this is an expression
7469 expansion. */
7470 if (allow_expansion_p
7471 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7473 /* Consume the `...'. */
7474 cp_lexer_consume_token (parser->lexer);
7476 /* Build the argument pack. */
7477 expr = make_pack_expansion (expr);
7480 /* Add it to the list. We add error_mark_node
7481 expressions to the list, so that we can still tell if
7482 the correct form for a parenthesized expression-list
7483 is found. That gives better errors. */
7484 vec_safe_push (expression_list, expr);
7486 if (expr == error_mark_node)
7487 goto skip_comma;
7490 /* After the first item, attribute lists look the same as
7491 expression lists. */
7492 is_attribute_list = non_attr;
7494 get_comma:;
7495 /* If the next token isn't a `,', then we are done. */
7496 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7497 break;
7499 /* Otherwise, consume the `,' and keep going. */
7500 cp_lexer_consume_token (parser->lexer);
7503 if (close_paren_loc)
7504 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7506 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7508 int ending;
7510 skip_comma:;
7511 /* We try and resync to an unnested comma, as that will give the
7512 user better diagnostics. */
7513 ending = cp_parser_skip_to_closing_parenthesis (parser,
7514 /*recovering=*/true,
7515 /*or_comma=*/true,
7516 /*consume_paren=*/true);
7517 if (ending < 0)
7518 goto get_comma;
7519 if (!ending)
7521 parser->greater_than_is_operator_p
7522 = saved_greater_than_is_operator_p;
7523 return NULL;
7527 parser->greater_than_is_operator_p
7528 = saved_greater_than_is_operator_p;
7530 if (identifier)
7531 vec_safe_insert (expression_list, 0, identifier);
7533 return expression_list;
7536 /* Parse a pseudo-destructor-name.
7538 pseudo-destructor-name:
7539 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7540 :: [opt] nested-name-specifier template template-id :: ~ type-name
7541 :: [opt] nested-name-specifier [opt] ~ type-name
7543 If either of the first two productions is used, sets *SCOPE to the
7544 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7545 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7546 or ERROR_MARK_NODE if the parse fails. */
7548 static void
7549 cp_parser_pseudo_destructor_name (cp_parser* parser,
7550 tree object,
7551 tree* scope,
7552 tree* type)
7554 bool nested_name_specifier_p;
7556 /* Handle ~auto. */
7557 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7558 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7559 && !type_dependent_expression_p (object))
7561 if (cxx_dialect < cxx14)
7562 pedwarn (input_location, 0,
7563 "%<~auto%> only available with "
7564 "-std=c++14 or -std=gnu++14");
7565 cp_lexer_consume_token (parser->lexer);
7566 cp_lexer_consume_token (parser->lexer);
7567 *scope = NULL_TREE;
7568 *type = TREE_TYPE (object);
7569 return;
7572 /* Assume that things will not work out. */
7573 *type = error_mark_node;
7575 /* Look for the optional `::' operator. */
7576 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7577 /* Look for the optional nested-name-specifier. */
7578 nested_name_specifier_p
7579 = (cp_parser_nested_name_specifier_opt (parser,
7580 /*typename_keyword_p=*/false,
7581 /*check_dependency_p=*/true,
7582 /*type_p=*/false,
7583 /*is_declaration=*/false)
7584 != NULL_TREE);
7585 /* Now, if we saw a nested-name-specifier, we might be doing the
7586 second production. */
7587 if (nested_name_specifier_p
7588 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7590 /* Consume the `template' keyword. */
7591 cp_lexer_consume_token (parser->lexer);
7592 /* Parse the template-id. */
7593 cp_parser_template_id (parser,
7594 /*template_keyword_p=*/true,
7595 /*check_dependency_p=*/false,
7596 class_type,
7597 /*is_declaration=*/true);
7598 /* Look for the `::' token. */
7599 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7601 /* If the next token is not a `~', then there might be some
7602 additional qualification. */
7603 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7605 /* At this point, we're looking for "type-name :: ~". The type-name
7606 must not be a class-name, since this is a pseudo-destructor. So,
7607 it must be either an enum-name, or a typedef-name -- both of which
7608 are just identifiers. So, we peek ahead to check that the "::"
7609 and "~" tokens are present; if they are not, then we can avoid
7610 calling type_name. */
7611 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7612 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7613 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7615 cp_parser_error (parser, "non-scalar type");
7616 return;
7619 /* Look for the type-name. */
7620 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7621 if (*scope == error_mark_node)
7622 return;
7624 /* Look for the `::' token. */
7625 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7627 else
7628 *scope = NULL_TREE;
7630 /* Look for the `~'. */
7631 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7633 /* Once we see the ~, this has to be a pseudo-destructor. */
7634 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7635 cp_parser_commit_to_topmost_tentative_parse (parser);
7637 /* Look for the type-name again. We are not responsible for
7638 checking that it matches the first type-name. */
7639 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7642 /* Parse a unary-expression.
7644 unary-expression:
7645 postfix-expression
7646 ++ cast-expression
7647 -- cast-expression
7648 unary-operator cast-expression
7649 sizeof unary-expression
7650 sizeof ( type-id )
7651 alignof ( type-id ) [C++0x]
7652 new-expression
7653 delete-expression
7655 GNU Extensions:
7657 unary-expression:
7658 __extension__ cast-expression
7659 __alignof__ unary-expression
7660 __alignof__ ( type-id )
7661 alignof unary-expression [C++0x]
7662 __real__ cast-expression
7663 __imag__ cast-expression
7664 && identifier
7665 sizeof ( type-id ) { initializer-list , [opt] }
7666 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7667 __alignof__ ( type-id ) { initializer-list , [opt] }
7669 ADDRESS_P is true iff the unary-expression is appearing as the
7670 operand of the `&' operator. CAST_P is true if this expression is
7671 the target of a cast.
7673 Returns a representation of the expression. */
7675 static cp_expr
7676 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7677 bool address_p, bool cast_p, bool decltype_p)
7679 cp_token *token;
7680 enum tree_code unary_operator;
7682 /* Peek at the next token. */
7683 token = cp_lexer_peek_token (parser->lexer);
7684 /* Some keywords give away the kind of expression. */
7685 if (token->type == CPP_KEYWORD)
7687 enum rid keyword = token->keyword;
7689 switch (keyword)
7691 case RID_ALIGNOF:
7692 case RID_SIZEOF:
7694 tree operand, ret;
7695 enum tree_code op;
7696 location_t first_loc;
7698 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7699 /* Consume the token. */
7700 cp_lexer_consume_token (parser->lexer);
7701 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7702 /* Parse the operand. */
7703 operand = cp_parser_sizeof_operand (parser, keyword);
7705 if (TYPE_P (operand))
7706 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7707 else
7709 /* ISO C++ defines alignof only with types, not with
7710 expressions. So pedwarn if alignof is used with a non-
7711 type expression. However, __alignof__ is ok. */
7712 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7713 pedwarn (token->location, OPT_Wpedantic,
7714 "ISO C++ does not allow %<alignof%> "
7715 "with a non-type");
7717 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7719 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7720 SIZEOF_EXPR with the original operand. */
7721 if (op == SIZEOF_EXPR && ret != error_mark_node)
7723 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7725 if (!processing_template_decl && TYPE_P (operand))
7727 ret = build_min (SIZEOF_EXPR, size_type_node,
7728 build1 (NOP_EXPR, operand,
7729 error_mark_node));
7730 SIZEOF_EXPR_TYPE_P (ret) = 1;
7732 else
7733 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7734 TREE_SIDE_EFFECTS (ret) = 0;
7735 TREE_READONLY (ret) = 1;
7737 SET_EXPR_LOCATION (ret, first_loc);
7739 return ret;
7742 case RID_NEW:
7743 return cp_parser_new_expression (parser);
7745 case RID_DELETE:
7746 return cp_parser_delete_expression (parser);
7748 case RID_EXTENSION:
7750 /* The saved value of the PEDANTIC flag. */
7751 int saved_pedantic;
7752 tree expr;
7754 /* Save away the PEDANTIC flag. */
7755 cp_parser_extension_opt (parser, &saved_pedantic);
7756 /* Parse the cast-expression. */
7757 expr = cp_parser_simple_cast_expression (parser);
7758 /* Restore the PEDANTIC flag. */
7759 pedantic = saved_pedantic;
7761 return expr;
7764 case RID_REALPART:
7765 case RID_IMAGPART:
7767 tree expression;
7769 /* Consume the `__real__' or `__imag__' token. */
7770 cp_lexer_consume_token (parser->lexer);
7771 /* Parse the cast-expression. */
7772 expression = cp_parser_simple_cast_expression (parser);
7773 /* Create the complete representation. */
7774 return build_x_unary_op (token->location,
7775 (keyword == RID_REALPART
7776 ? REALPART_EXPR : IMAGPART_EXPR),
7777 expression,
7778 tf_warning_or_error);
7780 break;
7782 case RID_TRANSACTION_ATOMIC:
7783 case RID_TRANSACTION_RELAXED:
7784 return cp_parser_transaction_expression (parser, keyword);
7786 case RID_NOEXCEPT:
7788 tree expr;
7789 const char *saved_message;
7790 bool saved_integral_constant_expression_p;
7791 bool saved_non_integral_constant_expression_p;
7792 bool saved_greater_than_is_operator_p;
7794 cp_lexer_consume_token (parser->lexer);
7795 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7797 saved_message = parser->type_definition_forbidden_message;
7798 parser->type_definition_forbidden_message
7799 = G_("types may not be defined in %<noexcept%> expressions");
7801 saved_integral_constant_expression_p
7802 = parser->integral_constant_expression_p;
7803 saved_non_integral_constant_expression_p
7804 = parser->non_integral_constant_expression_p;
7805 parser->integral_constant_expression_p = false;
7807 saved_greater_than_is_operator_p
7808 = parser->greater_than_is_operator_p;
7809 parser->greater_than_is_operator_p = true;
7811 ++cp_unevaluated_operand;
7812 ++c_inhibit_evaluation_warnings;
7813 ++cp_noexcept_operand;
7814 expr = cp_parser_expression (parser);
7815 --cp_noexcept_operand;
7816 --c_inhibit_evaluation_warnings;
7817 --cp_unevaluated_operand;
7819 parser->greater_than_is_operator_p
7820 = saved_greater_than_is_operator_p;
7822 parser->integral_constant_expression_p
7823 = saved_integral_constant_expression_p;
7824 parser->non_integral_constant_expression_p
7825 = saved_non_integral_constant_expression_p;
7827 parser->type_definition_forbidden_message = saved_message;
7829 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7830 return finish_noexcept_expr (expr, tf_warning_or_error);
7833 default:
7834 break;
7838 /* Look for the `:: new' and `:: delete', which also signal the
7839 beginning of a new-expression, or delete-expression,
7840 respectively. If the next token is `::', then it might be one of
7841 these. */
7842 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7844 enum rid keyword;
7846 /* See if the token after the `::' is one of the keywords in
7847 which we're interested. */
7848 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7849 /* If it's `new', we have a new-expression. */
7850 if (keyword == RID_NEW)
7851 return cp_parser_new_expression (parser);
7852 /* Similarly, for `delete'. */
7853 else if (keyword == RID_DELETE)
7854 return cp_parser_delete_expression (parser);
7857 /* Look for a unary operator. */
7858 unary_operator = cp_parser_unary_operator (token);
7859 /* The `++' and `--' operators can be handled similarly, even though
7860 they are not technically unary-operators in the grammar. */
7861 if (unary_operator == ERROR_MARK)
7863 if (token->type == CPP_PLUS_PLUS)
7864 unary_operator = PREINCREMENT_EXPR;
7865 else if (token->type == CPP_MINUS_MINUS)
7866 unary_operator = PREDECREMENT_EXPR;
7867 /* Handle the GNU address-of-label extension. */
7868 else if (cp_parser_allow_gnu_extensions_p (parser)
7869 && token->type == CPP_AND_AND)
7871 tree identifier;
7872 tree expression;
7873 location_t start_loc = token->location;
7875 /* Consume the '&&' token. */
7876 cp_lexer_consume_token (parser->lexer);
7877 /* Look for the identifier. */
7878 location_t finish_loc
7879 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
7880 identifier = cp_parser_identifier (parser);
7881 /* Construct a location of the form:
7882 &&label
7883 ^~~~~~~
7884 with caret==start at the "&&", finish at the end of the label. */
7885 location_t combined_loc
7886 = make_location (start_loc, start_loc, finish_loc);
7887 /* Create an expression representing the address. */
7888 expression = finish_label_address_expr (identifier, combined_loc);
7889 if (cp_parser_non_integral_constant_expression (parser,
7890 NIC_ADDR_LABEL))
7891 expression = error_mark_node;
7892 return expression;
7895 if (unary_operator != ERROR_MARK)
7897 cp_expr cast_expression;
7898 cp_expr expression = error_mark_node;
7899 non_integral_constant non_constant_p = NIC_NONE;
7900 location_t loc = token->location;
7901 tsubst_flags_t complain = complain_flags (decltype_p);
7903 /* Consume the operator token. */
7904 token = cp_lexer_consume_token (parser->lexer);
7905 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
7907 /* Parse the cast-expression. */
7908 cast_expression
7909 = cp_parser_cast_expression (parser,
7910 unary_operator == ADDR_EXPR,
7911 /*cast_p=*/false,
7912 /*decltype*/false,
7913 pidk);
7915 /* Make a location:
7916 OP_TOKEN CAST_EXPRESSION
7917 ^~~~~~~~~~~~~~~~~~~~~~~~~
7918 with start==caret at the operator token, and
7919 extending to the end of the cast_expression. */
7920 loc = make_location (loc, loc, cast_expression.get_finish ());
7922 /* Now, build an appropriate representation. */
7923 switch (unary_operator)
7925 case INDIRECT_REF:
7926 non_constant_p = NIC_STAR;
7927 expression = build_x_indirect_ref (loc, cast_expression,
7928 RO_UNARY_STAR,
7929 complain);
7930 /* TODO: build_x_indirect_ref does not always honor the
7931 location, so ensure it is set. */
7932 expression.set_location (loc);
7933 break;
7935 case ADDR_EXPR:
7936 non_constant_p = NIC_ADDR;
7937 /* Fall through. */
7938 case BIT_NOT_EXPR:
7939 expression = build_x_unary_op (loc, unary_operator,
7940 cast_expression,
7941 complain);
7942 /* TODO: build_x_unary_op does not always honor the location,
7943 so ensure it is set. */
7944 expression.set_location (loc);
7945 break;
7947 case PREINCREMENT_EXPR:
7948 case PREDECREMENT_EXPR:
7949 non_constant_p = unary_operator == PREINCREMENT_EXPR
7950 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7951 /* Fall through. */
7952 case NEGATE_EXPR:
7953 /* Immediately fold negation of a constant, unless the constant is 0
7954 (since -0 == 0) or it would overflow. */
7955 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
7956 && CONSTANT_CLASS_P (cast_expression)
7957 && !integer_zerop (cast_expression)
7958 && !TREE_OVERFLOW (cast_expression))
7960 tree folded = fold_build1 (unary_operator,
7961 TREE_TYPE (cast_expression),
7962 cast_expression);
7963 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
7965 expression = cp_expr (folded, loc);
7966 break;
7969 /* Fall through. */
7970 case UNARY_PLUS_EXPR:
7971 case TRUTH_NOT_EXPR:
7972 expression = finish_unary_op_expr (loc, unary_operator,
7973 cast_expression, complain);
7974 break;
7976 default:
7977 gcc_unreachable ();
7980 if (non_constant_p != NIC_NONE
7981 && cp_parser_non_integral_constant_expression (parser,
7982 non_constant_p))
7983 expression = error_mark_node;
7985 return expression;
7988 return cp_parser_postfix_expression (parser, address_p, cast_p,
7989 /*member_access_only_p=*/false,
7990 decltype_p,
7991 pidk);
7994 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7995 unary-operator, the corresponding tree code is returned. */
7997 static enum tree_code
7998 cp_parser_unary_operator (cp_token* token)
8000 switch (token->type)
8002 case CPP_MULT:
8003 return INDIRECT_REF;
8005 case CPP_AND:
8006 return ADDR_EXPR;
8008 case CPP_PLUS:
8009 return UNARY_PLUS_EXPR;
8011 case CPP_MINUS:
8012 return NEGATE_EXPR;
8014 case CPP_NOT:
8015 return TRUTH_NOT_EXPR;
8017 case CPP_COMPL:
8018 return BIT_NOT_EXPR;
8020 default:
8021 return ERROR_MARK;
8025 /* Parse a new-expression.
8027 new-expression:
8028 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8029 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8031 Returns a representation of the expression. */
8033 static tree
8034 cp_parser_new_expression (cp_parser* parser)
8036 bool global_scope_p;
8037 vec<tree, va_gc> *placement;
8038 tree type;
8039 vec<tree, va_gc> *initializer;
8040 tree nelts = NULL_TREE;
8041 tree ret;
8043 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8045 /* Look for the optional `::' operator. */
8046 global_scope_p
8047 = (cp_parser_global_scope_opt (parser,
8048 /*current_scope_valid_p=*/false)
8049 != NULL_TREE);
8050 /* Look for the `new' operator. */
8051 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8052 /* There's no easy way to tell a new-placement from the
8053 `( type-id )' construct. */
8054 cp_parser_parse_tentatively (parser);
8055 /* Look for a new-placement. */
8056 placement = cp_parser_new_placement (parser);
8057 /* If that didn't work out, there's no new-placement. */
8058 if (!cp_parser_parse_definitely (parser))
8060 if (placement != NULL)
8061 release_tree_vector (placement);
8062 placement = NULL;
8065 /* If the next token is a `(', then we have a parenthesized
8066 type-id. */
8067 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8069 cp_token *token;
8070 const char *saved_message = parser->type_definition_forbidden_message;
8072 /* Consume the `('. */
8073 cp_lexer_consume_token (parser->lexer);
8075 /* Parse the type-id. */
8076 parser->type_definition_forbidden_message
8077 = G_("types may not be defined in a new-expression");
8079 type_id_in_expr_sentinel s (parser);
8080 type = cp_parser_type_id (parser);
8082 parser->type_definition_forbidden_message = saved_message;
8084 /* Look for the closing `)'. */
8085 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8086 token = cp_lexer_peek_token (parser->lexer);
8087 /* There should not be a direct-new-declarator in this production,
8088 but GCC used to allowed this, so we check and emit a sensible error
8089 message for this case. */
8090 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8092 error_at (token->location,
8093 "array bound forbidden after parenthesized type-id");
8094 inform (token->location,
8095 "try removing the parentheses around the type-id");
8096 cp_parser_direct_new_declarator (parser);
8099 /* Otherwise, there must be a new-type-id. */
8100 else
8101 type = cp_parser_new_type_id (parser, &nelts);
8103 /* If the next token is a `(' or '{', then we have a new-initializer. */
8104 cp_token *token = cp_lexer_peek_token (parser->lexer);
8105 if (token->type == CPP_OPEN_PAREN
8106 || token->type == CPP_OPEN_BRACE)
8107 initializer = cp_parser_new_initializer (parser);
8108 else
8109 initializer = NULL;
8111 /* A new-expression may not appear in an integral constant
8112 expression. */
8113 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8114 ret = error_mark_node;
8115 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8116 of a new-type-id or type-id of a new-expression, the new-expression shall
8117 contain a new-initializer of the form ( assignment-expression )".
8118 Additionally, consistently with the spirit of DR 1467, we want to accept
8119 'new auto { 2 }' too. */
8120 else if (type_uses_auto (type)
8121 && (vec_safe_length (initializer) != 1
8122 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8123 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8125 error_at (token->location,
8126 "initialization of new-expression for type %<auto%> "
8127 "requires exactly one element");
8128 ret = error_mark_node;
8130 else
8132 /* Construct a location e.g.:
8133 ptr = new int[100]
8134 ^~~~~~~~~~~~
8135 with caret == start at the start of the "new" token, and the end
8136 at the end of the final token we consumed. */
8137 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8138 location_t end_loc = get_finish (end_tok->location);
8139 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8141 /* Create a representation of the new-expression. */
8142 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8143 tf_warning_or_error);
8144 protected_set_expr_location (ret, combined_loc);
8147 if (placement != NULL)
8148 release_tree_vector (placement);
8149 if (initializer != NULL)
8150 release_tree_vector (initializer);
8152 return ret;
8155 /* Parse a new-placement.
8157 new-placement:
8158 ( expression-list )
8160 Returns the same representation as for an expression-list. */
8162 static vec<tree, va_gc> *
8163 cp_parser_new_placement (cp_parser* parser)
8165 vec<tree, va_gc> *expression_list;
8167 /* Parse the expression-list. */
8168 expression_list = (cp_parser_parenthesized_expression_list
8169 (parser, non_attr, /*cast_p=*/false,
8170 /*allow_expansion_p=*/true,
8171 /*non_constant_p=*/NULL));
8173 if (expression_list && expression_list->is_empty ())
8174 error ("expected expression-list or type-id");
8176 return expression_list;
8179 /* Parse a new-type-id.
8181 new-type-id:
8182 type-specifier-seq new-declarator [opt]
8184 Returns the TYPE allocated. If the new-type-id indicates an array
8185 type, *NELTS is set to the number of elements in the last array
8186 bound; the TYPE will not include the last array bound. */
8188 static tree
8189 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8191 cp_decl_specifier_seq type_specifier_seq;
8192 cp_declarator *new_declarator;
8193 cp_declarator *declarator;
8194 cp_declarator *outer_declarator;
8195 const char *saved_message;
8197 /* The type-specifier sequence must not contain type definitions.
8198 (It cannot contain declarations of new types either, but if they
8199 are not definitions we will catch that because they are not
8200 complete.) */
8201 saved_message = parser->type_definition_forbidden_message;
8202 parser->type_definition_forbidden_message
8203 = G_("types may not be defined in a new-type-id");
8204 /* Parse the type-specifier-seq. */
8205 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8206 /*is_trailing_return=*/false,
8207 &type_specifier_seq);
8208 /* Restore the old message. */
8209 parser->type_definition_forbidden_message = saved_message;
8211 if (type_specifier_seq.type == error_mark_node)
8212 return error_mark_node;
8214 /* Parse the new-declarator. */
8215 new_declarator = cp_parser_new_declarator_opt (parser);
8217 /* Determine the number of elements in the last array dimension, if
8218 any. */
8219 *nelts = NULL_TREE;
8220 /* Skip down to the last array dimension. */
8221 declarator = new_declarator;
8222 outer_declarator = NULL;
8223 while (declarator && (declarator->kind == cdk_pointer
8224 || declarator->kind == cdk_ptrmem))
8226 outer_declarator = declarator;
8227 declarator = declarator->declarator;
8229 while (declarator
8230 && declarator->kind == cdk_array
8231 && declarator->declarator
8232 && declarator->declarator->kind == cdk_array)
8234 outer_declarator = declarator;
8235 declarator = declarator->declarator;
8238 if (declarator && declarator->kind == cdk_array)
8240 *nelts = declarator->u.array.bounds;
8241 if (*nelts == error_mark_node)
8242 *nelts = integer_one_node;
8244 if (outer_declarator)
8245 outer_declarator->declarator = declarator->declarator;
8246 else
8247 new_declarator = NULL;
8250 return groktypename (&type_specifier_seq, new_declarator, false);
8253 /* Parse an (optional) new-declarator.
8255 new-declarator:
8256 ptr-operator new-declarator [opt]
8257 direct-new-declarator
8259 Returns the declarator. */
8261 static cp_declarator *
8262 cp_parser_new_declarator_opt (cp_parser* parser)
8264 enum tree_code code;
8265 tree type, std_attributes = NULL_TREE;
8266 cp_cv_quals cv_quals;
8268 /* We don't know if there's a ptr-operator next, or not. */
8269 cp_parser_parse_tentatively (parser);
8270 /* Look for a ptr-operator. */
8271 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8272 /* If that worked, look for more new-declarators. */
8273 if (cp_parser_parse_definitely (parser))
8275 cp_declarator *declarator;
8277 /* Parse another optional declarator. */
8278 declarator = cp_parser_new_declarator_opt (parser);
8280 declarator = cp_parser_make_indirect_declarator
8281 (code, type, cv_quals, declarator, std_attributes);
8283 return declarator;
8286 /* If the next token is a `[', there is a direct-new-declarator. */
8287 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8288 return cp_parser_direct_new_declarator (parser);
8290 return NULL;
8293 /* Parse a direct-new-declarator.
8295 direct-new-declarator:
8296 [ expression ]
8297 direct-new-declarator [constant-expression]
8301 static cp_declarator *
8302 cp_parser_direct_new_declarator (cp_parser* parser)
8304 cp_declarator *declarator = NULL;
8306 while (true)
8308 tree expression;
8309 cp_token *token;
8311 /* Look for the opening `['. */
8312 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8314 token = cp_lexer_peek_token (parser->lexer);
8315 expression = cp_parser_expression (parser);
8316 /* The standard requires that the expression have integral
8317 type. DR 74 adds enumeration types. We believe that the
8318 real intent is that these expressions be handled like the
8319 expression in a `switch' condition, which also allows
8320 classes with a single conversion to integral or
8321 enumeration type. */
8322 if (!processing_template_decl)
8324 expression
8325 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8326 expression,
8327 /*complain=*/true);
8328 if (!expression)
8330 error_at (token->location,
8331 "expression in new-declarator must have integral "
8332 "or enumeration type");
8333 expression = error_mark_node;
8337 /* Look for the closing `]'. */
8338 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8340 /* Add this bound to the declarator. */
8341 declarator = make_array_declarator (declarator, expression);
8343 /* If the next token is not a `[', then there are no more
8344 bounds. */
8345 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8346 break;
8349 return declarator;
8352 /* Parse a new-initializer.
8354 new-initializer:
8355 ( expression-list [opt] )
8356 braced-init-list
8358 Returns a representation of the expression-list. */
8360 static vec<tree, va_gc> *
8361 cp_parser_new_initializer (cp_parser* parser)
8363 vec<tree, va_gc> *expression_list;
8365 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8367 tree t;
8368 bool expr_non_constant_p;
8369 cp_lexer_set_source_position (parser->lexer);
8370 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8371 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8372 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8373 expression_list = make_tree_vector_single (t);
8375 else
8376 expression_list = (cp_parser_parenthesized_expression_list
8377 (parser, non_attr, /*cast_p=*/false,
8378 /*allow_expansion_p=*/true,
8379 /*non_constant_p=*/NULL));
8381 return expression_list;
8384 /* Parse a delete-expression.
8386 delete-expression:
8387 :: [opt] delete cast-expression
8388 :: [opt] delete [ ] cast-expression
8390 Returns a representation of the expression. */
8392 static tree
8393 cp_parser_delete_expression (cp_parser* parser)
8395 bool global_scope_p;
8396 bool array_p;
8397 tree expression;
8399 /* Look for the optional `::' operator. */
8400 global_scope_p
8401 = (cp_parser_global_scope_opt (parser,
8402 /*current_scope_valid_p=*/false)
8403 != NULL_TREE);
8404 /* Look for the `delete' keyword. */
8405 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8406 /* See if the array syntax is in use. */
8407 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8409 /* Consume the `[' token. */
8410 cp_lexer_consume_token (parser->lexer);
8411 /* Look for the `]' token. */
8412 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8413 /* Remember that this is the `[]' construct. */
8414 array_p = true;
8416 else
8417 array_p = false;
8419 /* Parse the cast-expression. */
8420 expression = cp_parser_simple_cast_expression (parser);
8422 /* A delete-expression may not appear in an integral constant
8423 expression. */
8424 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8425 return error_mark_node;
8427 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8428 tf_warning_or_error);
8431 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8432 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8433 0 otherwise. */
8435 static int
8436 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8438 cp_token *token = cp_lexer_peek_token (parser->lexer);
8439 switch (token->type)
8441 case CPP_COMMA:
8442 case CPP_SEMICOLON:
8443 case CPP_QUERY:
8444 case CPP_COLON:
8445 case CPP_CLOSE_SQUARE:
8446 case CPP_CLOSE_PAREN:
8447 case CPP_CLOSE_BRACE:
8448 case CPP_OPEN_BRACE:
8449 case CPP_DOT:
8450 case CPP_DOT_STAR:
8451 case CPP_DEREF:
8452 case CPP_DEREF_STAR:
8453 case CPP_DIV:
8454 case CPP_MOD:
8455 case CPP_LSHIFT:
8456 case CPP_RSHIFT:
8457 case CPP_LESS:
8458 case CPP_GREATER:
8459 case CPP_LESS_EQ:
8460 case CPP_GREATER_EQ:
8461 case CPP_EQ_EQ:
8462 case CPP_NOT_EQ:
8463 case CPP_EQ:
8464 case CPP_MULT_EQ:
8465 case CPP_DIV_EQ:
8466 case CPP_MOD_EQ:
8467 case CPP_PLUS_EQ:
8468 case CPP_MINUS_EQ:
8469 case CPP_RSHIFT_EQ:
8470 case CPP_LSHIFT_EQ:
8471 case CPP_AND_EQ:
8472 case CPP_XOR_EQ:
8473 case CPP_OR_EQ:
8474 case CPP_XOR:
8475 case CPP_OR:
8476 case CPP_OR_OR:
8477 case CPP_EOF:
8478 case CPP_ELLIPSIS:
8479 return 0;
8481 case CPP_OPEN_PAREN:
8482 /* In ((type ()) () the last () isn't a valid cast-expression,
8483 so the whole must be parsed as postfix-expression. */
8484 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8485 != CPP_CLOSE_PAREN;
8487 case CPP_OPEN_SQUARE:
8488 /* '[' may start a primary-expression in obj-c++ and in C++11,
8489 as a lambda-expression, eg, '(void)[]{}'. */
8490 if (cxx_dialect >= cxx11)
8491 return -1;
8492 return c_dialect_objc ();
8494 case CPP_PLUS_PLUS:
8495 case CPP_MINUS_MINUS:
8496 /* '++' and '--' may or may not start a cast-expression:
8498 struct T { void operator++(int); };
8499 void f() { (T())++; }
8503 int a;
8504 (int)++a; */
8505 return -1;
8507 default:
8508 return 1;
8512 /* Parse a cast-expression.
8514 cast-expression:
8515 unary-expression
8516 ( type-id ) cast-expression
8518 ADDRESS_P is true iff the unary-expression is appearing as the
8519 operand of the `&' operator. CAST_P is true if this expression is
8520 the target of a cast.
8522 Returns a representation of the expression. */
8524 static cp_expr
8525 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8526 bool decltype_p, cp_id_kind * pidk)
8528 /* If it's a `(', then we might be looking at a cast. */
8529 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8531 tree type = NULL_TREE;
8532 cp_expr expr (NULL_TREE);
8533 int cast_expression = 0;
8534 const char *saved_message;
8536 /* There's no way to know yet whether or not this is a cast.
8537 For example, `(int (3))' is a unary-expression, while `(int)
8538 3' is a cast. So, we resort to parsing tentatively. */
8539 cp_parser_parse_tentatively (parser);
8540 /* Types may not be defined in a cast. */
8541 saved_message = parser->type_definition_forbidden_message;
8542 parser->type_definition_forbidden_message
8543 = G_("types may not be defined in casts");
8544 /* Consume the `('. */
8545 cp_token *open_paren = cp_lexer_consume_token (parser->lexer);
8546 location_t open_paren_loc = open_paren->location;
8548 /* A very tricky bit is that `(struct S) { 3 }' is a
8549 compound-literal (which we permit in C++ as an extension).
8550 But, that construct is not a cast-expression -- it is a
8551 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8552 is legal; if the compound-literal were a cast-expression,
8553 you'd need an extra set of parentheses.) But, if we parse
8554 the type-id, and it happens to be a class-specifier, then we
8555 will commit to the parse at that point, because we cannot
8556 undo the action that is done when creating a new class. So,
8557 then we cannot back up and do a postfix-expression.
8559 Another tricky case is the following (c++/29234):
8561 struct S { void operator () (); };
8563 void foo ()
8565 ( S()() );
8568 As a type-id we parse the parenthesized S()() as a function
8569 returning a function, groktypename complains and we cannot
8570 back up in this case either.
8572 Therefore, we scan ahead to the closing `)', and check to see
8573 if the tokens after the `)' can start a cast-expression. Otherwise
8574 we are dealing with an unary-expression, a postfix-expression
8575 or something else.
8577 Yet another tricky case, in C++11, is the following (c++/54891):
8579 (void)[]{};
8581 The issue is that usually, besides the case of lambda-expressions,
8582 the parenthesized type-id cannot be followed by '[', and, eg, we
8583 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8584 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8585 we don't commit, we try a cast-expression, then an unary-expression.
8587 Save tokens so that we can put them back. */
8588 cp_lexer_save_tokens (parser->lexer);
8590 /* We may be looking at a cast-expression. */
8591 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8592 /*consume_paren=*/true))
8593 cast_expression
8594 = cp_parser_tokens_start_cast_expression (parser);
8596 /* Roll back the tokens we skipped. */
8597 cp_lexer_rollback_tokens (parser->lexer);
8598 /* If we aren't looking at a cast-expression, simulate an error so
8599 that the call to cp_parser_error_occurred below returns true. */
8600 if (!cast_expression)
8601 cp_parser_simulate_error (parser);
8602 else
8604 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8605 parser->in_type_id_in_expr_p = true;
8606 /* Look for the type-id. */
8607 type = cp_parser_type_id (parser);
8608 /* Look for the closing `)'. */
8609 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8610 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8613 /* Restore the saved message. */
8614 parser->type_definition_forbidden_message = saved_message;
8616 /* At this point this can only be either a cast or a
8617 parenthesized ctor such as `(T ())' that looks like a cast to
8618 function returning T. */
8619 if (!cp_parser_error_occurred (parser))
8621 /* Only commit if the cast-expression doesn't start with
8622 '++', '--', or '[' in C++11. */
8623 if (cast_expression > 0)
8624 cp_parser_commit_to_topmost_tentative_parse (parser);
8626 expr = cp_parser_cast_expression (parser,
8627 /*address_p=*/false,
8628 /*cast_p=*/true,
8629 /*decltype_p=*/false,
8630 pidk);
8632 if (cp_parser_parse_definitely (parser))
8634 /* Warn about old-style casts, if so requested. */
8635 if (warn_old_style_cast
8636 && !in_system_header_at (input_location)
8637 && !VOID_TYPE_P (type)
8638 && current_lang_name != lang_name_c)
8639 warning (OPT_Wold_style_cast, "use of old-style cast");
8641 /* Only type conversions to integral or enumeration types
8642 can be used in constant-expressions. */
8643 if (!cast_valid_in_integral_constant_expression_p (type)
8644 && cp_parser_non_integral_constant_expression (parser,
8645 NIC_CAST))
8646 return error_mark_node;
8648 /* Perform the cast. */
8649 /* Make a location:
8650 (TYPE) EXPR
8651 ^~~~~~~~~~~
8652 with start==caret at the open paren, extending to the
8653 end of "expr". */
8654 location_t cast_loc = make_location (open_paren_loc,
8655 open_paren_loc,
8656 expr.get_finish ());
8657 expr = build_c_cast (cast_loc, type, expr);
8658 return expr;
8661 else
8662 cp_parser_abort_tentative_parse (parser);
8665 /* If we get here, then it's not a cast, so it must be a
8666 unary-expression. */
8667 return cp_parser_unary_expression (parser, pidk, address_p,
8668 cast_p, decltype_p);
8671 /* Parse a binary expression of the general form:
8673 pm-expression:
8674 cast-expression
8675 pm-expression .* cast-expression
8676 pm-expression ->* cast-expression
8678 multiplicative-expression:
8679 pm-expression
8680 multiplicative-expression * pm-expression
8681 multiplicative-expression / pm-expression
8682 multiplicative-expression % pm-expression
8684 additive-expression:
8685 multiplicative-expression
8686 additive-expression + multiplicative-expression
8687 additive-expression - multiplicative-expression
8689 shift-expression:
8690 additive-expression
8691 shift-expression << additive-expression
8692 shift-expression >> additive-expression
8694 relational-expression:
8695 shift-expression
8696 relational-expression < shift-expression
8697 relational-expression > shift-expression
8698 relational-expression <= shift-expression
8699 relational-expression >= shift-expression
8701 GNU Extension:
8703 relational-expression:
8704 relational-expression <? shift-expression
8705 relational-expression >? shift-expression
8707 equality-expression:
8708 relational-expression
8709 equality-expression == relational-expression
8710 equality-expression != relational-expression
8712 and-expression:
8713 equality-expression
8714 and-expression & equality-expression
8716 exclusive-or-expression:
8717 and-expression
8718 exclusive-or-expression ^ and-expression
8720 inclusive-or-expression:
8721 exclusive-or-expression
8722 inclusive-or-expression | exclusive-or-expression
8724 logical-and-expression:
8725 inclusive-or-expression
8726 logical-and-expression && inclusive-or-expression
8728 logical-or-expression:
8729 logical-and-expression
8730 logical-or-expression || logical-and-expression
8732 All these are implemented with a single function like:
8734 binary-expression:
8735 simple-cast-expression
8736 binary-expression <token> binary-expression
8738 CAST_P is true if this expression is the target of a cast.
8740 The binops_by_token map is used to get the tree codes for each <token> type.
8741 binary-expressions are associated according to a precedence table. */
8743 #define TOKEN_PRECEDENCE(token) \
8744 (((token->type == CPP_GREATER \
8745 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8746 && !parser->greater_than_is_operator_p) \
8747 ? PREC_NOT_OPERATOR \
8748 : binops_by_token[token->type].prec)
8750 static cp_expr
8751 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8752 bool no_toplevel_fold_p,
8753 bool decltype_p,
8754 enum cp_parser_prec prec,
8755 cp_id_kind * pidk)
8757 cp_parser_expression_stack stack;
8758 cp_parser_expression_stack_entry *sp = &stack[0];
8759 cp_parser_expression_stack_entry current;
8760 cp_expr rhs;
8761 cp_token *token;
8762 enum tree_code rhs_type;
8763 enum cp_parser_prec new_prec, lookahead_prec;
8764 tree overload;
8766 /* Parse the first expression. */
8767 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8768 ? TRUTH_NOT_EXPR : ERROR_MARK);
8769 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8770 cast_p, decltype_p, pidk);
8771 current.prec = prec;
8773 if (cp_parser_error_occurred (parser))
8774 return error_mark_node;
8776 for (;;)
8778 /* Get an operator token. */
8779 token = cp_lexer_peek_token (parser->lexer);
8781 if (warn_cxx11_compat
8782 && token->type == CPP_RSHIFT
8783 && !parser->greater_than_is_operator_p)
8785 if (warning_at (token->location, OPT_Wc__11_compat,
8786 "%<>>%> operator is treated"
8787 " as two right angle brackets in C++11"))
8788 inform (token->location,
8789 "suggest parentheses around %<>>%> expression");
8792 new_prec = TOKEN_PRECEDENCE (token);
8793 if (new_prec != PREC_NOT_OPERATOR
8794 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8795 /* This is a fold-expression; handle it later. */
8796 new_prec = PREC_NOT_OPERATOR;
8798 /* Popping an entry off the stack means we completed a subexpression:
8799 - either we found a token which is not an operator (`>' where it is not
8800 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8801 will happen repeatedly;
8802 - or, we found an operator which has lower priority. This is the case
8803 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8804 parsing `3 * 4'. */
8805 if (new_prec <= current.prec)
8807 if (sp == stack)
8808 break;
8809 else
8810 goto pop;
8813 get_rhs:
8814 current.tree_type = binops_by_token[token->type].tree_type;
8815 current.loc = token->location;
8817 /* We used the operator token. */
8818 cp_lexer_consume_token (parser->lexer);
8820 /* For "false && x" or "true || x", x will never be executed;
8821 disable warnings while evaluating it. */
8822 if (current.tree_type == TRUTH_ANDIF_EXPR)
8823 c_inhibit_evaluation_warnings +=
8824 cp_fully_fold (current.lhs) == truthvalue_false_node;
8825 else if (current.tree_type == TRUTH_ORIF_EXPR)
8826 c_inhibit_evaluation_warnings +=
8827 cp_fully_fold (current.lhs) == truthvalue_true_node;
8829 /* Extract another operand. It may be the RHS of this expression
8830 or the LHS of a new, higher priority expression. */
8831 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8832 ? TRUTH_NOT_EXPR : ERROR_MARK);
8833 rhs = cp_parser_simple_cast_expression (parser);
8835 /* Get another operator token. Look up its precedence to avoid
8836 building a useless (immediately popped) stack entry for common
8837 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8838 token = cp_lexer_peek_token (parser->lexer);
8839 lookahead_prec = TOKEN_PRECEDENCE (token);
8840 if (lookahead_prec != PREC_NOT_OPERATOR
8841 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8842 lookahead_prec = PREC_NOT_OPERATOR;
8843 if (lookahead_prec > new_prec)
8845 /* ... and prepare to parse the RHS of the new, higher priority
8846 expression. Since precedence levels on the stack are
8847 monotonically increasing, we do not have to care about
8848 stack overflows. */
8849 *sp = current;
8850 ++sp;
8851 current.lhs = rhs;
8852 current.lhs_type = rhs_type;
8853 current.prec = new_prec;
8854 new_prec = lookahead_prec;
8855 goto get_rhs;
8857 pop:
8858 lookahead_prec = new_prec;
8859 /* If the stack is not empty, we have parsed into LHS the right side
8860 (`4' in the example above) of an expression we had suspended.
8861 We can use the information on the stack to recover the LHS (`3')
8862 from the stack together with the tree code (`MULT_EXPR'), and
8863 the precedence of the higher level subexpression
8864 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8865 which will be used to actually build the additive expression. */
8866 rhs = current.lhs;
8867 rhs_type = current.lhs_type;
8868 --sp;
8869 current = *sp;
8872 /* Undo the disabling of warnings done above. */
8873 if (current.tree_type == TRUTH_ANDIF_EXPR)
8874 c_inhibit_evaluation_warnings -=
8875 cp_fully_fold (current.lhs) == truthvalue_false_node;
8876 else if (current.tree_type == TRUTH_ORIF_EXPR)
8877 c_inhibit_evaluation_warnings -=
8878 cp_fully_fold (current.lhs) == truthvalue_true_node;
8880 if (warn_logical_not_paren
8881 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8882 && current.lhs_type == TRUTH_NOT_EXPR
8883 /* Avoid warning for !!x == y. */
8884 && (TREE_CODE (current.lhs) != NE_EXPR
8885 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8886 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8887 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8888 /* Avoid warning for !b == y where b is boolean. */
8889 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8890 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8891 != BOOLEAN_TYPE))))
8892 /* Avoid warning for !!b == y where b is boolean. */
8893 && (!DECL_P (current.lhs)
8894 || TREE_TYPE (current.lhs) == NULL_TREE
8895 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8896 warn_logical_not_parentheses (current.loc, current.tree_type,
8897 maybe_constant_value (rhs));
8899 overload = NULL;
8901 location_t combined_loc = make_location (current.loc,
8902 current.lhs.get_start (),
8903 rhs.get_finish ());
8905 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8906 ERROR_MARK for everything that is not a binary expression.
8907 This makes warn_about_parentheses miss some warnings that
8908 involve unary operators. For unary expressions we should
8909 pass the correct tree_code unless the unary expression was
8910 surrounded by parentheses.
8912 if (no_toplevel_fold_p
8913 && lookahead_prec <= current.prec
8914 && sp == stack)
8915 current.lhs = build2_loc (combined_loc,
8916 current.tree_type,
8917 TREE_CODE_CLASS (current.tree_type)
8918 == tcc_comparison
8919 ? boolean_type_node : TREE_TYPE (current.lhs),
8920 current.lhs, rhs);
8921 else
8923 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
8924 current.lhs, current.lhs_type,
8925 rhs, rhs_type, &overload,
8926 complain_flags (decltype_p));
8927 /* TODO: build_x_binary_op doesn't always honor the location. */
8928 current.lhs.set_location (combined_loc);
8930 current.lhs_type = current.tree_type;
8932 /* If the binary operator required the use of an overloaded operator,
8933 then this expression cannot be an integral constant-expression.
8934 An overloaded operator can be used even if both operands are
8935 otherwise permissible in an integral constant-expression if at
8936 least one of the operands is of enumeration type. */
8938 if (overload
8939 && cp_parser_non_integral_constant_expression (parser,
8940 NIC_OVERLOADED))
8941 return error_mark_node;
8944 return current.lhs;
8947 static cp_expr
8948 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8949 bool no_toplevel_fold_p,
8950 enum cp_parser_prec prec,
8951 cp_id_kind * pidk)
8953 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8954 /*decltype*/false, prec, pidk);
8957 /* Parse the `? expression : assignment-expression' part of a
8958 conditional-expression. The LOGICAL_OR_EXPR is the
8959 logical-or-expression that started the conditional-expression.
8960 Returns a representation of the entire conditional-expression.
8962 This routine is used by cp_parser_assignment_expression.
8964 ? expression : assignment-expression
8966 GNU Extensions:
8968 ? : assignment-expression */
8970 static tree
8971 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
8973 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
8974 cp_expr assignment_expr;
8975 struct cp_token *token;
8976 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8978 /* Consume the `?' token. */
8979 cp_lexer_consume_token (parser->lexer);
8980 token = cp_lexer_peek_token (parser->lexer);
8981 if (cp_parser_allow_gnu_extensions_p (parser)
8982 && token->type == CPP_COLON)
8984 pedwarn (token->location, OPT_Wpedantic,
8985 "ISO C++ does not allow ?: with omitted middle operand");
8986 /* Implicit true clause. */
8987 expr = NULL_TREE;
8988 c_inhibit_evaluation_warnings +=
8989 folded_logical_or_expr == truthvalue_true_node;
8990 warn_for_omitted_condop (token->location, logical_or_expr);
8992 else
8994 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8995 parser->colon_corrects_to_scope_p = false;
8996 /* Parse the expression. */
8997 c_inhibit_evaluation_warnings +=
8998 folded_logical_or_expr == truthvalue_false_node;
8999 expr = cp_parser_expression (parser);
9000 c_inhibit_evaluation_warnings +=
9001 ((folded_logical_or_expr == truthvalue_true_node)
9002 - (folded_logical_or_expr == truthvalue_false_node));
9003 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9006 /* The next token should be a `:'. */
9007 cp_parser_require (parser, CPP_COLON, RT_COLON);
9008 /* Parse the assignment-expression. */
9009 assignment_expr = cp_parser_assignment_expression (parser);
9010 c_inhibit_evaluation_warnings -=
9011 folded_logical_or_expr == truthvalue_true_node;
9013 /* Make a location:
9014 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9015 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9016 with the caret at the "?", ranging from the start of
9017 the logical_or_expr to the end of the assignment_expr. */
9018 loc = make_location (loc,
9019 logical_or_expr.get_start (),
9020 assignment_expr.get_finish ());
9022 /* Build the conditional-expression. */
9023 return build_x_conditional_expr (loc, logical_or_expr,
9024 expr,
9025 assignment_expr,
9026 tf_warning_or_error);
9029 /* Parse an assignment-expression.
9031 assignment-expression:
9032 conditional-expression
9033 logical-or-expression assignment-operator assignment_expression
9034 throw-expression
9036 CAST_P is true if this expression is the target of a cast.
9037 DECLTYPE_P is true if this expression is the operand of decltype.
9039 Returns a representation for the expression. */
9041 static cp_expr
9042 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9043 bool cast_p, bool decltype_p)
9045 cp_expr expr;
9047 /* If the next token is the `throw' keyword, then we're looking at
9048 a throw-expression. */
9049 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9050 expr = cp_parser_throw_expression (parser);
9051 /* Otherwise, it must be that we are looking at a
9052 logical-or-expression. */
9053 else
9055 /* Parse the binary expressions (logical-or-expression). */
9056 expr = cp_parser_binary_expression (parser, cast_p, false,
9057 decltype_p,
9058 PREC_NOT_OPERATOR, pidk);
9059 /* If the next token is a `?' then we're actually looking at a
9060 conditional-expression. */
9061 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9062 return cp_parser_question_colon_clause (parser, expr);
9063 else
9065 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9067 /* If it's an assignment-operator, we're using the second
9068 production. */
9069 enum tree_code assignment_operator
9070 = cp_parser_assignment_operator_opt (parser);
9071 if (assignment_operator != ERROR_MARK)
9073 bool non_constant_p;
9075 /* Parse the right-hand side of the assignment. */
9076 cp_expr rhs = cp_parser_initializer_clause (parser,
9077 &non_constant_p);
9079 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9080 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9082 /* An assignment may not appear in a
9083 constant-expression. */
9084 if (cp_parser_non_integral_constant_expression (parser,
9085 NIC_ASSIGNMENT))
9086 return error_mark_node;
9087 /* Build the assignment expression. Its default
9088 location:
9089 LHS = RHS
9090 ~~~~^~~~~
9091 is the location of the '=' token as the
9092 caret, ranging from the start of the lhs to the
9093 end of the rhs. */
9094 loc = make_location (loc,
9095 expr.get_start (),
9096 rhs.get_finish ());
9097 expr = build_x_modify_expr (loc, expr,
9098 assignment_operator,
9099 rhs,
9100 complain_flags (decltype_p));
9101 /* TODO: build_x_modify_expr doesn't honor the location,
9102 so we must set it here. */
9103 expr.set_location (loc);
9108 return expr;
9111 /* Parse an (optional) assignment-operator.
9113 assignment-operator: one of
9114 = *= /= %= += -= >>= <<= &= ^= |=
9116 GNU Extension:
9118 assignment-operator: one of
9119 <?= >?=
9121 If the next token is an assignment operator, the corresponding tree
9122 code is returned, and the token is consumed. For example, for
9123 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9124 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9125 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9126 operator, ERROR_MARK is returned. */
9128 static enum tree_code
9129 cp_parser_assignment_operator_opt (cp_parser* parser)
9131 enum tree_code op;
9132 cp_token *token;
9134 /* Peek at the next token. */
9135 token = cp_lexer_peek_token (parser->lexer);
9137 switch (token->type)
9139 case CPP_EQ:
9140 op = NOP_EXPR;
9141 break;
9143 case CPP_MULT_EQ:
9144 op = MULT_EXPR;
9145 break;
9147 case CPP_DIV_EQ:
9148 op = TRUNC_DIV_EXPR;
9149 break;
9151 case CPP_MOD_EQ:
9152 op = TRUNC_MOD_EXPR;
9153 break;
9155 case CPP_PLUS_EQ:
9156 op = PLUS_EXPR;
9157 break;
9159 case CPP_MINUS_EQ:
9160 op = MINUS_EXPR;
9161 break;
9163 case CPP_RSHIFT_EQ:
9164 op = RSHIFT_EXPR;
9165 break;
9167 case CPP_LSHIFT_EQ:
9168 op = LSHIFT_EXPR;
9169 break;
9171 case CPP_AND_EQ:
9172 op = BIT_AND_EXPR;
9173 break;
9175 case CPP_XOR_EQ:
9176 op = BIT_XOR_EXPR;
9177 break;
9179 case CPP_OR_EQ:
9180 op = BIT_IOR_EXPR;
9181 break;
9183 default:
9184 /* Nothing else is an assignment operator. */
9185 op = ERROR_MARK;
9188 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9189 if (op != ERROR_MARK
9190 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9191 op = ERROR_MARK;
9193 /* If it was an assignment operator, consume it. */
9194 if (op != ERROR_MARK)
9195 cp_lexer_consume_token (parser->lexer);
9197 return op;
9200 /* Parse an expression.
9202 expression:
9203 assignment-expression
9204 expression , assignment-expression
9206 CAST_P is true if this expression is the target of a cast.
9207 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9208 except possibly parenthesized or on the RHS of a comma (N3276).
9210 Returns a representation of the expression. */
9212 static cp_expr
9213 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9214 bool cast_p, bool decltype_p)
9216 cp_expr expression = NULL_TREE;
9217 location_t loc = UNKNOWN_LOCATION;
9219 while (true)
9221 cp_expr assignment_expression;
9223 /* Parse the next assignment-expression. */
9224 assignment_expression
9225 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9227 /* We don't create a temporary for a call that is the immediate operand
9228 of decltype or on the RHS of a comma. But when we see a comma, we
9229 need to create a temporary for a call on the LHS. */
9230 if (decltype_p && !processing_template_decl
9231 && TREE_CODE (assignment_expression) == CALL_EXPR
9232 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9233 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9234 assignment_expression
9235 = build_cplus_new (TREE_TYPE (assignment_expression),
9236 assignment_expression, tf_warning_or_error);
9238 /* If this is the first assignment-expression, we can just
9239 save it away. */
9240 if (!expression)
9241 expression = assignment_expression;
9242 else
9244 /* Create a location with caret at the comma, ranging
9245 from the start of the LHS to the end of the RHS. */
9246 loc = make_location (loc,
9247 expression.get_start (),
9248 assignment_expression.get_finish ());
9249 expression = build_x_compound_expr (loc, expression,
9250 assignment_expression,
9251 complain_flags (decltype_p));
9252 expression.set_location (loc);
9254 /* If the next token is not a comma, or we're in a fold-expression, then
9255 we are done with the expression. */
9256 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9257 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9258 break;
9259 /* Consume the `,'. */
9260 loc = cp_lexer_peek_token (parser->lexer)->location;
9261 cp_lexer_consume_token (parser->lexer);
9262 /* A comma operator cannot appear in a constant-expression. */
9263 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9264 expression = error_mark_node;
9267 return expression;
9270 /* Parse a constant-expression.
9272 constant-expression:
9273 conditional-expression
9275 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9276 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9277 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9278 is false, NON_CONSTANT_P should be NULL. */
9280 static cp_expr
9281 cp_parser_constant_expression (cp_parser* parser,
9282 bool allow_non_constant_p,
9283 bool *non_constant_p)
9285 bool saved_integral_constant_expression_p;
9286 bool saved_allow_non_integral_constant_expression_p;
9287 bool saved_non_integral_constant_expression_p;
9288 cp_expr expression;
9290 /* It might seem that we could simply parse the
9291 conditional-expression, and then check to see if it were
9292 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9293 one that the compiler can figure out is constant, possibly after
9294 doing some simplifications or optimizations. The standard has a
9295 precise definition of constant-expression, and we must honor
9296 that, even though it is somewhat more restrictive.
9298 For example:
9300 int i[(2, 3)];
9302 is not a legal declaration, because `(2, 3)' is not a
9303 constant-expression. The `,' operator is forbidden in a
9304 constant-expression. However, GCC's constant-folding machinery
9305 will fold this operation to an INTEGER_CST for `3'. */
9307 /* Save the old settings. */
9308 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9309 saved_allow_non_integral_constant_expression_p
9310 = parser->allow_non_integral_constant_expression_p;
9311 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9312 /* We are now parsing a constant-expression. */
9313 parser->integral_constant_expression_p = true;
9314 parser->allow_non_integral_constant_expression_p
9315 = (allow_non_constant_p || cxx_dialect >= cxx11);
9316 parser->non_integral_constant_expression_p = false;
9317 /* Although the grammar says "conditional-expression", we parse an
9318 "assignment-expression", which also permits "throw-expression"
9319 and the use of assignment operators. In the case that
9320 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9321 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9322 actually essential that we look for an assignment-expression.
9323 For example, cp_parser_initializer_clauses uses this function to
9324 determine whether a particular assignment-expression is in fact
9325 constant. */
9326 expression = cp_parser_assignment_expression (parser);
9327 /* Restore the old settings. */
9328 parser->integral_constant_expression_p
9329 = saved_integral_constant_expression_p;
9330 parser->allow_non_integral_constant_expression_p
9331 = saved_allow_non_integral_constant_expression_p;
9332 if (cxx_dialect >= cxx11)
9334 /* Require an rvalue constant expression here; that's what our
9335 callers expect. Reference constant expressions are handled
9336 separately in e.g. cp_parser_template_argument. */
9337 bool is_const = potential_rvalue_constant_expression (expression);
9338 parser->non_integral_constant_expression_p = !is_const;
9339 if (!is_const && !allow_non_constant_p)
9340 require_potential_rvalue_constant_expression (expression);
9342 if (allow_non_constant_p)
9343 *non_constant_p = parser->non_integral_constant_expression_p;
9344 parser->non_integral_constant_expression_p
9345 = saved_non_integral_constant_expression_p;
9347 return expression;
9350 /* Parse __builtin_offsetof.
9352 offsetof-expression:
9353 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9355 offsetof-member-designator:
9356 id-expression
9357 | offsetof-member-designator "." id-expression
9358 | offsetof-member-designator "[" expression "]"
9359 | offsetof-member-designator "->" id-expression */
9361 static cp_expr
9362 cp_parser_builtin_offsetof (cp_parser *parser)
9364 int save_ice_p, save_non_ice_p;
9365 tree type;
9366 cp_expr expr;
9367 cp_id_kind dummy;
9368 cp_token *token;
9369 location_t finish_loc;
9371 /* We're about to accept non-integral-constant things, but will
9372 definitely yield an integral constant expression. Save and
9373 restore these values around our local parsing. */
9374 save_ice_p = parser->integral_constant_expression_p;
9375 save_non_ice_p = parser->non_integral_constant_expression_p;
9377 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9379 /* Consume the "__builtin_offsetof" token. */
9380 cp_lexer_consume_token (parser->lexer);
9381 /* Consume the opening `('. */
9382 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9383 /* Parse the type-id. */
9384 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9385 type = cp_parser_type_id (parser);
9386 /* Look for the `,'. */
9387 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9388 token = cp_lexer_peek_token (parser->lexer);
9390 /* Build the (type *)null that begins the traditional offsetof macro. */
9391 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
9392 tf_warning_or_error);
9394 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9395 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
9396 true, &dummy, token->location);
9397 while (true)
9399 token = cp_lexer_peek_token (parser->lexer);
9400 switch (token->type)
9402 case CPP_OPEN_SQUARE:
9403 /* offsetof-member-designator "[" expression "]" */
9404 expr = cp_parser_postfix_open_square_expression (parser, expr,
9405 true, false);
9406 break;
9408 case CPP_DEREF:
9409 /* offsetof-member-designator "->" identifier */
9410 expr = grok_array_decl (token->location, expr,
9411 integer_zero_node, false);
9412 /* FALLTHRU */
9414 case CPP_DOT:
9415 /* offsetof-member-designator "." identifier */
9416 cp_lexer_consume_token (parser->lexer);
9417 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9418 expr, true, &dummy,
9419 token->location);
9420 break;
9422 case CPP_CLOSE_PAREN:
9423 /* Consume the ")" token. */
9424 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9425 cp_lexer_consume_token (parser->lexer);
9426 goto success;
9428 default:
9429 /* Error. We know the following require will fail, but
9430 that gives the proper error message. */
9431 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9432 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9433 expr = error_mark_node;
9434 goto failure;
9438 success:
9439 /* Make a location of the form:
9440 __builtin_offsetof (struct s, f)
9441 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9442 with caret at the type-id, ranging from the start of the
9443 "_builtin_offsetof" token to the close paren. */
9444 loc = make_location (loc, start_loc, finish_loc);
9445 /* The result will be an INTEGER_CST, so we need to explicitly
9446 preserve the location. */
9447 expr = cp_expr (finish_offsetof (expr, loc), loc);
9449 failure:
9450 parser->integral_constant_expression_p = save_ice_p;
9451 parser->non_integral_constant_expression_p = save_non_ice_p;
9453 return expr;
9456 /* Parse a trait expression.
9458 Returns a representation of the expression, the underlying type
9459 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9461 static tree
9462 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9464 cp_trait_kind kind;
9465 tree type1, type2 = NULL_TREE;
9466 bool binary = false;
9467 bool variadic = false;
9469 switch (keyword)
9471 case RID_HAS_NOTHROW_ASSIGN:
9472 kind = CPTK_HAS_NOTHROW_ASSIGN;
9473 break;
9474 case RID_HAS_NOTHROW_CONSTRUCTOR:
9475 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9476 break;
9477 case RID_HAS_NOTHROW_COPY:
9478 kind = CPTK_HAS_NOTHROW_COPY;
9479 break;
9480 case RID_HAS_TRIVIAL_ASSIGN:
9481 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9482 break;
9483 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9484 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9485 break;
9486 case RID_HAS_TRIVIAL_COPY:
9487 kind = CPTK_HAS_TRIVIAL_COPY;
9488 break;
9489 case RID_HAS_TRIVIAL_DESTRUCTOR:
9490 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9491 break;
9492 case RID_HAS_VIRTUAL_DESTRUCTOR:
9493 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9494 break;
9495 case RID_IS_ABSTRACT:
9496 kind = CPTK_IS_ABSTRACT;
9497 break;
9498 case RID_IS_BASE_OF:
9499 kind = CPTK_IS_BASE_OF;
9500 binary = true;
9501 break;
9502 case RID_IS_CLASS:
9503 kind = CPTK_IS_CLASS;
9504 break;
9505 case RID_IS_EMPTY:
9506 kind = CPTK_IS_EMPTY;
9507 break;
9508 case RID_IS_ENUM:
9509 kind = CPTK_IS_ENUM;
9510 break;
9511 case RID_IS_FINAL:
9512 kind = CPTK_IS_FINAL;
9513 break;
9514 case RID_IS_LITERAL_TYPE:
9515 kind = CPTK_IS_LITERAL_TYPE;
9516 break;
9517 case RID_IS_POD:
9518 kind = CPTK_IS_POD;
9519 break;
9520 case RID_IS_POLYMORPHIC:
9521 kind = CPTK_IS_POLYMORPHIC;
9522 break;
9523 case RID_IS_SAME_AS:
9524 kind = CPTK_IS_SAME_AS;
9525 binary = true;
9526 break;
9527 case RID_IS_STD_LAYOUT:
9528 kind = CPTK_IS_STD_LAYOUT;
9529 break;
9530 case RID_IS_TRIVIAL:
9531 kind = CPTK_IS_TRIVIAL;
9532 break;
9533 case RID_IS_TRIVIALLY_ASSIGNABLE:
9534 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9535 binary = true;
9536 break;
9537 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9538 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9539 variadic = true;
9540 break;
9541 case RID_IS_TRIVIALLY_COPYABLE:
9542 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9543 break;
9544 case RID_IS_UNION:
9545 kind = CPTK_IS_UNION;
9546 break;
9547 case RID_UNDERLYING_TYPE:
9548 kind = CPTK_UNDERLYING_TYPE;
9549 break;
9550 case RID_BASES:
9551 kind = CPTK_BASES;
9552 break;
9553 case RID_DIRECT_BASES:
9554 kind = CPTK_DIRECT_BASES;
9555 break;
9556 default:
9557 gcc_unreachable ();
9560 /* Consume the token. */
9561 cp_lexer_consume_token (parser->lexer);
9563 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9566 type_id_in_expr_sentinel s (parser);
9567 type1 = cp_parser_type_id (parser);
9570 if (type1 == error_mark_node)
9571 return error_mark_node;
9573 if (binary)
9575 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9578 type_id_in_expr_sentinel s (parser);
9579 type2 = cp_parser_type_id (parser);
9582 if (type2 == error_mark_node)
9583 return error_mark_node;
9585 else if (variadic)
9587 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9589 cp_lexer_consume_token (parser->lexer);
9590 tree elt = cp_parser_type_id (parser);
9591 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9593 cp_lexer_consume_token (parser->lexer);
9594 elt = make_pack_expansion (elt);
9596 if (elt == error_mark_node)
9597 return error_mark_node;
9598 type2 = tree_cons (NULL_TREE, elt, type2);
9602 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9604 /* Complete the trait expression, which may mean either processing
9605 the trait expr now or saving it for template instantiation. */
9606 switch(kind)
9608 case CPTK_UNDERLYING_TYPE:
9609 return finish_underlying_type (type1);
9610 case CPTK_BASES:
9611 return finish_bases (type1, false);
9612 case CPTK_DIRECT_BASES:
9613 return finish_bases (type1, true);
9614 default:
9615 return finish_trait_expr (kind, type1, type2);
9619 /* Lambdas that appear in variable initializer or default argument scope
9620 get that in their mangling, so we need to record it. We might as well
9621 use the count for function and namespace scopes as well. */
9622 static GTY(()) tree lambda_scope;
9623 static GTY(()) int lambda_count;
9624 struct GTY(()) tree_int
9626 tree t;
9627 int i;
9629 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9631 static void
9632 start_lambda_scope (tree decl)
9634 tree_int ti;
9635 gcc_assert (decl);
9636 /* Once we're inside a function, we ignore other scopes and just push
9637 the function again so that popping works properly. */
9638 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9639 decl = current_function_decl;
9640 ti.t = lambda_scope;
9641 ti.i = lambda_count;
9642 vec_safe_push (lambda_scope_stack, ti);
9643 if (lambda_scope != decl)
9645 /* Don't reset the count if we're still in the same function. */
9646 lambda_scope = decl;
9647 lambda_count = 0;
9651 static void
9652 record_lambda_scope (tree lambda)
9654 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9655 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9658 static void
9659 finish_lambda_scope (void)
9661 tree_int *p = &lambda_scope_stack->last ();
9662 if (lambda_scope != p->t)
9664 lambda_scope = p->t;
9665 lambda_count = p->i;
9667 lambda_scope_stack->pop ();
9670 /* Parse a lambda expression.
9672 lambda-expression:
9673 lambda-introducer lambda-declarator [opt] compound-statement
9675 Returns a representation of the expression. */
9677 static cp_expr
9678 cp_parser_lambda_expression (cp_parser* parser)
9680 tree lambda_expr = build_lambda_expr ();
9681 tree type;
9682 bool ok = true;
9683 cp_token *token = cp_lexer_peek_token (parser->lexer);
9684 cp_token_position start = 0;
9686 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9688 if (cp_unevaluated_operand)
9690 if (!token->error_reported)
9692 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9693 "lambda-expression in unevaluated context");
9694 token->error_reported = true;
9696 ok = false;
9698 else if (parser->in_template_argument_list_p)
9700 if (!token->error_reported)
9702 error_at (token->location, "lambda-expression in template-argument");
9703 token->error_reported = true;
9705 ok = false;
9708 /* We may be in the middle of deferred access check. Disable
9709 it now. */
9710 push_deferring_access_checks (dk_no_deferred);
9712 cp_parser_lambda_introducer (parser, lambda_expr);
9714 type = begin_lambda_type (lambda_expr);
9715 if (type == error_mark_node)
9716 return error_mark_node;
9718 record_lambda_scope (lambda_expr);
9720 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9721 determine_visibility (TYPE_NAME (type));
9723 /* Now that we've started the type, add the capture fields for any
9724 explicit captures. */
9725 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9728 /* Inside the class, surrounding template-parameter-lists do not apply. */
9729 unsigned int saved_num_template_parameter_lists
9730 = parser->num_template_parameter_lists;
9731 unsigned char in_statement = parser->in_statement;
9732 bool in_switch_statement_p = parser->in_switch_statement_p;
9733 bool fully_implicit_function_template_p
9734 = parser->fully_implicit_function_template_p;
9735 tree implicit_template_parms = parser->implicit_template_parms;
9736 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9737 bool auto_is_implicit_function_template_parm_p
9738 = parser->auto_is_implicit_function_template_parm_p;
9740 parser->num_template_parameter_lists = 0;
9741 parser->in_statement = 0;
9742 parser->in_switch_statement_p = false;
9743 parser->fully_implicit_function_template_p = false;
9744 parser->implicit_template_parms = 0;
9745 parser->implicit_template_scope = 0;
9746 parser->auto_is_implicit_function_template_parm_p = false;
9748 /* By virtue of defining a local class, a lambda expression has access to
9749 the private variables of enclosing classes. */
9751 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9753 if (ok)
9755 if (!cp_parser_error_occurred (parser)
9756 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9757 && cp_parser_start_tentative_firewall (parser))
9758 start = token;
9759 cp_parser_lambda_body (parser, lambda_expr);
9761 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9763 if (cp_parser_skip_to_closing_brace (parser))
9764 cp_lexer_consume_token (parser->lexer);
9767 /* The capture list was built up in reverse order; fix that now. */
9768 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9769 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9771 if (ok)
9772 maybe_add_lambda_conv_op (type);
9774 type = finish_struct (type, /*attributes=*/NULL_TREE);
9776 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9777 parser->in_statement = in_statement;
9778 parser->in_switch_statement_p = in_switch_statement_p;
9779 parser->fully_implicit_function_template_p
9780 = fully_implicit_function_template_p;
9781 parser->implicit_template_parms = implicit_template_parms;
9782 parser->implicit_template_scope = implicit_template_scope;
9783 parser->auto_is_implicit_function_template_parm_p
9784 = auto_is_implicit_function_template_parm_p;
9787 /* This field is only used during parsing of the lambda. */
9788 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9790 /* This lambda shouldn't have any proxies left at this point. */
9791 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9792 /* And now that we're done, push proxies for an enclosing lambda. */
9793 insert_pending_capture_proxies ();
9795 if (ok)
9796 lambda_expr = build_lambda_object (lambda_expr);
9797 else
9798 lambda_expr = error_mark_node;
9800 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9802 pop_deferring_access_checks ();
9804 return lambda_expr;
9807 /* Parse the beginning of a lambda expression.
9809 lambda-introducer:
9810 [ lambda-capture [opt] ]
9812 LAMBDA_EXPR is the current representation of the lambda expression. */
9814 static void
9815 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9817 /* Need commas after the first capture. */
9818 bool first = true;
9820 /* Eat the leading `['. */
9821 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9823 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9824 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9825 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9826 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9827 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9828 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9830 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9832 cp_lexer_consume_token (parser->lexer);
9833 first = false;
9836 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9838 cp_token* capture_token;
9839 tree capture_id;
9840 tree capture_init_expr;
9841 cp_id_kind idk = CP_ID_KIND_NONE;
9842 bool explicit_init_p = false;
9844 enum capture_kind_type
9846 BY_COPY,
9847 BY_REFERENCE
9849 enum capture_kind_type capture_kind = BY_COPY;
9851 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9853 error ("expected end of capture-list");
9854 return;
9857 if (first)
9858 first = false;
9859 else
9860 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9862 /* Possibly capture `this'. */
9863 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9865 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9866 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9867 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9868 "with by-copy capture default");
9869 cp_lexer_consume_token (parser->lexer);
9870 add_capture (lambda_expr,
9871 /*id=*/this_identifier,
9872 /*initializer=*/finish_this_expr(),
9873 /*by_reference_p=*/false,
9874 explicit_init_p);
9875 continue;
9878 /* Remember whether we want to capture as a reference or not. */
9879 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9881 capture_kind = BY_REFERENCE;
9882 cp_lexer_consume_token (parser->lexer);
9885 /* Get the identifier. */
9886 capture_token = cp_lexer_peek_token (parser->lexer);
9887 capture_id = cp_parser_identifier (parser);
9889 if (capture_id == error_mark_node)
9890 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9891 delimiters, but I modified this to stop on unnested ']' as well. It
9892 was already changed to stop on unnested '}', so the
9893 "closing_parenthesis" name is no more misleading with my change. */
9895 cp_parser_skip_to_closing_parenthesis (parser,
9896 /*recovering=*/true,
9897 /*or_comma=*/true,
9898 /*consume_paren=*/true);
9899 break;
9902 /* Find the initializer for this capture. */
9903 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9904 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9905 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9907 bool direct, non_constant;
9908 /* An explicit initializer exists. */
9909 if (cxx_dialect < cxx14)
9910 pedwarn (input_location, 0,
9911 "lambda capture initializers "
9912 "only available with -std=c++14 or -std=gnu++14");
9913 capture_init_expr = cp_parser_initializer (parser, &direct,
9914 &non_constant);
9915 explicit_init_p = true;
9916 if (capture_init_expr == NULL_TREE)
9918 error ("empty initializer for lambda init-capture");
9919 capture_init_expr = error_mark_node;
9922 else
9924 const char* error_msg;
9926 /* Turn the identifier into an id-expression. */
9927 capture_init_expr
9928 = cp_parser_lookup_name_simple (parser, capture_id,
9929 capture_token->location);
9931 if (capture_init_expr == error_mark_node)
9933 unqualified_name_lookup_error (capture_id);
9934 continue;
9936 else if (DECL_P (capture_init_expr)
9937 && (!VAR_P (capture_init_expr)
9938 && TREE_CODE (capture_init_expr) != PARM_DECL))
9940 error_at (capture_token->location,
9941 "capture of non-variable %qD ",
9942 capture_init_expr);
9943 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9944 "%q#D declared here", capture_init_expr);
9945 continue;
9947 if (VAR_P (capture_init_expr)
9948 && decl_storage_duration (capture_init_expr) != dk_auto)
9950 if (pedwarn (capture_token->location, 0, "capture of variable "
9951 "%qD with non-automatic storage duration",
9952 capture_init_expr))
9953 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9954 "%q#D declared here", capture_init_expr);
9955 continue;
9958 capture_init_expr
9959 = finish_id_expression
9960 (capture_id,
9961 capture_init_expr,
9962 parser->scope,
9963 &idk,
9964 /*integral_constant_expression_p=*/false,
9965 /*allow_non_integral_constant_expression_p=*/false,
9966 /*non_integral_constant_expression_p=*/NULL,
9967 /*template_p=*/false,
9968 /*done=*/true,
9969 /*address_p=*/false,
9970 /*template_arg_p=*/false,
9971 &error_msg,
9972 capture_token->location);
9974 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9976 cp_lexer_consume_token (parser->lexer);
9977 capture_init_expr = make_pack_expansion (capture_init_expr);
9979 else
9980 check_for_bare_parameter_packs (capture_init_expr);
9983 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9984 && !explicit_init_p)
9986 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9987 && capture_kind == BY_COPY)
9988 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9989 "of %qD redundant with by-copy capture default",
9990 capture_id);
9991 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9992 && capture_kind == BY_REFERENCE)
9993 pedwarn (capture_token->location, 0, "explicit by-reference "
9994 "capture of %qD redundant with by-reference capture "
9995 "default", capture_id);
9998 add_capture (lambda_expr,
9999 capture_id,
10000 capture_init_expr,
10001 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10002 explicit_init_p);
10005 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10008 /* Parse the (optional) middle of a lambda expression.
10010 lambda-declarator:
10011 < template-parameter-list [opt] >
10012 ( parameter-declaration-clause [opt] )
10013 attribute-specifier [opt]
10014 mutable [opt]
10015 exception-specification [opt]
10016 lambda-return-type-clause [opt]
10018 LAMBDA_EXPR is the current representation of the lambda expression. */
10020 static bool
10021 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10023 /* 5.1.1.4 of the standard says:
10024 If a lambda-expression does not include a lambda-declarator, it is as if
10025 the lambda-declarator were ().
10026 This means an empty parameter list, no attributes, and no exception
10027 specification. */
10028 tree param_list = void_list_node;
10029 tree attributes = NULL_TREE;
10030 tree exception_spec = NULL_TREE;
10031 tree template_param_list = NULL_TREE;
10032 tree tx_qual = NULL_TREE;
10034 /* The template-parameter-list is optional, but must begin with
10035 an opening angle if present. */
10036 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10038 if (cxx_dialect < cxx14)
10039 pedwarn (parser->lexer->next_token->location, 0,
10040 "lambda templates are only available with "
10041 "-std=c++14 or -std=gnu++14");
10043 cp_lexer_consume_token (parser->lexer);
10045 template_param_list = cp_parser_template_parameter_list (parser);
10047 cp_parser_skip_to_end_of_template_parameter_list (parser);
10049 /* We just processed one more parameter list. */
10050 ++parser->num_template_parameter_lists;
10053 /* The parameter-declaration-clause is optional (unless
10054 template-parameter-list was given), but must begin with an
10055 opening parenthesis if present. */
10056 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10058 cp_lexer_consume_token (parser->lexer);
10060 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10062 /* Parse parameters. */
10063 param_list = cp_parser_parameter_declaration_clause (parser);
10065 /* Default arguments shall not be specified in the
10066 parameter-declaration-clause of a lambda-declarator. */
10067 for (tree t = param_list; t; t = TREE_CHAIN (t))
10068 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
10069 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10070 "default argument specified for lambda parameter");
10072 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10074 attributes = cp_parser_attributes_opt (parser);
10076 /* Parse optional `mutable' keyword. */
10077 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
10079 cp_lexer_consume_token (parser->lexer);
10080 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10083 tx_qual = cp_parser_tx_qualifier_opt (parser);
10085 /* Parse optional exception specification. */
10086 exception_spec = cp_parser_exception_specification_opt (parser);
10088 /* Parse optional trailing return type. */
10089 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10091 cp_lexer_consume_token (parser->lexer);
10092 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10093 = cp_parser_trailing_type_id (parser);
10096 /* The function parameters must be in scope all the way until after the
10097 trailing-return-type in case of decltype. */
10098 pop_bindings_and_leave_scope ();
10100 else if (template_param_list != NULL_TREE) // generate diagnostic
10101 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10103 /* Create the function call operator.
10105 Messing with declarators like this is no uglier than building up the
10106 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10107 other code. */
10109 cp_decl_specifier_seq return_type_specs;
10110 cp_declarator* declarator;
10111 tree fco;
10112 int quals;
10113 void *p;
10115 clear_decl_specs (&return_type_specs);
10116 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10117 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
10118 else
10119 /* Maybe we will deduce the return type later. */
10120 return_type_specs.type = make_auto ();
10122 p = obstack_alloc (&declarator_obstack, 0);
10124 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
10125 sfk_none);
10127 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10128 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10129 declarator = make_call_declarator (declarator, param_list, quals,
10130 VIRT_SPEC_UNSPECIFIED,
10131 REF_QUAL_NONE,
10132 tx_qual,
10133 exception_spec,
10134 /*late_return_type=*/NULL_TREE,
10135 /*requires_clause*/NULL_TREE);
10136 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10138 fco = grokmethod (&return_type_specs,
10139 declarator,
10140 attributes);
10141 if (fco != error_mark_node)
10143 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10144 DECL_ARTIFICIAL (fco) = 1;
10145 /* Give the object parameter a different name. */
10146 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10147 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10148 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10150 if (template_param_list)
10152 fco = finish_member_template_decl (fco);
10153 finish_template_decl (template_param_list);
10154 --parser->num_template_parameter_lists;
10156 else if (parser->fully_implicit_function_template_p)
10157 fco = finish_fully_implicit_template (parser, fco);
10159 finish_member_declaration (fco);
10161 obstack_free (&declarator_obstack, p);
10163 return (fco != error_mark_node);
10167 /* Parse the body of a lambda expression, which is simply
10169 compound-statement
10171 but which requires special handling.
10172 LAMBDA_EXPR is the current representation of the lambda expression. */
10174 static void
10175 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10177 bool nested = (current_function_decl != NULL_TREE);
10178 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10179 if (nested)
10180 push_function_context ();
10181 else
10182 /* Still increment function_depth so that we don't GC in the
10183 middle of an expression. */
10184 ++function_depth;
10185 vec<tree> omp_privatization_save;
10186 save_omp_privatization_clauses (omp_privatization_save);
10187 /* Clear this in case we're in the middle of a default argument. */
10188 parser->local_variables_forbidden_p = false;
10190 /* Finish the function call operator
10191 - class_specifier
10192 + late_parsing_for_member
10193 + function_definition_after_declarator
10194 + ctor_initializer_opt_and_function_body */
10196 tree fco = lambda_function (lambda_expr);
10197 tree body;
10198 bool done = false;
10199 tree compound_stmt;
10200 tree cap;
10202 /* Let the front end know that we are going to be defining this
10203 function. */
10204 start_preparsed_function (fco,
10205 NULL_TREE,
10206 SF_PRE_PARSED | SF_INCLASS_INLINE);
10208 start_lambda_scope (fco);
10209 body = begin_function_body ();
10211 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10212 goto out;
10214 /* Push the proxies for any explicit captures. */
10215 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
10216 cap = TREE_CHAIN (cap))
10217 build_capture_proxy (TREE_PURPOSE (cap));
10219 compound_stmt = begin_compound_stmt (0);
10221 /* 5.1.1.4 of the standard says:
10222 If a lambda-expression does not include a trailing-return-type, it
10223 is as if the trailing-return-type denotes the following type:
10224 * if the compound-statement is of the form
10225 { return attribute-specifier [opt] expression ; }
10226 the type of the returned expression after lvalue-to-rvalue
10227 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10228 (_conv.array_ 4.2), and function-to-pointer conversion
10229 (_conv.func_ 4.3);
10230 * otherwise, void. */
10232 /* In a lambda that has neither a lambda-return-type-clause
10233 nor a deducible form, errors should be reported for return statements
10234 in the body. Since we used void as the placeholder return type, parsing
10235 the body as usual will give such desired behavior. */
10236 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10237 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10238 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10240 tree expr = NULL_TREE;
10241 cp_id_kind idk = CP_ID_KIND_NONE;
10243 /* Parse tentatively in case there's more after the initial return
10244 statement. */
10245 cp_parser_parse_tentatively (parser);
10247 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10249 expr = cp_parser_expression (parser, &idk);
10251 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10252 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10254 if (cp_parser_parse_definitely (parser))
10256 if (!processing_template_decl)
10258 tree type = lambda_return_type (expr);
10259 apply_deduced_return_type (fco, type);
10260 if (type == error_mark_node)
10261 expr = error_mark_node;
10264 /* Will get error here if type not deduced yet. */
10265 finish_return_stmt (expr);
10267 done = true;
10271 if (!done)
10273 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10274 cp_parser_label_declaration (parser);
10275 cp_parser_statement_seq_opt (parser, NULL_TREE);
10276 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10279 finish_compound_stmt (compound_stmt);
10281 out:
10282 finish_function_body (body);
10283 finish_lambda_scope ();
10285 /* Finish the function and generate code for it if necessary. */
10286 tree fn = finish_function (/*inline*/2);
10288 /* Only expand if the call op is not a template. */
10289 if (!DECL_TEMPLATE_INFO (fco))
10290 expand_or_defer_fn (fn);
10293 restore_omp_privatization_clauses (omp_privatization_save);
10294 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10295 if (nested)
10296 pop_function_context();
10297 else
10298 --function_depth;
10301 /* Statements [gram.stmt.stmt] */
10303 /* Parse a statement.
10305 statement:
10306 labeled-statement
10307 expression-statement
10308 compound-statement
10309 selection-statement
10310 iteration-statement
10311 jump-statement
10312 declaration-statement
10313 try-block
10315 C++11:
10317 statement:
10318 labeled-statement
10319 attribute-specifier-seq (opt) expression-statement
10320 attribute-specifier-seq (opt) compound-statement
10321 attribute-specifier-seq (opt) selection-statement
10322 attribute-specifier-seq (opt) iteration-statement
10323 attribute-specifier-seq (opt) jump-statement
10324 declaration-statement
10325 attribute-specifier-seq (opt) try-block
10327 TM Extension:
10329 statement:
10330 atomic-statement
10332 IN_COMPOUND is true when the statement is nested inside a
10333 cp_parser_compound_statement; this matters for certain pragmas.
10335 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10336 is a (possibly labeled) if statement which is not enclosed in braces
10337 and has an else clause. This is used to implement -Wparentheses.
10339 CHAIN is a vector of if-else-if conditions. */
10341 static void
10342 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10343 bool in_compound, bool *if_p, vec<tree> *chain)
10345 tree statement, std_attrs = NULL_TREE;
10346 cp_token *token;
10347 location_t statement_location, attrs_location;
10349 restart:
10350 if (if_p != NULL)
10351 *if_p = false;
10352 /* There is no statement yet. */
10353 statement = NULL_TREE;
10355 saved_token_sentinel saved_tokens (parser->lexer);
10356 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10357 if (c_dialect_objc ())
10358 /* In obj-c++, seeing '[[' might be the either the beginning of
10359 c++11 attributes, or a nested objc-message-expression. So
10360 let's parse the c++11 attributes tentatively. */
10361 cp_parser_parse_tentatively (parser);
10362 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10363 if (c_dialect_objc ())
10365 if (!cp_parser_parse_definitely (parser))
10366 std_attrs = NULL_TREE;
10369 /* Peek at the next token. */
10370 token = cp_lexer_peek_token (parser->lexer);
10371 /* Remember the location of the first token in the statement. */
10372 statement_location = token->location;
10373 /* If this is a keyword, then that will often determine what kind of
10374 statement we have. */
10375 if (token->type == CPP_KEYWORD)
10377 enum rid keyword = token->keyword;
10379 switch (keyword)
10381 case RID_CASE:
10382 case RID_DEFAULT:
10383 /* Looks like a labeled-statement with a case label.
10384 Parse the label, and then use tail recursion to parse
10385 the statement. */
10386 cp_parser_label_for_labeled_statement (parser, std_attrs);
10387 in_compound = false;
10388 goto restart;
10390 case RID_IF:
10391 case RID_SWITCH:
10392 statement = cp_parser_selection_statement (parser, if_p, chain);
10393 break;
10395 case RID_WHILE:
10396 case RID_DO:
10397 case RID_FOR:
10398 statement = cp_parser_iteration_statement (parser, if_p, false);
10399 break;
10401 case RID_CILK_FOR:
10402 if (!flag_cilkplus)
10404 error_at (cp_lexer_peek_token (parser->lexer)->location,
10405 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10406 cp_lexer_consume_token (parser->lexer);
10407 statement = error_mark_node;
10409 else
10410 statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10411 break;
10413 case RID_BREAK:
10414 case RID_CONTINUE:
10415 case RID_RETURN:
10416 case RID_GOTO:
10417 statement = cp_parser_jump_statement (parser);
10418 break;
10420 case RID_CILK_SYNC:
10421 cp_lexer_consume_token (parser->lexer);
10422 if (flag_cilkplus)
10424 tree sync_expr = build_cilk_sync ();
10425 SET_EXPR_LOCATION (sync_expr,
10426 token->location);
10427 statement = finish_expr_stmt (sync_expr);
10429 else
10431 error_at (token->location, "-fcilkplus must be enabled to use"
10432 " %<_Cilk_sync%>");
10433 statement = error_mark_node;
10435 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10436 break;
10438 /* Objective-C++ exception-handling constructs. */
10439 case RID_AT_TRY:
10440 case RID_AT_CATCH:
10441 case RID_AT_FINALLY:
10442 case RID_AT_SYNCHRONIZED:
10443 case RID_AT_THROW:
10444 statement = cp_parser_objc_statement (parser);
10445 break;
10447 case RID_TRY:
10448 statement = cp_parser_try_block (parser);
10449 break;
10451 case RID_NAMESPACE:
10452 /* This must be a namespace alias definition. */
10453 cp_parser_declaration_statement (parser);
10454 return;
10456 case RID_TRANSACTION_ATOMIC:
10457 case RID_TRANSACTION_RELAXED:
10458 case RID_SYNCHRONIZED:
10459 case RID_ATOMIC_NOEXCEPT:
10460 case RID_ATOMIC_CANCEL:
10461 statement = cp_parser_transaction (parser, token);
10462 break;
10463 case RID_TRANSACTION_CANCEL:
10464 statement = cp_parser_transaction_cancel (parser);
10465 break;
10467 default:
10468 /* It might be a keyword like `int' that can start a
10469 declaration-statement. */
10470 break;
10473 else if (token->type == CPP_NAME)
10475 /* If the next token is a `:', then we are looking at a
10476 labeled-statement. */
10477 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10478 if (token->type == CPP_COLON)
10480 /* Looks like a labeled-statement with an ordinary label.
10481 Parse the label, and then use tail recursion to parse
10482 the statement. */
10484 cp_parser_label_for_labeled_statement (parser, std_attrs);
10485 in_compound = false;
10486 goto restart;
10489 /* Anything that starts with a `{' must be a compound-statement. */
10490 else if (token->type == CPP_OPEN_BRACE)
10491 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10492 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10493 a statement all its own. */
10494 else if (token->type == CPP_PRAGMA)
10496 /* Only certain OpenMP pragmas are attached to statements, and thus
10497 are considered statements themselves. All others are not. In
10498 the context of a compound, accept the pragma as a "statement" and
10499 return so that we can check for a close brace. Otherwise we
10500 require a real statement and must go back and read one. */
10501 if (in_compound)
10502 cp_parser_pragma (parser, pragma_compound, if_p);
10503 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10504 goto restart;
10505 return;
10507 else if (token->type == CPP_EOF)
10509 cp_parser_error (parser, "expected statement");
10510 return;
10513 /* Everything else must be a declaration-statement or an
10514 expression-statement. Try for the declaration-statement
10515 first, unless we are looking at a `;', in which case we know that
10516 we have an expression-statement. */
10517 if (!statement)
10519 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10521 if (std_attrs != NULL_TREE)
10523 /* Attributes should be parsed as part of the the
10524 declaration, so let's un-parse them. */
10525 saved_tokens.rollback();
10526 std_attrs = NULL_TREE;
10529 cp_parser_parse_tentatively (parser);
10530 /* Try to parse the declaration-statement. */
10531 cp_parser_declaration_statement (parser);
10532 /* If that worked, we're done. */
10533 if (cp_parser_parse_definitely (parser))
10534 return;
10536 /* Look for an expression-statement instead. */
10537 statement = cp_parser_expression_statement (parser, in_statement_expr);
10540 /* Set the line number for the statement. */
10541 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10542 SET_EXPR_LOCATION (statement, statement_location);
10544 /* Note that for now, we don't do anything with c++11 statements
10545 parsed at this level. */
10546 if (std_attrs != NULL_TREE)
10547 warning_at (attrs_location,
10548 OPT_Wattributes,
10549 "attributes at the beginning of statement are ignored");
10552 /* Parse the label for a labeled-statement, i.e.
10554 identifier :
10555 case constant-expression :
10556 default :
10558 GNU Extension:
10559 case constant-expression ... constant-expression : statement
10561 When a label is parsed without errors, the label is added to the
10562 parse tree by the finish_* functions, so this function doesn't
10563 have to return the label. */
10565 static void
10566 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10568 cp_token *token;
10569 tree label = NULL_TREE;
10570 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10572 /* The next token should be an identifier. */
10573 token = cp_lexer_peek_token (parser->lexer);
10574 if (token->type != CPP_NAME
10575 && token->type != CPP_KEYWORD)
10577 cp_parser_error (parser, "expected labeled-statement");
10578 return;
10581 parser->colon_corrects_to_scope_p = false;
10582 switch (token->keyword)
10584 case RID_CASE:
10586 tree expr, expr_hi;
10587 cp_token *ellipsis;
10589 /* Consume the `case' token. */
10590 cp_lexer_consume_token (parser->lexer);
10591 /* Parse the constant-expression. */
10592 expr = cp_parser_constant_expression (parser);
10593 if (check_for_bare_parameter_packs (expr))
10594 expr = error_mark_node;
10596 ellipsis = cp_lexer_peek_token (parser->lexer);
10597 if (ellipsis->type == CPP_ELLIPSIS)
10599 /* Consume the `...' token. */
10600 cp_lexer_consume_token (parser->lexer);
10601 expr_hi = cp_parser_constant_expression (parser);
10602 if (check_for_bare_parameter_packs (expr_hi))
10603 expr_hi = error_mark_node;
10605 /* We don't need to emit warnings here, as the common code
10606 will do this for us. */
10608 else
10609 expr_hi = NULL_TREE;
10611 if (parser->in_switch_statement_p)
10612 finish_case_label (token->location, expr, expr_hi);
10613 else
10614 error_at (token->location,
10615 "case label %qE not within a switch statement",
10616 expr);
10618 break;
10620 case RID_DEFAULT:
10621 /* Consume the `default' token. */
10622 cp_lexer_consume_token (parser->lexer);
10624 if (parser->in_switch_statement_p)
10625 finish_case_label (token->location, NULL_TREE, NULL_TREE);
10626 else
10627 error_at (token->location, "case label not within a switch statement");
10628 break;
10630 default:
10631 /* Anything else must be an ordinary label. */
10632 label = finish_label_stmt (cp_parser_identifier (parser));
10633 break;
10636 /* Require the `:' token. */
10637 cp_parser_require (parser, CPP_COLON, RT_COLON);
10639 /* An ordinary label may optionally be followed by attributes.
10640 However, this is only permitted if the attributes are then
10641 followed by a semicolon. This is because, for backward
10642 compatibility, when parsing
10643 lab: __attribute__ ((unused)) int i;
10644 we want the attribute to attach to "i", not "lab". */
10645 if (label != NULL_TREE
10646 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10648 tree attrs;
10649 cp_parser_parse_tentatively (parser);
10650 attrs = cp_parser_gnu_attributes_opt (parser);
10651 if (attrs == NULL_TREE
10652 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10653 cp_parser_abort_tentative_parse (parser);
10654 else if (!cp_parser_parse_definitely (parser))
10656 else
10657 attributes = chainon (attributes, attrs);
10660 if (attributes != NULL_TREE)
10661 cplus_decl_attributes (&label, attributes, 0);
10663 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10666 /* Parse an expression-statement.
10668 expression-statement:
10669 expression [opt] ;
10671 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10672 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10673 indicates whether this expression-statement is part of an
10674 expression statement. */
10676 static tree
10677 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10679 tree statement = NULL_TREE;
10680 cp_token *token = cp_lexer_peek_token (parser->lexer);
10682 /* If the next token is a ';', then there is no expression
10683 statement. */
10684 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10686 statement = cp_parser_expression (parser);
10687 if (statement == error_mark_node
10688 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10690 cp_parser_skip_to_end_of_block_or_statement (parser);
10691 return error_mark_node;
10695 /* Give a helpful message for "A<T>::type t;" and the like. */
10696 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10697 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10699 if (TREE_CODE (statement) == SCOPE_REF)
10700 error_at (token->location, "need %<typename%> before %qE because "
10701 "%qT is a dependent scope",
10702 statement, TREE_OPERAND (statement, 0));
10703 else if (is_overloaded_fn (statement)
10704 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10706 /* A::A a; */
10707 tree fn = get_first_fn (statement);
10708 error_at (token->location,
10709 "%<%T::%D%> names the constructor, not the type",
10710 DECL_CONTEXT (fn), DECL_NAME (fn));
10714 /* Consume the final `;'. */
10715 cp_parser_consume_semicolon_at_end_of_statement (parser);
10717 if (in_statement_expr
10718 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10719 /* This is the final expression statement of a statement
10720 expression. */
10721 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10722 else if (statement)
10723 statement = finish_expr_stmt (statement);
10725 return statement;
10728 /* Parse a compound-statement.
10730 compound-statement:
10731 { statement-seq [opt] }
10733 GNU extension:
10735 compound-statement:
10736 { label-declaration-seq [opt] statement-seq [opt] }
10738 label-declaration-seq:
10739 label-declaration
10740 label-declaration-seq label-declaration
10742 Returns a tree representing the statement. */
10744 static tree
10745 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10746 int bcs_flags, bool function_body)
10748 tree compound_stmt;
10750 /* Consume the `{'. */
10751 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10752 return error_mark_node;
10753 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10754 && !function_body && cxx_dialect < cxx14)
10755 pedwarn (input_location, OPT_Wpedantic,
10756 "compound-statement in constexpr function");
10757 /* Begin the compound-statement. */
10758 compound_stmt = begin_compound_stmt (bcs_flags);
10759 /* If the next keyword is `__label__' we have a label declaration. */
10760 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10761 cp_parser_label_declaration (parser);
10762 /* Parse an (optional) statement-seq. */
10763 cp_parser_statement_seq_opt (parser, in_statement_expr);
10764 /* Finish the compound-statement. */
10765 finish_compound_stmt (compound_stmt);
10766 /* Consume the `}'. */
10767 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10769 return compound_stmt;
10772 /* Parse an (optional) statement-seq.
10774 statement-seq:
10775 statement
10776 statement-seq [opt] statement */
10778 static void
10779 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10781 /* Scan statements until there aren't any more. */
10782 while (true)
10784 cp_token *token = cp_lexer_peek_token (parser->lexer);
10786 /* If we are looking at a `}', then we have run out of
10787 statements; the same is true if we have reached the end
10788 of file, or have stumbled upon a stray '@end'. */
10789 if (token->type == CPP_CLOSE_BRACE
10790 || token->type == CPP_EOF
10791 || token->type == CPP_PRAGMA_EOL
10792 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10793 break;
10795 /* If we are in a compound statement and find 'else' then
10796 something went wrong. */
10797 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10799 if (parser->in_statement & IN_IF_STMT)
10800 break;
10801 else
10803 token = cp_lexer_consume_token (parser->lexer);
10804 error_at (token->location, "%<else%> without a previous %<if%>");
10808 /* Parse the statement. */
10809 cp_parser_statement (parser, in_statement_expr, true, NULL);
10813 /* Parse a selection-statement.
10815 selection-statement:
10816 if ( condition ) statement
10817 if ( condition ) statement else statement
10818 switch ( condition ) statement
10820 Returns the new IF_STMT or SWITCH_STMT.
10822 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10823 is a (possibly labeled) if statement which is not enclosed in
10824 braces and has an else clause. This is used to implement
10825 -Wparentheses.
10827 CHAIN is a vector of if-else-if conditions. This is used to implement
10828 -Wduplicated-cond. */
10830 static tree
10831 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
10832 vec<tree> *chain)
10834 cp_token *token;
10835 enum rid keyword;
10836 token_indent_info guard_tinfo;
10838 if (if_p != NULL)
10839 *if_p = false;
10841 /* Peek at the next token. */
10842 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10843 guard_tinfo = get_token_indent_info (token);
10845 /* See what kind of keyword it is. */
10846 keyword = token->keyword;
10847 switch (keyword)
10849 case RID_IF:
10850 case RID_SWITCH:
10852 tree statement;
10853 tree condition;
10855 /* Look for the `('. */
10856 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10858 cp_parser_skip_to_end_of_statement (parser);
10859 return error_mark_node;
10862 /* Begin the selection-statement. */
10863 if (keyword == RID_IF)
10864 statement = begin_if_stmt ();
10865 else
10866 statement = begin_switch_stmt ();
10868 /* Parse the condition. */
10869 condition = cp_parser_condition (parser);
10870 /* Look for the `)'. */
10871 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10872 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10873 /*consume_paren=*/true);
10875 if (keyword == RID_IF)
10877 bool nested_if;
10878 unsigned char in_statement;
10880 /* Add the condition. */
10881 finish_if_stmt_cond (condition, statement);
10883 if (warn_duplicated_cond)
10884 warn_duplicated_cond_add_or_warn (token->location, condition,
10885 &chain);
10887 /* Parse the then-clause. */
10888 in_statement = parser->in_statement;
10889 parser->in_statement |= IN_IF_STMT;
10890 cp_parser_implicitly_scoped_statement (parser, &nested_if,
10891 guard_tinfo);
10892 parser->in_statement = in_statement;
10894 finish_then_clause (statement);
10896 /* If the next token is `else', parse the else-clause. */
10897 if (cp_lexer_next_token_is_keyword (parser->lexer,
10898 RID_ELSE))
10900 guard_tinfo
10901 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
10902 /* Consume the `else' keyword. */
10903 cp_lexer_consume_token (parser->lexer);
10904 if (warn_duplicated_cond)
10906 if (cp_lexer_next_token_is_keyword (parser->lexer,
10907 RID_IF)
10908 && chain == NULL)
10910 /* We've got "if (COND) else if (COND2)". Start
10911 the condition chain and add COND as the first
10912 element. */
10913 chain = new vec<tree> ();
10914 if (!CONSTANT_CLASS_P (condition)
10915 && !TREE_SIDE_EFFECTS (condition))
10917 /* Wrap it in a NOP_EXPR so that we can set the
10918 location of the condition. */
10919 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
10920 condition);
10921 SET_EXPR_LOCATION (e, token->location);
10922 chain->safe_push (e);
10925 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
10926 RID_IF))
10928 /* This is if-else without subsequent if. Zap the
10929 condition chain; we would have already warned at
10930 this point. */
10931 delete chain;
10932 chain = NULL;
10935 begin_else_clause (statement);
10936 /* Parse the else-clause. */
10937 cp_parser_implicitly_scoped_statement (parser, NULL,
10938 guard_tinfo, chain);
10940 finish_else_clause (statement);
10942 /* If we are currently parsing a then-clause, then
10943 IF_P will not be NULL. We set it to true to
10944 indicate that this if statement has an else clause.
10945 This may trigger the Wparentheses warning below
10946 when we get back up to the parent if statement. */
10947 if (if_p != NULL)
10948 *if_p = true;
10950 else
10952 /* This if statement does not have an else clause. If
10953 NESTED_IF is true, then the then-clause has an if
10954 statement which does have an else clause. We warn
10955 about the potential ambiguity. */
10956 if (nested_if)
10957 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
10958 "suggest explicit braces to avoid ambiguous"
10959 " %<else%>");
10960 if (warn_duplicated_cond)
10962 /* We don't need the condition chain anymore. */
10963 delete chain;
10964 chain = NULL;
10968 /* Now we're all done with the if-statement. */
10969 finish_if_stmt (statement);
10971 else
10973 bool in_switch_statement_p;
10974 unsigned char in_statement;
10976 /* Add the condition. */
10977 finish_switch_cond (condition, statement);
10979 /* Parse the body of the switch-statement. */
10980 in_switch_statement_p = parser->in_switch_statement_p;
10981 in_statement = parser->in_statement;
10982 parser->in_switch_statement_p = true;
10983 parser->in_statement |= IN_SWITCH_STMT;
10984 cp_parser_implicitly_scoped_statement (parser, if_p,
10985 guard_tinfo);
10986 parser->in_switch_statement_p = in_switch_statement_p;
10987 parser->in_statement = in_statement;
10989 /* Now we're all done with the switch-statement. */
10990 finish_switch_stmt (statement);
10993 return statement;
10995 break;
10997 default:
10998 cp_parser_error (parser, "expected selection-statement");
10999 return error_mark_node;
11003 /* Parse a condition.
11005 condition:
11006 expression
11007 type-specifier-seq declarator = initializer-clause
11008 type-specifier-seq declarator braced-init-list
11010 GNU Extension:
11012 condition:
11013 type-specifier-seq declarator asm-specification [opt]
11014 attributes [opt] = assignment-expression
11016 Returns the expression that should be tested. */
11018 static tree
11019 cp_parser_condition (cp_parser* parser)
11021 cp_decl_specifier_seq type_specifiers;
11022 const char *saved_message;
11023 int declares_class_or_enum;
11025 /* Try the declaration first. */
11026 cp_parser_parse_tentatively (parser);
11027 /* New types are not allowed in the type-specifier-seq for a
11028 condition. */
11029 saved_message = parser->type_definition_forbidden_message;
11030 parser->type_definition_forbidden_message
11031 = G_("types may not be defined in conditions");
11032 /* Parse the type-specifier-seq. */
11033 cp_parser_decl_specifier_seq (parser,
11034 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11035 &type_specifiers,
11036 &declares_class_or_enum);
11037 /* Restore the saved message. */
11038 parser->type_definition_forbidden_message = saved_message;
11039 /* If all is well, we might be looking at a declaration. */
11040 if (!cp_parser_error_occurred (parser))
11042 tree decl;
11043 tree asm_specification;
11044 tree attributes;
11045 cp_declarator *declarator;
11046 tree initializer = NULL_TREE;
11048 /* Parse the declarator. */
11049 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11050 /*ctor_dtor_or_conv_p=*/NULL,
11051 /*parenthesized_p=*/NULL,
11052 /*member_p=*/false,
11053 /*friend_p=*/false);
11054 /* Parse the attributes. */
11055 attributes = cp_parser_attributes_opt (parser);
11056 /* Parse the asm-specification. */
11057 asm_specification = cp_parser_asm_specification_opt (parser);
11058 /* If the next token is not an `=' or '{', then we might still be
11059 looking at an expression. For example:
11061 if (A(a).x)
11063 looks like a decl-specifier-seq and a declarator -- but then
11064 there is no `=', so this is an expression. */
11065 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11066 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11067 cp_parser_simulate_error (parser);
11069 /* If we did see an `=' or '{', then we are looking at a declaration
11070 for sure. */
11071 if (cp_parser_parse_definitely (parser))
11073 tree pushed_scope;
11074 bool non_constant_p;
11075 bool flags = LOOKUP_ONLYCONVERTING;
11077 /* Create the declaration. */
11078 decl = start_decl (declarator, &type_specifiers,
11079 /*initialized_p=*/true,
11080 attributes, /*prefix_attributes=*/NULL_TREE,
11081 &pushed_scope);
11083 /* Parse the initializer. */
11084 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11086 initializer = cp_parser_braced_list (parser, &non_constant_p);
11087 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11088 flags = 0;
11090 else
11092 /* Consume the `='. */
11093 cp_parser_require (parser, CPP_EQ, RT_EQ);
11094 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11096 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11097 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11099 /* Process the initializer. */
11100 cp_finish_decl (decl,
11101 initializer, !non_constant_p,
11102 asm_specification,
11103 flags);
11105 if (pushed_scope)
11106 pop_scope (pushed_scope);
11108 return convert_from_reference (decl);
11111 /* If we didn't even get past the declarator successfully, we are
11112 definitely not looking at a declaration. */
11113 else
11114 cp_parser_abort_tentative_parse (parser);
11116 /* Otherwise, we are looking at an expression. */
11117 return cp_parser_expression (parser);
11120 /* Parses a for-statement or range-for-statement until the closing ')',
11121 not included. */
11123 static tree
11124 cp_parser_for (cp_parser *parser, bool ivdep)
11126 tree init, scope, decl;
11127 bool is_range_for;
11129 /* Begin the for-statement. */
11130 scope = begin_for_scope (&init);
11132 /* Parse the initialization. */
11133 is_range_for = cp_parser_for_init_statement (parser, &decl);
11135 if (is_range_for)
11136 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11137 else
11138 return cp_parser_c_for (parser, scope, init, ivdep);
11141 static tree
11142 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11144 /* Normal for loop */
11145 tree condition = NULL_TREE;
11146 tree expression = NULL_TREE;
11147 tree stmt;
11149 stmt = begin_for_stmt (scope, init);
11150 /* The for-init-statement has already been parsed in
11151 cp_parser_for_init_statement, so no work is needed here. */
11152 finish_for_init_stmt (stmt);
11154 /* If there's a condition, process it. */
11155 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11156 condition = cp_parser_condition (parser);
11157 else if (ivdep)
11159 cp_parser_error (parser, "missing loop condition in loop with "
11160 "%<GCC ivdep%> pragma");
11161 condition = error_mark_node;
11163 finish_for_cond (condition, stmt, ivdep);
11164 /* Look for the `;'. */
11165 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11167 /* If there's an expression, process it. */
11168 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11169 expression = cp_parser_expression (parser);
11170 finish_for_expr (expression, stmt);
11172 return stmt;
11175 /* Tries to parse a range-based for-statement:
11177 range-based-for:
11178 decl-specifier-seq declarator : expression
11180 The decl-specifier-seq declarator and the `:' are already parsed by
11181 cp_parser_for_init_statement. If processing_template_decl it returns a
11182 newly created RANGE_FOR_STMT; if not, it is converted to a
11183 regular FOR_STMT. */
11185 static tree
11186 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11187 bool ivdep)
11189 tree stmt, range_expr;
11191 /* Get the range declaration momentarily out of the way so that
11192 the range expression doesn't clash with it. */
11193 if (range_decl != error_mark_node)
11194 pop_binding (DECL_NAME (range_decl), range_decl);
11196 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11198 bool expr_non_constant_p;
11199 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11201 else
11202 range_expr = cp_parser_expression (parser);
11204 /* Put the range declaration back into scope. */
11205 if (range_decl != error_mark_node)
11206 push_binding (DECL_NAME (range_decl), range_decl, current_binding_level);
11208 /* If in template, STMT is converted to a normal for-statement
11209 at instantiation. If not, it is done just ahead. */
11210 if (processing_template_decl)
11212 if (check_for_bare_parameter_packs (range_expr))
11213 range_expr = error_mark_node;
11214 stmt = begin_range_for_stmt (scope, init);
11215 if (ivdep)
11216 RANGE_FOR_IVDEP (stmt) = 1;
11217 finish_range_for_decl (stmt, range_decl, range_expr);
11218 if (!type_dependent_expression_p (range_expr)
11219 /* do_auto_deduction doesn't mess with template init-lists. */
11220 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11221 do_range_for_auto_deduction (range_decl, range_expr);
11223 else
11225 stmt = begin_for_stmt (scope, init);
11226 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
11228 return stmt;
11231 /* Subroutine of cp_convert_range_for: given the initializer expression,
11232 builds up the range temporary. */
11234 static tree
11235 build_range_temp (tree range_expr)
11237 tree range_type, range_temp;
11239 /* Find out the type deduced by the declaration
11240 `auto &&__range = range_expr'. */
11241 range_type = cp_build_reference_type (make_auto (), true);
11242 range_type = do_auto_deduction (range_type, range_expr,
11243 type_uses_auto (range_type));
11245 /* Create the __range variable. */
11246 range_temp = build_decl (input_location, VAR_DECL,
11247 get_identifier ("__for_range"), range_type);
11248 TREE_USED (range_temp) = 1;
11249 DECL_ARTIFICIAL (range_temp) = 1;
11251 return range_temp;
11254 /* Used by cp_parser_range_for in template context: we aren't going to
11255 do a full conversion yet, but we still need to resolve auto in the
11256 type of the for-range-declaration if present. This is basically
11257 a shortcut version of cp_convert_range_for. */
11259 static void
11260 do_range_for_auto_deduction (tree decl, tree range_expr)
11262 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11263 if (auto_node)
11265 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11266 range_temp = convert_from_reference (build_range_temp (range_expr));
11267 iter_type = (cp_parser_perform_range_for_lookup
11268 (range_temp, &begin_dummy, &end_dummy));
11269 if (iter_type)
11271 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11272 iter_type);
11273 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11274 tf_warning_or_error);
11275 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11276 iter_decl, auto_node);
11281 /* Converts a range-based for-statement into a normal
11282 for-statement, as per the definition.
11284 for (RANGE_DECL : RANGE_EXPR)
11285 BLOCK
11287 should be equivalent to:
11290 auto &&__range = RANGE_EXPR;
11291 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11292 __begin != __end;
11293 ++__begin)
11295 RANGE_DECL = *__begin;
11296 BLOCK
11300 If RANGE_EXPR is an array:
11301 BEGIN_EXPR = __range
11302 END_EXPR = __range + ARRAY_SIZE(__range)
11303 Else if RANGE_EXPR has a member 'begin' or 'end':
11304 BEGIN_EXPR = __range.begin()
11305 END_EXPR = __range.end()
11306 Else:
11307 BEGIN_EXPR = begin(__range)
11308 END_EXPR = end(__range);
11310 If __range has a member 'begin' but not 'end', or vice versa, we must
11311 still use the second alternative (it will surely fail, however).
11312 When calling begin()/end() in the third alternative we must use
11313 argument dependent lookup, but always considering 'std' as an associated
11314 namespace. */
11316 tree
11317 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11318 bool ivdep)
11320 tree begin, end;
11321 tree iter_type, begin_expr, end_expr;
11322 tree condition, expression;
11324 if (range_decl == error_mark_node || range_expr == error_mark_node)
11325 /* If an error happened previously do nothing or else a lot of
11326 unhelpful errors would be issued. */
11327 begin_expr = end_expr = iter_type = error_mark_node;
11328 else
11330 tree range_temp;
11332 if (VAR_P (range_expr)
11333 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11334 /* Can't bind a reference to an array of runtime bound. */
11335 range_temp = range_expr;
11336 else
11338 range_temp = build_range_temp (range_expr);
11339 pushdecl (range_temp);
11340 cp_finish_decl (range_temp, range_expr,
11341 /*is_constant_init*/false, NULL_TREE,
11342 LOOKUP_ONLYCONVERTING);
11343 range_temp = convert_from_reference (range_temp);
11345 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11346 &begin_expr, &end_expr);
11349 /* The new for initialization statement. */
11350 begin = build_decl (input_location, VAR_DECL,
11351 get_identifier ("__for_begin"), iter_type);
11352 TREE_USED (begin) = 1;
11353 DECL_ARTIFICIAL (begin) = 1;
11354 pushdecl (begin);
11355 cp_finish_decl (begin, begin_expr,
11356 /*is_constant_init*/false, NULL_TREE,
11357 LOOKUP_ONLYCONVERTING);
11359 if (cxx_dialect >= cxx1z)
11360 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11361 end = build_decl (input_location, VAR_DECL,
11362 get_identifier ("__for_end"), iter_type);
11363 TREE_USED (end) = 1;
11364 DECL_ARTIFICIAL (end) = 1;
11365 pushdecl (end);
11366 cp_finish_decl (end, end_expr,
11367 /*is_constant_init*/false, NULL_TREE,
11368 LOOKUP_ONLYCONVERTING);
11370 finish_for_init_stmt (statement);
11372 /* The new for condition. */
11373 condition = build_x_binary_op (input_location, NE_EXPR,
11374 begin, ERROR_MARK,
11375 end, ERROR_MARK,
11376 NULL, tf_warning_or_error);
11377 finish_for_cond (condition, statement, ivdep);
11379 /* The new increment expression. */
11380 expression = finish_unary_op_expr (input_location,
11381 PREINCREMENT_EXPR, begin,
11382 tf_warning_or_error);
11383 finish_for_expr (expression, statement);
11385 /* The declaration is initialized with *__begin inside the loop body. */
11386 cp_finish_decl (range_decl,
11387 build_x_indirect_ref (input_location, begin, RO_NULL,
11388 tf_warning_or_error),
11389 /*is_constant_init*/false, NULL_TREE,
11390 LOOKUP_ONLYCONVERTING);
11392 return statement;
11395 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11396 We need to solve both at the same time because the method used
11397 depends on the existence of members begin or end.
11398 Returns the type deduced for the iterator expression. */
11400 static tree
11401 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11403 if (error_operand_p (range))
11405 *begin = *end = error_mark_node;
11406 return error_mark_node;
11409 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11411 error ("range-based %<for%> expression of type %qT "
11412 "has incomplete type", TREE_TYPE (range));
11413 *begin = *end = error_mark_node;
11414 return error_mark_node;
11416 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11418 /* If RANGE is an array, we will use pointer arithmetic. */
11419 *begin = decay_conversion (range, tf_warning_or_error);
11420 *end = build_binary_op (input_location, PLUS_EXPR,
11421 range,
11422 array_type_nelts_top (TREE_TYPE (range)),
11424 return TREE_TYPE (*begin);
11426 else
11428 /* If it is not an array, we must do a bit of magic. */
11429 tree id_begin, id_end;
11430 tree member_begin, member_end;
11432 *begin = *end = error_mark_node;
11434 id_begin = get_identifier ("begin");
11435 id_end = get_identifier ("end");
11436 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11437 /*protect=*/2, /*want_type=*/false,
11438 tf_warning_or_error);
11439 member_end = lookup_member (TREE_TYPE (range), id_end,
11440 /*protect=*/2, /*want_type=*/false,
11441 tf_warning_or_error);
11443 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11445 /* Use the member functions. */
11446 if (member_begin != NULL_TREE)
11447 *begin = cp_parser_range_for_member_function (range, id_begin);
11448 else
11449 error ("range-based %<for%> expression of type %qT has an "
11450 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11452 if (member_end != NULL_TREE)
11453 *end = cp_parser_range_for_member_function (range, id_end);
11454 else
11455 error ("range-based %<for%> expression of type %qT has a "
11456 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11458 else
11460 /* Use global functions with ADL. */
11461 vec<tree, va_gc> *vec;
11462 vec = make_tree_vector ();
11464 vec_safe_push (vec, range);
11466 member_begin = perform_koenig_lookup (id_begin, vec,
11467 tf_warning_or_error);
11468 *begin = finish_call_expr (member_begin, &vec, false, true,
11469 tf_warning_or_error);
11470 member_end = perform_koenig_lookup (id_end, vec,
11471 tf_warning_or_error);
11472 *end = finish_call_expr (member_end, &vec, false, true,
11473 tf_warning_or_error);
11475 release_tree_vector (vec);
11478 /* Last common checks. */
11479 if (*begin == error_mark_node || *end == error_mark_node)
11481 /* If one of the expressions is an error do no more checks. */
11482 *begin = *end = error_mark_node;
11483 return error_mark_node;
11485 else if (type_dependent_expression_p (*begin)
11486 || type_dependent_expression_p (*end))
11487 /* Can happen, when, eg, in a template context, Koenig lookup
11488 can't resolve begin/end (c++/58503). */
11489 return NULL_TREE;
11490 else
11492 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11493 /* The unqualified type of the __begin and __end temporaries should
11494 be the same, as required by the multiple auto declaration. */
11495 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11497 if (cxx_dialect >= cxx1z
11498 && (build_x_binary_op (input_location, NE_EXPR,
11499 *begin, ERROR_MARK,
11500 *end, ERROR_MARK,
11501 NULL, tf_none)
11502 != error_mark_node))
11503 /* P0184R0 allows __begin and __end to have different types,
11504 but make sure they are comparable so we can give a better
11505 diagnostic. */;
11506 else
11507 error ("inconsistent begin/end types in range-based %<for%> "
11508 "statement: %qT and %qT",
11509 TREE_TYPE (*begin), TREE_TYPE (*end));
11511 return iter_type;
11516 /* Helper function for cp_parser_perform_range_for_lookup.
11517 Builds a tree for RANGE.IDENTIFIER(). */
11519 static tree
11520 cp_parser_range_for_member_function (tree range, tree identifier)
11522 tree member, res;
11523 vec<tree, va_gc> *vec;
11525 member = finish_class_member_access_expr (range, identifier,
11526 false, tf_warning_or_error);
11527 if (member == error_mark_node)
11528 return error_mark_node;
11530 vec = make_tree_vector ();
11531 res = finish_call_expr (member, &vec,
11532 /*disallow_virtual=*/false,
11533 /*koenig_p=*/false,
11534 tf_warning_or_error);
11535 release_tree_vector (vec);
11536 return res;
11539 /* Parse an iteration-statement.
11541 iteration-statement:
11542 while ( condition ) statement
11543 do statement while ( expression ) ;
11544 for ( for-init-statement condition [opt] ; expression [opt] )
11545 statement
11547 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11549 static tree
11550 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
11552 cp_token *token;
11553 enum rid keyword;
11554 tree statement;
11555 unsigned char in_statement;
11556 token_indent_info guard_tinfo;
11558 /* Peek at the next token. */
11559 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11560 if (!token)
11561 return error_mark_node;
11563 guard_tinfo = get_token_indent_info (token);
11565 /* Remember whether or not we are already within an iteration
11566 statement. */
11567 in_statement = parser->in_statement;
11569 /* See what kind of keyword it is. */
11570 keyword = token->keyword;
11571 switch (keyword)
11573 case RID_WHILE:
11575 tree condition;
11577 /* Begin the while-statement. */
11578 statement = begin_while_stmt ();
11579 /* Look for the `('. */
11580 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11581 /* Parse the condition. */
11582 condition = cp_parser_condition (parser);
11583 finish_while_stmt_cond (condition, statement, ivdep);
11584 /* Look for the `)'. */
11585 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11586 /* Parse the dependent statement. */
11587 parser->in_statement = IN_ITERATION_STMT;
11588 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11589 parser->in_statement = in_statement;
11590 /* We're done with the while-statement. */
11591 finish_while_stmt (statement);
11593 break;
11595 case RID_DO:
11597 tree expression;
11599 /* Begin the do-statement. */
11600 statement = begin_do_stmt ();
11601 /* Parse the body of the do-statement. */
11602 parser->in_statement = IN_ITERATION_STMT;
11603 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11604 parser->in_statement = in_statement;
11605 finish_do_body (statement);
11606 /* Look for the `while' keyword. */
11607 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11608 /* Look for the `('. */
11609 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11610 /* Parse the expression. */
11611 expression = cp_parser_expression (parser);
11612 /* We're done with the do-statement. */
11613 finish_do_stmt (expression, statement, ivdep);
11614 /* Look for the `)'. */
11615 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11616 /* Look for the `;'. */
11617 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11619 break;
11621 case RID_FOR:
11623 /* Look for the `('. */
11624 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11626 statement = cp_parser_for (parser, ivdep);
11628 /* Look for the `)'. */
11629 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11631 /* Parse the body of the for-statement. */
11632 parser->in_statement = IN_ITERATION_STMT;
11633 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11634 parser->in_statement = in_statement;
11636 /* We're done with the for-statement. */
11637 finish_for_stmt (statement);
11639 break;
11641 default:
11642 cp_parser_error (parser, "expected iteration-statement");
11643 statement = error_mark_node;
11644 break;
11647 return statement;
11650 /* Parse a for-init-statement or the declarator of a range-based-for.
11651 Returns true if a range-based-for declaration is seen.
11653 for-init-statement:
11654 expression-statement
11655 simple-declaration */
11657 static bool
11658 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
11660 /* If the next token is a `;', then we have an empty
11661 expression-statement. Grammatically, this is also a
11662 simple-declaration, but an invalid one, because it does not
11663 declare anything. Therefore, if we did not handle this case
11664 specially, we would issue an error message about an invalid
11665 declaration. */
11666 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11668 bool is_range_for = false;
11669 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11671 /* A colon is used in range-based for. */
11672 parser->colon_corrects_to_scope_p = false;
11674 /* We're going to speculatively look for a declaration, falling back
11675 to an expression, if necessary. */
11676 cp_parser_parse_tentatively (parser);
11677 /* Parse the declaration. */
11678 cp_parser_simple_declaration (parser,
11679 /*function_definition_allowed_p=*/false,
11680 decl);
11681 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11682 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11684 /* It is a range-for, consume the ':' */
11685 cp_lexer_consume_token (parser->lexer);
11686 is_range_for = true;
11687 if (cxx_dialect < cxx11)
11689 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11690 "range-based %<for%> loops only available with "
11691 "-std=c++11 or -std=gnu++11");
11692 *decl = error_mark_node;
11695 else
11696 /* The ';' is not consumed yet because we told
11697 cp_parser_simple_declaration not to. */
11698 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11700 if (cp_parser_parse_definitely (parser))
11701 return is_range_for;
11702 /* If the tentative parse failed, then we shall need to look for an
11703 expression-statement. */
11705 /* If we are here, it is an expression-statement. */
11706 cp_parser_expression_statement (parser, NULL_TREE);
11707 return false;
11710 /* Parse a jump-statement.
11712 jump-statement:
11713 break ;
11714 continue ;
11715 return expression [opt] ;
11716 return braced-init-list ;
11717 goto identifier ;
11719 GNU extension:
11721 jump-statement:
11722 goto * expression ;
11724 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
11726 static tree
11727 cp_parser_jump_statement (cp_parser* parser)
11729 tree statement = error_mark_node;
11730 cp_token *token;
11731 enum rid keyword;
11732 unsigned char in_statement;
11734 /* Peek at the next token. */
11735 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
11736 if (!token)
11737 return error_mark_node;
11739 /* See what kind of keyword it is. */
11740 keyword = token->keyword;
11741 switch (keyword)
11743 case RID_BREAK:
11744 in_statement = parser->in_statement & ~IN_IF_STMT;
11745 switch (in_statement)
11747 case 0:
11748 error_at (token->location, "break statement not within loop or switch");
11749 break;
11750 default:
11751 gcc_assert ((in_statement & IN_SWITCH_STMT)
11752 || in_statement == IN_ITERATION_STMT);
11753 statement = finish_break_stmt ();
11754 if (in_statement == IN_ITERATION_STMT)
11755 break_maybe_infinite_loop ();
11756 break;
11757 case IN_OMP_BLOCK:
11758 error_at (token->location, "invalid exit from OpenMP structured block");
11759 break;
11760 case IN_OMP_FOR:
11761 error_at (token->location, "break statement used with OpenMP for loop");
11762 break;
11763 case IN_CILK_SIMD_FOR:
11764 error_at (token->location, "break statement used with Cilk Plus for loop");
11765 break;
11767 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11768 break;
11770 case RID_CONTINUE:
11771 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11773 case 0:
11774 error_at (token->location, "continue statement not within a loop");
11775 break;
11776 case IN_CILK_SIMD_FOR:
11777 error_at (token->location,
11778 "continue statement within %<#pragma simd%> loop body");
11779 /* Fall through. */
11780 case IN_ITERATION_STMT:
11781 case IN_OMP_FOR:
11782 statement = finish_continue_stmt ();
11783 break;
11784 case IN_OMP_BLOCK:
11785 error_at (token->location, "invalid exit from OpenMP structured block");
11786 break;
11787 default:
11788 gcc_unreachable ();
11790 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11791 break;
11793 case RID_RETURN:
11795 tree expr;
11796 bool expr_non_constant_p;
11798 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11800 cp_lexer_set_source_position (parser->lexer);
11801 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11802 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11804 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11805 expr = cp_parser_expression (parser);
11806 else
11807 /* If the next token is a `;', then there is no
11808 expression. */
11809 expr = NULL_TREE;
11810 /* Build the return-statement. */
11811 statement = finish_return_stmt (expr);
11812 /* Look for the final `;'. */
11813 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11815 break;
11817 case RID_GOTO:
11818 if (parser->in_function_body
11819 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11821 error ("%<goto%> in %<constexpr%> function");
11822 cp_function_chain->invalid_constexpr = true;
11825 /* Create the goto-statement. */
11826 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11828 /* Issue a warning about this use of a GNU extension. */
11829 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11830 /* Consume the '*' token. */
11831 cp_lexer_consume_token (parser->lexer);
11832 /* Parse the dependent expression. */
11833 finish_goto_stmt (cp_parser_expression (parser));
11835 else
11836 finish_goto_stmt (cp_parser_identifier (parser));
11837 /* Look for the final `;'. */
11838 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11839 break;
11841 default:
11842 cp_parser_error (parser, "expected jump-statement");
11843 break;
11846 return statement;
11849 /* Parse a declaration-statement.
11851 declaration-statement:
11852 block-declaration */
11854 static void
11855 cp_parser_declaration_statement (cp_parser* parser)
11857 void *p;
11859 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11860 p = obstack_alloc (&declarator_obstack, 0);
11862 /* Parse the block-declaration. */
11863 cp_parser_block_declaration (parser, /*statement_p=*/true);
11865 /* Free any declarators allocated. */
11866 obstack_free (&declarator_obstack, p);
11869 /* Some dependent statements (like `if (cond) statement'), are
11870 implicitly in their own scope. In other words, if the statement is
11871 a single statement (as opposed to a compound-statement), it is
11872 none-the-less treated as if it were enclosed in braces. Any
11873 declarations appearing in the dependent statement are out of scope
11874 after control passes that point. This function parses a statement,
11875 but ensures that is in its own scope, even if it is not a
11876 compound-statement.
11878 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11879 is a (possibly labeled) if statement which is not enclosed in
11880 braces and has an else clause. This is used to implement
11881 -Wparentheses.
11883 CHAIN is a vector of if-else-if conditions. This is used to implement
11884 -Wduplicated-cond.
11886 Returns the new statement. */
11888 static tree
11889 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
11890 const token_indent_info &guard_tinfo,
11891 vec<tree> *chain)
11893 tree statement;
11894 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
11895 token_indent_info body_tinfo
11896 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11898 if (if_p != NULL)
11899 *if_p = false;
11901 /* Mark if () ; with a special NOP_EXPR. */
11902 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11904 cp_lexer_consume_token (parser->lexer);
11905 statement = add_stmt (build_empty_stmt (body_loc));
11907 if (guard_tinfo.keyword == RID_IF
11908 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
11909 warning_at (body_loc, OPT_Wempty_body,
11910 "suggest braces around empty body in an %<if%> statement");
11911 else if (guard_tinfo.keyword == RID_ELSE)
11912 warning_at (body_loc, OPT_Wempty_body,
11913 "suggest braces around empty body in an %<else%> statement");
11915 /* if a compound is opened, we simply parse the statement directly. */
11916 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11917 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11918 /* If the token is not a `{', then we must take special action. */
11919 else
11921 /* Create a compound-statement. */
11922 statement = begin_compound_stmt (0);
11923 /* Parse the dependent-statement. */
11924 cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
11925 /* Finish the dummy compound-statement. */
11926 finish_compound_stmt (statement);
11929 token_indent_info next_tinfo
11930 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11931 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11933 /* Return the statement. */
11934 return statement;
11937 /* For some dependent statements (like `while (cond) statement'), we
11938 have already created a scope. Therefore, even if the dependent
11939 statement is a compound-statement, we do not want to create another
11940 scope. */
11942 static void
11943 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
11944 const token_indent_info &guard_tinfo)
11946 /* If the token is a `{', then we must take special action. */
11947 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11949 token_indent_info body_tinfo
11950 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11952 cp_parser_statement (parser, NULL_TREE, false, if_p);
11953 token_indent_info next_tinfo
11954 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11955 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11957 else
11959 /* Avoid calling cp_parser_compound_statement, so that we
11960 don't create a new scope. Do everything else by hand. */
11961 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11962 /* If the next keyword is `__label__' we have a label declaration. */
11963 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11964 cp_parser_label_declaration (parser);
11965 /* Parse an (optional) statement-seq. */
11966 cp_parser_statement_seq_opt (parser, NULL_TREE);
11967 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11971 /* Declarations [gram.dcl.dcl] */
11973 /* Parse an optional declaration-sequence.
11975 declaration-seq:
11976 declaration
11977 declaration-seq declaration */
11979 static void
11980 cp_parser_declaration_seq_opt (cp_parser* parser)
11982 while (true)
11984 cp_token *token;
11986 token = cp_lexer_peek_token (parser->lexer);
11988 if (token->type == CPP_CLOSE_BRACE
11989 || token->type == CPP_EOF
11990 || token->type == CPP_PRAGMA_EOL)
11991 break;
11993 if (token->type == CPP_SEMICOLON)
11995 /* A declaration consisting of a single semicolon is
11996 invalid. Allow it unless we're being pedantic. */
11997 cp_lexer_consume_token (parser->lexer);
11998 if (!in_system_header_at (input_location))
11999 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12000 continue;
12003 /* If we're entering or exiting a region that's implicitly
12004 extern "C", modify the lang context appropriately. */
12005 if (!parser->implicit_extern_c && token->implicit_extern_c)
12007 push_lang_context (lang_name_c);
12008 parser->implicit_extern_c = true;
12010 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12012 pop_lang_context ();
12013 parser->implicit_extern_c = false;
12016 if (token->type == CPP_PRAGMA)
12018 /* A top-level declaration can consist solely of a #pragma.
12019 A nested declaration cannot, so this is done here and not
12020 in cp_parser_declaration. (A #pragma at block scope is
12021 handled in cp_parser_statement.) */
12022 cp_parser_pragma (parser, pragma_external, NULL);
12023 continue;
12026 /* Parse the declaration itself. */
12027 cp_parser_declaration (parser);
12031 /* Parse a declaration.
12033 declaration:
12034 block-declaration
12035 function-definition
12036 template-declaration
12037 explicit-instantiation
12038 explicit-specialization
12039 linkage-specification
12040 namespace-definition
12042 GNU extension:
12044 declaration:
12045 __extension__ declaration */
12047 static void
12048 cp_parser_declaration (cp_parser* parser)
12050 cp_token token1;
12051 cp_token token2;
12052 int saved_pedantic;
12053 void *p;
12054 tree attributes = NULL_TREE;
12056 /* Check for the `__extension__' keyword. */
12057 if (cp_parser_extension_opt (parser, &saved_pedantic))
12059 /* Parse the qualified declaration. */
12060 cp_parser_declaration (parser);
12061 /* Restore the PEDANTIC flag. */
12062 pedantic = saved_pedantic;
12064 return;
12067 /* Try to figure out what kind of declaration is present. */
12068 token1 = *cp_lexer_peek_token (parser->lexer);
12070 if (token1.type != CPP_EOF)
12071 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12072 else
12074 token2.type = CPP_EOF;
12075 token2.keyword = RID_MAX;
12078 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12079 p = obstack_alloc (&declarator_obstack, 0);
12081 /* If the next token is `extern' and the following token is a string
12082 literal, then we have a linkage specification. */
12083 if (token1.keyword == RID_EXTERN
12084 && cp_parser_is_pure_string_literal (&token2))
12085 cp_parser_linkage_specification (parser);
12086 /* If the next token is `template', then we have either a template
12087 declaration, an explicit instantiation, or an explicit
12088 specialization. */
12089 else if (token1.keyword == RID_TEMPLATE)
12091 /* `template <>' indicates a template specialization. */
12092 if (token2.type == CPP_LESS
12093 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12094 cp_parser_explicit_specialization (parser);
12095 /* `template <' indicates a template declaration. */
12096 else if (token2.type == CPP_LESS)
12097 cp_parser_template_declaration (parser, /*member_p=*/false);
12098 /* Anything else must be an explicit instantiation. */
12099 else
12100 cp_parser_explicit_instantiation (parser);
12102 /* If the next token is `export', then we have a template
12103 declaration. */
12104 else if (token1.keyword == RID_EXPORT)
12105 cp_parser_template_declaration (parser, /*member_p=*/false);
12106 /* If the next token is `extern', 'static' or 'inline' and the one
12107 after that is `template', we have a GNU extended explicit
12108 instantiation directive. */
12109 else if (cp_parser_allow_gnu_extensions_p (parser)
12110 && (token1.keyword == RID_EXTERN
12111 || token1.keyword == RID_STATIC
12112 || token1.keyword == RID_INLINE)
12113 && token2.keyword == RID_TEMPLATE)
12114 cp_parser_explicit_instantiation (parser);
12115 /* If the next token is `namespace', check for a named or unnamed
12116 namespace definition. */
12117 else if (token1.keyword == RID_NAMESPACE
12118 && (/* A named namespace definition. */
12119 (token2.type == CPP_NAME
12120 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12121 != CPP_EQ))
12122 || (token2.type == CPP_OPEN_SQUARE
12123 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12124 == CPP_OPEN_SQUARE)
12125 /* An unnamed namespace definition. */
12126 || token2.type == CPP_OPEN_BRACE
12127 || token2.keyword == RID_ATTRIBUTE))
12128 cp_parser_namespace_definition (parser);
12129 /* An inline (associated) namespace definition. */
12130 else if (token1.keyword == RID_INLINE
12131 && token2.keyword == RID_NAMESPACE)
12132 cp_parser_namespace_definition (parser);
12133 /* Objective-C++ declaration/definition. */
12134 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12135 cp_parser_objc_declaration (parser, NULL_TREE);
12136 else if (c_dialect_objc ()
12137 && token1.keyword == RID_ATTRIBUTE
12138 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12139 cp_parser_objc_declaration (parser, attributes);
12140 /* At this point we may have a template declared by a concept
12141 introduction. */
12142 else if (flag_concepts
12143 && cp_parser_template_declaration_after_export (parser,
12144 /*member_p=*/false))
12145 /* We did. */;
12146 else
12147 /* Try to parse a block-declaration, or a function-definition. */
12148 cp_parser_block_declaration (parser, /*statement_p=*/false);
12150 /* Free any declarators allocated. */
12151 obstack_free (&declarator_obstack, p);
12154 /* Parse a block-declaration.
12156 block-declaration:
12157 simple-declaration
12158 asm-definition
12159 namespace-alias-definition
12160 using-declaration
12161 using-directive
12163 GNU Extension:
12165 block-declaration:
12166 __extension__ block-declaration
12168 C++0x Extension:
12170 block-declaration:
12171 static_assert-declaration
12173 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12174 part of a declaration-statement. */
12176 static void
12177 cp_parser_block_declaration (cp_parser *parser,
12178 bool statement_p)
12180 cp_token *token1;
12181 int saved_pedantic;
12183 /* Check for the `__extension__' keyword. */
12184 if (cp_parser_extension_opt (parser, &saved_pedantic))
12186 /* Parse the qualified declaration. */
12187 cp_parser_block_declaration (parser, statement_p);
12188 /* Restore the PEDANTIC flag. */
12189 pedantic = saved_pedantic;
12191 return;
12194 /* Peek at the next token to figure out which kind of declaration is
12195 present. */
12196 token1 = cp_lexer_peek_token (parser->lexer);
12198 /* If the next keyword is `asm', we have an asm-definition. */
12199 if (token1->keyword == RID_ASM)
12201 if (statement_p)
12202 cp_parser_commit_to_tentative_parse (parser);
12203 cp_parser_asm_definition (parser);
12205 /* If the next keyword is `namespace', we have a
12206 namespace-alias-definition. */
12207 else if (token1->keyword == RID_NAMESPACE)
12208 cp_parser_namespace_alias_definition (parser);
12209 /* If the next keyword is `using', we have a
12210 using-declaration, a using-directive, or an alias-declaration. */
12211 else if (token1->keyword == RID_USING)
12213 cp_token *token2;
12215 if (statement_p)
12216 cp_parser_commit_to_tentative_parse (parser);
12217 /* If the token after `using' is `namespace', then we have a
12218 using-directive. */
12219 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12220 if (token2->keyword == RID_NAMESPACE)
12221 cp_parser_using_directive (parser);
12222 /* If the second token after 'using' is '=', then we have an
12223 alias-declaration. */
12224 else if (cxx_dialect >= cxx11
12225 && token2->type == CPP_NAME
12226 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12227 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12228 cp_parser_alias_declaration (parser);
12229 /* Otherwise, it's a using-declaration. */
12230 else
12231 cp_parser_using_declaration (parser,
12232 /*access_declaration_p=*/false);
12234 /* If the next keyword is `__label__' we have a misplaced label
12235 declaration. */
12236 else if (token1->keyword == RID_LABEL)
12238 cp_lexer_consume_token (parser->lexer);
12239 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12240 cp_parser_skip_to_end_of_statement (parser);
12241 /* If the next token is now a `;', consume it. */
12242 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12243 cp_lexer_consume_token (parser->lexer);
12245 /* If the next token is `static_assert' we have a static assertion. */
12246 else if (token1->keyword == RID_STATIC_ASSERT)
12247 cp_parser_static_assert (parser, /*member_p=*/false);
12248 /* Anything else must be a simple-declaration. */
12249 else
12250 cp_parser_simple_declaration (parser, !statement_p,
12251 /*maybe_range_for_decl*/NULL);
12254 /* Parse a simple-declaration.
12256 simple-declaration:
12257 decl-specifier-seq [opt] init-declarator-list [opt] ;
12259 init-declarator-list:
12260 init-declarator
12261 init-declarator-list , init-declarator
12263 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12264 function-definition as a simple-declaration.
12266 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12267 parsed declaration if it is an uninitialized single declarator not followed
12268 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12269 if present, will not be consumed. */
12271 static void
12272 cp_parser_simple_declaration (cp_parser* parser,
12273 bool function_definition_allowed_p,
12274 tree *maybe_range_for_decl)
12276 cp_decl_specifier_seq decl_specifiers;
12277 int declares_class_or_enum;
12278 bool saw_declarator;
12279 location_t comma_loc = UNKNOWN_LOCATION;
12280 location_t init_loc = UNKNOWN_LOCATION;
12282 if (maybe_range_for_decl)
12283 *maybe_range_for_decl = NULL_TREE;
12285 /* Defer access checks until we know what is being declared; the
12286 checks for names appearing in the decl-specifier-seq should be
12287 done as if we were in the scope of the thing being declared. */
12288 push_deferring_access_checks (dk_deferred);
12290 /* Parse the decl-specifier-seq. We have to keep track of whether
12291 or not the decl-specifier-seq declares a named class or
12292 enumeration type, since that is the only case in which the
12293 init-declarator-list is allowed to be empty.
12295 [dcl.dcl]
12297 In a simple-declaration, the optional init-declarator-list can be
12298 omitted only when declaring a class or enumeration, that is when
12299 the decl-specifier-seq contains either a class-specifier, an
12300 elaborated-type-specifier, or an enum-specifier. */
12301 cp_parser_decl_specifier_seq (parser,
12302 CP_PARSER_FLAGS_OPTIONAL,
12303 &decl_specifiers,
12304 &declares_class_or_enum);
12305 /* We no longer need to defer access checks. */
12306 stop_deferring_access_checks ();
12308 /* In a block scope, a valid declaration must always have a
12309 decl-specifier-seq. By not trying to parse declarators, we can
12310 resolve the declaration/expression ambiguity more quickly. */
12311 if (!function_definition_allowed_p
12312 && !decl_specifiers.any_specifiers_p)
12314 cp_parser_error (parser, "expected declaration");
12315 goto done;
12318 /* If the next two tokens are both identifiers, the code is
12319 erroneous. The usual cause of this situation is code like:
12321 T t;
12323 where "T" should name a type -- but does not. */
12324 if (!decl_specifiers.any_type_specifiers_p
12325 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12327 /* If parsing tentatively, we should commit; we really are
12328 looking at a declaration. */
12329 cp_parser_commit_to_tentative_parse (parser);
12330 /* Give up. */
12331 goto done;
12334 /* If we have seen at least one decl-specifier, and the next token
12335 is not a parenthesis, then we must be looking at a declaration.
12336 (After "int (" we might be looking at a functional cast.) */
12337 if (decl_specifiers.any_specifiers_p
12338 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12339 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12340 && !cp_parser_error_occurred (parser))
12341 cp_parser_commit_to_tentative_parse (parser);
12343 tree last_type;
12345 last_type = NULL_TREE;
12347 /* Keep going until we hit the `;' at the end of the simple
12348 declaration. */
12349 saw_declarator = false;
12350 while (cp_lexer_next_token_is_not (parser->lexer,
12351 CPP_SEMICOLON))
12353 cp_token *token;
12354 bool function_definition_p;
12355 tree decl;
12356 tree auto_result = NULL_TREE;
12358 if (saw_declarator)
12360 /* If we are processing next declarator, comma is expected */
12361 token = cp_lexer_peek_token (parser->lexer);
12362 gcc_assert (token->type == CPP_COMMA);
12363 cp_lexer_consume_token (parser->lexer);
12364 if (maybe_range_for_decl)
12366 *maybe_range_for_decl = error_mark_node;
12367 if (comma_loc == UNKNOWN_LOCATION)
12368 comma_loc = token->location;
12371 else
12372 saw_declarator = true;
12374 /* Parse the init-declarator. */
12375 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12376 /*checks=*/NULL,
12377 function_definition_allowed_p,
12378 /*member_p=*/false,
12379 declares_class_or_enum,
12380 &function_definition_p,
12381 maybe_range_for_decl,
12382 &init_loc,
12383 &auto_result);
12384 /* If an error occurred while parsing tentatively, exit quickly.
12385 (That usually happens when in the body of a function; each
12386 statement is treated as a declaration-statement until proven
12387 otherwise.) */
12388 if (cp_parser_error_occurred (parser))
12389 goto done;
12391 if (auto_result)
12393 if (last_type && last_type != error_mark_node
12394 && !same_type_p (auto_result, last_type))
12396 /* If the list of declarators contains more than one declarator,
12397 the type of each declared variable is determined as described
12398 above. If the type deduced for the template parameter U is not
12399 the same in each deduction, the program is ill-formed. */
12400 error_at (decl_specifiers.locations[ds_type_spec],
12401 "inconsistent deduction for %qT: %qT and then %qT",
12402 decl_specifiers.type, last_type, auto_result);
12403 last_type = error_mark_node;
12405 else
12406 last_type = auto_result;
12409 /* Handle function definitions specially. */
12410 if (function_definition_p)
12412 /* If the next token is a `,', then we are probably
12413 processing something like:
12415 void f() {}, *p;
12417 which is erroneous. */
12418 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12420 cp_token *token = cp_lexer_peek_token (parser->lexer);
12421 error_at (token->location,
12422 "mixing"
12423 " declarations and function-definitions is forbidden");
12425 /* Otherwise, we're done with the list of declarators. */
12426 else
12428 pop_deferring_access_checks ();
12429 return;
12432 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12433 *maybe_range_for_decl = decl;
12434 /* The next token should be either a `,' or a `;'. */
12435 token = cp_lexer_peek_token (parser->lexer);
12436 /* If it's a `,', there are more declarators to come. */
12437 if (token->type == CPP_COMMA)
12438 /* will be consumed next time around */;
12439 /* If it's a `;', we are done. */
12440 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12441 break;
12442 /* Anything else is an error. */
12443 else
12445 /* If we have already issued an error message we don't need
12446 to issue another one. */
12447 if ((decl != error_mark_node
12448 && DECL_INITIAL (decl) != error_mark_node)
12449 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12450 cp_parser_error (parser, "expected %<,%> or %<;%>");
12451 /* Skip tokens until we reach the end of the statement. */
12452 cp_parser_skip_to_end_of_statement (parser);
12453 /* If the next token is now a `;', consume it. */
12454 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12455 cp_lexer_consume_token (parser->lexer);
12456 goto done;
12458 /* After the first time around, a function-definition is not
12459 allowed -- even if it was OK at first. For example:
12461 int i, f() {}
12463 is not valid. */
12464 function_definition_allowed_p = false;
12467 /* Issue an error message if no declarators are present, and the
12468 decl-specifier-seq does not itself declare a class or
12469 enumeration: [dcl.dcl]/3. */
12470 if (!saw_declarator)
12472 if (cp_parser_declares_only_class_p (parser))
12474 if (!declares_class_or_enum
12475 && decl_specifiers.type
12476 && OVERLOAD_TYPE_P (decl_specifiers.type))
12477 /* Ensure an error is issued anyway when finish_decltype_type,
12478 called via cp_parser_decl_specifier_seq, returns a class or
12479 an enumeration (c++/51786). */
12480 decl_specifiers.type = NULL_TREE;
12481 shadow_tag (&decl_specifiers);
12483 /* Perform any deferred access checks. */
12484 perform_deferred_access_checks (tf_warning_or_error);
12487 /* Consume the `;'. */
12488 if (!maybe_range_for_decl)
12489 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12490 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12492 if (init_loc != UNKNOWN_LOCATION)
12493 error_at (init_loc, "initializer in range-based %<for%> loop");
12494 if (comma_loc != UNKNOWN_LOCATION)
12495 error_at (comma_loc,
12496 "multiple declarations in range-based %<for%> loop");
12499 done:
12500 pop_deferring_access_checks ();
12503 /* Parse a decl-specifier-seq.
12505 decl-specifier-seq:
12506 decl-specifier-seq [opt] decl-specifier
12507 decl-specifier attribute-specifier-seq [opt] (C++11)
12509 decl-specifier:
12510 storage-class-specifier
12511 type-specifier
12512 function-specifier
12513 friend
12514 typedef
12516 GNU Extension:
12518 decl-specifier:
12519 attributes
12521 Concepts Extension:
12523 decl-specifier:
12524 concept
12526 Set *DECL_SPECS to a representation of the decl-specifier-seq.
12528 The parser flags FLAGS is used to control type-specifier parsing.
12530 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
12531 flags:
12533 1: one of the decl-specifiers is an elaborated-type-specifier
12534 (i.e., a type declaration)
12535 2: one of the decl-specifiers is an enum-specifier or a
12536 class-specifier (i.e., a type definition)
12540 static void
12541 cp_parser_decl_specifier_seq (cp_parser* parser,
12542 cp_parser_flags flags,
12543 cp_decl_specifier_seq *decl_specs,
12544 int* declares_class_or_enum)
12546 bool constructor_possible_p = !parser->in_declarator_p;
12547 bool found_decl_spec = false;
12548 cp_token *start_token = NULL;
12549 cp_decl_spec ds;
12551 /* Clear DECL_SPECS. */
12552 clear_decl_specs (decl_specs);
12554 /* Assume no class or enumeration type is declared. */
12555 *declares_class_or_enum = 0;
12557 /* Keep reading specifiers until there are no more to read. */
12558 while (true)
12560 bool constructor_p;
12561 cp_token *token;
12562 ds = ds_last;
12564 /* Peek at the next token. */
12565 token = cp_lexer_peek_token (parser->lexer);
12567 /* Save the first token of the decl spec list for error
12568 reporting. */
12569 if (!start_token)
12570 start_token = token;
12571 /* Handle attributes. */
12572 if (cp_next_tokens_can_be_attribute_p (parser))
12574 /* Parse the attributes. */
12575 tree attrs = cp_parser_attributes_opt (parser);
12577 /* In a sequence of declaration specifiers, c++11 attributes
12578 appertain to the type that precede them. In that case
12579 [dcl.spec]/1 says:
12581 The attribute-specifier-seq affects the type only for
12582 the declaration it appears in, not other declarations
12583 involving the same type.
12585 But for now let's force the user to position the
12586 attribute either at the beginning of the declaration or
12587 after the declarator-id, which would clearly mean that it
12588 applies to the declarator. */
12589 if (cxx11_attribute_p (attrs))
12591 if (!found_decl_spec)
12592 /* The c++11 attribute is at the beginning of the
12593 declaration. It appertains to the entity being
12594 declared. */;
12595 else
12597 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
12599 /* This is an attribute following a
12600 class-specifier. */
12601 if (decl_specs->type_definition_p)
12602 warn_misplaced_attr_for_class_type (token->location,
12603 decl_specs->type);
12604 attrs = NULL_TREE;
12606 else
12608 decl_specs->std_attributes
12609 = chainon (decl_specs->std_attributes,
12610 attrs);
12611 if (decl_specs->locations[ds_std_attribute] == 0)
12612 decl_specs->locations[ds_std_attribute] = token->location;
12614 continue;
12618 decl_specs->attributes
12619 = chainon (decl_specs->attributes,
12620 attrs);
12621 if (decl_specs->locations[ds_attribute] == 0)
12622 decl_specs->locations[ds_attribute] = token->location;
12623 continue;
12625 /* Assume we will find a decl-specifier keyword. */
12626 found_decl_spec = true;
12627 /* If the next token is an appropriate keyword, we can simply
12628 add it to the list. */
12629 switch (token->keyword)
12631 /* decl-specifier:
12632 friend
12633 constexpr */
12634 case RID_FRIEND:
12635 if (!at_class_scope_p ())
12637 error_at (token->location, "%<friend%> used outside of class");
12638 cp_lexer_purge_token (parser->lexer);
12640 else
12642 ds = ds_friend;
12643 /* Consume the token. */
12644 cp_lexer_consume_token (parser->lexer);
12646 break;
12648 case RID_CONSTEXPR:
12649 ds = ds_constexpr;
12650 cp_lexer_consume_token (parser->lexer);
12651 break;
12653 case RID_CONCEPT:
12654 ds = ds_concept;
12655 cp_lexer_consume_token (parser->lexer);
12656 break;
12658 /* function-specifier:
12659 inline
12660 virtual
12661 explicit */
12662 case RID_INLINE:
12663 case RID_VIRTUAL:
12664 case RID_EXPLICIT:
12665 cp_parser_function_specifier_opt (parser, decl_specs);
12666 break;
12668 /* decl-specifier:
12669 typedef */
12670 case RID_TYPEDEF:
12671 ds = ds_typedef;
12672 /* Consume the token. */
12673 cp_lexer_consume_token (parser->lexer);
12674 /* A constructor declarator cannot appear in a typedef. */
12675 constructor_possible_p = false;
12676 /* The "typedef" keyword can only occur in a declaration; we
12677 may as well commit at this point. */
12678 cp_parser_commit_to_tentative_parse (parser);
12680 if (decl_specs->storage_class != sc_none)
12681 decl_specs->conflicting_specifiers_p = true;
12682 break;
12684 /* storage-class-specifier:
12685 auto
12686 register
12687 static
12688 extern
12689 mutable
12691 GNU Extension:
12692 thread */
12693 case RID_AUTO:
12694 if (cxx_dialect == cxx98)
12696 /* Consume the token. */
12697 cp_lexer_consume_token (parser->lexer);
12699 /* Complain about `auto' as a storage specifier, if
12700 we're complaining about C++0x compatibility. */
12701 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
12702 " changes meaning in C++11; please remove it");
12704 /* Set the storage class anyway. */
12705 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
12706 token);
12708 else
12709 /* C++0x auto type-specifier. */
12710 found_decl_spec = false;
12711 break;
12713 case RID_REGISTER:
12714 case RID_STATIC:
12715 case RID_EXTERN:
12716 case RID_MUTABLE:
12717 /* Consume the token. */
12718 cp_lexer_consume_token (parser->lexer);
12719 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
12720 token);
12721 break;
12722 case RID_THREAD:
12723 /* Consume the token. */
12724 ds = ds_thread;
12725 cp_lexer_consume_token (parser->lexer);
12726 break;
12728 default:
12729 /* We did not yet find a decl-specifier yet. */
12730 found_decl_spec = false;
12731 break;
12734 if (found_decl_spec
12735 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
12736 && token->keyword != RID_CONSTEXPR)
12737 error ("decl-specifier invalid in condition");
12739 if (ds != ds_last)
12740 set_and_check_decl_spec_loc (decl_specs, ds, token);
12742 /* Constructors are a special case. The `S' in `S()' is not a
12743 decl-specifier; it is the beginning of the declarator. */
12744 constructor_p
12745 = (!found_decl_spec
12746 && constructor_possible_p
12747 && (cp_parser_constructor_declarator_p
12748 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
12750 /* If we don't have a DECL_SPEC yet, then we must be looking at
12751 a type-specifier. */
12752 if (!found_decl_spec && !constructor_p)
12754 int decl_spec_declares_class_or_enum;
12755 bool is_cv_qualifier;
12756 tree type_spec;
12758 type_spec
12759 = cp_parser_type_specifier (parser, flags,
12760 decl_specs,
12761 /*is_declaration=*/true,
12762 &decl_spec_declares_class_or_enum,
12763 &is_cv_qualifier);
12764 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
12766 /* If this type-specifier referenced a user-defined type
12767 (a typedef, class-name, etc.), then we can't allow any
12768 more such type-specifiers henceforth.
12770 [dcl.spec]
12772 The longest sequence of decl-specifiers that could
12773 possibly be a type name is taken as the
12774 decl-specifier-seq of a declaration. The sequence shall
12775 be self-consistent as described below.
12777 [dcl.type]
12779 As a general rule, at most one type-specifier is allowed
12780 in the complete decl-specifier-seq of a declaration. The
12781 only exceptions are the following:
12783 -- const or volatile can be combined with any other
12784 type-specifier.
12786 -- signed or unsigned can be combined with char, long,
12787 short, or int.
12789 -- ..
12791 Example:
12793 typedef char* Pc;
12794 void g (const int Pc);
12796 Here, Pc is *not* part of the decl-specifier seq; it's
12797 the declarator. Therefore, once we see a type-specifier
12798 (other than a cv-qualifier), we forbid any additional
12799 user-defined types. We *do* still allow things like `int
12800 int' to be considered a decl-specifier-seq, and issue the
12801 error message later. */
12802 if (type_spec && !is_cv_qualifier)
12803 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12804 /* A constructor declarator cannot follow a type-specifier. */
12805 if (type_spec)
12807 constructor_possible_p = false;
12808 found_decl_spec = true;
12809 if (!is_cv_qualifier)
12810 decl_specs->any_type_specifiers_p = true;
12814 /* If we still do not have a DECL_SPEC, then there are no more
12815 decl-specifiers. */
12816 if (!found_decl_spec)
12817 break;
12819 decl_specs->any_specifiers_p = true;
12820 /* After we see one decl-specifier, further decl-specifiers are
12821 always optional. */
12822 flags |= CP_PARSER_FLAGS_OPTIONAL;
12825 /* Don't allow a friend specifier with a class definition. */
12826 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12827 && (*declares_class_or_enum & 2))
12828 error_at (decl_specs->locations[ds_friend],
12829 "class definition may not be declared a friend");
12832 /* Parse an (optional) storage-class-specifier.
12834 storage-class-specifier:
12835 auto
12836 register
12837 static
12838 extern
12839 mutable
12841 GNU Extension:
12843 storage-class-specifier:
12844 thread
12846 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12848 static tree
12849 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12851 switch (cp_lexer_peek_token (parser->lexer)->keyword)
12853 case RID_AUTO:
12854 if (cxx_dialect != cxx98)
12855 return NULL_TREE;
12856 /* Fall through for C++98. */
12858 case RID_REGISTER:
12859 case RID_STATIC:
12860 case RID_EXTERN:
12861 case RID_MUTABLE:
12862 case RID_THREAD:
12863 /* Consume the token. */
12864 return cp_lexer_consume_token (parser->lexer)->u.value;
12866 default:
12867 return NULL_TREE;
12871 /* Parse an (optional) function-specifier.
12873 function-specifier:
12874 inline
12875 virtual
12876 explicit
12878 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12879 Updates DECL_SPECS, if it is non-NULL. */
12881 static tree
12882 cp_parser_function_specifier_opt (cp_parser* parser,
12883 cp_decl_specifier_seq *decl_specs)
12885 cp_token *token = cp_lexer_peek_token (parser->lexer);
12886 switch (token->keyword)
12888 case RID_INLINE:
12889 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12890 break;
12892 case RID_VIRTUAL:
12893 /* 14.5.2.3 [temp.mem]
12895 A member function template shall not be virtual. */
12896 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12897 && current_class_type)
12898 error_at (token->location, "templates may not be %<virtual%>");
12899 else
12900 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12901 break;
12903 case RID_EXPLICIT:
12904 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12905 break;
12907 default:
12908 return NULL_TREE;
12911 /* Consume the token. */
12912 return cp_lexer_consume_token (parser->lexer)->u.value;
12915 /* Parse a linkage-specification.
12917 linkage-specification:
12918 extern string-literal { declaration-seq [opt] }
12919 extern string-literal declaration */
12921 static void
12922 cp_parser_linkage_specification (cp_parser* parser)
12924 tree linkage;
12926 /* Look for the `extern' keyword. */
12927 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12929 /* Look for the string-literal. */
12930 linkage = cp_parser_string_literal (parser, false, false);
12932 /* Transform the literal into an identifier. If the literal is a
12933 wide-character string, or contains embedded NULs, then we can't
12934 handle it as the user wants. */
12935 if (strlen (TREE_STRING_POINTER (linkage))
12936 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12938 cp_parser_error (parser, "invalid linkage-specification");
12939 /* Assume C++ linkage. */
12940 linkage = lang_name_cplusplus;
12942 else
12943 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12945 /* We're now using the new linkage. */
12946 push_lang_context (linkage);
12948 /* If the next token is a `{', then we're using the first
12949 production. */
12950 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12952 cp_ensure_no_omp_declare_simd (parser);
12953 cp_ensure_no_oacc_routine (parser);
12955 /* Consume the `{' token. */
12956 cp_lexer_consume_token (parser->lexer);
12957 /* Parse the declarations. */
12958 cp_parser_declaration_seq_opt (parser);
12959 /* Look for the closing `}'. */
12960 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12962 /* Otherwise, there's just one declaration. */
12963 else
12965 bool saved_in_unbraced_linkage_specification_p;
12967 saved_in_unbraced_linkage_specification_p
12968 = parser->in_unbraced_linkage_specification_p;
12969 parser->in_unbraced_linkage_specification_p = true;
12970 cp_parser_declaration (parser);
12971 parser->in_unbraced_linkage_specification_p
12972 = saved_in_unbraced_linkage_specification_p;
12975 /* We're done with the linkage-specification. */
12976 pop_lang_context ();
12979 /* Parse a static_assert-declaration.
12981 static_assert-declaration:
12982 static_assert ( constant-expression , string-literal ) ;
12983 static_assert ( constant-expression ) ; (C++1Z)
12985 If MEMBER_P, this static_assert is a class member. */
12987 static void
12988 cp_parser_static_assert(cp_parser *parser, bool member_p)
12990 tree condition;
12991 tree message;
12992 cp_token *token;
12993 location_t saved_loc;
12994 bool dummy;
12996 /* Peek at the `static_assert' token so we can keep track of exactly
12997 where the static assertion started. */
12998 token = cp_lexer_peek_token (parser->lexer);
12999 saved_loc = token->location;
13001 /* Look for the `static_assert' keyword. */
13002 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13003 RT_STATIC_ASSERT))
13004 return;
13006 /* We know we are in a static assertion; commit to any tentative
13007 parse. */
13008 if (cp_parser_parsing_tentatively (parser))
13009 cp_parser_commit_to_tentative_parse (parser);
13011 /* Parse the `(' starting the static assertion condition. */
13012 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
13014 /* Parse the constant-expression. Allow a non-constant expression
13015 here in order to give better diagnostics in finish_static_assert. */
13016 condition =
13017 cp_parser_constant_expression (parser,
13018 /*allow_non_constant_p=*/true,
13019 /*non_constant_p=*/&dummy);
13021 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13023 if (cxx_dialect < cxx1z)
13024 pedwarn (input_location, OPT_Wpedantic,
13025 "static_assert without a message "
13026 "only available with -std=c++1z or -std=gnu++1z");
13027 /* Eat the ')' */
13028 cp_lexer_consume_token (parser->lexer);
13029 message = build_string (1, "");
13030 TREE_TYPE (message) = char_array_type_node;
13031 fix_string_type (message);
13033 else
13035 /* Parse the separating `,'. */
13036 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13038 /* Parse the string-literal message. */
13039 message = cp_parser_string_literal (parser,
13040 /*translate=*/false,
13041 /*wide_ok=*/true);
13043 /* A `)' completes the static assertion. */
13044 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13045 cp_parser_skip_to_closing_parenthesis (parser,
13046 /*recovering=*/true,
13047 /*or_comma=*/false,
13048 /*consume_paren=*/true);
13051 /* A semicolon terminates the declaration. */
13052 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13054 /* Complete the static assertion, which may mean either processing
13055 the static assert now or saving it for template instantiation. */
13056 finish_static_assert (condition, message, saved_loc, member_p);
13059 /* Parse the expression in decltype ( expression ). */
13061 static tree
13062 cp_parser_decltype_expr (cp_parser *parser,
13063 bool &id_expression_or_member_access_p)
13065 cp_token *id_expr_start_token;
13066 tree expr;
13068 /* Since we're going to preserve any side-effects from this parse, set up a
13069 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13070 in the expression. */
13071 tentative_firewall firewall (parser);
13073 /* First, try parsing an id-expression. */
13074 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13075 cp_parser_parse_tentatively (parser);
13076 expr = cp_parser_id_expression (parser,
13077 /*template_keyword_p=*/false,
13078 /*check_dependency_p=*/true,
13079 /*template_p=*/NULL,
13080 /*declarator_p=*/false,
13081 /*optional_p=*/false);
13083 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13085 bool non_integral_constant_expression_p = false;
13086 tree id_expression = expr;
13087 cp_id_kind idk;
13088 const char *error_msg;
13090 if (identifier_p (expr))
13091 /* Lookup the name we got back from the id-expression. */
13092 expr = cp_parser_lookup_name_simple (parser, expr,
13093 id_expr_start_token->location);
13095 if (expr
13096 && expr != error_mark_node
13097 && TREE_CODE (expr) != TYPE_DECL
13098 && (TREE_CODE (expr) != BIT_NOT_EXPR
13099 || !TYPE_P (TREE_OPERAND (expr, 0)))
13100 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13102 /* Complete lookup of the id-expression. */
13103 expr = (finish_id_expression
13104 (id_expression, expr, parser->scope, &idk,
13105 /*integral_constant_expression_p=*/false,
13106 /*allow_non_integral_constant_expression_p=*/true,
13107 &non_integral_constant_expression_p,
13108 /*template_p=*/false,
13109 /*done=*/true,
13110 /*address_p=*/false,
13111 /*template_arg_p=*/false,
13112 &error_msg,
13113 id_expr_start_token->location));
13115 if (expr == error_mark_node)
13116 /* We found an id-expression, but it was something that we
13117 should not have found. This is an error, not something
13118 we can recover from, so note that we found an
13119 id-expression and we'll recover as gracefully as
13120 possible. */
13121 id_expression_or_member_access_p = true;
13124 if (expr
13125 && expr != error_mark_node
13126 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13127 /* We have an id-expression. */
13128 id_expression_or_member_access_p = true;
13131 if (!id_expression_or_member_access_p)
13133 /* Abort the id-expression parse. */
13134 cp_parser_abort_tentative_parse (parser);
13136 /* Parsing tentatively, again. */
13137 cp_parser_parse_tentatively (parser);
13139 /* Parse a class member access. */
13140 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13141 /*cast_p=*/false, /*decltype*/true,
13142 /*member_access_only_p=*/true, NULL);
13144 if (expr
13145 && expr != error_mark_node
13146 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13147 /* We have an id-expression. */
13148 id_expression_or_member_access_p = true;
13151 if (id_expression_or_member_access_p)
13152 /* We have parsed the complete id-expression or member access. */
13153 cp_parser_parse_definitely (parser);
13154 else
13156 /* Abort our attempt to parse an id-expression or member access
13157 expression. */
13158 cp_parser_abort_tentative_parse (parser);
13160 /* Parse a full expression. */
13161 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13162 /*decltype_p=*/true);
13165 return expr;
13168 /* Parse a `decltype' type. Returns the type.
13170 simple-type-specifier:
13171 decltype ( expression )
13172 C++14 proposal:
13173 decltype ( auto ) */
13175 static tree
13176 cp_parser_decltype (cp_parser *parser)
13178 tree expr;
13179 bool id_expression_or_member_access_p = false;
13180 const char *saved_message;
13181 bool saved_integral_constant_expression_p;
13182 bool saved_non_integral_constant_expression_p;
13183 bool saved_greater_than_is_operator_p;
13184 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13186 if (start_token->type == CPP_DECLTYPE)
13188 /* Already parsed. */
13189 cp_lexer_consume_token (parser->lexer);
13190 return saved_checks_value (start_token->u.tree_check_value);
13193 /* Look for the `decltype' token. */
13194 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13195 return error_mark_node;
13197 /* Parse the opening `('. */
13198 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13199 return error_mark_node;
13201 /* decltype (auto) */
13202 if (cxx_dialect >= cxx14
13203 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13205 cp_lexer_consume_token (parser->lexer);
13206 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13207 return error_mark_node;
13208 expr = make_decltype_auto ();
13209 AUTO_IS_DECLTYPE (expr) = true;
13210 goto rewrite;
13213 /* Types cannot be defined in a `decltype' expression. Save away the
13214 old message. */
13215 saved_message = parser->type_definition_forbidden_message;
13217 /* And create the new one. */
13218 parser->type_definition_forbidden_message
13219 = G_("types may not be defined in %<decltype%> expressions");
13221 /* The restrictions on constant-expressions do not apply inside
13222 decltype expressions. */
13223 saved_integral_constant_expression_p
13224 = parser->integral_constant_expression_p;
13225 saved_non_integral_constant_expression_p
13226 = parser->non_integral_constant_expression_p;
13227 parser->integral_constant_expression_p = false;
13229 /* Within a parenthesized expression, a `>' token is always
13230 the greater-than operator. */
13231 saved_greater_than_is_operator_p
13232 = parser->greater_than_is_operator_p;
13233 parser->greater_than_is_operator_p = true;
13235 /* Do not actually evaluate the expression. */
13236 ++cp_unevaluated_operand;
13238 /* Do not warn about problems with the expression. */
13239 ++c_inhibit_evaluation_warnings;
13241 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13243 /* Go back to evaluating expressions. */
13244 --cp_unevaluated_operand;
13245 --c_inhibit_evaluation_warnings;
13247 /* The `>' token might be the end of a template-id or
13248 template-parameter-list now. */
13249 parser->greater_than_is_operator_p
13250 = saved_greater_than_is_operator_p;
13252 /* Restore the old message and the integral constant expression
13253 flags. */
13254 parser->type_definition_forbidden_message = saved_message;
13255 parser->integral_constant_expression_p
13256 = saved_integral_constant_expression_p;
13257 parser->non_integral_constant_expression_p
13258 = saved_non_integral_constant_expression_p;
13260 /* Parse to the closing `)'. */
13261 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13263 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13264 /*consume_paren=*/true);
13265 return error_mark_node;
13268 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
13269 tf_warning_or_error);
13271 rewrite:
13272 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13273 it again. */
13274 start_token->type = CPP_DECLTYPE;
13275 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13276 start_token->u.tree_check_value->value = expr;
13277 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
13278 start_token->keyword = RID_MAX;
13279 cp_lexer_purge_tokens_after (parser->lexer, start_token);
13281 return expr;
13284 /* Special member functions [gram.special] */
13286 /* Parse a conversion-function-id.
13288 conversion-function-id:
13289 operator conversion-type-id
13291 Returns an IDENTIFIER_NODE representing the operator. */
13293 static tree
13294 cp_parser_conversion_function_id (cp_parser* parser)
13296 tree type;
13297 tree saved_scope;
13298 tree saved_qualifying_scope;
13299 tree saved_object_scope;
13300 tree pushed_scope = NULL_TREE;
13302 /* Look for the `operator' token. */
13303 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13304 return error_mark_node;
13305 /* When we parse the conversion-type-id, the current scope will be
13306 reset. However, we need that information in able to look up the
13307 conversion function later, so we save it here. */
13308 saved_scope = parser->scope;
13309 saved_qualifying_scope = parser->qualifying_scope;
13310 saved_object_scope = parser->object_scope;
13311 /* We must enter the scope of the class so that the names of
13312 entities declared within the class are available in the
13313 conversion-type-id. For example, consider:
13315 struct S {
13316 typedef int I;
13317 operator I();
13320 S::operator I() { ... }
13322 In order to see that `I' is a type-name in the definition, we
13323 must be in the scope of `S'. */
13324 if (saved_scope)
13325 pushed_scope = push_scope (saved_scope);
13326 /* Parse the conversion-type-id. */
13327 type = cp_parser_conversion_type_id (parser);
13328 /* Leave the scope of the class, if any. */
13329 if (pushed_scope)
13330 pop_scope (pushed_scope);
13331 /* Restore the saved scope. */
13332 parser->scope = saved_scope;
13333 parser->qualifying_scope = saved_qualifying_scope;
13334 parser->object_scope = saved_object_scope;
13335 /* If the TYPE is invalid, indicate failure. */
13336 if (type == error_mark_node)
13337 return error_mark_node;
13338 return mangle_conv_op_name_for_type (type);
13341 /* Parse a conversion-type-id:
13343 conversion-type-id:
13344 type-specifier-seq conversion-declarator [opt]
13346 Returns the TYPE specified. */
13348 static tree
13349 cp_parser_conversion_type_id (cp_parser* parser)
13351 tree attributes;
13352 cp_decl_specifier_seq type_specifiers;
13353 cp_declarator *declarator;
13354 tree type_specified;
13355 const char *saved_message;
13357 /* Parse the attributes. */
13358 attributes = cp_parser_attributes_opt (parser);
13360 saved_message = parser->type_definition_forbidden_message;
13361 parser->type_definition_forbidden_message
13362 = G_("types may not be defined in a conversion-type-id");
13364 /* Parse the type-specifiers. */
13365 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13366 /*is_trailing_return=*/false,
13367 &type_specifiers);
13369 parser->type_definition_forbidden_message = saved_message;
13371 /* If that didn't work, stop. */
13372 if (type_specifiers.type == error_mark_node)
13373 return error_mark_node;
13374 /* Parse the conversion-declarator. */
13375 declarator = cp_parser_conversion_declarator_opt (parser);
13377 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
13378 /*initialized=*/0, &attributes);
13379 if (attributes)
13380 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
13382 /* Don't give this error when parsing tentatively. This happens to
13383 work because we always parse this definitively once. */
13384 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
13385 && type_uses_auto (type_specified))
13387 if (cxx_dialect < cxx14)
13389 error ("invalid use of %<auto%> in conversion operator");
13390 return error_mark_node;
13392 else if (template_parm_scope_p ())
13393 warning (0, "use of %<auto%> in member template "
13394 "conversion operator can never be deduced");
13397 return type_specified;
13400 /* Parse an (optional) conversion-declarator.
13402 conversion-declarator:
13403 ptr-operator conversion-declarator [opt]
13407 static cp_declarator *
13408 cp_parser_conversion_declarator_opt (cp_parser* parser)
13410 enum tree_code code;
13411 tree class_type, std_attributes = NULL_TREE;
13412 cp_cv_quals cv_quals;
13414 /* We don't know if there's a ptr-operator next, or not. */
13415 cp_parser_parse_tentatively (parser);
13416 /* Try the ptr-operator. */
13417 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
13418 &std_attributes);
13419 /* If it worked, look for more conversion-declarators. */
13420 if (cp_parser_parse_definitely (parser))
13422 cp_declarator *declarator;
13424 /* Parse another optional declarator. */
13425 declarator = cp_parser_conversion_declarator_opt (parser);
13427 declarator = cp_parser_make_indirect_declarator
13428 (code, class_type, cv_quals, declarator, std_attributes);
13430 return declarator;
13433 return NULL;
13436 /* Parse an (optional) ctor-initializer.
13438 ctor-initializer:
13439 : mem-initializer-list
13441 Returns TRUE iff the ctor-initializer was actually present. */
13443 static bool
13444 cp_parser_ctor_initializer_opt (cp_parser* parser)
13446 /* If the next token is not a `:', then there is no
13447 ctor-initializer. */
13448 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13450 /* Do default initialization of any bases and members. */
13451 if (DECL_CONSTRUCTOR_P (current_function_decl))
13452 finish_mem_initializers (NULL_TREE);
13454 return false;
13457 /* Consume the `:' token. */
13458 cp_lexer_consume_token (parser->lexer);
13459 /* And the mem-initializer-list. */
13460 cp_parser_mem_initializer_list (parser);
13462 return true;
13465 /* Parse a mem-initializer-list.
13467 mem-initializer-list:
13468 mem-initializer ... [opt]
13469 mem-initializer ... [opt] , mem-initializer-list */
13471 static void
13472 cp_parser_mem_initializer_list (cp_parser* parser)
13474 tree mem_initializer_list = NULL_TREE;
13475 tree target_ctor = error_mark_node;
13476 cp_token *token = cp_lexer_peek_token (parser->lexer);
13478 /* Let the semantic analysis code know that we are starting the
13479 mem-initializer-list. */
13480 if (!DECL_CONSTRUCTOR_P (current_function_decl))
13481 error_at (token->location,
13482 "only constructors take member initializers");
13484 /* Loop through the list. */
13485 while (true)
13487 tree mem_initializer;
13489 token = cp_lexer_peek_token (parser->lexer);
13490 /* Parse the mem-initializer. */
13491 mem_initializer = cp_parser_mem_initializer (parser);
13492 /* If the next token is a `...', we're expanding member initializers. */
13493 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13495 /* Consume the `...'. */
13496 cp_lexer_consume_token (parser->lexer);
13498 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
13499 can be expanded but members cannot. */
13500 if (mem_initializer != error_mark_node
13501 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
13503 error_at (token->location,
13504 "cannot expand initializer for member %<%D%>",
13505 TREE_PURPOSE (mem_initializer));
13506 mem_initializer = error_mark_node;
13509 /* Construct the pack expansion type. */
13510 if (mem_initializer != error_mark_node)
13511 mem_initializer = make_pack_expansion (mem_initializer);
13513 if (target_ctor != error_mark_node
13514 && mem_initializer != error_mark_node)
13516 error ("mem-initializer for %qD follows constructor delegation",
13517 TREE_PURPOSE (mem_initializer));
13518 mem_initializer = error_mark_node;
13520 /* Look for a target constructor. */
13521 if (mem_initializer != error_mark_node
13522 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
13523 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
13525 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
13526 if (mem_initializer_list)
13528 error ("constructor delegation follows mem-initializer for %qD",
13529 TREE_PURPOSE (mem_initializer_list));
13530 mem_initializer = error_mark_node;
13532 target_ctor = mem_initializer;
13534 /* Add it to the list, unless it was erroneous. */
13535 if (mem_initializer != error_mark_node)
13537 TREE_CHAIN (mem_initializer) = mem_initializer_list;
13538 mem_initializer_list = mem_initializer;
13540 /* If the next token is not a `,', we're done. */
13541 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13542 break;
13543 /* Consume the `,' token. */
13544 cp_lexer_consume_token (parser->lexer);
13547 /* Perform semantic analysis. */
13548 if (DECL_CONSTRUCTOR_P (current_function_decl))
13549 finish_mem_initializers (mem_initializer_list);
13552 /* Parse a mem-initializer.
13554 mem-initializer:
13555 mem-initializer-id ( expression-list [opt] )
13556 mem-initializer-id braced-init-list
13558 GNU extension:
13560 mem-initializer:
13561 ( expression-list [opt] )
13563 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
13564 class) or FIELD_DECL (for a non-static data member) to initialize;
13565 the TREE_VALUE is the expression-list. An empty initialization
13566 list is represented by void_list_node. */
13568 static tree
13569 cp_parser_mem_initializer (cp_parser* parser)
13571 tree mem_initializer_id;
13572 tree expression_list;
13573 tree member;
13574 cp_token *token = cp_lexer_peek_token (parser->lexer);
13576 /* Find out what is being initialized. */
13577 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13579 permerror (token->location,
13580 "anachronistic old-style base class initializer");
13581 mem_initializer_id = NULL_TREE;
13583 else
13585 mem_initializer_id = cp_parser_mem_initializer_id (parser);
13586 if (mem_initializer_id == error_mark_node)
13587 return mem_initializer_id;
13589 member = expand_member_init (mem_initializer_id);
13590 if (member && !DECL_P (member))
13591 in_base_initializer = 1;
13593 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13595 bool expr_non_constant_p;
13596 cp_lexer_set_source_position (parser->lexer);
13597 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13598 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
13599 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
13600 expression_list = build_tree_list (NULL_TREE, expression_list);
13602 else
13604 vec<tree, va_gc> *vec;
13605 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
13606 /*cast_p=*/false,
13607 /*allow_expansion_p=*/true,
13608 /*non_constant_p=*/NULL);
13609 if (vec == NULL)
13610 return error_mark_node;
13611 expression_list = build_tree_list_vec (vec);
13612 release_tree_vector (vec);
13615 if (expression_list == error_mark_node)
13616 return error_mark_node;
13617 if (!expression_list)
13618 expression_list = void_type_node;
13620 in_base_initializer = 0;
13622 return member ? build_tree_list (member, expression_list) : error_mark_node;
13625 /* Parse a mem-initializer-id.
13627 mem-initializer-id:
13628 :: [opt] nested-name-specifier [opt] class-name
13629 decltype-specifier (C++11)
13630 identifier
13632 Returns a TYPE indicating the class to be initialized for the first
13633 production (and the second in C++11). Returns an IDENTIFIER_NODE
13634 indicating the data member to be initialized for the last production. */
13636 static tree
13637 cp_parser_mem_initializer_id (cp_parser* parser)
13639 bool global_scope_p;
13640 bool nested_name_specifier_p;
13641 bool template_p = false;
13642 tree id;
13644 cp_token *token = cp_lexer_peek_token (parser->lexer);
13646 /* `typename' is not allowed in this context ([temp.res]). */
13647 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13649 error_at (token->location,
13650 "keyword %<typename%> not allowed in this context (a qualified "
13651 "member initializer is implicitly a type)");
13652 cp_lexer_consume_token (parser->lexer);
13654 /* Look for the optional `::' operator. */
13655 global_scope_p
13656 = (cp_parser_global_scope_opt (parser,
13657 /*current_scope_valid_p=*/false)
13658 != NULL_TREE);
13659 /* Look for the optional nested-name-specifier. The simplest way to
13660 implement:
13662 [temp.res]
13664 The keyword `typename' is not permitted in a base-specifier or
13665 mem-initializer; in these contexts a qualified name that
13666 depends on a template-parameter is implicitly assumed to be a
13667 type name.
13669 is to assume that we have seen the `typename' keyword at this
13670 point. */
13671 nested_name_specifier_p
13672 = (cp_parser_nested_name_specifier_opt (parser,
13673 /*typename_keyword_p=*/true,
13674 /*check_dependency_p=*/true,
13675 /*type_p=*/true,
13676 /*is_declaration=*/true)
13677 != NULL_TREE);
13678 if (nested_name_specifier_p)
13679 template_p = cp_parser_optional_template_keyword (parser);
13680 /* If there is a `::' operator or a nested-name-specifier, then we
13681 are definitely looking for a class-name. */
13682 if (global_scope_p || nested_name_specifier_p)
13683 return cp_parser_class_name (parser,
13684 /*typename_keyword_p=*/true,
13685 /*template_keyword_p=*/template_p,
13686 typename_type,
13687 /*check_dependency_p=*/true,
13688 /*class_head_p=*/false,
13689 /*is_declaration=*/true);
13690 /* Otherwise, we could also be looking for an ordinary identifier. */
13691 cp_parser_parse_tentatively (parser);
13692 if (cp_lexer_next_token_is_decltype (parser->lexer))
13693 /* Try a decltype-specifier. */
13694 id = cp_parser_decltype (parser);
13695 else
13696 /* Otherwise, try a class-name. */
13697 id = cp_parser_class_name (parser,
13698 /*typename_keyword_p=*/true,
13699 /*template_keyword_p=*/false,
13700 none_type,
13701 /*check_dependency_p=*/true,
13702 /*class_head_p=*/false,
13703 /*is_declaration=*/true);
13704 /* If we found one, we're done. */
13705 if (cp_parser_parse_definitely (parser))
13706 return id;
13707 /* Otherwise, look for an ordinary identifier. */
13708 return cp_parser_identifier (parser);
13711 /* Overloading [gram.over] */
13713 /* Parse an operator-function-id.
13715 operator-function-id:
13716 operator operator
13718 Returns an IDENTIFIER_NODE for the operator which is a
13719 human-readable spelling of the identifier, e.g., `operator +'. */
13721 static cp_expr
13722 cp_parser_operator_function_id (cp_parser* parser)
13724 /* Look for the `operator' keyword. */
13725 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13726 return error_mark_node;
13727 /* And then the name of the operator itself. */
13728 return cp_parser_operator (parser);
13731 /* Return an identifier node for a user-defined literal operator.
13732 The suffix identifier is chained to the operator name identifier. */
13734 static tree
13735 cp_literal_operator_id (const char* name)
13737 tree identifier;
13738 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
13739 + strlen (name) + 10);
13740 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
13741 identifier = get_identifier (buffer);
13743 return identifier;
13746 /* Parse an operator.
13748 operator:
13749 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
13750 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
13751 || ++ -- , ->* -> () []
13753 GNU Extensions:
13755 operator:
13756 <? >? <?= >?=
13758 Returns an IDENTIFIER_NODE for the operator which is a
13759 human-readable spelling of the identifier, e.g., `operator +'. */
13761 static cp_expr
13762 cp_parser_operator (cp_parser* parser)
13764 tree id = NULL_TREE;
13765 cp_token *token;
13766 bool utf8 = false;
13768 /* Peek at the next token. */
13769 token = cp_lexer_peek_token (parser->lexer);
13771 location_t start_loc = token->location;
13773 /* Figure out which operator we have. */
13774 switch (token->type)
13776 case CPP_KEYWORD:
13778 enum tree_code op;
13780 /* The keyword should be either `new' or `delete'. */
13781 if (token->keyword == RID_NEW)
13782 op = NEW_EXPR;
13783 else if (token->keyword == RID_DELETE)
13784 op = DELETE_EXPR;
13785 else
13786 break;
13788 /* Consume the `new' or `delete' token. */
13789 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
13791 /* Peek at the next token. */
13792 token = cp_lexer_peek_token (parser->lexer);
13793 /* If it's a `[' token then this is the array variant of the
13794 operator. */
13795 if (token->type == CPP_OPEN_SQUARE)
13797 /* Consume the `[' token. */
13798 cp_lexer_consume_token (parser->lexer);
13799 /* Look for the `]' token. */
13800 if (cp_token *close_token
13801 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13802 end_loc = close_token->location;
13803 id = ansi_opname (op == NEW_EXPR
13804 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
13806 /* Otherwise, we have the non-array variant. */
13807 else
13808 id = ansi_opname (op);
13810 location_t loc = make_location (start_loc, start_loc, end_loc);
13812 return cp_expr (id, loc);
13815 case CPP_PLUS:
13816 id = ansi_opname (PLUS_EXPR);
13817 break;
13819 case CPP_MINUS:
13820 id = ansi_opname (MINUS_EXPR);
13821 break;
13823 case CPP_MULT:
13824 id = ansi_opname (MULT_EXPR);
13825 break;
13827 case CPP_DIV:
13828 id = ansi_opname (TRUNC_DIV_EXPR);
13829 break;
13831 case CPP_MOD:
13832 id = ansi_opname (TRUNC_MOD_EXPR);
13833 break;
13835 case CPP_XOR:
13836 id = ansi_opname (BIT_XOR_EXPR);
13837 break;
13839 case CPP_AND:
13840 id = ansi_opname (BIT_AND_EXPR);
13841 break;
13843 case CPP_OR:
13844 id = ansi_opname (BIT_IOR_EXPR);
13845 break;
13847 case CPP_COMPL:
13848 id = ansi_opname (BIT_NOT_EXPR);
13849 break;
13851 case CPP_NOT:
13852 id = ansi_opname (TRUTH_NOT_EXPR);
13853 break;
13855 case CPP_EQ:
13856 id = ansi_assopname (NOP_EXPR);
13857 break;
13859 case CPP_LESS:
13860 id = ansi_opname (LT_EXPR);
13861 break;
13863 case CPP_GREATER:
13864 id = ansi_opname (GT_EXPR);
13865 break;
13867 case CPP_PLUS_EQ:
13868 id = ansi_assopname (PLUS_EXPR);
13869 break;
13871 case CPP_MINUS_EQ:
13872 id = ansi_assopname (MINUS_EXPR);
13873 break;
13875 case CPP_MULT_EQ:
13876 id = ansi_assopname (MULT_EXPR);
13877 break;
13879 case CPP_DIV_EQ:
13880 id = ansi_assopname (TRUNC_DIV_EXPR);
13881 break;
13883 case CPP_MOD_EQ:
13884 id = ansi_assopname (TRUNC_MOD_EXPR);
13885 break;
13887 case CPP_XOR_EQ:
13888 id = ansi_assopname (BIT_XOR_EXPR);
13889 break;
13891 case CPP_AND_EQ:
13892 id = ansi_assopname (BIT_AND_EXPR);
13893 break;
13895 case CPP_OR_EQ:
13896 id = ansi_assopname (BIT_IOR_EXPR);
13897 break;
13899 case CPP_LSHIFT:
13900 id = ansi_opname (LSHIFT_EXPR);
13901 break;
13903 case CPP_RSHIFT:
13904 id = ansi_opname (RSHIFT_EXPR);
13905 break;
13907 case CPP_LSHIFT_EQ:
13908 id = ansi_assopname (LSHIFT_EXPR);
13909 break;
13911 case CPP_RSHIFT_EQ:
13912 id = ansi_assopname (RSHIFT_EXPR);
13913 break;
13915 case CPP_EQ_EQ:
13916 id = ansi_opname (EQ_EXPR);
13917 break;
13919 case CPP_NOT_EQ:
13920 id = ansi_opname (NE_EXPR);
13921 break;
13923 case CPP_LESS_EQ:
13924 id = ansi_opname (LE_EXPR);
13925 break;
13927 case CPP_GREATER_EQ:
13928 id = ansi_opname (GE_EXPR);
13929 break;
13931 case CPP_AND_AND:
13932 id = ansi_opname (TRUTH_ANDIF_EXPR);
13933 break;
13935 case CPP_OR_OR:
13936 id = ansi_opname (TRUTH_ORIF_EXPR);
13937 break;
13939 case CPP_PLUS_PLUS:
13940 id = ansi_opname (POSTINCREMENT_EXPR);
13941 break;
13943 case CPP_MINUS_MINUS:
13944 id = ansi_opname (PREDECREMENT_EXPR);
13945 break;
13947 case CPP_COMMA:
13948 id = ansi_opname (COMPOUND_EXPR);
13949 break;
13951 case CPP_DEREF_STAR:
13952 id = ansi_opname (MEMBER_REF);
13953 break;
13955 case CPP_DEREF:
13956 id = ansi_opname (COMPONENT_REF);
13957 break;
13959 case CPP_OPEN_PAREN:
13960 /* Consume the `('. */
13961 cp_lexer_consume_token (parser->lexer);
13962 /* Look for the matching `)'. */
13963 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13964 return ansi_opname (CALL_EXPR);
13966 case CPP_OPEN_SQUARE:
13967 /* Consume the `['. */
13968 cp_lexer_consume_token (parser->lexer);
13969 /* Look for the matching `]'. */
13970 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13971 return ansi_opname (ARRAY_REF);
13973 case CPP_UTF8STRING:
13974 case CPP_UTF8STRING_USERDEF:
13975 utf8 = true;
13976 case CPP_STRING:
13977 case CPP_WSTRING:
13978 case CPP_STRING16:
13979 case CPP_STRING32:
13980 case CPP_STRING_USERDEF:
13981 case CPP_WSTRING_USERDEF:
13982 case CPP_STRING16_USERDEF:
13983 case CPP_STRING32_USERDEF:
13985 tree str, string_tree;
13986 int sz, len;
13988 if (cxx_dialect == cxx98)
13989 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13991 /* Consume the string. */
13992 str = cp_parser_string_literal (parser, /*translate=*/true,
13993 /*wide_ok=*/true, /*lookup_udlit=*/false);
13994 if (str == error_mark_node)
13995 return error_mark_node;
13996 else if (TREE_CODE (str) == USERDEF_LITERAL)
13998 string_tree = USERDEF_LITERAL_VALUE (str);
13999 id = USERDEF_LITERAL_SUFFIX_ID (str);
14001 else
14003 string_tree = str;
14004 /* Look for the suffix identifier. */
14005 token = cp_lexer_peek_token (parser->lexer);
14006 if (token->type == CPP_NAME)
14007 id = cp_parser_identifier (parser);
14008 else if (token->type == CPP_KEYWORD)
14010 error ("unexpected keyword;"
14011 " remove space between quotes and suffix identifier");
14012 return error_mark_node;
14014 else
14016 error ("expected suffix identifier");
14017 return error_mark_node;
14020 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14021 (TREE_TYPE (TREE_TYPE (string_tree))));
14022 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14023 if (len != 0)
14025 error ("expected empty string after %<operator%> keyword");
14026 return error_mark_node;
14028 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14029 != char_type_node)
14031 error ("invalid encoding prefix in literal operator");
14032 return error_mark_node;
14034 if (id != error_mark_node)
14036 const char *name = IDENTIFIER_POINTER (id);
14037 id = cp_literal_operator_id (name);
14039 return id;
14042 default:
14043 /* Anything else is an error. */
14044 break;
14047 /* If we have selected an identifier, we need to consume the
14048 operator token. */
14049 if (id)
14050 cp_lexer_consume_token (parser->lexer);
14051 /* Otherwise, no valid operator name was present. */
14052 else
14054 cp_parser_error (parser, "expected operator");
14055 id = error_mark_node;
14058 return cp_expr (id, start_loc);
14061 /* Parse a template-declaration.
14063 template-declaration:
14064 export [opt] template < template-parameter-list > declaration
14066 If MEMBER_P is TRUE, this template-declaration occurs within a
14067 class-specifier.
14069 The grammar rule given by the standard isn't correct. What
14070 is really meant is:
14072 template-declaration:
14073 export [opt] template-parameter-list-seq
14074 decl-specifier-seq [opt] init-declarator [opt] ;
14075 export [opt] template-parameter-list-seq
14076 function-definition
14078 template-parameter-list-seq:
14079 template-parameter-list-seq [opt]
14080 template < template-parameter-list >
14082 Concept Extensions:
14084 template-parameter-list-seq:
14085 template < template-parameter-list > requires-clause [opt]
14087 requires-clause:
14088 requires logical-or-expression */
14090 static void
14091 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14093 /* Check for `export'. */
14094 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14096 /* Consume the `export' token. */
14097 cp_lexer_consume_token (parser->lexer);
14098 /* Warn that we do not support `export'. */
14099 warning (0, "keyword %<export%> not implemented, and will be ignored");
14102 cp_parser_template_declaration_after_export (parser, member_p);
14105 /* Parse a template-parameter-list.
14107 template-parameter-list:
14108 template-parameter
14109 template-parameter-list , template-parameter
14111 Returns a TREE_LIST. Each node represents a template parameter.
14112 The nodes are connected via their TREE_CHAINs. */
14114 static tree
14115 cp_parser_template_parameter_list (cp_parser* parser)
14117 tree parameter_list = NULL_TREE;
14119 begin_template_parm_list ();
14121 /* The loop below parses the template parms. We first need to know
14122 the total number of template parms to be able to compute proper
14123 canonical types of each dependent type. So after the loop, when
14124 we know the total number of template parms,
14125 end_template_parm_list computes the proper canonical types and
14126 fixes up the dependent types accordingly. */
14127 while (true)
14129 tree parameter;
14130 bool is_non_type;
14131 bool is_parameter_pack;
14132 location_t parm_loc;
14134 /* Parse the template-parameter. */
14135 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14136 parameter = cp_parser_template_parameter (parser,
14137 &is_non_type,
14138 &is_parameter_pack);
14139 /* Add it to the list. */
14140 if (parameter != error_mark_node)
14141 parameter_list = process_template_parm (parameter_list,
14142 parm_loc,
14143 parameter,
14144 is_non_type,
14145 is_parameter_pack);
14146 else
14148 tree err_parm = build_tree_list (parameter, parameter);
14149 parameter_list = chainon (parameter_list, err_parm);
14152 /* If the next token is not a `,', we're done. */
14153 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14154 break;
14155 /* Otherwise, consume the `,' token. */
14156 cp_lexer_consume_token (parser->lexer);
14159 return end_template_parm_list (parameter_list);
14162 /* Parse a introduction-list.
14164 introduction-list:
14165 introduced-parameter
14166 introduction-list , introduced-parameter
14168 introduced-parameter:
14169 ...[opt] identifier
14171 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14172 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14173 WILDCARD_DECL will also have DECL_NAME set and token location in
14174 DECL_SOURCE_LOCATION. */
14176 static tree
14177 cp_parser_introduction_list (cp_parser *parser)
14179 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14181 while (true)
14183 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14184 if (is_pack)
14185 cp_lexer_consume_token (parser->lexer);
14187 /* Build placeholder. */
14188 tree parm = build_nt (WILDCARD_DECL);
14189 DECL_SOURCE_LOCATION (parm)
14190 = cp_lexer_peek_token (parser->lexer)->location;
14191 DECL_NAME (parm) = cp_parser_identifier (parser);
14192 WILDCARD_PACK_P (parm) = is_pack;
14193 vec_safe_push (introduction_vec, parm);
14195 /* If the next token is not a `,', we're done. */
14196 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14197 break;
14198 /* Otherwise, consume the `,' token. */
14199 cp_lexer_consume_token (parser->lexer);
14202 /* Convert the vec into a TREE_VEC. */
14203 tree introduction_list = make_tree_vec (introduction_vec->length ());
14204 unsigned int n;
14205 tree parm;
14206 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14207 TREE_VEC_ELT (introduction_list, n) = parm;
14209 release_tree_vector (introduction_vec);
14210 return introduction_list;
14213 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14214 is an abstract declarator. */
14216 static inline cp_declarator*
14217 get_id_declarator (cp_declarator *declarator)
14219 cp_declarator *d = declarator;
14220 while (d && d->kind != cdk_id)
14221 d = d->declarator;
14222 return d;
14225 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14226 is an abstract declarator. */
14228 static inline tree
14229 get_unqualified_id (cp_declarator *declarator)
14231 declarator = get_id_declarator (declarator);
14232 if (declarator)
14233 return declarator->u.id.unqualified_name;
14234 else
14235 return NULL_TREE;
14238 /* Returns true if DECL represents a constrained-parameter. */
14240 static inline bool
14241 is_constrained_parameter (tree decl)
14243 return (decl
14244 && TREE_CODE (decl) == TYPE_DECL
14245 && CONSTRAINED_PARM_CONCEPT (decl)
14246 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14249 /* Returns true if PARM declares a constrained-parameter. */
14251 static inline bool
14252 is_constrained_parameter (cp_parameter_declarator *parm)
14254 return is_constrained_parameter (parm->decl_specifiers.type);
14257 /* Check that the type parameter is only a declarator-id, and that its
14258 type is not cv-qualified. */
14260 bool
14261 cp_parser_check_constrained_type_parm (cp_parser *parser,
14262 cp_parameter_declarator *parm)
14264 if (!parm->declarator)
14265 return true;
14267 if (parm->declarator->kind != cdk_id)
14269 cp_parser_error (parser, "invalid constrained type parameter");
14270 return false;
14273 /* Don't allow cv-qualified type parameters. */
14274 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
14275 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
14277 cp_parser_error (parser, "cv-qualified type parameter");
14278 return false;
14281 return true;
14284 /* Finish parsing/processing a template type parameter and checking
14285 various restrictions. */
14287 static inline tree
14288 cp_parser_constrained_type_template_parm (cp_parser *parser,
14289 tree id,
14290 cp_parameter_declarator* parmdecl)
14292 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
14293 return finish_template_type_parm (class_type_node, id);
14294 else
14295 return error_mark_node;
14298 static tree
14299 finish_constrained_template_template_parm (tree proto, tree id)
14301 /* FIXME: This should probably be copied, and we may need to adjust
14302 the template parameter depths. */
14303 tree saved_parms = current_template_parms;
14304 begin_template_parm_list ();
14305 current_template_parms = DECL_TEMPLATE_PARMS (proto);
14306 end_template_parm_list ();
14308 tree parm = finish_template_template_parm (class_type_node, id);
14309 current_template_parms = saved_parms;
14311 return parm;
14314 /* Finish parsing/processing a template template parameter by borrowing
14315 the template parameter list from the prototype parameter. */
14317 static tree
14318 cp_parser_constrained_template_template_parm (cp_parser *parser,
14319 tree proto,
14320 tree id,
14321 cp_parameter_declarator *parmdecl)
14323 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
14324 return error_mark_node;
14325 return finish_constrained_template_template_parm (proto, id);
14328 /* Create a new non-type template parameter from the given PARM
14329 declarator. */
14331 static tree
14332 constrained_non_type_template_parm (bool *is_non_type,
14333 cp_parameter_declarator *parm)
14335 *is_non_type = true;
14336 cp_declarator *decl = parm->declarator;
14337 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
14338 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
14339 return grokdeclarator (decl, specs, TPARM, 0, NULL);
14342 /* Build a constrained template parameter based on the PARMDECL
14343 declarator. The type of PARMDECL is the constrained type, which
14344 refers to the prototype template parameter that ultimately
14345 specifies the type of the declared parameter. */
14347 static tree
14348 finish_constrained_parameter (cp_parser *parser,
14349 cp_parameter_declarator *parmdecl,
14350 bool *is_non_type,
14351 bool *is_parameter_pack)
14353 tree decl = parmdecl->decl_specifiers.type;
14354 tree id = get_unqualified_id (parmdecl->declarator);
14355 tree def = parmdecl->default_argument;
14356 tree proto = DECL_INITIAL (decl);
14358 /* A template parameter constrained by a variadic concept shall also
14359 be declared as a template parameter pack. */
14360 bool is_variadic = template_parameter_pack_p (proto);
14361 if (is_variadic && !*is_parameter_pack)
14362 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
14364 /* Build the parameter. Return an error if the declarator was invalid. */
14365 tree parm;
14366 if (TREE_CODE (proto) == TYPE_DECL)
14367 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
14368 else if (TREE_CODE (proto) == TEMPLATE_DECL)
14369 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
14370 parmdecl);
14371 else
14372 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
14373 if (parm == error_mark_node)
14374 return error_mark_node;
14376 /* Finish the parameter decl and create a node attaching the
14377 default argument and constraint. */
14378 parm = build_tree_list (def, parm);
14379 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
14381 return parm;
14384 /* Returns true if the parsed type actually represents the declaration
14385 of a type template-parameter. */
14387 static inline bool
14388 declares_constrained_type_template_parameter (tree type)
14390 return (is_constrained_parameter (type)
14391 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
14395 /* Returns true if the parsed type actually represents the declaration of
14396 a template template-parameter. */
14398 static bool
14399 declares_constrained_template_template_parameter (tree type)
14401 return (is_constrained_parameter (type)
14402 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
14405 /* Parse a default argument for a type template-parameter.
14406 Note that diagnostics are handled in cp_parser_template_parameter. */
14408 static tree
14409 cp_parser_default_type_template_argument (cp_parser *parser)
14411 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14413 /* Consume the `=' token. */
14414 cp_lexer_consume_token (parser->lexer);
14416 cp_token *token = cp_lexer_peek_token (parser->lexer);
14418 /* Parse the default-argument. */
14419 push_deferring_access_checks (dk_no_deferred);
14420 tree default_argument = cp_parser_type_id (parser);
14421 pop_deferring_access_checks ();
14423 if (flag_concepts && type_uses_auto (default_argument))
14425 error_at (token->location,
14426 "invalid use of %<auto%> in default template argument");
14427 return error_mark_node;
14430 return default_argument;
14433 /* Parse a default argument for a template template-parameter. */
14435 static tree
14436 cp_parser_default_template_template_argument (cp_parser *parser)
14438 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14440 bool is_template;
14442 /* Consume the `='. */
14443 cp_lexer_consume_token (parser->lexer);
14444 /* Parse the id-expression. */
14445 push_deferring_access_checks (dk_no_deferred);
14446 /* save token before parsing the id-expression, for error
14447 reporting */
14448 const cp_token* token = cp_lexer_peek_token (parser->lexer);
14449 tree default_argument
14450 = cp_parser_id_expression (parser,
14451 /*template_keyword_p=*/false,
14452 /*check_dependency_p=*/true,
14453 /*template_p=*/&is_template,
14454 /*declarator_p=*/false,
14455 /*optional_p=*/false);
14456 if (TREE_CODE (default_argument) == TYPE_DECL)
14457 /* If the id-expression was a template-id that refers to
14458 a template-class, we already have the declaration here,
14459 so no further lookup is needed. */
14461 else
14462 /* Look up the name. */
14463 default_argument
14464 = cp_parser_lookup_name (parser, default_argument,
14465 none_type,
14466 /*is_template=*/is_template,
14467 /*is_namespace=*/false,
14468 /*check_dependency=*/true,
14469 /*ambiguous_decls=*/NULL,
14470 token->location);
14471 /* See if the default argument is valid. */
14472 default_argument = check_template_template_default_arg (default_argument);
14473 pop_deferring_access_checks ();
14474 return default_argument;
14477 /* Parse a template-parameter.
14479 template-parameter:
14480 type-parameter
14481 parameter-declaration
14483 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
14484 the parameter. The TREE_PURPOSE is the default value, if any.
14485 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
14486 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
14487 set to true iff this parameter is a parameter pack. */
14489 static tree
14490 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
14491 bool *is_parameter_pack)
14493 cp_token *token;
14494 cp_parameter_declarator *parameter_declarator;
14495 tree parm;
14497 /* Assume it is a type parameter or a template parameter. */
14498 *is_non_type = false;
14499 /* Assume it not a parameter pack. */
14500 *is_parameter_pack = false;
14501 /* Peek at the next token. */
14502 token = cp_lexer_peek_token (parser->lexer);
14503 /* If it is `class' or `template', we have a type-parameter. */
14504 if (token->keyword == RID_TEMPLATE)
14505 return cp_parser_type_parameter (parser, is_parameter_pack);
14506 /* If it is `class' or `typename' we do not know yet whether it is a
14507 type parameter or a non-type parameter. Consider:
14509 template <typename T, typename T::X X> ...
14513 template <class C, class D*> ...
14515 Here, the first parameter is a type parameter, and the second is
14516 a non-type parameter. We can tell by looking at the token after
14517 the identifier -- if it is a `,', `=', or `>' then we have a type
14518 parameter. */
14519 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
14521 /* Peek at the token after `class' or `typename'. */
14522 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14523 /* If it's an ellipsis, we have a template type parameter
14524 pack. */
14525 if (token->type == CPP_ELLIPSIS)
14526 return cp_parser_type_parameter (parser, is_parameter_pack);
14527 /* If it's an identifier, skip it. */
14528 if (token->type == CPP_NAME)
14529 token = cp_lexer_peek_nth_token (parser->lexer, 3);
14530 /* Now, see if the token looks like the end of a template
14531 parameter. */
14532 if (token->type == CPP_COMMA
14533 || token->type == CPP_EQ
14534 || token->type == CPP_GREATER)
14535 return cp_parser_type_parameter (parser, is_parameter_pack);
14538 /* Otherwise, it is a non-type parameter or a constrained parameter.
14540 [temp.param]
14542 When parsing a default template-argument for a non-type
14543 template-parameter, the first non-nested `>' is taken as the end
14544 of the template parameter-list rather than a greater-than
14545 operator. */
14546 parameter_declarator
14547 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
14548 /*parenthesized_p=*/NULL);
14550 if (!parameter_declarator)
14551 return error_mark_node;
14553 /* If the parameter declaration is marked as a parameter pack, set
14554 *IS_PARAMETER_PACK to notify the caller. */
14555 if (parameter_declarator->template_parameter_pack_p)
14556 *is_parameter_pack = true;
14558 if (parameter_declarator->default_argument)
14560 /* Can happen in some cases of erroneous input (c++/34892). */
14561 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14562 /* Consume the `...' for better error recovery. */
14563 cp_lexer_consume_token (parser->lexer);
14566 // The parameter may have been constrained.
14567 if (is_constrained_parameter (parameter_declarator))
14568 return finish_constrained_parameter (parser,
14569 parameter_declarator,
14570 is_non_type,
14571 is_parameter_pack);
14573 // Now we're sure that the parameter is a non-type parameter.
14574 *is_non_type = true;
14576 parm = grokdeclarator (parameter_declarator->declarator,
14577 &parameter_declarator->decl_specifiers,
14578 TPARM, /*initialized=*/0,
14579 /*attrlist=*/NULL);
14580 if (parm == error_mark_node)
14581 return error_mark_node;
14583 return build_tree_list (parameter_declarator->default_argument, parm);
14586 /* Parse a type-parameter.
14588 type-parameter:
14589 class identifier [opt]
14590 class identifier [opt] = type-id
14591 typename identifier [opt]
14592 typename identifier [opt] = type-id
14593 template < template-parameter-list > class identifier [opt]
14594 template < template-parameter-list > class identifier [opt]
14595 = id-expression
14597 GNU Extension (variadic templates):
14599 type-parameter:
14600 class ... identifier [opt]
14601 typename ... identifier [opt]
14603 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
14604 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
14605 the declaration of the parameter.
14607 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
14609 static tree
14610 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
14612 cp_token *token;
14613 tree parameter;
14615 /* Look for a keyword to tell us what kind of parameter this is. */
14616 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
14617 if (!token)
14618 return error_mark_node;
14620 switch (token->keyword)
14622 case RID_CLASS:
14623 case RID_TYPENAME:
14625 tree identifier;
14626 tree default_argument;
14628 /* If the next token is an ellipsis, we have a template
14629 argument pack. */
14630 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14632 /* Consume the `...' token. */
14633 cp_lexer_consume_token (parser->lexer);
14634 maybe_warn_variadic_templates ();
14636 *is_parameter_pack = true;
14639 /* If the next token is an identifier, then it names the
14640 parameter. */
14641 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14642 identifier = cp_parser_identifier (parser);
14643 else
14644 identifier = NULL_TREE;
14646 /* Create the parameter. */
14647 parameter = finish_template_type_parm (class_type_node, identifier);
14649 /* If the next token is an `=', we have a default argument. */
14650 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14652 default_argument
14653 = cp_parser_default_type_template_argument (parser);
14655 /* Template parameter packs cannot have default
14656 arguments. */
14657 if (*is_parameter_pack)
14659 if (identifier)
14660 error_at (token->location,
14661 "template parameter pack %qD cannot have a "
14662 "default argument", identifier);
14663 else
14664 error_at (token->location,
14665 "template parameter packs cannot have "
14666 "default arguments");
14667 default_argument = NULL_TREE;
14669 else if (check_for_bare_parameter_packs (default_argument))
14670 default_argument = error_mark_node;
14672 else
14673 default_argument = NULL_TREE;
14675 /* Create the combined representation of the parameter and the
14676 default argument. */
14677 parameter = build_tree_list (default_argument, parameter);
14679 break;
14681 case RID_TEMPLATE:
14683 tree identifier;
14684 tree default_argument;
14686 /* Look for the `<'. */
14687 cp_parser_require (parser, CPP_LESS, RT_LESS);
14688 /* Parse the template-parameter-list. */
14689 cp_parser_template_parameter_list (parser);
14690 /* Look for the `>'. */
14691 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14693 // If template requirements are present, parse them.
14694 tree reqs = get_shorthand_constraints (current_template_parms);
14695 if (tree r = cp_parser_requires_clause_opt (parser))
14696 reqs = conjoin_constraints (reqs, make_predicate_constraint (r));
14697 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
14699 /* Look for the `class' or 'typename' keywords. */
14700 cp_parser_type_parameter_key (parser);
14701 /* If the next token is an ellipsis, we have a template
14702 argument pack. */
14703 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14705 /* Consume the `...' token. */
14706 cp_lexer_consume_token (parser->lexer);
14707 maybe_warn_variadic_templates ();
14709 *is_parameter_pack = true;
14711 /* If the next token is an `=', then there is a
14712 default-argument. If the next token is a `>', we are at
14713 the end of the parameter-list. If the next token is a `,',
14714 then we are at the end of this parameter. */
14715 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
14716 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
14717 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14719 identifier = cp_parser_identifier (parser);
14720 /* Treat invalid names as if the parameter were nameless. */
14721 if (identifier == error_mark_node)
14722 identifier = NULL_TREE;
14724 else
14725 identifier = NULL_TREE;
14727 /* Create the template parameter. */
14728 parameter = finish_template_template_parm (class_type_node,
14729 identifier);
14731 /* If the next token is an `=', then there is a
14732 default-argument. */
14733 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14735 default_argument
14736 = cp_parser_default_template_template_argument (parser);
14738 /* Template parameter packs cannot have default
14739 arguments. */
14740 if (*is_parameter_pack)
14742 if (identifier)
14743 error_at (token->location,
14744 "template parameter pack %qD cannot "
14745 "have a default argument",
14746 identifier);
14747 else
14748 error_at (token->location, "template parameter packs cannot "
14749 "have default arguments");
14750 default_argument = NULL_TREE;
14753 else
14754 default_argument = NULL_TREE;
14756 /* Create the combined representation of the parameter and the
14757 default argument. */
14758 parameter = build_tree_list (default_argument, parameter);
14760 break;
14762 default:
14763 gcc_unreachable ();
14764 break;
14767 return parameter;
14770 /* Parse a template-id.
14772 template-id:
14773 template-name < template-argument-list [opt] >
14775 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
14776 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
14777 returned. Otherwise, if the template-name names a function, or set
14778 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
14779 names a class, returns a TYPE_DECL for the specialization.
14781 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
14782 uninstantiated templates. */
14784 static tree
14785 cp_parser_template_id (cp_parser *parser,
14786 bool template_keyword_p,
14787 bool check_dependency_p,
14788 enum tag_types tag_type,
14789 bool is_declaration)
14791 tree templ;
14792 tree arguments;
14793 tree template_id;
14794 cp_token_position start_of_id = 0;
14795 cp_token *next_token = NULL, *next_token_2 = NULL;
14796 bool is_identifier;
14798 /* If the next token corresponds to a template-id, there is no need
14799 to reparse it. */
14800 next_token = cp_lexer_peek_token (parser->lexer);
14801 if (next_token->type == CPP_TEMPLATE_ID)
14803 cp_lexer_consume_token (parser->lexer);
14804 return saved_checks_value (next_token->u.tree_check_value);
14807 /* Avoid performing name lookup if there is no possibility of
14808 finding a template-id. */
14809 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
14810 || (next_token->type == CPP_NAME
14811 && !cp_parser_nth_token_starts_template_argument_list_p
14812 (parser, 2)))
14814 cp_parser_error (parser, "expected template-id");
14815 return error_mark_node;
14818 /* Remember where the template-id starts. */
14819 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
14820 start_of_id = cp_lexer_token_position (parser->lexer, false);
14822 push_deferring_access_checks (dk_deferred);
14824 /* Parse the template-name. */
14825 is_identifier = false;
14826 templ = cp_parser_template_name (parser, template_keyword_p,
14827 check_dependency_p,
14828 is_declaration,
14829 tag_type,
14830 &is_identifier);
14831 if (templ == error_mark_node || is_identifier)
14833 pop_deferring_access_checks ();
14834 return templ;
14837 /* Since we're going to preserve any side-effects from this parse, set up a
14838 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14839 in the template arguments. */
14840 tentative_firewall firewall (parser);
14842 /* If we find the sequence `[:' after a template-name, it's probably
14843 a digraph-typo for `< ::'. Substitute the tokens and check if we can
14844 parse correctly the argument list. */
14845 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
14846 == CPP_OPEN_SQUARE)
14847 && next_token->flags & DIGRAPH
14848 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
14849 == CPP_COLON)
14850 && !(next_token_2->flags & PREV_WHITE))
14852 cp_parser_parse_tentatively (parser);
14853 /* Change `:' into `::'. */
14854 next_token_2->type = CPP_SCOPE;
14855 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
14856 CPP_LESS. */
14857 cp_lexer_consume_token (parser->lexer);
14859 /* Parse the arguments. */
14860 arguments = cp_parser_enclosed_template_argument_list (parser);
14861 if (!cp_parser_parse_definitely (parser))
14863 /* If we couldn't parse an argument list, then we revert our changes
14864 and return simply an error. Maybe this is not a template-id
14865 after all. */
14866 next_token_2->type = CPP_COLON;
14867 cp_parser_error (parser, "expected %<<%>");
14868 pop_deferring_access_checks ();
14869 return error_mark_node;
14871 /* Otherwise, emit an error about the invalid digraph, but continue
14872 parsing because we got our argument list. */
14873 if (permerror (next_token->location,
14874 "%<<::%> cannot begin a template-argument list"))
14876 static bool hint = false;
14877 inform (next_token->location,
14878 "%<<:%> is an alternate spelling for %<[%>."
14879 " Insert whitespace between %<<%> and %<::%>");
14880 if (!hint && !flag_permissive)
14882 inform (next_token->location, "(if you use %<-fpermissive%> "
14883 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
14884 "accept your code)");
14885 hint = true;
14889 else
14891 /* Look for the `<' that starts the template-argument-list. */
14892 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
14894 pop_deferring_access_checks ();
14895 return error_mark_node;
14897 /* Parse the arguments. */
14898 arguments = cp_parser_enclosed_template_argument_list (parser);
14901 /* Build a representation of the specialization. */
14902 if (identifier_p (templ))
14903 template_id = build_min_nt_loc (next_token->location,
14904 TEMPLATE_ID_EXPR,
14905 templ, arguments);
14906 else if (DECL_TYPE_TEMPLATE_P (templ)
14907 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
14909 bool entering_scope;
14910 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
14911 template (rather than some instantiation thereof) only if
14912 is not nested within some other construct. For example, in
14913 "template <typename T> void f(T) { A<T>::", A<T> is just an
14914 instantiation of A. */
14915 entering_scope = (template_parm_scope_p ()
14916 && cp_lexer_next_token_is (parser->lexer,
14917 CPP_SCOPE));
14918 template_id
14919 = finish_template_type (templ, arguments, entering_scope);
14921 /* A template-like identifier may be a partial concept id. */
14922 else if (flag_concepts
14923 && (template_id = (cp_parser_maybe_partial_concept_id
14924 (parser, templ, arguments))))
14925 return template_id;
14926 else if (variable_template_p (templ))
14928 template_id = lookup_template_variable (templ, arguments);
14929 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14930 SET_EXPR_LOCATION (template_id, next_token->location);
14932 else
14934 /* If it's not a class-template or a template-template, it should be
14935 a function-template. */
14936 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
14937 || TREE_CODE (templ) == OVERLOAD
14938 || BASELINK_P (templ)));
14940 template_id = lookup_template_function (templ, arguments);
14941 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14942 SET_EXPR_LOCATION (template_id, next_token->location);
14945 /* If parsing tentatively, replace the sequence of tokens that makes
14946 up the template-id with a CPP_TEMPLATE_ID token. That way,
14947 should we re-parse the token stream, we will not have to repeat
14948 the effort required to do the parse, nor will we issue duplicate
14949 error messages about problems during instantiation of the
14950 template. */
14951 if (start_of_id
14952 /* Don't do this if we had a parse error in a declarator; re-parsing
14953 might succeed if a name changes meaning (60361). */
14954 && !(cp_parser_error_occurred (parser)
14955 && cp_parser_parsing_tentatively (parser)
14956 && parser->in_declarator_p))
14958 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
14960 /* Reset the contents of the START_OF_ID token. */
14961 token->type = CPP_TEMPLATE_ID;
14963 /* Update the location to be of the form:
14964 template-name < template-argument-list [opt] >
14965 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14966 with caret == start at the start of the template-name,
14967 ranging until the closing '>'. */
14968 location_t finish_loc
14969 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
14970 location_t combined_loc
14971 = make_location (token->location, token->location, finish_loc);
14972 token->location = combined_loc;
14974 /* Retrieve any deferred checks. Do not pop this access checks yet
14975 so the memory will not be reclaimed during token replacing below. */
14976 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14977 token->u.tree_check_value->value = template_id;
14978 token->u.tree_check_value->checks = get_deferred_access_checks ();
14979 token->keyword = RID_MAX;
14981 /* Purge all subsequent tokens. */
14982 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
14984 /* ??? Can we actually assume that, if template_id ==
14985 error_mark_node, we will have issued a diagnostic to the
14986 user, as opposed to simply marking the tentative parse as
14987 failed? */
14988 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
14989 error_at (token->location, "parse error in template argument list");
14992 pop_to_parent_deferring_access_checks ();
14993 return template_id;
14996 /* Parse a template-name.
14998 template-name:
14999 identifier
15001 The standard should actually say:
15003 template-name:
15004 identifier
15005 operator-function-id
15007 A defect report has been filed about this issue.
15009 A conversion-function-id cannot be a template name because they cannot
15010 be part of a template-id. In fact, looking at this code:
15012 a.operator K<int>()
15014 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15015 It is impossible to call a templated conversion-function-id with an
15016 explicit argument list, since the only allowed template parameter is
15017 the type to which it is converting.
15019 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15020 `template' keyword, in a construction like:
15022 T::template f<3>()
15024 In that case `f' is taken to be a template-name, even though there
15025 is no way of knowing for sure.
15027 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15028 name refers to a set of overloaded functions, at least one of which
15029 is a template, or an IDENTIFIER_NODE with the name of the template,
15030 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15031 names are looked up inside uninstantiated templates. */
15033 static tree
15034 cp_parser_template_name (cp_parser* parser,
15035 bool template_keyword_p,
15036 bool check_dependency_p,
15037 bool is_declaration,
15038 enum tag_types tag_type,
15039 bool *is_identifier)
15041 tree identifier;
15042 tree decl;
15043 tree fns;
15044 cp_token *token = cp_lexer_peek_token (parser->lexer);
15046 /* If the next token is `operator', then we have either an
15047 operator-function-id or a conversion-function-id. */
15048 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15050 /* We don't know whether we're looking at an
15051 operator-function-id or a conversion-function-id. */
15052 cp_parser_parse_tentatively (parser);
15053 /* Try an operator-function-id. */
15054 identifier = cp_parser_operator_function_id (parser);
15055 /* If that didn't work, try a conversion-function-id. */
15056 if (!cp_parser_parse_definitely (parser))
15058 cp_parser_error (parser, "expected template-name");
15059 return error_mark_node;
15062 /* Look for the identifier. */
15063 else
15064 identifier = cp_parser_identifier (parser);
15066 /* If we didn't find an identifier, we don't have a template-id. */
15067 if (identifier == error_mark_node)
15068 return error_mark_node;
15070 /* If the name immediately followed the `template' keyword, then it
15071 is a template-name. However, if the next token is not `<', then
15072 we do not treat it as a template-name, since it is not being used
15073 as part of a template-id. This enables us to handle constructs
15074 like:
15076 template <typename T> struct S { S(); };
15077 template <typename T> S<T>::S();
15079 correctly. We would treat `S' as a template -- if it were `S<T>'
15080 -- but we do not if there is no `<'. */
15082 if (processing_template_decl
15083 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15085 /* In a declaration, in a dependent context, we pretend that the
15086 "template" keyword was present in order to improve error
15087 recovery. For example, given:
15089 template <typename T> void f(T::X<int>);
15091 we want to treat "X<int>" as a template-id. */
15092 if (is_declaration
15093 && !template_keyword_p
15094 && parser->scope && TYPE_P (parser->scope)
15095 && check_dependency_p
15096 && dependent_scope_p (parser->scope)
15097 /* Do not do this for dtors (or ctors), since they never
15098 need the template keyword before their name. */
15099 && !constructor_name_p (identifier, parser->scope))
15101 cp_token_position start = 0;
15103 /* Explain what went wrong. */
15104 error_at (token->location, "non-template %qD used as template",
15105 identifier);
15106 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15107 parser->scope, identifier);
15108 /* If parsing tentatively, find the location of the "<" token. */
15109 if (cp_parser_simulate_error (parser))
15110 start = cp_lexer_token_position (parser->lexer, true);
15111 /* Parse the template arguments so that we can issue error
15112 messages about them. */
15113 cp_lexer_consume_token (parser->lexer);
15114 cp_parser_enclosed_template_argument_list (parser);
15115 /* Skip tokens until we find a good place from which to
15116 continue parsing. */
15117 cp_parser_skip_to_closing_parenthesis (parser,
15118 /*recovering=*/true,
15119 /*or_comma=*/true,
15120 /*consume_paren=*/false);
15121 /* If parsing tentatively, permanently remove the
15122 template argument list. That will prevent duplicate
15123 error messages from being issued about the missing
15124 "template" keyword. */
15125 if (start)
15126 cp_lexer_purge_tokens_after (parser->lexer, start);
15127 if (is_identifier)
15128 *is_identifier = true;
15129 return identifier;
15132 /* If the "template" keyword is present, then there is generally
15133 no point in doing name-lookup, so we just return IDENTIFIER.
15134 But, if the qualifying scope is non-dependent then we can
15135 (and must) do name-lookup normally. */
15136 if (template_keyword_p
15137 && (!parser->scope
15138 || (TYPE_P (parser->scope)
15139 && dependent_type_p (parser->scope))))
15140 return identifier;
15143 /* Look up the name. */
15144 decl = cp_parser_lookup_name (parser, identifier,
15145 tag_type,
15146 /*is_template=*/true,
15147 /*is_namespace=*/false,
15148 check_dependency_p,
15149 /*ambiguous_decls=*/NULL,
15150 token->location);
15152 decl = strip_using_decl (decl);
15154 /* If DECL is a template, then the name was a template-name. */
15155 if (TREE_CODE (decl) == TEMPLATE_DECL)
15157 if (TREE_DEPRECATED (decl)
15158 && deprecated_state != DEPRECATED_SUPPRESS)
15159 warn_deprecated_use (decl, NULL_TREE);
15161 else
15163 tree fn = NULL_TREE;
15165 /* The standard does not explicitly indicate whether a name that
15166 names a set of overloaded declarations, some of which are
15167 templates, is a template-name. However, such a name should
15168 be a template-name; otherwise, there is no way to form a
15169 template-id for the overloaded templates. */
15170 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
15171 if (TREE_CODE (fns) == OVERLOAD)
15172 for (fn = fns; fn; fn = OVL_NEXT (fn))
15173 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
15174 break;
15176 if (!fn)
15178 /* The name does not name a template. */
15179 cp_parser_error (parser, "expected template-name");
15180 return error_mark_node;
15184 /* If DECL is dependent, and refers to a function, then just return
15185 its name; we will look it up again during template instantiation. */
15186 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15188 tree scope = ovl_scope (decl);
15189 if (TYPE_P (scope) && dependent_type_p (scope))
15190 return identifier;
15193 return decl;
15196 /* Parse a template-argument-list.
15198 template-argument-list:
15199 template-argument ... [opt]
15200 template-argument-list , template-argument ... [opt]
15202 Returns a TREE_VEC containing the arguments. */
15204 static tree
15205 cp_parser_template_argument_list (cp_parser* parser)
15207 tree fixed_args[10];
15208 unsigned n_args = 0;
15209 unsigned alloced = 10;
15210 tree *arg_ary = fixed_args;
15211 tree vec;
15212 bool saved_in_template_argument_list_p;
15213 bool saved_ice_p;
15214 bool saved_non_ice_p;
15216 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15217 parser->in_template_argument_list_p = true;
15218 /* Even if the template-id appears in an integral
15219 constant-expression, the contents of the argument list do
15220 not. */
15221 saved_ice_p = parser->integral_constant_expression_p;
15222 parser->integral_constant_expression_p = false;
15223 saved_non_ice_p = parser->non_integral_constant_expression_p;
15224 parser->non_integral_constant_expression_p = false;
15226 /* Parse the arguments. */
15229 tree argument;
15231 if (n_args)
15232 /* Consume the comma. */
15233 cp_lexer_consume_token (parser->lexer);
15235 /* Parse the template-argument. */
15236 argument = cp_parser_template_argument (parser);
15238 /* If the next token is an ellipsis, we're expanding a template
15239 argument pack. */
15240 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15242 if (argument == error_mark_node)
15244 cp_token *token = cp_lexer_peek_token (parser->lexer);
15245 error_at (token->location,
15246 "expected parameter pack before %<...%>");
15248 /* Consume the `...' token. */
15249 cp_lexer_consume_token (parser->lexer);
15251 /* Make the argument into a TYPE_PACK_EXPANSION or
15252 EXPR_PACK_EXPANSION. */
15253 argument = make_pack_expansion (argument);
15256 if (n_args == alloced)
15258 alloced *= 2;
15260 if (arg_ary == fixed_args)
15262 arg_ary = XNEWVEC (tree, alloced);
15263 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
15265 else
15266 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
15268 arg_ary[n_args++] = argument;
15270 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15272 vec = make_tree_vec (n_args);
15274 while (n_args--)
15275 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
15277 if (arg_ary != fixed_args)
15278 free (arg_ary);
15279 parser->non_integral_constant_expression_p = saved_non_ice_p;
15280 parser->integral_constant_expression_p = saved_ice_p;
15281 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
15282 if (CHECKING_P)
15283 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
15284 return vec;
15287 /* Parse a template-argument.
15289 template-argument:
15290 assignment-expression
15291 type-id
15292 id-expression
15294 The representation is that of an assignment-expression, type-id, or
15295 id-expression -- except that the qualified id-expression is
15296 evaluated, so that the value returned is either a DECL or an
15297 OVERLOAD.
15299 Although the standard says "assignment-expression", it forbids
15300 throw-expressions or assignments in the template argument.
15301 Therefore, we use "conditional-expression" instead. */
15303 static tree
15304 cp_parser_template_argument (cp_parser* parser)
15306 tree argument;
15307 bool template_p;
15308 bool address_p;
15309 bool maybe_type_id = false;
15310 cp_token *token = NULL, *argument_start_token = NULL;
15311 location_t loc = 0;
15312 cp_id_kind idk;
15314 /* There's really no way to know what we're looking at, so we just
15315 try each alternative in order.
15317 [temp.arg]
15319 In a template-argument, an ambiguity between a type-id and an
15320 expression is resolved to a type-id, regardless of the form of
15321 the corresponding template-parameter.
15323 Therefore, we try a type-id first. */
15324 cp_parser_parse_tentatively (parser);
15325 argument = cp_parser_template_type_arg (parser);
15326 /* If there was no error parsing the type-id but the next token is a
15327 '>>', our behavior depends on which dialect of C++ we're
15328 parsing. In C++98, we probably found a typo for '> >'. But there
15329 are type-id which are also valid expressions. For instance:
15331 struct X { int operator >> (int); };
15332 template <int V> struct Foo {};
15333 Foo<X () >> 5> r;
15335 Here 'X()' is a valid type-id of a function type, but the user just
15336 wanted to write the expression "X() >> 5". Thus, we remember that we
15337 found a valid type-id, but we still try to parse the argument as an
15338 expression to see what happens.
15340 In C++0x, the '>>' will be considered two separate '>'
15341 tokens. */
15342 if (!cp_parser_error_occurred (parser)
15343 && cxx_dialect == cxx98
15344 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15346 maybe_type_id = true;
15347 cp_parser_abort_tentative_parse (parser);
15349 else
15351 /* If the next token isn't a `,' or a `>', then this argument wasn't
15352 really finished. This means that the argument is not a valid
15353 type-id. */
15354 if (!cp_parser_next_token_ends_template_argument_p (parser))
15355 cp_parser_error (parser, "expected template-argument");
15356 /* If that worked, we're done. */
15357 if (cp_parser_parse_definitely (parser))
15358 return argument;
15360 /* We're still not sure what the argument will be. */
15361 cp_parser_parse_tentatively (parser);
15362 /* Try a template. */
15363 argument_start_token = cp_lexer_peek_token (parser->lexer);
15364 argument = cp_parser_id_expression (parser,
15365 /*template_keyword_p=*/false,
15366 /*check_dependency_p=*/true,
15367 &template_p,
15368 /*declarator_p=*/false,
15369 /*optional_p=*/false);
15370 /* If the next token isn't a `,' or a `>', then this argument wasn't
15371 really finished. */
15372 if (!cp_parser_next_token_ends_template_argument_p (parser))
15373 cp_parser_error (parser, "expected template-argument");
15374 if (!cp_parser_error_occurred (parser))
15376 /* Figure out what is being referred to. If the id-expression
15377 was for a class template specialization, then we will have a
15378 TYPE_DECL at this point. There is no need to do name lookup
15379 at this point in that case. */
15380 if (TREE_CODE (argument) != TYPE_DECL)
15381 argument = cp_parser_lookup_name (parser, argument,
15382 none_type,
15383 /*is_template=*/template_p,
15384 /*is_namespace=*/false,
15385 /*check_dependency=*/true,
15386 /*ambiguous_decls=*/NULL,
15387 argument_start_token->location);
15388 /* Handle a constrained-type-specifier for a non-type template
15389 parameter. */
15390 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
15391 argument = decl;
15392 else if (TREE_CODE (argument) != TEMPLATE_DECL
15393 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
15394 cp_parser_error (parser, "expected template-name");
15396 if (cp_parser_parse_definitely (parser))
15398 if (TREE_DEPRECATED (argument))
15399 warn_deprecated_use (argument, NULL_TREE);
15400 return argument;
15402 /* It must be a non-type argument. In C++17 any constant-expression is
15403 allowed. */
15404 if (cxx_dialect > cxx14)
15405 goto general_expr;
15407 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
15409 -- an integral constant-expression of integral or enumeration
15410 type; or
15412 -- the name of a non-type template-parameter; or
15414 -- the name of an object or function with external linkage...
15416 -- the address of an object or function with external linkage...
15418 -- a pointer to member... */
15419 /* Look for a non-type template parameter. */
15420 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15422 cp_parser_parse_tentatively (parser);
15423 argument = cp_parser_primary_expression (parser,
15424 /*address_p=*/false,
15425 /*cast_p=*/false,
15426 /*template_arg_p=*/true,
15427 &idk);
15428 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
15429 || !cp_parser_next_token_ends_template_argument_p (parser))
15430 cp_parser_simulate_error (parser);
15431 if (cp_parser_parse_definitely (parser))
15432 return argument;
15435 /* If the next token is "&", the argument must be the address of an
15436 object or function with external linkage. */
15437 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
15438 if (address_p)
15440 loc = cp_lexer_peek_token (parser->lexer)->location;
15441 cp_lexer_consume_token (parser->lexer);
15443 /* See if we might have an id-expression. */
15444 token = cp_lexer_peek_token (parser->lexer);
15445 if (token->type == CPP_NAME
15446 || token->keyword == RID_OPERATOR
15447 || token->type == CPP_SCOPE
15448 || token->type == CPP_TEMPLATE_ID
15449 || token->type == CPP_NESTED_NAME_SPECIFIER)
15451 cp_parser_parse_tentatively (parser);
15452 argument = cp_parser_primary_expression (parser,
15453 address_p,
15454 /*cast_p=*/false,
15455 /*template_arg_p=*/true,
15456 &idk);
15457 if (cp_parser_error_occurred (parser)
15458 || !cp_parser_next_token_ends_template_argument_p (parser))
15459 cp_parser_abort_tentative_parse (parser);
15460 else
15462 tree probe;
15464 if (INDIRECT_REF_P (argument))
15466 /* Strip the dereference temporarily. */
15467 gcc_assert (REFERENCE_REF_P (argument));
15468 argument = TREE_OPERAND (argument, 0);
15471 /* If we're in a template, we represent a qualified-id referring
15472 to a static data member as a SCOPE_REF even if the scope isn't
15473 dependent so that we can check access control later. */
15474 probe = argument;
15475 if (TREE_CODE (probe) == SCOPE_REF)
15476 probe = TREE_OPERAND (probe, 1);
15477 if (VAR_P (probe))
15479 /* A variable without external linkage might still be a
15480 valid constant-expression, so no error is issued here
15481 if the external-linkage check fails. */
15482 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
15483 cp_parser_simulate_error (parser);
15485 else if (is_overloaded_fn (argument))
15486 /* All overloaded functions are allowed; if the external
15487 linkage test does not pass, an error will be issued
15488 later. */
15490 else if (address_p
15491 && (TREE_CODE (argument) == OFFSET_REF
15492 || TREE_CODE (argument) == SCOPE_REF))
15493 /* A pointer-to-member. */
15495 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
15497 else
15498 cp_parser_simulate_error (parser);
15500 if (cp_parser_parse_definitely (parser))
15502 if (address_p)
15503 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
15504 tf_warning_or_error);
15505 else
15506 argument = convert_from_reference (argument);
15507 return argument;
15511 /* If the argument started with "&", there are no other valid
15512 alternatives at this point. */
15513 if (address_p)
15515 cp_parser_error (parser, "invalid non-type template argument");
15516 return error_mark_node;
15519 general_expr:
15520 /* If the argument wasn't successfully parsed as a type-id followed
15521 by '>>', the argument can only be a constant expression now.
15522 Otherwise, we try parsing the constant-expression tentatively,
15523 because the argument could really be a type-id. */
15524 if (maybe_type_id)
15525 cp_parser_parse_tentatively (parser);
15527 if (cxx_dialect <= cxx14)
15528 argument = cp_parser_constant_expression (parser);
15529 else
15531 /* With C++17 generalized non-type template arguments we need to handle
15532 lvalue constant expressions, too. */
15533 argument = cp_parser_assignment_expression (parser);
15534 require_potential_constant_expression (argument);
15537 if (!maybe_type_id)
15538 return argument;
15539 if (!cp_parser_next_token_ends_template_argument_p (parser))
15540 cp_parser_error (parser, "expected template-argument");
15541 if (cp_parser_parse_definitely (parser))
15542 return argument;
15543 /* We did our best to parse the argument as a non type-id, but that
15544 was the only alternative that matched (albeit with a '>' after
15545 it). We can assume it's just a typo from the user, and a
15546 diagnostic will then be issued. */
15547 return cp_parser_template_type_arg (parser);
15550 /* Parse an explicit-instantiation.
15552 explicit-instantiation:
15553 template declaration
15555 Although the standard says `declaration', what it really means is:
15557 explicit-instantiation:
15558 template decl-specifier-seq [opt] declarator [opt] ;
15560 Things like `template int S<int>::i = 5, int S<double>::j;' are not
15561 supposed to be allowed. A defect report has been filed about this
15562 issue.
15564 GNU Extension:
15566 explicit-instantiation:
15567 storage-class-specifier template
15568 decl-specifier-seq [opt] declarator [opt] ;
15569 function-specifier template
15570 decl-specifier-seq [opt] declarator [opt] ; */
15572 static void
15573 cp_parser_explicit_instantiation (cp_parser* parser)
15575 int declares_class_or_enum;
15576 cp_decl_specifier_seq decl_specifiers;
15577 tree extension_specifier = NULL_TREE;
15579 timevar_push (TV_TEMPLATE_INST);
15581 /* Look for an (optional) storage-class-specifier or
15582 function-specifier. */
15583 if (cp_parser_allow_gnu_extensions_p (parser))
15585 extension_specifier
15586 = cp_parser_storage_class_specifier_opt (parser);
15587 if (!extension_specifier)
15588 extension_specifier
15589 = cp_parser_function_specifier_opt (parser,
15590 /*decl_specs=*/NULL);
15593 /* Look for the `template' keyword. */
15594 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15595 /* Let the front end know that we are processing an explicit
15596 instantiation. */
15597 begin_explicit_instantiation ();
15598 /* [temp.explicit] says that we are supposed to ignore access
15599 control while processing explicit instantiation directives. */
15600 push_deferring_access_checks (dk_no_check);
15601 /* Parse a decl-specifier-seq. */
15602 cp_parser_decl_specifier_seq (parser,
15603 CP_PARSER_FLAGS_OPTIONAL,
15604 &decl_specifiers,
15605 &declares_class_or_enum);
15606 /* If there was exactly one decl-specifier, and it declared a class,
15607 and there's no declarator, then we have an explicit type
15608 instantiation. */
15609 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
15611 tree type;
15613 type = check_tag_decl (&decl_specifiers,
15614 /*explicit_type_instantiation_p=*/true);
15615 /* Turn access control back on for names used during
15616 template instantiation. */
15617 pop_deferring_access_checks ();
15618 if (type)
15619 do_type_instantiation (type, extension_specifier,
15620 /*complain=*/tf_error);
15622 else
15624 cp_declarator *declarator;
15625 tree decl;
15627 /* Parse the declarator. */
15628 declarator
15629 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15630 /*ctor_dtor_or_conv_p=*/NULL,
15631 /*parenthesized_p=*/NULL,
15632 /*member_p=*/false,
15633 /*friend_p=*/false);
15634 if (declares_class_or_enum & 2)
15635 cp_parser_check_for_definition_in_return_type (declarator,
15636 decl_specifiers.type,
15637 decl_specifiers.locations[ds_type_spec]);
15638 if (declarator != cp_error_declarator)
15640 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
15641 permerror (decl_specifiers.locations[ds_inline],
15642 "explicit instantiation shall not use"
15643 " %<inline%> specifier");
15644 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
15645 permerror (decl_specifiers.locations[ds_constexpr],
15646 "explicit instantiation shall not use"
15647 " %<constexpr%> specifier");
15649 decl = grokdeclarator (declarator, &decl_specifiers,
15650 NORMAL, 0, &decl_specifiers.attributes);
15651 /* Turn access control back on for names used during
15652 template instantiation. */
15653 pop_deferring_access_checks ();
15654 /* Do the explicit instantiation. */
15655 do_decl_instantiation (decl, extension_specifier);
15657 else
15659 pop_deferring_access_checks ();
15660 /* Skip the body of the explicit instantiation. */
15661 cp_parser_skip_to_end_of_statement (parser);
15664 /* We're done with the instantiation. */
15665 end_explicit_instantiation ();
15667 cp_parser_consume_semicolon_at_end_of_statement (parser);
15669 timevar_pop (TV_TEMPLATE_INST);
15672 /* Parse an explicit-specialization.
15674 explicit-specialization:
15675 template < > declaration
15677 Although the standard says `declaration', what it really means is:
15679 explicit-specialization:
15680 template <> decl-specifier [opt] init-declarator [opt] ;
15681 template <> function-definition
15682 template <> explicit-specialization
15683 template <> template-declaration */
15685 static void
15686 cp_parser_explicit_specialization (cp_parser* parser)
15688 bool need_lang_pop;
15689 cp_token *token = cp_lexer_peek_token (parser->lexer);
15691 /* Look for the `template' keyword. */
15692 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15693 /* Look for the `<'. */
15694 cp_parser_require (parser, CPP_LESS, RT_LESS);
15695 /* Look for the `>'. */
15696 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15697 /* We have processed another parameter list. */
15698 ++parser->num_template_parameter_lists;
15699 /* [temp]
15701 A template ... explicit specialization ... shall not have C
15702 linkage. */
15703 if (current_lang_name == lang_name_c)
15705 error_at (token->location, "template specialization with C linkage");
15706 /* Give it C++ linkage to avoid confusing other parts of the
15707 front end. */
15708 push_lang_context (lang_name_cplusplus);
15709 need_lang_pop = true;
15711 else
15712 need_lang_pop = false;
15713 /* Let the front end know that we are beginning a specialization. */
15714 if (!begin_specialization ())
15716 end_specialization ();
15717 return;
15720 /* If the next keyword is `template', we need to figure out whether
15721 or not we're looking a template-declaration. */
15722 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15724 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15725 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
15726 cp_parser_template_declaration_after_export (parser,
15727 /*member_p=*/false);
15728 else
15729 cp_parser_explicit_specialization (parser);
15731 else
15732 /* Parse the dependent declaration. */
15733 cp_parser_single_declaration (parser,
15734 /*checks=*/NULL,
15735 /*member_p=*/false,
15736 /*explicit_specialization_p=*/true,
15737 /*friend_p=*/NULL);
15738 /* We're done with the specialization. */
15739 end_specialization ();
15740 /* For the erroneous case of a template with C linkage, we pushed an
15741 implicit C++ linkage scope; exit that scope now. */
15742 if (need_lang_pop)
15743 pop_lang_context ();
15744 /* We're done with this parameter list. */
15745 --parser->num_template_parameter_lists;
15748 /* Parse a type-specifier.
15750 type-specifier:
15751 simple-type-specifier
15752 class-specifier
15753 enum-specifier
15754 elaborated-type-specifier
15755 cv-qualifier
15757 GNU Extension:
15759 type-specifier:
15760 __complex__
15762 Returns a representation of the type-specifier. For a
15763 class-specifier, enum-specifier, or elaborated-type-specifier, a
15764 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
15766 The parser flags FLAGS is used to control type-specifier parsing.
15768 If IS_DECLARATION is TRUE, then this type-specifier is appearing
15769 in a decl-specifier-seq.
15771 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
15772 class-specifier, enum-specifier, or elaborated-type-specifier, then
15773 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
15774 if a type is declared; 2 if it is defined. Otherwise, it is set to
15775 zero.
15777 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
15778 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
15779 is set to FALSE. */
15781 static tree
15782 cp_parser_type_specifier (cp_parser* parser,
15783 cp_parser_flags flags,
15784 cp_decl_specifier_seq *decl_specs,
15785 bool is_declaration,
15786 int* declares_class_or_enum,
15787 bool* is_cv_qualifier)
15789 tree type_spec = NULL_TREE;
15790 cp_token *token;
15791 enum rid keyword;
15792 cp_decl_spec ds = ds_last;
15794 /* Assume this type-specifier does not declare a new type. */
15795 if (declares_class_or_enum)
15796 *declares_class_or_enum = 0;
15797 /* And that it does not specify a cv-qualifier. */
15798 if (is_cv_qualifier)
15799 *is_cv_qualifier = false;
15800 /* Peek at the next token. */
15801 token = cp_lexer_peek_token (parser->lexer);
15803 /* If we're looking at a keyword, we can use that to guide the
15804 production we choose. */
15805 keyword = token->keyword;
15806 switch (keyword)
15808 case RID_ENUM:
15809 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15810 goto elaborated_type_specifier;
15812 /* Look for the enum-specifier. */
15813 type_spec = cp_parser_enum_specifier (parser);
15814 /* If that worked, we're done. */
15815 if (type_spec)
15817 if (declares_class_or_enum)
15818 *declares_class_or_enum = 2;
15819 if (decl_specs)
15820 cp_parser_set_decl_spec_type (decl_specs,
15821 type_spec,
15822 token,
15823 /*type_definition_p=*/true);
15824 return type_spec;
15826 else
15827 goto elaborated_type_specifier;
15829 /* Any of these indicate either a class-specifier, or an
15830 elaborated-type-specifier. */
15831 case RID_CLASS:
15832 case RID_STRUCT:
15833 case RID_UNION:
15834 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15835 goto elaborated_type_specifier;
15837 /* Parse tentatively so that we can back up if we don't find a
15838 class-specifier. */
15839 cp_parser_parse_tentatively (parser);
15840 /* Look for the class-specifier. */
15841 type_spec = cp_parser_class_specifier (parser);
15842 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
15843 /* If that worked, we're done. */
15844 if (cp_parser_parse_definitely (parser))
15846 if (declares_class_or_enum)
15847 *declares_class_or_enum = 2;
15848 if (decl_specs)
15849 cp_parser_set_decl_spec_type (decl_specs,
15850 type_spec,
15851 token,
15852 /*type_definition_p=*/true);
15853 return type_spec;
15856 /* Fall through. */
15857 elaborated_type_specifier:
15858 /* We're declaring (not defining) a class or enum. */
15859 if (declares_class_or_enum)
15860 *declares_class_or_enum = 1;
15862 /* Fall through. */
15863 case RID_TYPENAME:
15864 /* Look for an elaborated-type-specifier. */
15865 type_spec
15866 = (cp_parser_elaborated_type_specifier
15867 (parser,
15868 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
15869 is_declaration));
15870 if (decl_specs)
15871 cp_parser_set_decl_spec_type (decl_specs,
15872 type_spec,
15873 token,
15874 /*type_definition_p=*/false);
15875 return type_spec;
15877 case RID_CONST:
15878 ds = ds_const;
15879 if (is_cv_qualifier)
15880 *is_cv_qualifier = true;
15881 break;
15883 case RID_VOLATILE:
15884 ds = ds_volatile;
15885 if (is_cv_qualifier)
15886 *is_cv_qualifier = true;
15887 break;
15889 case RID_RESTRICT:
15890 ds = ds_restrict;
15891 if (is_cv_qualifier)
15892 *is_cv_qualifier = true;
15893 break;
15895 case RID_COMPLEX:
15896 /* The `__complex__' keyword is a GNU extension. */
15897 ds = ds_complex;
15898 break;
15900 default:
15901 break;
15904 /* Handle simple keywords. */
15905 if (ds != ds_last)
15907 if (decl_specs)
15909 set_and_check_decl_spec_loc (decl_specs, ds, token);
15910 decl_specs->any_specifiers_p = true;
15912 return cp_lexer_consume_token (parser->lexer)->u.value;
15915 /* If we do not already have a type-specifier, assume we are looking
15916 at a simple-type-specifier. */
15917 type_spec = cp_parser_simple_type_specifier (parser,
15918 decl_specs,
15919 flags);
15921 /* If we didn't find a type-specifier, and a type-specifier was not
15922 optional in this context, issue an error message. */
15923 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15925 cp_parser_error (parser, "expected type specifier");
15926 return error_mark_node;
15929 return type_spec;
15932 /* Parse a simple-type-specifier.
15934 simple-type-specifier:
15935 :: [opt] nested-name-specifier [opt] type-name
15936 :: [opt] nested-name-specifier template template-id
15937 char
15938 wchar_t
15939 bool
15940 short
15942 long
15943 signed
15944 unsigned
15945 float
15946 double
15947 void
15949 C++0x Extension:
15951 simple-type-specifier:
15952 auto
15953 decltype ( expression )
15954 char16_t
15955 char32_t
15956 __underlying_type ( type-id )
15958 GNU Extension:
15960 simple-type-specifier:
15961 __int128
15962 __typeof__ unary-expression
15963 __typeof__ ( type-id )
15964 __typeof__ ( type-id ) { initializer-list , [opt] }
15966 Concepts Extension:
15968 simple-type-specifier:
15969 constrained-type-specifier
15971 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
15972 appropriately updated. */
15974 static tree
15975 cp_parser_simple_type_specifier (cp_parser* parser,
15976 cp_decl_specifier_seq *decl_specs,
15977 cp_parser_flags flags)
15979 tree type = NULL_TREE;
15980 cp_token *token;
15981 int idx;
15983 /* Peek at the next token. */
15984 token = cp_lexer_peek_token (parser->lexer);
15986 /* If we're looking at a keyword, things are easy. */
15987 switch (token->keyword)
15989 case RID_CHAR:
15990 if (decl_specs)
15991 decl_specs->explicit_char_p = true;
15992 type = char_type_node;
15993 break;
15994 case RID_CHAR16:
15995 type = char16_type_node;
15996 break;
15997 case RID_CHAR32:
15998 type = char32_type_node;
15999 break;
16000 case RID_WCHAR:
16001 type = wchar_type_node;
16002 break;
16003 case RID_BOOL:
16004 type = boolean_type_node;
16005 break;
16006 case RID_SHORT:
16007 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16008 type = short_integer_type_node;
16009 break;
16010 case RID_INT:
16011 if (decl_specs)
16012 decl_specs->explicit_int_p = true;
16013 type = integer_type_node;
16014 break;
16015 case RID_INT_N_0:
16016 case RID_INT_N_1:
16017 case RID_INT_N_2:
16018 case RID_INT_N_3:
16019 idx = token->keyword - RID_INT_N_0;
16020 if (! int_n_enabled_p [idx])
16021 break;
16022 if (decl_specs)
16024 decl_specs->explicit_intN_p = true;
16025 decl_specs->int_n_idx = idx;
16027 type = int_n_trees [idx].signed_type;
16028 break;
16029 case RID_LONG:
16030 if (decl_specs)
16031 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16032 type = long_integer_type_node;
16033 break;
16034 case RID_SIGNED:
16035 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16036 type = integer_type_node;
16037 break;
16038 case RID_UNSIGNED:
16039 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16040 type = unsigned_type_node;
16041 break;
16042 case RID_FLOAT:
16043 type = float_type_node;
16044 break;
16045 case RID_DOUBLE:
16046 type = double_type_node;
16047 break;
16048 case RID_VOID:
16049 type = void_type_node;
16050 break;
16052 case RID_AUTO:
16053 maybe_warn_cpp0x (CPP0X_AUTO);
16054 if (parser->auto_is_implicit_function_template_parm_p)
16056 /* The 'auto' might be the placeholder return type for a function decl
16057 with trailing return type. */
16058 bool have_trailing_return_fn_decl = false;
16060 cp_parser_parse_tentatively (parser);
16061 cp_lexer_consume_token (parser->lexer);
16062 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16063 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16064 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16065 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16067 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16069 cp_lexer_consume_token (parser->lexer);
16070 cp_parser_skip_to_closing_parenthesis (parser,
16071 /*recovering*/false,
16072 /*or_comma*/false,
16073 /*consume_paren*/true);
16074 continue;
16077 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16079 have_trailing_return_fn_decl = true;
16080 break;
16083 cp_lexer_consume_token (parser->lexer);
16085 cp_parser_abort_tentative_parse (parser);
16087 if (have_trailing_return_fn_decl)
16089 type = make_auto ();
16090 break;
16093 if (cxx_dialect >= cxx14)
16095 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16096 type = TREE_TYPE (type);
16098 else
16099 type = error_mark_node;
16101 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16103 if (cxx_dialect < cxx14)
16104 error_at (token->location,
16105 "use of %<auto%> in lambda parameter declaration "
16106 "only available with "
16107 "-std=c++14 or -std=gnu++14");
16109 else if (cxx_dialect < cxx14)
16110 error_at (token->location,
16111 "use of %<auto%> in parameter declaration "
16112 "only available with "
16113 "-std=c++14 or -std=gnu++14");
16114 else if (!flag_concepts)
16115 pedwarn (token->location, OPT_Wpedantic,
16116 "ISO C++ forbids use of %<auto%> in parameter "
16117 "declaration");
16119 else
16120 type = make_auto ();
16121 break;
16123 case RID_DECLTYPE:
16124 /* Since DR 743, decltype can either be a simple-type-specifier by
16125 itself or begin a nested-name-specifier. Parsing it will replace
16126 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16127 handling below decide what to do. */
16128 cp_parser_decltype (parser);
16129 cp_lexer_set_token_position (parser->lexer, token);
16130 break;
16132 case RID_TYPEOF:
16133 /* Consume the `typeof' token. */
16134 cp_lexer_consume_token (parser->lexer);
16135 /* Parse the operand to `typeof'. */
16136 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16137 /* If it is not already a TYPE, take its type. */
16138 if (!TYPE_P (type))
16139 type = finish_typeof (type);
16141 if (decl_specs)
16142 cp_parser_set_decl_spec_type (decl_specs, type,
16143 token,
16144 /*type_definition_p=*/false);
16146 return type;
16148 case RID_UNDERLYING_TYPE:
16149 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16150 if (decl_specs)
16151 cp_parser_set_decl_spec_type (decl_specs, type,
16152 token,
16153 /*type_definition_p=*/false);
16155 return type;
16157 case RID_BASES:
16158 case RID_DIRECT_BASES:
16159 type = cp_parser_trait_expr (parser, token->keyword);
16160 if (decl_specs)
16161 cp_parser_set_decl_spec_type (decl_specs, type,
16162 token,
16163 /*type_definition_p=*/false);
16164 return type;
16165 default:
16166 break;
16169 /* If token is an already-parsed decltype not followed by ::,
16170 it's a simple-type-specifier. */
16171 if (token->type == CPP_DECLTYPE
16172 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16174 type = saved_checks_value (token->u.tree_check_value);
16175 if (decl_specs)
16177 cp_parser_set_decl_spec_type (decl_specs, type,
16178 token,
16179 /*type_definition_p=*/false);
16180 /* Remember that we are handling a decltype in order to
16181 implement the resolution of DR 1510 when the argument
16182 isn't instantiation dependent. */
16183 decl_specs->decltype_p = true;
16185 cp_lexer_consume_token (parser->lexer);
16186 return type;
16189 /* If the type-specifier was for a built-in type, we're done. */
16190 if (type)
16192 /* Record the type. */
16193 if (decl_specs
16194 && (token->keyword != RID_SIGNED
16195 && token->keyword != RID_UNSIGNED
16196 && token->keyword != RID_SHORT
16197 && token->keyword != RID_LONG))
16198 cp_parser_set_decl_spec_type (decl_specs,
16199 type,
16200 token,
16201 /*type_definition_p=*/false);
16202 if (decl_specs)
16203 decl_specs->any_specifiers_p = true;
16205 /* Consume the token. */
16206 cp_lexer_consume_token (parser->lexer);
16208 if (type == error_mark_node)
16209 return error_mark_node;
16211 /* There is no valid C++ program where a non-template type is
16212 followed by a "<". That usually indicates that the user thought
16213 that the type was a template. */
16214 cp_parser_check_for_invalid_template_id (parser, type, none_type,
16215 token->location);
16217 return TYPE_NAME (type);
16220 /* The type-specifier must be a user-defined type. */
16221 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16223 bool qualified_p;
16224 bool global_p;
16226 /* Don't gobble tokens or issue error messages if this is an
16227 optional type-specifier. */
16228 if (flags & CP_PARSER_FLAGS_OPTIONAL)
16229 cp_parser_parse_tentatively (parser);
16231 /* Look for the optional `::' operator. */
16232 global_p
16233 = (cp_parser_global_scope_opt (parser,
16234 /*current_scope_valid_p=*/false)
16235 != NULL_TREE);
16236 /* Look for the nested-name specifier. */
16237 qualified_p
16238 = (cp_parser_nested_name_specifier_opt (parser,
16239 /*typename_keyword_p=*/false,
16240 /*check_dependency_p=*/true,
16241 /*type_p=*/false,
16242 /*is_declaration=*/false)
16243 != NULL_TREE);
16244 token = cp_lexer_peek_token (parser->lexer);
16245 /* If we have seen a nested-name-specifier, and the next token
16246 is `template', then we are using the template-id production. */
16247 if (parser->scope
16248 && cp_parser_optional_template_keyword (parser))
16250 /* Look for the template-id. */
16251 type = cp_parser_template_id (parser,
16252 /*template_keyword_p=*/true,
16253 /*check_dependency_p=*/true,
16254 none_type,
16255 /*is_declaration=*/false);
16256 /* If the template-id did not name a type, we are out of
16257 luck. */
16258 if (TREE_CODE (type) != TYPE_DECL)
16260 cp_parser_error (parser, "expected template-id for type");
16261 type = NULL_TREE;
16264 /* Otherwise, look for a type-name. */
16265 else
16266 type = cp_parser_type_name (parser);
16267 /* Keep track of all name-lookups performed in class scopes. */
16268 if (type
16269 && !global_p
16270 && !qualified_p
16271 && TREE_CODE (type) == TYPE_DECL
16272 && identifier_p (DECL_NAME (type)))
16273 maybe_note_name_used_in_class (DECL_NAME (type), type);
16274 /* If it didn't work out, we don't have a TYPE. */
16275 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
16276 && !cp_parser_parse_definitely (parser))
16277 type = NULL_TREE;
16278 if (type && decl_specs)
16279 cp_parser_set_decl_spec_type (decl_specs, type,
16280 token,
16281 /*type_definition_p=*/false);
16284 /* If we didn't get a type-name, issue an error message. */
16285 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16287 cp_parser_error (parser, "expected type-name");
16288 return error_mark_node;
16291 if (type && type != error_mark_node)
16293 /* See if TYPE is an Objective-C type, and if so, parse and
16294 accept any protocol references following it. Do this before
16295 the cp_parser_check_for_invalid_template_id() call, because
16296 Objective-C types can be followed by '<...>' which would
16297 enclose protocol names rather than template arguments, and so
16298 everything is fine. */
16299 if (c_dialect_objc () && !parser->scope
16300 && (objc_is_id (type) || objc_is_class_name (type)))
16302 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16303 tree qual_type = objc_get_protocol_qualified_type (type, protos);
16305 /* Clobber the "unqualified" type previously entered into
16306 DECL_SPECS with the new, improved protocol-qualified version. */
16307 if (decl_specs)
16308 decl_specs->type = qual_type;
16310 return qual_type;
16313 /* There is no valid C++ program where a non-template type is
16314 followed by a "<". That usually indicates that the user
16315 thought that the type was a template. */
16316 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
16317 none_type,
16318 token->location);
16321 return type;
16324 /* Parse a type-name.
16326 type-name:
16327 class-name
16328 enum-name
16329 typedef-name
16330 simple-template-id [in c++0x]
16332 enum-name:
16333 identifier
16335 typedef-name:
16336 identifier
16338 Concepts:
16340 type-name:
16341 concept-name
16342 partial-concept-id
16344 concept-name:
16345 identifier
16347 Returns a TYPE_DECL for the type. */
16349 static tree
16350 cp_parser_type_name (cp_parser* parser)
16352 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
16355 /* See above. */
16356 static tree
16357 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
16359 tree type_decl;
16361 /* We can't know yet whether it is a class-name or not. */
16362 cp_parser_parse_tentatively (parser);
16363 /* Try a class-name. */
16364 type_decl = cp_parser_class_name (parser,
16365 typename_keyword_p,
16366 /*template_keyword_p=*/false,
16367 none_type,
16368 /*check_dependency_p=*/true,
16369 /*class_head_p=*/false,
16370 /*is_declaration=*/false);
16371 /* If it's not a class-name, keep looking. */
16372 if (!cp_parser_parse_definitely (parser))
16374 if (cxx_dialect < cxx11)
16375 /* It must be a typedef-name or an enum-name. */
16376 return cp_parser_nonclass_name (parser);
16378 cp_parser_parse_tentatively (parser);
16379 /* It is either a simple-template-id representing an
16380 instantiation of an alias template... */
16381 type_decl = cp_parser_template_id (parser,
16382 /*template_keyword_p=*/false,
16383 /*check_dependency_p=*/true,
16384 none_type,
16385 /*is_declaration=*/false);
16386 /* Note that this must be an instantiation of an alias template
16387 because [temp.names]/6 says:
16389 A template-id that names an alias template specialization
16390 is a type-name.
16392 Whereas [temp.names]/7 says:
16394 A simple-template-id that names a class template
16395 specialization is a class-name.
16397 With concepts, this could also be a partial-concept-id that
16398 declares a non-type template parameter. */
16399 if (type_decl != NULL_TREE
16400 && TREE_CODE (type_decl) == TYPE_DECL
16401 && TYPE_DECL_ALIAS_P (type_decl))
16402 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
16403 else if (is_constrained_parameter (type_decl))
16404 /* Don't do anything. */ ;
16405 else
16406 cp_parser_simulate_error (parser);
16408 if (!cp_parser_parse_definitely (parser))
16409 /* ... Or a typedef-name or an enum-name. */
16410 return cp_parser_nonclass_name (parser);
16413 return type_decl;
16416 /* Check if DECL and ARGS can form a constrained-type-specifier.
16417 If ARGS is non-null, we try to form a concept check of the
16418 form DECL<?, ARGS> where ? is a wildcard that matches any
16419 kind of template argument. If ARGS is NULL, then we try to
16420 form a concept check of the form DECL<?>. */
16422 static tree
16423 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
16424 tree decl, tree args)
16426 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
16428 /* If we a constrained-type-specifier cannot be deduced. */
16429 if (parser->prevent_constrained_type_specifiers)
16430 return NULL_TREE;
16432 /* A constrained type specifier can only be found in an
16433 overload set or as a reference to a template declaration.
16435 FIXME: This might be masking a bug. It's possible that
16436 that the deduction below is causing template specializations
16437 to be formed with the wildcard as an argument. */
16438 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
16439 return NULL_TREE;
16441 /* Try to build a call expression that evaluates the
16442 concept. This can fail if the overload set refers
16443 only to non-templates. */
16444 tree placeholder = build_nt (WILDCARD_DECL);
16445 tree check = build_concept_check (decl, placeholder, args);
16446 if (check == error_mark_node)
16447 return NULL_TREE;
16449 /* Deduce the checked constraint and the prototype parameter.
16451 FIXME: In certain cases, failure to deduce should be a
16452 diagnosable error. */
16453 tree conc;
16454 tree proto;
16455 if (!deduce_constrained_parameter (check, conc, proto))
16456 return NULL_TREE;
16458 /* In template parameter scope, this results in a constrained
16459 parameter. Return a descriptor of that parm. */
16460 if (processing_template_parmlist)
16461 return build_constrained_parameter (conc, proto, args);
16463 /* In a parameter-declaration-clause, constrained-type
16464 specifiers result in invented template parameters. */
16465 if (parser->auto_is_implicit_function_template_parm_p)
16467 tree x = build_constrained_parameter (conc, proto, args);
16468 return synthesize_implicit_template_parm (parser, x);
16470 else
16472 /* Otherwise, we're in a context where the constrained
16473 type name is deduced and the constraint applies
16474 after deduction. */
16475 return make_constrained_auto (conc, args);
16478 return NULL_TREE;
16481 /* If DECL refers to a concept, return a TYPE_DECL representing
16482 the result of using the constrained type specifier in the
16483 current context. DECL refers to a concept if
16485 - it is an overload set containing a function concept taking a single
16486 type argument, or
16488 - it is a variable concept taking a single type argument. */
16490 static tree
16491 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
16493 if (flag_concepts
16494 && (TREE_CODE (decl) == OVERLOAD
16495 || BASELINK_P (decl)
16496 || variable_concept_p (decl)))
16497 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
16498 else
16499 return NULL_TREE;
16502 /* Check if DECL and ARGS form a partial-concept-id. If so,
16503 assign ID to the resulting constrained placeholder.
16505 Returns true if the partial-concept-id designates a placeholder
16506 and false otherwise. Note that *id is set to NULL_TREE in
16507 this case. */
16509 static tree
16510 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
16512 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
16515 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
16516 or a concept-name.
16518 enum-name:
16519 identifier
16521 typedef-name:
16522 identifier
16524 concept-name:
16525 identifier
16527 Returns a TYPE_DECL for the type. */
16529 static tree
16530 cp_parser_nonclass_name (cp_parser* parser)
16532 tree type_decl;
16533 tree identifier;
16535 cp_token *token = cp_lexer_peek_token (parser->lexer);
16536 identifier = cp_parser_identifier (parser);
16537 if (identifier == error_mark_node)
16538 return error_mark_node;
16540 /* Look up the type-name. */
16541 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
16543 type_decl = strip_using_decl (type_decl);
16545 /* If we found an overload set, then it may refer to a concept-name. */
16546 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
16547 type_decl = decl;
16549 if (TREE_CODE (type_decl) != TYPE_DECL
16550 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
16552 /* See if this is an Objective-C type. */
16553 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16554 tree type = objc_get_protocol_qualified_type (identifier, protos);
16555 if (type)
16556 type_decl = TYPE_NAME (type);
16559 /* Issue an error if we did not find a type-name. */
16560 if (TREE_CODE (type_decl) != TYPE_DECL
16561 /* In Objective-C, we have the complication that class names are
16562 normally type names and start declarations (eg, the
16563 "NSObject" in "NSObject *object;"), but can be used in an
16564 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
16565 is an expression. So, a classname followed by a dot is not a
16566 valid type-name. */
16567 || (objc_is_class_name (TREE_TYPE (type_decl))
16568 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
16570 if (!cp_parser_simulate_error (parser))
16571 cp_parser_name_lookup_error (parser, identifier, type_decl,
16572 NLE_TYPE, token->location);
16573 return error_mark_node;
16575 /* Remember that the name was used in the definition of the
16576 current class so that we can check later to see if the
16577 meaning would have been different after the class was
16578 entirely defined. */
16579 else if (type_decl != error_mark_node
16580 && !parser->scope)
16581 maybe_note_name_used_in_class (identifier, type_decl);
16583 return type_decl;
16586 /* Parse an elaborated-type-specifier. Note that the grammar given
16587 here incorporates the resolution to DR68.
16589 elaborated-type-specifier:
16590 class-key :: [opt] nested-name-specifier [opt] identifier
16591 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
16592 enum-key :: [opt] nested-name-specifier [opt] identifier
16593 typename :: [opt] nested-name-specifier identifier
16594 typename :: [opt] nested-name-specifier template [opt]
16595 template-id
16597 GNU extension:
16599 elaborated-type-specifier:
16600 class-key attributes :: [opt] nested-name-specifier [opt] identifier
16601 class-key attributes :: [opt] nested-name-specifier [opt]
16602 template [opt] template-id
16603 enum attributes :: [opt] nested-name-specifier [opt] identifier
16605 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
16606 declared `friend'. If IS_DECLARATION is TRUE, then this
16607 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
16608 something is being declared.
16610 Returns the TYPE specified. */
16612 static tree
16613 cp_parser_elaborated_type_specifier (cp_parser* parser,
16614 bool is_friend,
16615 bool is_declaration)
16617 enum tag_types tag_type;
16618 tree identifier;
16619 tree type = NULL_TREE;
16620 tree attributes = NULL_TREE;
16621 tree globalscope;
16622 cp_token *token = NULL;
16624 /* See if we're looking at the `enum' keyword. */
16625 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
16627 /* Consume the `enum' token. */
16628 cp_lexer_consume_token (parser->lexer);
16629 /* Remember that it's an enumeration type. */
16630 tag_type = enum_type;
16631 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
16632 enums) is used here. */
16633 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
16634 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
16636 pedwarn (input_location, 0, "elaborated-type-specifier "
16637 "for a scoped enum must not use the %<%D%> keyword",
16638 cp_lexer_peek_token (parser->lexer)->u.value);
16639 /* Consume the `struct' or `class' and parse it anyway. */
16640 cp_lexer_consume_token (parser->lexer);
16642 /* Parse the attributes. */
16643 attributes = cp_parser_attributes_opt (parser);
16645 /* Or, it might be `typename'. */
16646 else if (cp_lexer_next_token_is_keyword (parser->lexer,
16647 RID_TYPENAME))
16649 /* Consume the `typename' token. */
16650 cp_lexer_consume_token (parser->lexer);
16651 /* Remember that it's a `typename' type. */
16652 tag_type = typename_type;
16654 /* Otherwise it must be a class-key. */
16655 else
16657 tag_type = cp_parser_class_key (parser);
16658 if (tag_type == none_type)
16659 return error_mark_node;
16660 /* Parse the attributes. */
16661 attributes = cp_parser_attributes_opt (parser);
16664 /* Look for the `::' operator. */
16665 globalscope = cp_parser_global_scope_opt (parser,
16666 /*current_scope_valid_p=*/false);
16667 /* Look for the nested-name-specifier. */
16668 if (tag_type == typename_type && !globalscope)
16670 if (!cp_parser_nested_name_specifier (parser,
16671 /*typename_keyword_p=*/true,
16672 /*check_dependency_p=*/true,
16673 /*type_p=*/true,
16674 is_declaration))
16675 return error_mark_node;
16677 else
16678 /* Even though `typename' is not present, the proposed resolution
16679 to Core Issue 180 says that in `class A<T>::B', `B' should be
16680 considered a type-name, even if `A<T>' is dependent. */
16681 cp_parser_nested_name_specifier_opt (parser,
16682 /*typename_keyword_p=*/true,
16683 /*check_dependency_p=*/true,
16684 /*type_p=*/true,
16685 is_declaration);
16686 /* For everything but enumeration types, consider a template-id.
16687 For an enumeration type, consider only a plain identifier. */
16688 if (tag_type != enum_type)
16690 bool template_p = false;
16691 tree decl;
16693 /* Allow the `template' keyword. */
16694 template_p = cp_parser_optional_template_keyword (parser);
16695 /* If we didn't see `template', we don't know if there's a
16696 template-id or not. */
16697 if (!template_p)
16698 cp_parser_parse_tentatively (parser);
16699 /* Parse the template-id. */
16700 token = cp_lexer_peek_token (parser->lexer);
16701 decl = cp_parser_template_id (parser, template_p,
16702 /*check_dependency_p=*/true,
16703 tag_type,
16704 is_declaration);
16705 /* If we didn't find a template-id, look for an ordinary
16706 identifier. */
16707 if (!template_p && !cp_parser_parse_definitely (parser))
16709 /* We can get here when cp_parser_template_id, called by
16710 cp_parser_class_name with tag_type == none_type, succeeds
16711 and caches a BASELINK. Then, when called again here,
16712 instead of failing and returning an error_mark_node
16713 returns it (see template/typename17.C in C++11).
16714 ??? Could we diagnose this earlier? */
16715 else if (tag_type == typename_type && BASELINK_P (decl))
16717 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
16718 type = error_mark_node;
16720 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
16721 in effect, then we must assume that, upon instantiation, the
16722 template will correspond to a class. */
16723 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16724 && tag_type == typename_type)
16725 type = make_typename_type (parser->scope, decl,
16726 typename_type,
16727 /*complain=*/tf_error);
16728 /* If the `typename' keyword is in effect and DECL is not a type
16729 decl, then type is non existent. */
16730 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
16732 else if (TREE_CODE (decl) == TYPE_DECL)
16733 type = check_elaborated_type_specifier (tag_type, decl,
16734 /*allow_template_p=*/true);
16735 else if (decl == error_mark_node)
16736 type = error_mark_node;
16739 if (!type)
16741 token = cp_lexer_peek_token (parser->lexer);
16742 identifier = cp_parser_identifier (parser);
16744 if (identifier == error_mark_node)
16746 parser->scope = NULL_TREE;
16747 return error_mark_node;
16750 /* For a `typename', we needn't call xref_tag. */
16751 if (tag_type == typename_type
16752 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
16753 return cp_parser_make_typename_type (parser, identifier,
16754 token->location);
16756 /* Template parameter lists apply only if we are not within a
16757 function parameter list. */
16758 bool template_parm_lists_apply
16759 = parser->num_template_parameter_lists;
16760 if (template_parm_lists_apply)
16761 for (cp_binding_level *s = current_binding_level;
16762 s && s->kind != sk_template_parms;
16763 s = s->level_chain)
16764 if (s->kind == sk_function_parms)
16765 template_parm_lists_apply = false;
16767 /* Look up a qualified name in the usual way. */
16768 if (parser->scope)
16770 tree decl;
16771 tree ambiguous_decls;
16773 decl = cp_parser_lookup_name (parser, identifier,
16774 tag_type,
16775 /*is_template=*/false,
16776 /*is_namespace=*/false,
16777 /*check_dependency=*/true,
16778 &ambiguous_decls,
16779 token->location);
16781 /* If the lookup was ambiguous, an error will already have been
16782 issued. */
16783 if (ambiguous_decls)
16784 return error_mark_node;
16786 /* If we are parsing friend declaration, DECL may be a
16787 TEMPLATE_DECL tree node here. However, we need to check
16788 whether this TEMPLATE_DECL results in valid code. Consider
16789 the following example:
16791 namespace N {
16792 template <class T> class C {};
16794 class X {
16795 template <class T> friend class N::C; // #1, valid code
16797 template <class T> class Y {
16798 friend class N::C; // #2, invalid code
16801 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
16802 name lookup of `N::C'. We see that friend declaration must
16803 be template for the code to be valid. Note that
16804 processing_template_decl does not work here since it is
16805 always 1 for the above two cases. */
16807 decl = (cp_parser_maybe_treat_template_as_class
16808 (decl, /*tag_name_p=*/is_friend
16809 && template_parm_lists_apply));
16811 if (TREE_CODE (decl) != TYPE_DECL)
16813 cp_parser_diagnose_invalid_type_name (parser,
16814 identifier,
16815 token->location);
16816 return error_mark_node;
16819 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
16821 bool allow_template = (template_parm_lists_apply
16822 || DECL_SELF_REFERENCE_P (decl));
16823 type = check_elaborated_type_specifier (tag_type, decl,
16824 allow_template);
16826 if (type == error_mark_node)
16827 return error_mark_node;
16830 /* Forward declarations of nested types, such as
16832 class C1::C2;
16833 class C1::C2::C3;
16835 are invalid unless all components preceding the final '::'
16836 are complete. If all enclosing types are complete, these
16837 declarations become merely pointless.
16839 Invalid forward declarations of nested types are errors
16840 caught elsewhere in parsing. Those that are pointless arrive
16841 here. */
16843 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16844 && !is_friend && !processing_explicit_instantiation)
16845 warning (0, "declaration %qD does not declare anything", decl);
16847 type = TREE_TYPE (decl);
16849 else
16851 /* An elaborated-type-specifier sometimes introduces a new type and
16852 sometimes names an existing type. Normally, the rule is that it
16853 introduces a new type only if there is not an existing type of
16854 the same name already in scope. For example, given:
16856 struct S {};
16857 void f() { struct S s; }
16859 the `struct S' in the body of `f' is the same `struct S' as in
16860 the global scope; the existing definition is used. However, if
16861 there were no global declaration, this would introduce a new
16862 local class named `S'.
16864 An exception to this rule applies to the following code:
16866 namespace N { struct S; }
16868 Here, the elaborated-type-specifier names a new type
16869 unconditionally; even if there is already an `S' in the
16870 containing scope this declaration names a new type.
16871 This exception only applies if the elaborated-type-specifier
16872 forms the complete declaration:
16874 [class.name]
16876 A declaration consisting solely of `class-key identifier ;' is
16877 either a redeclaration of the name in the current scope or a
16878 forward declaration of the identifier as a class name. It
16879 introduces the name into the current scope.
16881 We are in this situation precisely when the next token is a `;'.
16883 An exception to the exception is that a `friend' declaration does
16884 *not* name a new type; i.e., given:
16886 struct S { friend struct T; };
16888 `T' is not a new type in the scope of `S'.
16890 Also, `new struct S' or `sizeof (struct S)' never results in the
16891 definition of a new type; a new type can only be declared in a
16892 declaration context. */
16894 tag_scope ts;
16895 bool template_p;
16897 if (is_friend)
16898 /* Friends have special name lookup rules. */
16899 ts = ts_within_enclosing_non_class;
16900 else if (is_declaration
16901 && cp_lexer_next_token_is (parser->lexer,
16902 CPP_SEMICOLON))
16903 /* This is a `class-key identifier ;' */
16904 ts = ts_current;
16905 else
16906 ts = ts_global;
16908 template_p =
16909 (template_parm_lists_apply
16910 && (cp_parser_next_token_starts_class_definition_p (parser)
16911 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
16912 /* An unqualified name was used to reference this type, so
16913 there were no qualifying templates. */
16914 if (template_parm_lists_apply
16915 && !cp_parser_check_template_parameters (parser,
16916 /*num_templates=*/0,
16917 token->location,
16918 /*declarator=*/NULL))
16919 return error_mark_node;
16920 type = xref_tag (tag_type, identifier, ts, template_p);
16924 if (type == error_mark_node)
16925 return error_mark_node;
16927 /* Allow attributes on forward declarations of classes. */
16928 if (attributes)
16930 if (TREE_CODE (type) == TYPENAME_TYPE)
16931 warning (OPT_Wattributes,
16932 "attributes ignored on uninstantiated type");
16933 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
16934 && ! processing_explicit_instantiation)
16935 warning (OPT_Wattributes,
16936 "attributes ignored on template instantiation");
16937 else if (is_declaration && cp_parser_declares_only_class_p (parser))
16938 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
16939 else
16940 warning (OPT_Wattributes,
16941 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
16944 if (tag_type != enum_type)
16946 /* Indicate whether this class was declared as a `class' or as a
16947 `struct'. */
16948 if (CLASS_TYPE_P (type))
16949 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
16950 cp_parser_check_class_key (tag_type, type);
16953 /* A "<" cannot follow an elaborated type specifier. If that
16954 happens, the user was probably trying to form a template-id. */
16955 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
16956 token->location);
16958 return type;
16961 /* Parse an enum-specifier.
16963 enum-specifier:
16964 enum-head { enumerator-list [opt] }
16965 enum-head { enumerator-list , } [C++0x]
16967 enum-head:
16968 enum-key identifier [opt] enum-base [opt]
16969 enum-key nested-name-specifier identifier enum-base [opt]
16971 enum-key:
16972 enum
16973 enum class [C++0x]
16974 enum struct [C++0x]
16976 enum-base: [C++0x]
16977 : type-specifier-seq
16979 opaque-enum-specifier:
16980 enum-key identifier enum-base [opt] ;
16982 GNU Extensions:
16983 enum-key attributes[opt] identifier [opt] enum-base [opt]
16984 { enumerator-list [opt] }attributes[opt]
16985 enum-key attributes[opt] identifier [opt] enum-base [opt]
16986 { enumerator-list, }attributes[opt] [C++0x]
16988 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
16989 if the token stream isn't an enum-specifier after all. */
16991 static tree
16992 cp_parser_enum_specifier (cp_parser* parser)
16994 tree identifier;
16995 tree type = NULL_TREE;
16996 tree prev_scope;
16997 tree nested_name_specifier = NULL_TREE;
16998 tree attributes;
16999 bool scoped_enum_p = false;
17000 bool has_underlying_type = false;
17001 bool nested_being_defined = false;
17002 bool new_value_list = false;
17003 bool is_new_type = false;
17004 bool is_anonymous = false;
17005 tree underlying_type = NULL_TREE;
17006 cp_token *type_start_token = NULL;
17007 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17009 parser->colon_corrects_to_scope_p = false;
17011 /* Parse tentatively so that we can back up if we don't find a
17012 enum-specifier. */
17013 cp_parser_parse_tentatively (parser);
17015 /* Caller guarantees that the current token is 'enum', an identifier
17016 possibly follows, and the token after that is an opening brace.
17017 If we don't have an identifier, fabricate an anonymous name for
17018 the enumeration being defined. */
17019 cp_lexer_consume_token (parser->lexer);
17021 /* Parse the "class" or "struct", which indicates a scoped
17022 enumeration type in C++0x. */
17023 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17024 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17026 if (cxx_dialect < cxx11)
17027 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17029 /* Consume the `struct' or `class' token. */
17030 cp_lexer_consume_token (parser->lexer);
17032 scoped_enum_p = true;
17035 attributes = cp_parser_attributes_opt (parser);
17037 /* Clear the qualification. */
17038 parser->scope = NULL_TREE;
17039 parser->qualifying_scope = NULL_TREE;
17040 parser->object_scope = NULL_TREE;
17042 /* Figure out in what scope the declaration is being placed. */
17043 prev_scope = current_scope ();
17045 type_start_token = cp_lexer_peek_token (parser->lexer);
17047 push_deferring_access_checks (dk_no_check);
17048 nested_name_specifier
17049 = cp_parser_nested_name_specifier_opt (parser,
17050 /*typename_keyword_p=*/true,
17051 /*check_dependency_p=*/false,
17052 /*type_p=*/false,
17053 /*is_declaration=*/false);
17055 if (nested_name_specifier)
17057 tree name;
17059 identifier = cp_parser_identifier (parser);
17060 name = cp_parser_lookup_name (parser, identifier,
17061 enum_type,
17062 /*is_template=*/false,
17063 /*is_namespace=*/false,
17064 /*check_dependency=*/true,
17065 /*ambiguous_decls=*/NULL,
17066 input_location);
17067 if (name && name != error_mark_node)
17069 type = TREE_TYPE (name);
17070 if (TREE_CODE (type) == TYPENAME_TYPE)
17072 /* Are template enums allowed in ISO? */
17073 if (template_parm_scope_p ())
17074 pedwarn (type_start_token->location, OPT_Wpedantic,
17075 "%qD is an enumeration template", name);
17076 /* ignore a typename reference, for it will be solved by name
17077 in start_enum. */
17078 type = NULL_TREE;
17081 else if (nested_name_specifier == error_mark_node)
17082 /* We already issued an error. */;
17083 else
17085 error_at (type_start_token->location,
17086 "%qD does not name an enumeration in %qT",
17087 identifier, nested_name_specifier);
17088 nested_name_specifier = error_mark_node;
17091 else
17093 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17094 identifier = cp_parser_identifier (parser);
17095 else
17097 identifier = make_anon_name ();
17098 is_anonymous = true;
17099 if (scoped_enum_p)
17100 error_at (type_start_token->location,
17101 "anonymous scoped enum is not allowed");
17104 pop_deferring_access_checks ();
17106 /* Check for the `:' that denotes a specified underlying type in C++0x.
17107 Note that a ':' could also indicate a bitfield width, however. */
17108 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17110 cp_decl_specifier_seq type_specifiers;
17112 /* Consume the `:'. */
17113 cp_lexer_consume_token (parser->lexer);
17115 /* Parse the type-specifier-seq. */
17116 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17117 /*is_trailing_return=*/false,
17118 &type_specifiers);
17120 /* At this point this is surely not elaborated type specifier. */
17121 if (!cp_parser_parse_definitely (parser))
17122 return NULL_TREE;
17124 if (cxx_dialect < cxx11)
17125 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17127 has_underlying_type = true;
17129 /* If that didn't work, stop. */
17130 if (type_specifiers.type != error_mark_node)
17132 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17133 /*initialized=*/0, NULL);
17134 if (underlying_type == error_mark_node
17135 || check_for_bare_parameter_packs (underlying_type))
17136 underlying_type = NULL_TREE;
17140 /* Look for the `{' but don't consume it yet. */
17141 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17143 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17145 cp_parser_error (parser, "expected %<{%>");
17146 if (has_underlying_type)
17148 type = NULL_TREE;
17149 goto out;
17152 /* An opaque-enum-specifier must have a ';' here. */
17153 if ((scoped_enum_p || underlying_type)
17154 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17156 cp_parser_error (parser, "expected %<;%> or %<{%>");
17157 if (has_underlying_type)
17159 type = NULL_TREE;
17160 goto out;
17165 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17166 return NULL_TREE;
17168 if (nested_name_specifier)
17170 if (CLASS_TYPE_P (nested_name_specifier))
17172 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
17173 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
17174 push_scope (nested_name_specifier);
17176 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17178 push_nested_namespace (nested_name_specifier);
17182 /* Issue an error message if type-definitions are forbidden here. */
17183 if (!cp_parser_check_type_definition (parser))
17184 type = error_mark_node;
17185 else
17186 /* Create the new type. We do this before consuming the opening
17187 brace so the enum will be recorded as being on the line of its
17188 tag (or the 'enum' keyword, if there is no tag). */
17189 type = start_enum (identifier, type, underlying_type,
17190 attributes, scoped_enum_p, &is_new_type);
17192 /* If the next token is not '{' it is an opaque-enum-specifier or an
17193 elaborated-type-specifier. */
17194 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17196 timevar_push (TV_PARSE_ENUM);
17197 if (nested_name_specifier
17198 && nested_name_specifier != error_mark_node)
17200 /* The following catches invalid code such as:
17201 enum class S<int>::E { A, B, C }; */
17202 if (!processing_specialization
17203 && CLASS_TYPE_P (nested_name_specifier)
17204 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
17205 error_at (type_start_token->location, "cannot add an enumerator "
17206 "list to a template instantiation");
17208 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
17210 error_at (type_start_token->location,
17211 "%<%T::%E%> has not been declared",
17212 TYPE_CONTEXT (nested_name_specifier),
17213 nested_name_specifier);
17214 type = error_mark_node;
17216 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
17217 && !CLASS_TYPE_P (nested_name_specifier))
17219 error_at (type_start_token->location, "nested name specifier "
17220 "%qT for enum declaration does not name a class "
17221 "or namespace", nested_name_specifier);
17222 type = error_mark_node;
17224 /* If that scope does not contain the scope in which the
17225 class was originally declared, the program is invalid. */
17226 else if (prev_scope && !is_ancestor (prev_scope,
17227 nested_name_specifier))
17229 if (at_namespace_scope_p ())
17230 error_at (type_start_token->location,
17231 "declaration of %qD in namespace %qD which does not "
17232 "enclose %qD",
17233 type, prev_scope, nested_name_specifier);
17234 else
17235 error_at (type_start_token->location,
17236 "declaration of %qD in %qD which does not "
17237 "enclose %qD",
17238 type, prev_scope, nested_name_specifier);
17239 type = error_mark_node;
17241 /* If that scope is the scope where the declaration is being placed
17242 the program is invalid. */
17243 else if (CLASS_TYPE_P (nested_name_specifier)
17244 && CLASS_TYPE_P (prev_scope)
17245 && same_type_p (nested_name_specifier, prev_scope))
17247 permerror (type_start_token->location,
17248 "extra qualification not allowed");
17249 nested_name_specifier = NULL_TREE;
17253 if (scoped_enum_p)
17254 begin_scope (sk_scoped_enum, type);
17256 /* Consume the opening brace. */
17257 cp_lexer_consume_token (parser->lexer);
17259 if (type == error_mark_node)
17260 ; /* Nothing to add */
17261 else if (OPAQUE_ENUM_P (type)
17262 || (cxx_dialect > cxx98 && processing_specialization))
17264 new_value_list = true;
17265 SET_OPAQUE_ENUM_P (type, false);
17266 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17268 else
17270 error_at (type_start_token->location,
17271 "multiple definition of %q#T", type);
17272 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
17273 "previous definition here");
17274 type = error_mark_node;
17277 if (type == error_mark_node)
17278 cp_parser_skip_to_end_of_block_or_statement (parser);
17279 /* If the next token is not '}', then there are some enumerators. */
17280 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17282 if (is_anonymous && !scoped_enum_p)
17283 pedwarn (type_start_token->location, OPT_Wpedantic,
17284 "ISO C++ forbids empty anonymous enum");
17286 else
17287 cp_parser_enumerator_list (parser, type);
17289 /* Consume the final '}'. */
17290 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17292 if (scoped_enum_p)
17293 finish_scope ();
17294 timevar_pop (TV_PARSE_ENUM);
17296 else
17298 /* If a ';' follows, then it is an opaque-enum-specifier
17299 and additional restrictions apply. */
17300 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17302 if (is_anonymous)
17303 error_at (type_start_token->location,
17304 "opaque-enum-specifier without name");
17305 else if (nested_name_specifier)
17306 error_at (type_start_token->location,
17307 "opaque-enum-specifier must use a simple identifier");
17311 /* Look for trailing attributes to apply to this enumeration, and
17312 apply them if appropriate. */
17313 if (cp_parser_allow_gnu_extensions_p (parser))
17315 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
17316 cplus_decl_attributes (&type,
17317 trailing_attr,
17318 (int) ATTR_FLAG_TYPE_IN_PLACE);
17321 /* Finish up the enumeration. */
17322 if (type != error_mark_node)
17324 if (new_value_list)
17325 finish_enum_value_list (type);
17326 if (is_new_type)
17327 finish_enum (type);
17330 if (nested_name_specifier)
17332 if (CLASS_TYPE_P (nested_name_specifier))
17334 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
17335 pop_scope (nested_name_specifier);
17337 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17339 pop_nested_namespace (nested_name_specifier);
17342 out:
17343 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17344 return type;
17347 /* Parse an enumerator-list. The enumerators all have the indicated
17348 TYPE.
17350 enumerator-list:
17351 enumerator-definition
17352 enumerator-list , enumerator-definition */
17354 static void
17355 cp_parser_enumerator_list (cp_parser* parser, tree type)
17357 while (true)
17359 /* Parse an enumerator-definition. */
17360 cp_parser_enumerator_definition (parser, type);
17362 /* If the next token is not a ',', we've reached the end of
17363 the list. */
17364 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17365 break;
17366 /* Otherwise, consume the `,' and keep going. */
17367 cp_lexer_consume_token (parser->lexer);
17368 /* If the next token is a `}', there is a trailing comma. */
17369 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17371 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
17372 pedwarn (input_location, OPT_Wpedantic,
17373 "comma at end of enumerator list");
17374 break;
17379 /* Parse an enumerator-definition. The enumerator has the indicated
17380 TYPE.
17382 enumerator-definition:
17383 enumerator
17384 enumerator = constant-expression
17386 enumerator:
17387 identifier
17389 GNU Extensions:
17391 enumerator-definition:
17392 enumerator attributes [opt]
17393 enumerator attributes [opt] = constant-expression */
17395 static void
17396 cp_parser_enumerator_definition (cp_parser* parser, tree type)
17398 tree identifier;
17399 tree value;
17400 location_t loc;
17402 /* Save the input location because we are interested in the location
17403 of the identifier and not the location of the explicit value. */
17404 loc = cp_lexer_peek_token (parser->lexer)->location;
17406 /* Look for the identifier. */
17407 identifier = cp_parser_identifier (parser);
17408 if (identifier == error_mark_node)
17409 return;
17411 /* Parse any specified attributes. */
17412 tree attrs = cp_parser_attributes_opt (parser);
17414 /* If the next token is an '=', then there is an explicit value. */
17415 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17417 /* Consume the `=' token. */
17418 cp_lexer_consume_token (parser->lexer);
17419 /* Parse the value. */
17420 value = cp_parser_constant_expression (parser);
17422 else
17423 value = NULL_TREE;
17425 /* If we are processing a template, make sure the initializer of the
17426 enumerator doesn't contain any bare template parameter pack. */
17427 if (check_for_bare_parameter_packs (value))
17428 value = error_mark_node;
17430 /* Create the enumerator. */
17431 build_enumerator (identifier, value, type, attrs, loc);
17434 /* Parse a namespace-name.
17436 namespace-name:
17437 original-namespace-name
17438 namespace-alias
17440 Returns the NAMESPACE_DECL for the namespace. */
17442 static tree
17443 cp_parser_namespace_name (cp_parser* parser)
17445 tree identifier;
17446 tree namespace_decl;
17448 cp_token *token = cp_lexer_peek_token (parser->lexer);
17450 /* Get the name of the namespace. */
17451 identifier = cp_parser_identifier (parser);
17452 if (identifier == error_mark_node)
17453 return error_mark_node;
17455 /* Look up the identifier in the currently active scope. Look only
17456 for namespaces, due to:
17458 [basic.lookup.udir]
17460 When looking up a namespace-name in a using-directive or alias
17461 definition, only namespace names are considered.
17463 And:
17465 [basic.lookup.qual]
17467 During the lookup of a name preceding the :: scope resolution
17468 operator, object, function, and enumerator names are ignored.
17470 (Note that cp_parser_qualifying_entity only calls this
17471 function if the token after the name is the scope resolution
17472 operator.) */
17473 namespace_decl = cp_parser_lookup_name (parser, identifier,
17474 none_type,
17475 /*is_template=*/false,
17476 /*is_namespace=*/true,
17477 /*check_dependency=*/true,
17478 /*ambiguous_decls=*/NULL,
17479 token->location);
17480 /* If it's not a namespace, issue an error. */
17481 if (namespace_decl == error_mark_node
17482 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
17484 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17485 error_at (token->location, "%qD is not a namespace-name", identifier);
17486 cp_parser_error (parser, "expected namespace-name");
17487 namespace_decl = error_mark_node;
17490 return namespace_decl;
17493 /* Parse a namespace-definition.
17495 namespace-definition:
17496 named-namespace-definition
17497 unnamed-namespace-definition
17499 named-namespace-definition:
17500 original-namespace-definition
17501 extension-namespace-definition
17503 original-namespace-definition:
17504 namespace identifier { namespace-body }
17506 extension-namespace-definition:
17507 namespace original-namespace-name { namespace-body }
17509 unnamed-namespace-definition:
17510 namespace { namespace-body } */
17512 static void
17513 cp_parser_namespace_definition (cp_parser* parser)
17515 tree identifier, attribs;
17516 bool has_visibility;
17517 bool is_inline;
17518 cp_token* token;
17519 int nested_definition_count = 0;
17521 cp_ensure_no_omp_declare_simd (parser);
17522 cp_ensure_no_oacc_routine (parser);
17523 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
17525 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
17526 is_inline = true;
17527 cp_lexer_consume_token (parser->lexer);
17529 else
17530 is_inline = false;
17532 /* Look for the `namespace' keyword. */
17533 token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17535 /* Parse any specified attributes before the identifier. */
17536 attribs = cp_parser_attributes_opt (parser);
17538 /* Get the name of the namespace. We do not attempt to distinguish
17539 between an original-namespace-definition and an
17540 extension-namespace-definition at this point. The semantic
17541 analysis routines are responsible for that. */
17542 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17543 identifier = cp_parser_identifier (parser);
17544 else
17545 identifier = NULL_TREE;
17547 /* Parse any specified attributes after the identifier. */
17548 tree post_ident_attribs = cp_parser_attributes_opt (parser);
17549 if (post_ident_attribs)
17551 if (attribs)
17552 attribs = chainon (attribs, post_ident_attribs);
17553 else
17554 attribs = post_ident_attribs;
17557 /* Start the namespace. */
17558 bool ok = push_namespace (identifier);
17560 /* Parse any nested namespace definition. */
17561 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17563 if (attribs)
17564 error_at (token->location, "a nested namespace definition cannot have attributes");
17565 if (cxx_dialect < cxx1z)
17566 pedwarn (input_location, OPT_Wpedantic,
17567 "nested namespace definitions only available with "
17568 "-std=c++1z or -std=gnu++1z");
17569 if (is_inline)
17570 error_at (token->location, "a nested namespace definition cannot be inline");
17571 while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17573 cp_lexer_consume_token (parser->lexer);
17574 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17575 identifier = cp_parser_identifier (parser);
17576 else
17578 cp_parser_error (parser, "nested identifier required");
17579 break;
17581 ++nested_definition_count;
17582 push_namespace (identifier);
17586 /* Look for the `{' to validate starting the namespace. */
17587 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
17589 /* "inline namespace" is equivalent to a stub namespace definition
17590 followed by a strong using directive. */
17591 if (is_inline && ok)
17593 tree name_space = current_namespace;
17594 /* Set up namespace association. */
17595 DECL_NAMESPACE_ASSOCIATIONS (name_space)
17596 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
17597 DECL_NAMESPACE_ASSOCIATIONS (name_space));
17598 /* Import the contents of the inline namespace. */
17599 pop_namespace ();
17600 do_using_directive (name_space);
17601 push_namespace (identifier);
17604 has_visibility = handle_namespace_attrs (current_namespace, attribs);
17606 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
17608 /* Parse the body of the namespace. */
17609 cp_parser_namespace_body (parser);
17611 if (has_visibility)
17612 pop_visibility (1);
17614 /* Finish the nested namespace definitions. */
17615 while (nested_definition_count--)
17616 pop_namespace ();
17618 /* Finish the namespace. */
17619 if (ok)
17620 pop_namespace ();
17621 /* Look for the final `}'. */
17622 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17625 /* Parse a namespace-body.
17627 namespace-body:
17628 declaration-seq [opt] */
17630 static void
17631 cp_parser_namespace_body (cp_parser* parser)
17633 cp_parser_declaration_seq_opt (parser);
17636 /* Parse a namespace-alias-definition.
17638 namespace-alias-definition:
17639 namespace identifier = qualified-namespace-specifier ; */
17641 static void
17642 cp_parser_namespace_alias_definition (cp_parser* parser)
17644 tree identifier;
17645 tree namespace_specifier;
17647 cp_token *token = cp_lexer_peek_token (parser->lexer);
17649 /* Look for the `namespace' keyword. */
17650 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17651 /* Look for the identifier. */
17652 identifier = cp_parser_identifier (parser);
17653 if (identifier == error_mark_node)
17654 return;
17655 /* Look for the `=' token. */
17656 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
17657 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17659 error_at (token->location, "%<namespace%> definition is not allowed here");
17660 /* Skip the definition. */
17661 cp_lexer_consume_token (parser->lexer);
17662 if (cp_parser_skip_to_closing_brace (parser))
17663 cp_lexer_consume_token (parser->lexer);
17664 return;
17666 cp_parser_require (parser, CPP_EQ, RT_EQ);
17667 /* Look for the qualified-namespace-specifier. */
17668 namespace_specifier
17669 = cp_parser_qualified_namespace_specifier (parser);
17670 /* Look for the `;' token. */
17671 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17673 /* Register the alias in the symbol table. */
17674 do_namespace_alias (identifier, namespace_specifier);
17677 /* Parse a qualified-namespace-specifier.
17679 qualified-namespace-specifier:
17680 :: [opt] nested-name-specifier [opt] namespace-name
17682 Returns a NAMESPACE_DECL corresponding to the specified
17683 namespace. */
17685 static tree
17686 cp_parser_qualified_namespace_specifier (cp_parser* parser)
17688 /* Look for the optional `::'. */
17689 cp_parser_global_scope_opt (parser,
17690 /*current_scope_valid_p=*/false);
17692 /* Look for the optional nested-name-specifier. */
17693 cp_parser_nested_name_specifier_opt (parser,
17694 /*typename_keyword_p=*/false,
17695 /*check_dependency_p=*/true,
17696 /*type_p=*/false,
17697 /*is_declaration=*/true);
17699 return cp_parser_namespace_name (parser);
17702 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
17703 access declaration.
17705 using-declaration:
17706 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
17707 using :: unqualified-id ;
17709 access-declaration:
17710 qualified-id ;
17714 static bool
17715 cp_parser_using_declaration (cp_parser* parser,
17716 bool access_declaration_p)
17718 cp_token *token;
17719 bool typename_p = false;
17720 bool global_scope_p;
17721 tree decl;
17722 tree identifier;
17723 tree qscope;
17724 int oldcount = errorcount;
17725 cp_token *diag_token = NULL;
17727 if (access_declaration_p)
17729 diag_token = cp_lexer_peek_token (parser->lexer);
17730 cp_parser_parse_tentatively (parser);
17732 else
17734 /* Look for the `using' keyword. */
17735 cp_parser_require_keyword (parser, RID_USING, RT_USING);
17737 /* Peek at the next token. */
17738 token = cp_lexer_peek_token (parser->lexer);
17739 /* See if it's `typename'. */
17740 if (token->keyword == RID_TYPENAME)
17742 /* Remember that we've seen it. */
17743 typename_p = true;
17744 /* Consume the `typename' token. */
17745 cp_lexer_consume_token (parser->lexer);
17749 /* Look for the optional global scope qualification. */
17750 global_scope_p
17751 = (cp_parser_global_scope_opt (parser,
17752 /*current_scope_valid_p=*/false)
17753 != NULL_TREE);
17755 /* If we saw `typename', or didn't see `::', then there must be a
17756 nested-name-specifier present. */
17757 if (typename_p || !global_scope_p)
17759 qscope = cp_parser_nested_name_specifier (parser, typename_p,
17760 /*check_dependency_p=*/true,
17761 /*type_p=*/false,
17762 /*is_declaration=*/true);
17763 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
17765 cp_parser_skip_to_end_of_block_or_statement (parser);
17766 return false;
17769 /* Otherwise, we could be in either of the two productions. In that
17770 case, treat the nested-name-specifier as optional. */
17771 else
17772 qscope = cp_parser_nested_name_specifier_opt (parser,
17773 /*typename_keyword_p=*/false,
17774 /*check_dependency_p=*/true,
17775 /*type_p=*/false,
17776 /*is_declaration=*/true);
17777 if (!qscope)
17778 qscope = global_namespace;
17779 else if (UNSCOPED_ENUM_P (qscope))
17780 qscope = CP_TYPE_CONTEXT (qscope);
17782 if (access_declaration_p && cp_parser_error_occurred (parser))
17783 /* Something has already gone wrong; there's no need to parse
17784 further. Since an error has occurred, the return value of
17785 cp_parser_parse_definitely will be false, as required. */
17786 return cp_parser_parse_definitely (parser);
17788 token = cp_lexer_peek_token (parser->lexer);
17789 /* Parse the unqualified-id. */
17790 identifier = cp_parser_unqualified_id (parser,
17791 /*template_keyword_p=*/false,
17792 /*check_dependency_p=*/true,
17793 /*declarator_p=*/true,
17794 /*optional_p=*/false);
17796 if (access_declaration_p)
17798 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17799 cp_parser_simulate_error (parser);
17800 if (!cp_parser_parse_definitely (parser))
17801 return false;
17804 /* The function we call to handle a using-declaration is different
17805 depending on what scope we are in. */
17806 if (qscope == error_mark_node || identifier == error_mark_node)
17808 else if (!identifier_p (identifier)
17809 && TREE_CODE (identifier) != BIT_NOT_EXPR)
17810 /* [namespace.udecl]
17812 A using declaration shall not name a template-id. */
17813 error_at (token->location,
17814 "a template-id may not appear in a using-declaration");
17815 else
17817 if (at_class_scope_p ())
17819 /* Create the USING_DECL. */
17820 decl = do_class_using_decl (parser->scope, identifier);
17822 if (decl && typename_p)
17823 USING_DECL_TYPENAME_P (decl) = 1;
17825 if (check_for_bare_parameter_packs (decl))
17827 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17828 return false;
17830 else
17831 /* Add it to the list of members in this class. */
17832 finish_member_declaration (decl);
17834 else
17836 decl = cp_parser_lookup_name_simple (parser,
17837 identifier,
17838 token->location);
17839 if (decl == error_mark_node)
17840 cp_parser_name_lookup_error (parser, identifier,
17841 decl, NLE_NULL,
17842 token->location);
17843 else if (check_for_bare_parameter_packs (decl))
17845 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17846 return false;
17848 else if (!at_namespace_scope_p ())
17849 do_local_using_decl (decl, qscope, identifier);
17850 else
17851 do_toplevel_using_decl (decl, qscope, identifier);
17855 /* Look for the final `;'. */
17856 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17858 if (access_declaration_p && errorcount == oldcount)
17859 warning_at (diag_token->location, OPT_Wdeprecated,
17860 "access declarations are deprecated "
17861 "in favour of using-declarations; "
17862 "suggestion: add the %<using%> keyword");
17864 return true;
17867 /* Parse an alias-declaration.
17869 alias-declaration:
17870 using identifier attribute-specifier-seq [opt] = type-id */
17872 static tree
17873 cp_parser_alias_declaration (cp_parser* parser)
17875 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
17876 location_t id_location;
17877 cp_declarator *declarator;
17878 cp_decl_specifier_seq decl_specs;
17879 bool member_p;
17880 const char *saved_message = NULL;
17882 /* Look for the `using' keyword. */
17883 cp_token *using_token
17884 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
17885 if (using_token == NULL)
17886 return error_mark_node;
17888 id_location = cp_lexer_peek_token (parser->lexer)->location;
17889 id = cp_parser_identifier (parser);
17890 if (id == error_mark_node)
17891 return error_mark_node;
17893 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
17894 attributes = cp_parser_attributes_opt (parser);
17895 if (attributes == error_mark_node)
17896 return error_mark_node;
17898 cp_parser_require (parser, CPP_EQ, RT_EQ);
17900 if (cp_parser_error_occurred (parser))
17901 return error_mark_node;
17903 cp_parser_commit_to_tentative_parse (parser);
17905 /* Now we are going to parse the type-id of the declaration. */
17908 [dcl.type]/3 says:
17910 "A type-specifier-seq shall not define a class or enumeration
17911 unless it appears in the type-id of an alias-declaration (7.1.3) that
17912 is not the declaration of a template-declaration."
17914 In other words, if we currently are in an alias template, the
17915 type-id should not define a type.
17917 So let's set parser->type_definition_forbidden_message in that
17918 case; cp_parser_check_type_definition (called by
17919 cp_parser_class_specifier) will then emit an error if a type is
17920 defined in the type-id. */
17921 if (parser->num_template_parameter_lists)
17923 saved_message = parser->type_definition_forbidden_message;
17924 parser->type_definition_forbidden_message =
17925 G_("types may not be defined in alias template declarations");
17928 type = cp_parser_type_id (parser);
17930 /* Restore the error message if need be. */
17931 if (parser->num_template_parameter_lists)
17932 parser->type_definition_forbidden_message = saved_message;
17934 if (type == error_mark_node
17935 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
17937 cp_parser_skip_to_end_of_block_or_statement (parser);
17938 return error_mark_node;
17941 /* A typedef-name can also be introduced by an alias-declaration. The
17942 identifier following the using keyword becomes a typedef-name. It has
17943 the same semantics as if it were introduced by the typedef
17944 specifier. In particular, it does not define a new type and it shall
17945 not appear in the type-id. */
17947 clear_decl_specs (&decl_specs);
17948 decl_specs.type = type;
17949 if (attributes != NULL_TREE)
17951 decl_specs.attributes = attributes;
17952 set_and_check_decl_spec_loc (&decl_specs,
17953 ds_attribute,
17954 attrs_token);
17956 set_and_check_decl_spec_loc (&decl_specs,
17957 ds_typedef,
17958 using_token);
17959 set_and_check_decl_spec_loc (&decl_specs,
17960 ds_alias,
17961 using_token);
17963 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
17964 declarator->id_loc = id_location;
17966 member_p = at_class_scope_p ();
17967 if (member_p)
17968 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
17969 NULL_TREE, attributes);
17970 else
17971 decl = start_decl (declarator, &decl_specs, 0,
17972 attributes, NULL_TREE, &pushed_scope);
17973 if (decl == error_mark_node)
17974 return decl;
17976 // Attach constraints to the alias declaration.
17977 if (flag_concepts && current_template_parms)
17979 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
17980 tree constr = build_constraints (reqs, NULL_TREE);
17981 set_constraints (decl, constr);
17984 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
17986 if (pushed_scope)
17987 pop_scope (pushed_scope);
17989 /* If decl is a template, return its TEMPLATE_DECL so that it gets
17990 added into the symbol table; otherwise, return the TYPE_DECL. */
17991 if (DECL_LANG_SPECIFIC (decl)
17992 && DECL_TEMPLATE_INFO (decl)
17993 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
17995 decl = DECL_TI_TEMPLATE (decl);
17996 if (member_p)
17997 check_member_template (decl);
18000 return decl;
18003 /* Parse a using-directive.
18005 using-directive:
18006 using namespace :: [opt] nested-name-specifier [opt]
18007 namespace-name ; */
18009 static void
18010 cp_parser_using_directive (cp_parser* parser)
18012 tree namespace_decl;
18013 tree attribs;
18015 /* Look for the `using' keyword. */
18016 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18017 /* And the `namespace' keyword. */
18018 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18019 /* Look for the optional `::' operator. */
18020 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18021 /* And the optional nested-name-specifier. */
18022 cp_parser_nested_name_specifier_opt (parser,
18023 /*typename_keyword_p=*/false,
18024 /*check_dependency_p=*/true,
18025 /*type_p=*/false,
18026 /*is_declaration=*/true);
18027 /* Get the namespace being used. */
18028 namespace_decl = cp_parser_namespace_name (parser);
18029 /* And any specified attributes. */
18030 attribs = cp_parser_attributes_opt (parser);
18031 /* Update the symbol table. */
18032 parse_using_directive (namespace_decl, attribs);
18033 /* Look for the final `;'. */
18034 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18037 /* Parse an asm-definition.
18039 asm-definition:
18040 asm ( string-literal ) ;
18042 GNU Extension:
18044 asm-definition:
18045 asm volatile [opt] ( string-literal ) ;
18046 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18047 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18048 : asm-operand-list [opt] ) ;
18049 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18050 : asm-operand-list [opt]
18051 : asm-clobber-list [opt] ) ;
18052 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18053 : asm-clobber-list [opt]
18054 : asm-goto-list ) ; */
18056 static void
18057 cp_parser_asm_definition (cp_parser* parser)
18059 tree string;
18060 tree outputs = NULL_TREE;
18061 tree inputs = NULL_TREE;
18062 tree clobbers = NULL_TREE;
18063 tree labels = NULL_TREE;
18064 tree asm_stmt;
18065 bool volatile_p = false;
18066 bool extended_p = false;
18067 bool invalid_inputs_p = false;
18068 bool invalid_outputs_p = false;
18069 bool goto_p = false;
18070 required_token missing = RT_NONE;
18072 /* Look for the `asm' keyword. */
18073 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18075 if (parser->in_function_body
18076 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18078 error ("%<asm%> in %<constexpr%> function");
18079 cp_function_chain->invalid_constexpr = true;
18082 /* See if the next token is `volatile'. */
18083 if (cp_parser_allow_gnu_extensions_p (parser)
18084 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18086 /* Remember that we saw the `volatile' keyword. */
18087 volatile_p = true;
18088 /* Consume the token. */
18089 cp_lexer_consume_token (parser->lexer);
18091 if (cp_parser_allow_gnu_extensions_p (parser)
18092 && parser->in_function_body
18093 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18095 /* Remember that we saw the `goto' keyword. */
18096 goto_p = true;
18097 /* Consume the token. */
18098 cp_lexer_consume_token (parser->lexer);
18100 /* Look for the opening `('. */
18101 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18102 return;
18103 /* Look for the string. */
18104 string = cp_parser_string_literal (parser, false, false);
18105 if (string == error_mark_node)
18107 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18108 /*consume_paren=*/true);
18109 return;
18112 /* If we're allowing GNU extensions, check for the extended assembly
18113 syntax. Unfortunately, the `:' tokens need not be separated by
18114 a space in C, and so, for compatibility, we tolerate that here
18115 too. Doing that means that we have to treat the `::' operator as
18116 two `:' tokens. */
18117 if (cp_parser_allow_gnu_extensions_p (parser)
18118 && parser->in_function_body
18119 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18120 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18122 bool inputs_p = false;
18123 bool clobbers_p = false;
18124 bool labels_p = false;
18126 /* The extended syntax was used. */
18127 extended_p = true;
18129 /* Look for outputs. */
18130 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18132 /* Consume the `:'. */
18133 cp_lexer_consume_token (parser->lexer);
18134 /* Parse the output-operands. */
18135 if (cp_lexer_next_token_is_not (parser->lexer,
18136 CPP_COLON)
18137 && cp_lexer_next_token_is_not (parser->lexer,
18138 CPP_SCOPE)
18139 && cp_lexer_next_token_is_not (parser->lexer,
18140 CPP_CLOSE_PAREN)
18141 && !goto_p)
18143 outputs = cp_parser_asm_operand_list (parser);
18144 if (outputs == error_mark_node)
18145 invalid_outputs_p = true;
18148 /* If the next token is `::', there are no outputs, and the
18149 next token is the beginning of the inputs. */
18150 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18151 /* The inputs are coming next. */
18152 inputs_p = true;
18154 /* Look for inputs. */
18155 if (inputs_p
18156 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18158 /* Consume the `:' or `::'. */
18159 cp_lexer_consume_token (parser->lexer);
18160 /* Parse the output-operands. */
18161 if (cp_lexer_next_token_is_not (parser->lexer,
18162 CPP_COLON)
18163 && cp_lexer_next_token_is_not (parser->lexer,
18164 CPP_SCOPE)
18165 && cp_lexer_next_token_is_not (parser->lexer,
18166 CPP_CLOSE_PAREN))
18168 inputs = cp_parser_asm_operand_list (parser);
18169 if (inputs == error_mark_node)
18170 invalid_inputs_p = true;
18173 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18174 /* The clobbers are coming next. */
18175 clobbers_p = true;
18177 /* Look for clobbers. */
18178 if (clobbers_p
18179 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18181 clobbers_p = true;
18182 /* Consume the `:' or `::'. */
18183 cp_lexer_consume_token (parser->lexer);
18184 /* Parse the clobbers. */
18185 if (cp_lexer_next_token_is_not (parser->lexer,
18186 CPP_COLON)
18187 && cp_lexer_next_token_is_not (parser->lexer,
18188 CPP_CLOSE_PAREN))
18189 clobbers = cp_parser_asm_clobber_list (parser);
18191 else if (goto_p
18192 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18193 /* The labels are coming next. */
18194 labels_p = true;
18196 /* Look for labels. */
18197 if (labels_p
18198 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
18200 labels_p = true;
18201 /* Consume the `:' or `::'. */
18202 cp_lexer_consume_token (parser->lexer);
18203 /* Parse the labels. */
18204 labels = cp_parser_asm_label_list (parser);
18207 if (goto_p && !labels_p)
18208 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
18210 else if (goto_p)
18211 missing = RT_COLON_SCOPE;
18213 /* Look for the closing `)'. */
18214 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
18215 missing ? missing : RT_CLOSE_PAREN))
18216 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18217 /*consume_paren=*/true);
18218 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18220 if (!invalid_inputs_p && !invalid_outputs_p)
18222 /* Create the ASM_EXPR. */
18223 if (parser->in_function_body)
18225 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
18226 inputs, clobbers, labels);
18227 /* If the extended syntax was not used, mark the ASM_EXPR. */
18228 if (!extended_p)
18230 tree temp = asm_stmt;
18231 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
18232 temp = TREE_OPERAND (temp, 0);
18234 ASM_INPUT_P (temp) = 1;
18237 else
18238 symtab->finalize_toplevel_asm (string);
18242 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
18243 type that comes from the decl-specifier-seq. */
18245 static tree
18246 strip_declarator_types (tree type, cp_declarator *declarator)
18248 for (cp_declarator *d = declarator; d;)
18249 switch (d->kind)
18251 case cdk_id:
18252 case cdk_error:
18253 d = NULL;
18254 break;
18256 default:
18257 if (TYPE_PTRMEMFUNC_P (type))
18258 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
18259 type = TREE_TYPE (type);
18260 d = d->declarator;
18261 break;
18264 return type;
18267 /* Declarators [gram.dcl.decl] */
18269 /* Parse an init-declarator.
18271 init-declarator:
18272 declarator initializer [opt]
18274 GNU Extension:
18276 init-declarator:
18277 declarator asm-specification [opt] attributes [opt] initializer [opt]
18279 function-definition:
18280 decl-specifier-seq [opt] declarator ctor-initializer [opt]
18281 function-body
18282 decl-specifier-seq [opt] declarator function-try-block
18284 GNU Extension:
18286 function-definition:
18287 __extension__ function-definition
18289 TM Extension:
18291 function-definition:
18292 decl-specifier-seq [opt] declarator function-transaction-block
18294 The DECL_SPECIFIERS apply to this declarator. Returns a
18295 representation of the entity declared. If MEMBER_P is TRUE, then
18296 this declarator appears in a class scope. The new DECL created by
18297 this declarator is returned.
18299 The CHECKS are access checks that should be performed once we know
18300 what entity is being declared (and, therefore, what classes have
18301 befriended it).
18303 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18304 for a function-definition here as well. If the declarator is a
18305 declarator for a function-definition, *FUNCTION_DEFINITION_P will
18306 be TRUE upon return. By that point, the function-definition will
18307 have been completely parsed.
18309 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18310 is FALSE.
18312 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18313 parsed declaration if it is an uninitialized single declarator not followed
18314 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18315 if present, will not be consumed. If returned, this declarator will be
18316 created with SD_INITIALIZED but will not call cp_finish_decl.
18318 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
18319 and there is an initializer, the pointed location_t is set to the
18320 location of the '=' or `(', or '{' in C++11 token introducing the
18321 initializer. */
18323 static tree
18324 cp_parser_init_declarator (cp_parser* parser,
18325 cp_decl_specifier_seq *decl_specifiers,
18326 vec<deferred_access_check, va_gc> *checks,
18327 bool function_definition_allowed_p,
18328 bool member_p,
18329 int declares_class_or_enum,
18330 bool* function_definition_p,
18331 tree* maybe_range_for_decl,
18332 location_t* init_loc,
18333 tree* auto_result)
18335 cp_token *token = NULL, *asm_spec_start_token = NULL,
18336 *attributes_start_token = NULL;
18337 cp_declarator *declarator;
18338 tree prefix_attributes;
18339 tree attributes = NULL;
18340 tree asm_specification;
18341 tree initializer;
18342 tree decl = NULL_TREE;
18343 tree scope;
18344 int is_initialized;
18345 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
18346 initialized with "= ..", CPP_OPEN_PAREN if initialized with
18347 "(...)". */
18348 enum cpp_ttype initialization_kind;
18349 bool is_direct_init = false;
18350 bool is_non_constant_init;
18351 int ctor_dtor_or_conv_p;
18352 bool friend_p = cp_parser_friend_p (decl_specifiers);
18353 tree pushed_scope = NULL_TREE;
18354 bool range_for_decl_p = false;
18355 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18356 location_t tmp_init_loc = UNKNOWN_LOCATION;
18358 /* Gather the attributes that were provided with the
18359 decl-specifiers. */
18360 prefix_attributes = decl_specifiers->attributes;
18362 /* Assume that this is not the declarator for a function
18363 definition. */
18364 if (function_definition_p)
18365 *function_definition_p = false;
18367 /* Default arguments are only permitted for function parameters. */
18368 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
18369 parser->default_arg_ok_p = false;
18371 /* Defer access checks while parsing the declarator; we cannot know
18372 what names are accessible until we know what is being
18373 declared. */
18374 resume_deferring_access_checks ();
18376 /* Parse the declarator. */
18377 token = cp_lexer_peek_token (parser->lexer);
18378 declarator
18379 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18380 &ctor_dtor_or_conv_p,
18381 /*parenthesized_p=*/NULL,
18382 member_p, friend_p);
18383 /* Gather up the deferred checks. */
18384 stop_deferring_access_checks ();
18386 parser->default_arg_ok_p = saved_default_arg_ok_p;
18388 /* If the DECLARATOR was erroneous, there's no need to go
18389 further. */
18390 if (declarator == cp_error_declarator)
18391 return error_mark_node;
18393 /* Check that the number of template-parameter-lists is OK. */
18394 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
18395 token->location))
18396 return error_mark_node;
18398 if (declares_class_or_enum & 2)
18399 cp_parser_check_for_definition_in_return_type (declarator,
18400 decl_specifiers->type,
18401 decl_specifiers->locations[ds_type_spec]);
18403 /* Figure out what scope the entity declared by the DECLARATOR is
18404 located in. `grokdeclarator' sometimes changes the scope, so
18405 we compute it now. */
18406 scope = get_scope_of_declarator (declarator);
18408 /* Perform any lookups in the declared type which were thought to be
18409 dependent, but are not in the scope of the declarator. */
18410 decl_specifiers->type
18411 = maybe_update_decl_type (decl_specifiers->type, scope);
18413 /* If we're allowing GNU extensions, look for an
18414 asm-specification. */
18415 if (cp_parser_allow_gnu_extensions_p (parser))
18417 /* Look for an asm-specification. */
18418 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
18419 asm_specification = cp_parser_asm_specification_opt (parser);
18421 else
18422 asm_specification = NULL_TREE;
18424 /* Look for attributes. */
18425 attributes_start_token = cp_lexer_peek_token (parser->lexer);
18426 attributes = cp_parser_attributes_opt (parser);
18428 /* Peek at the next token. */
18429 token = cp_lexer_peek_token (parser->lexer);
18431 bool bogus_implicit_tmpl = false;
18433 if (function_declarator_p (declarator))
18435 /* Check to see if the token indicates the start of a
18436 function-definition. */
18437 if (cp_parser_token_starts_function_definition_p (token))
18439 if (!function_definition_allowed_p)
18441 /* If a function-definition should not appear here, issue an
18442 error message. */
18443 cp_parser_error (parser,
18444 "a function-definition is not allowed here");
18445 return error_mark_node;
18448 location_t func_brace_location
18449 = cp_lexer_peek_token (parser->lexer)->location;
18451 /* Neither attributes nor an asm-specification are allowed
18452 on a function-definition. */
18453 if (asm_specification)
18454 error_at (asm_spec_start_token->location,
18455 "an asm-specification is not allowed "
18456 "on a function-definition");
18457 if (attributes)
18458 error_at (attributes_start_token->location,
18459 "attributes are not allowed "
18460 "on a function-definition");
18461 /* This is a function-definition. */
18462 *function_definition_p = true;
18464 /* Parse the function definition. */
18465 if (member_p)
18466 decl = cp_parser_save_member_function_body (parser,
18467 decl_specifiers,
18468 declarator,
18469 prefix_attributes);
18470 else
18471 decl =
18472 (cp_parser_function_definition_from_specifiers_and_declarator
18473 (parser, decl_specifiers, prefix_attributes, declarator));
18475 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
18477 /* This is where the prologue starts... */
18478 DECL_STRUCT_FUNCTION (decl)->function_start_locus
18479 = func_brace_location;
18482 return decl;
18485 else if (parser->fully_implicit_function_template_p)
18487 /* A non-template declaration involving a function parameter list
18488 containing an implicit template parameter will be made into a
18489 template. If the resulting declaration is not going to be an
18490 actual function then finish the template scope here to prevent it.
18491 An error message will be issued once we have a decl to talk about.
18493 FIXME probably we should do type deduction rather than create an
18494 implicit template, but the standard currently doesn't allow it. */
18495 bogus_implicit_tmpl = true;
18496 finish_fully_implicit_template (parser, NULL_TREE);
18499 /* [dcl.dcl]
18501 Only in function declarations for constructors, destructors, and
18502 type conversions can the decl-specifier-seq be omitted.
18504 We explicitly postpone this check past the point where we handle
18505 function-definitions because we tolerate function-definitions
18506 that are missing their return types in some modes. */
18507 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
18509 cp_parser_error (parser,
18510 "expected constructor, destructor, or type conversion");
18511 return error_mark_node;
18514 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
18515 if (token->type == CPP_EQ
18516 || token->type == CPP_OPEN_PAREN
18517 || token->type == CPP_OPEN_BRACE)
18519 is_initialized = SD_INITIALIZED;
18520 initialization_kind = token->type;
18521 if (maybe_range_for_decl)
18522 *maybe_range_for_decl = error_mark_node;
18523 tmp_init_loc = token->location;
18524 if (init_loc && *init_loc == UNKNOWN_LOCATION)
18525 *init_loc = tmp_init_loc;
18527 if (token->type == CPP_EQ
18528 && function_declarator_p (declarator))
18530 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
18531 if (t2->keyword == RID_DEFAULT)
18532 is_initialized = SD_DEFAULTED;
18533 else if (t2->keyword == RID_DELETE)
18534 is_initialized = SD_DELETED;
18537 else
18539 /* If the init-declarator isn't initialized and isn't followed by a
18540 `,' or `;', it's not a valid init-declarator. */
18541 if (token->type != CPP_COMMA
18542 && token->type != CPP_SEMICOLON)
18544 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
18545 range_for_decl_p = true;
18546 else
18548 if (!maybe_range_for_decl)
18549 cp_parser_error (parser, "expected initializer");
18550 return error_mark_node;
18553 is_initialized = SD_UNINITIALIZED;
18554 initialization_kind = CPP_EOF;
18557 /* Because start_decl has side-effects, we should only call it if we
18558 know we're going ahead. By this point, we know that we cannot
18559 possibly be looking at any other construct. */
18560 cp_parser_commit_to_tentative_parse (parser);
18562 /* Enter the newly declared entry in the symbol table. If we're
18563 processing a declaration in a class-specifier, we wait until
18564 after processing the initializer. */
18565 if (!member_p)
18567 if (parser->in_unbraced_linkage_specification_p)
18568 decl_specifiers->storage_class = sc_extern;
18569 decl = start_decl (declarator, decl_specifiers,
18570 range_for_decl_p? SD_INITIALIZED : is_initialized,
18571 attributes, prefix_attributes, &pushed_scope);
18572 cp_finalize_omp_declare_simd (parser, decl);
18573 cp_finalize_oacc_routine (parser, decl, false);
18574 /* Adjust location of decl if declarator->id_loc is more appropriate:
18575 set, and decl wasn't merged with another decl, in which case its
18576 location would be different from input_location, and more accurate. */
18577 if (DECL_P (decl)
18578 && declarator->id_loc != UNKNOWN_LOCATION
18579 && DECL_SOURCE_LOCATION (decl) == input_location)
18580 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
18582 else if (scope)
18583 /* Enter the SCOPE. That way unqualified names appearing in the
18584 initializer will be looked up in SCOPE. */
18585 pushed_scope = push_scope (scope);
18587 /* Perform deferred access control checks, now that we know in which
18588 SCOPE the declared entity resides. */
18589 if (!member_p && decl)
18591 tree saved_current_function_decl = NULL_TREE;
18593 /* If the entity being declared is a function, pretend that we
18594 are in its scope. If it is a `friend', it may have access to
18595 things that would not otherwise be accessible. */
18596 if (TREE_CODE (decl) == FUNCTION_DECL)
18598 saved_current_function_decl = current_function_decl;
18599 current_function_decl = decl;
18602 /* Perform access checks for template parameters. */
18603 cp_parser_perform_template_parameter_access_checks (checks);
18605 /* Perform the access control checks for the declarator and the
18606 decl-specifiers. */
18607 perform_deferred_access_checks (tf_warning_or_error);
18609 /* Restore the saved value. */
18610 if (TREE_CODE (decl) == FUNCTION_DECL)
18611 current_function_decl = saved_current_function_decl;
18614 /* Parse the initializer. */
18615 initializer = NULL_TREE;
18616 is_direct_init = false;
18617 is_non_constant_init = true;
18618 if (is_initialized)
18620 if (function_declarator_p (declarator))
18622 if (initialization_kind == CPP_EQ)
18623 initializer = cp_parser_pure_specifier (parser);
18624 else
18626 /* If the declaration was erroneous, we don't really
18627 know what the user intended, so just silently
18628 consume the initializer. */
18629 if (decl != error_mark_node)
18630 error_at (tmp_init_loc, "initializer provided for function");
18631 cp_parser_skip_to_closing_parenthesis (parser,
18632 /*recovering=*/true,
18633 /*or_comma=*/false,
18634 /*consume_paren=*/true);
18637 else
18639 /* We want to record the extra mangling scope for in-class
18640 initializers of class members and initializers of static data
18641 member templates. The former involves deferring
18642 parsing of the initializer until end of class as with default
18643 arguments. So right here we only handle the latter. */
18644 if (!member_p && processing_template_decl)
18645 start_lambda_scope (decl);
18646 initializer = cp_parser_initializer (parser,
18647 &is_direct_init,
18648 &is_non_constant_init);
18649 if (!member_p && processing_template_decl)
18650 finish_lambda_scope ();
18651 if (initializer == error_mark_node)
18652 cp_parser_skip_to_end_of_statement (parser);
18656 /* The old parser allows attributes to appear after a parenthesized
18657 initializer. Mark Mitchell proposed removing this functionality
18658 on the GCC mailing lists on 2002-08-13. This parser accepts the
18659 attributes -- but ignores them. */
18660 if (cp_parser_allow_gnu_extensions_p (parser)
18661 && initialization_kind == CPP_OPEN_PAREN)
18662 if (cp_parser_attributes_opt (parser))
18663 warning (OPT_Wattributes,
18664 "attributes after parenthesized initializer ignored");
18666 /* And now complain about a non-function implicit template. */
18667 if (bogus_implicit_tmpl && decl != error_mark_node)
18668 error_at (DECL_SOURCE_LOCATION (decl),
18669 "non-function %qD declared as implicit template", decl);
18671 /* For an in-class declaration, use `grokfield' to create the
18672 declaration. */
18673 if (member_p)
18675 if (pushed_scope)
18677 pop_scope (pushed_scope);
18678 pushed_scope = NULL_TREE;
18680 decl = grokfield (declarator, decl_specifiers,
18681 initializer, !is_non_constant_init,
18682 /*asmspec=*/NULL_TREE,
18683 chainon (attributes, prefix_attributes));
18684 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
18685 cp_parser_save_default_args (parser, decl);
18686 cp_finalize_omp_declare_simd (parser, decl);
18687 cp_finalize_oacc_routine (parser, decl, false);
18690 /* Finish processing the declaration. But, skip member
18691 declarations. */
18692 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
18694 cp_finish_decl (decl,
18695 initializer, !is_non_constant_init,
18696 asm_specification,
18697 /* If the initializer is in parentheses, then this is
18698 a direct-initialization, which means that an
18699 `explicit' constructor is OK. Otherwise, an
18700 `explicit' constructor cannot be used. */
18701 ((is_direct_init || !is_initialized)
18702 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
18704 else if ((cxx_dialect != cxx98) && friend_p
18705 && decl && TREE_CODE (decl) == FUNCTION_DECL)
18706 /* Core issue #226 (C++0x only): A default template-argument
18707 shall not be specified in a friend class template
18708 declaration. */
18709 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
18710 /*is_partial=*/false, /*is_friend_decl=*/1);
18712 if (!friend_p && pushed_scope)
18713 pop_scope (pushed_scope);
18715 if (function_declarator_p (declarator)
18716 && parser->fully_implicit_function_template_p)
18718 if (member_p)
18719 decl = finish_fully_implicit_template (parser, decl);
18720 else
18721 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
18724 if (auto_result && is_initialized && decl_specifiers->type
18725 && type_uses_auto (decl_specifiers->type))
18726 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
18728 return decl;
18731 /* Parse a declarator.
18733 declarator:
18734 direct-declarator
18735 ptr-operator declarator
18737 abstract-declarator:
18738 ptr-operator abstract-declarator [opt]
18739 direct-abstract-declarator
18741 GNU Extensions:
18743 declarator:
18744 attributes [opt] direct-declarator
18745 attributes [opt] ptr-operator declarator
18747 abstract-declarator:
18748 attributes [opt] ptr-operator abstract-declarator [opt]
18749 attributes [opt] direct-abstract-declarator
18751 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
18752 detect constructor, destructor or conversion operators. It is set
18753 to -1 if the declarator is a name, and +1 if it is a
18754 function. Otherwise it is set to zero. Usually you just want to
18755 test for >0, but internally the negative value is used.
18757 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
18758 a decl-specifier-seq unless it declares a constructor, destructor,
18759 or conversion. It might seem that we could check this condition in
18760 semantic analysis, rather than parsing, but that makes it difficult
18761 to handle something like `f()'. We want to notice that there are
18762 no decl-specifiers, and therefore realize that this is an
18763 expression, not a declaration.)
18765 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
18766 the declarator is a direct-declarator of the form "(...)".
18768 MEMBER_P is true iff this declarator is a member-declarator.
18770 FRIEND_P is true iff this declarator is a friend. */
18772 static cp_declarator *
18773 cp_parser_declarator (cp_parser* parser,
18774 cp_parser_declarator_kind dcl_kind,
18775 int* ctor_dtor_or_conv_p,
18776 bool* parenthesized_p,
18777 bool member_p, bool friend_p)
18779 cp_declarator *declarator;
18780 enum tree_code code;
18781 cp_cv_quals cv_quals;
18782 tree class_type;
18783 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
18785 /* Assume this is not a constructor, destructor, or type-conversion
18786 operator. */
18787 if (ctor_dtor_or_conv_p)
18788 *ctor_dtor_or_conv_p = 0;
18790 if (cp_parser_allow_gnu_extensions_p (parser))
18791 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
18793 /* Check for the ptr-operator production. */
18794 cp_parser_parse_tentatively (parser);
18795 /* Parse the ptr-operator. */
18796 code = cp_parser_ptr_operator (parser,
18797 &class_type,
18798 &cv_quals,
18799 &std_attributes);
18801 /* If that worked, then we have a ptr-operator. */
18802 if (cp_parser_parse_definitely (parser))
18804 /* If a ptr-operator was found, then this declarator was not
18805 parenthesized. */
18806 if (parenthesized_p)
18807 *parenthesized_p = true;
18808 /* The dependent declarator is optional if we are parsing an
18809 abstract-declarator. */
18810 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18811 cp_parser_parse_tentatively (parser);
18813 /* Parse the dependent declarator. */
18814 declarator = cp_parser_declarator (parser, dcl_kind,
18815 /*ctor_dtor_or_conv_p=*/NULL,
18816 /*parenthesized_p=*/NULL,
18817 /*member_p=*/false,
18818 friend_p);
18820 /* If we are parsing an abstract-declarator, we must handle the
18821 case where the dependent declarator is absent. */
18822 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
18823 && !cp_parser_parse_definitely (parser))
18824 declarator = NULL;
18826 declarator = cp_parser_make_indirect_declarator
18827 (code, class_type, cv_quals, declarator, std_attributes);
18829 /* Everything else is a direct-declarator. */
18830 else
18832 if (parenthesized_p)
18833 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
18834 CPP_OPEN_PAREN);
18835 declarator = cp_parser_direct_declarator (parser, dcl_kind,
18836 ctor_dtor_or_conv_p,
18837 member_p, friend_p);
18840 if (gnu_attributes && declarator && declarator != cp_error_declarator)
18841 declarator->attributes = gnu_attributes;
18842 return declarator;
18845 /* Parse a direct-declarator or direct-abstract-declarator.
18847 direct-declarator:
18848 declarator-id
18849 direct-declarator ( parameter-declaration-clause )
18850 cv-qualifier-seq [opt]
18851 ref-qualifier [opt]
18852 exception-specification [opt]
18853 direct-declarator [ constant-expression [opt] ]
18854 ( declarator )
18856 direct-abstract-declarator:
18857 direct-abstract-declarator [opt]
18858 ( parameter-declaration-clause )
18859 cv-qualifier-seq [opt]
18860 ref-qualifier [opt]
18861 exception-specification [opt]
18862 direct-abstract-declarator [opt] [ constant-expression [opt] ]
18863 ( abstract-declarator )
18865 Returns a representation of the declarator. DCL_KIND is
18866 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
18867 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
18868 we are parsing a direct-declarator. It is
18869 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
18870 of ambiguity we prefer an abstract declarator, as per
18871 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
18872 as for cp_parser_declarator. */
18874 static cp_declarator *
18875 cp_parser_direct_declarator (cp_parser* parser,
18876 cp_parser_declarator_kind dcl_kind,
18877 int* ctor_dtor_or_conv_p,
18878 bool member_p, bool friend_p)
18880 cp_token *token;
18881 cp_declarator *declarator = NULL;
18882 tree scope = NULL_TREE;
18883 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18884 bool saved_in_declarator_p = parser->in_declarator_p;
18885 bool first = true;
18886 tree pushed_scope = NULL_TREE;
18888 while (true)
18890 /* Peek at the next token. */
18891 token = cp_lexer_peek_token (parser->lexer);
18892 if (token->type == CPP_OPEN_PAREN)
18894 /* This is either a parameter-declaration-clause, or a
18895 parenthesized declarator. When we know we are parsing a
18896 named declarator, it must be a parenthesized declarator
18897 if FIRST is true. For instance, `(int)' is a
18898 parameter-declaration-clause, with an omitted
18899 direct-abstract-declarator. But `((*))', is a
18900 parenthesized abstract declarator. Finally, when T is a
18901 template parameter `(T)' is a
18902 parameter-declaration-clause, and not a parenthesized
18903 named declarator.
18905 We first try and parse a parameter-declaration-clause,
18906 and then try a nested declarator (if FIRST is true).
18908 It is not an error for it not to be a
18909 parameter-declaration-clause, even when FIRST is
18910 false. Consider,
18912 int i (int);
18913 int i (3);
18915 The first is the declaration of a function while the
18916 second is the definition of a variable, including its
18917 initializer.
18919 Having seen only the parenthesis, we cannot know which of
18920 these two alternatives should be selected. Even more
18921 complex are examples like:
18923 int i (int (a));
18924 int i (int (3));
18926 The former is a function-declaration; the latter is a
18927 variable initialization.
18929 Thus again, we try a parameter-declaration-clause, and if
18930 that fails, we back out and return. */
18932 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18934 tree params;
18935 bool is_declarator = false;
18937 /* In a member-declarator, the only valid interpretation
18938 of a parenthesis is the start of a
18939 parameter-declaration-clause. (It is invalid to
18940 initialize a static data member with a parenthesized
18941 initializer; only the "=" form of initialization is
18942 permitted.) */
18943 if (!member_p)
18944 cp_parser_parse_tentatively (parser);
18946 /* Consume the `('. */
18947 cp_lexer_consume_token (parser->lexer);
18948 if (first)
18950 /* If this is going to be an abstract declarator, we're
18951 in a declarator and we can't have default args. */
18952 parser->default_arg_ok_p = false;
18953 parser->in_declarator_p = true;
18956 begin_scope (sk_function_parms, NULL_TREE);
18958 /* Parse the parameter-declaration-clause. */
18959 params = cp_parser_parameter_declaration_clause (parser);
18961 /* Consume the `)'. */
18962 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18964 /* If all went well, parse the cv-qualifier-seq,
18965 ref-qualifier and the exception-specification. */
18966 if (member_p || cp_parser_parse_definitely (parser))
18968 cp_cv_quals cv_quals;
18969 cp_virt_specifiers virt_specifiers;
18970 cp_ref_qualifier ref_qual;
18971 tree exception_specification;
18972 tree late_return;
18973 tree attrs;
18974 bool memfn = (member_p || (pushed_scope
18975 && CLASS_TYPE_P (pushed_scope)));
18977 is_declarator = true;
18979 if (ctor_dtor_or_conv_p)
18980 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
18981 first = false;
18983 /* Parse the cv-qualifier-seq. */
18984 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18985 /* Parse the ref-qualifier. */
18986 ref_qual = cp_parser_ref_qualifier_opt (parser);
18987 /* Parse the tx-qualifier. */
18988 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
18989 /* And the exception-specification. */
18990 exception_specification
18991 = cp_parser_exception_specification_opt (parser);
18993 attrs = cp_parser_std_attribute_spec_seq (parser);
18995 /* In here, we handle cases where attribute is used after
18996 the function declaration. For example:
18997 void func (int x) __attribute__((vector(..))); */
18998 tree gnu_attrs = NULL_TREE;
18999 if (flag_cilkplus
19000 && cp_next_tokens_can_be_gnu_attribute_p (parser))
19002 cp_parser_parse_tentatively (parser);
19003 tree attr = cp_parser_gnu_attributes_opt (parser);
19004 if (cp_lexer_next_token_is_not (parser->lexer,
19005 CPP_SEMICOLON)
19006 && cp_lexer_next_token_is_not (parser->lexer,
19007 CPP_OPEN_BRACE))
19008 cp_parser_abort_tentative_parse (parser);
19009 else if (!cp_parser_parse_definitely (parser))
19011 else
19012 gnu_attrs = attr;
19014 tree requires_clause = NULL_TREE;
19015 late_return = (cp_parser_late_return_type_opt
19016 (parser, declarator, requires_clause,
19017 memfn ? cv_quals : -1));
19019 /* Parse the virt-specifier-seq. */
19020 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19022 /* Create the function-declarator. */
19023 declarator = make_call_declarator (declarator,
19024 params,
19025 cv_quals,
19026 virt_specifiers,
19027 ref_qual,
19028 tx_qual,
19029 exception_specification,
19030 late_return,
19031 requires_clause);
19032 declarator->std_attributes = attrs;
19033 declarator->attributes = gnu_attrs;
19034 /* Any subsequent parameter lists are to do with
19035 return type, so are not those of the declared
19036 function. */
19037 parser->default_arg_ok_p = false;
19040 /* Remove the function parms from scope. */
19041 pop_bindings_and_leave_scope ();
19043 if (is_declarator)
19044 /* Repeat the main loop. */
19045 continue;
19048 /* If this is the first, we can try a parenthesized
19049 declarator. */
19050 if (first)
19052 bool saved_in_type_id_in_expr_p;
19054 parser->default_arg_ok_p = saved_default_arg_ok_p;
19055 parser->in_declarator_p = saved_in_declarator_p;
19057 /* Consume the `('. */
19058 cp_lexer_consume_token (parser->lexer);
19059 /* Parse the nested declarator. */
19060 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19061 parser->in_type_id_in_expr_p = true;
19062 declarator
19063 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19064 /*parenthesized_p=*/NULL,
19065 member_p, friend_p);
19066 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19067 first = false;
19068 /* Expect a `)'. */
19069 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19070 declarator = cp_error_declarator;
19071 if (declarator == cp_error_declarator)
19072 break;
19074 goto handle_declarator;
19076 /* Otherwise, we must be done. */
19077 else
19078 break;
19080 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19081 && token->type == CPP_OPEN_SQUARE
19082 && !cp_next_tokens_can_be_attribute_p (parser))
19084 /* Parse an array-declarator. */
19085 tree bounds, attrs;
19087 if (ctor_dtor_or_conv_p)
19088 *ctor_dtor_or_conv_p = 0;
19090 first = false;
19091 parser->default_arg_ok_p = false;
19092 parser->in_declarator_p = true;
19093 /* Consume the `['. */
19094 cp_lexer_consume_token (parser->lexer);
19095 /* Peek at the next token. */
19096 token = cp_lexer_peek_token (parser->lexer);
19097 /* If the next token is `]', then there is no
19098 constant-expression. */
19099 if (token->type != CPP_CLOSE_SQUARE)
19101 bool non_constant_p;
19102 bounds
19103 = cp_parser_constant_expression (parser,
19104 /*allow_non_constant=*/true,
19105 &non_constant_p);
19106 if (!non_constant_p)
19107 /* OK */;
19108 else if (error_operand_p (bounds))
19109 /* Already gave an error. */;
19110 else if (!parser->in_function_body
19111 || current_binding_level->kind == sk_function_parms)
19113 /* Normally, the array bound must be an integral constant
19114 expression. However, as an extension, we allow VLAs
19115 in function scopes as long as they aren't part of a
19116 parameter declaration. */
19117 cp_parser_error (parser,
19118 "array bound is not an integer constant");
19119 bounds = error_mark_node;
19121 else if (processing_template_decl
19122 && !type_dependent_expression_p (bounds))
19124 /* Remember this wasn't a constant-expression. */
19125 bounds = build_nop (TREE_TYPE (bounds), bounds);
19126 TREE_SIDE_EFFECTS (bounds) = 1;
19129 else
19130 bounds = NULL_TREE;
19131 /* Look for the closing `]'. */
19132 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19134 declarator = cp_error_declarator;
19135 break;
19138 attrs = cp_parser_std_attribute_spec_seq (parser);
19139 declarator = make_array_declarator (declarator, bounds);
19140 declarator->std_attributes = attrs;
19142 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19145 tree qualifying_scope;
19146 tree unqualified_name;
19147 tree attrs;
19148 special_function_kind sfk;
19149 bool abstract_ok;
19150 bool pack_expansion_p = false;
19151 cp_token *declarator_id_start_token;
19153 /* Parse a declarator-id */
19154 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
19155 if (abstract_ok)
19157 cp_parser_parse_tentatively (parser);
19159 /* If we see an ellipsis, we should be looking at a
19160 parameter pack. */
19161 if (token->type == CPP_ELLIPSIS)
19163 /* Consume the `...' */
19164 cp_lexer_consume_token (parser->lexer);
19166 pack_expansion_p = true;
19170 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
19171 unqualified_name
19172 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
19173 qualifying_scope = parser->scope;
19174 if (abstract_ok)
19176 bool okay = false;
19178 if (!unqualified_name && pack_expansion_p)
19180 /* Check whether an error occurred. */
19181 okay = !cp_parser_error_occurred (parser);
19183 /* We already consumed the ellipsis to mark a
19184 parameter pack, but we have no way to report it,
19185 so abort the tentative parse. We will be exiting
19186 immediately anyway. */
19187 cp_parser_abort_tentative_parse (parser);
19189 else
19190 okay = cp_parser_parse_definitely (parser);
19192 if (!okay)
19193 unqualified_name = error_mark_node;
19194 else if (unqualified_name
19195 && (qualifying_scope
19196 || (!identifier_p (unqualified_name))))
19198 cp_parser_error (parser, "expected unqualified-id");
19199 unqualified_name = error_mark_node;
19203 if (!unqualified_name)
19204 return NULL;
19205 if (unqualified_name == error_mark_node)
19207 declarator = cp_error_declarator;
19208 pack_expansion_p = false;
19209 declarator->parameter_pack_p = false;
19210 break;
19213 attrs = cp_parser_std_attribute_spec_seq (parser);
19215 if (qualifying_scope && at_namespace_scope_p ()
19216 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
19218 /* In the declaration of a member of a template class
19219 outside of the class itself, the SCOPE will sometimes
19220 be a TYPENAME_TYPE. For example, given:
19222 template <typename T>
19223 int S<T>::R::i = 3;
19225 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
19226 this context, we must resolve S<T>::R to an ordinary
19227 type, rather than a typename type.
19229 The reason we normally avoid resolving TYPENAME_TYPEs
19230 is that a specialization of `S' might render
19231 `S<T>::R' not a type. However, if `S' is
19232 specialized, then this `i' will not be used, so there
19233 is no harm in resolving the types here. */
19234 tree type;
19236 /* Resolve the TYPENAME_TYPE. */
19237 type = resolve_typename_type (qualifying_scope,
19238 /*only_current_p=*/false);
19239 /* If that failed, the declarator is invalid. */
19240 if (TREE_CODE (type) == TYPENAME_TYPE)
19242 if (typedef_variant_p (type))
19243 error_at (declarator_id_start_token->location,
19244 "cannot define member of dependent typedef "
19245 "%qT", type);
19246 else
19247 error_at (declarator_id_start_token->location,
19248 "%<%T::%E%> is not a type",
19249 TYPE_CONTEXT (qualifying_scope),
19250 TYPE_IDENTIFIER (qualifying_scope));
19252 qualifying_scope = type;
19255 sfk = sfk_none;
19257 if (unqualified_name)
19259 tree class_type;
19261 if (qualifying_scope
19262 && CLASS_TYPE_P (qualifying_scope))
19263 class_type = qualifying_scope;
19264 else
19265 class_type = current_class_type;
19267 if (TREE_CODE (unqualified_name) == TYPE_DECL)
19269 tree name_type = TREE_TYPE (unqualified_name);
19270 if (class_type && same_type_p (name_type, class_type))
19272 if (qualifying_scope
19273 && CLASSTYPE_USE_TEMPLATE (name_type))
19275 error_at (declarator_id_start_token->location,
19276 "invalid use of constructor as a template");
19277 inform (declarator_id_start_token->location,
19278 "use %<%T::%D%> instead of %<%T::%D%> to "
19279 "name the constructor in a qualified name",
19280 class_type,
19281 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
19282 class_type, name_type);
19283 declarator = cp_error_declarator;
19284 break;
19286 else
19287 unqualified_name = constructor_name (class_type);
19289 else
19291 /* We do not attempt to print the declarator
19292 here because we do not have enough
19293 information about its original syntactic
19294 form. */
19295 cp_parser_error (parser, "invalid declarator");
19296 declarator = cp_error_declarator;
19297 break;
19301 if (class_type)
19303 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
19304 sfk = sfk_destructor;
19305 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
19306 sfk = sfk_conversion;
19307 else if (/* There's no way to declare a constructor
19308 for an anonymous type, even if the type
19309 got a name for linkage purposes. */
19310 !TYPE_WAS_ANONYMOUS (class_type)
19311 /* Handle correctly (c++/19200):
19313 struct S {
19314 struct T{};
19315 friend void S(T);
19318 and also:
19320 namespace N {
19321 void S();
19324 struct S {
19325 friend void N::S();
19326 }; */
19327 && !(friend_p
19328 && class_type != qualifying_scope)
19329 && constructor_name_p (unqualified_name,
19330 class_type))
19332 unqualified_name = constructor_name (class_type);
19333 sfk = sfk_constructor;
19335 else if (is_overloaded_fn (unqualified_name)
19336 && DECL_CONSTRUCTOR_P (get_first_fn
19337 (unqualified_name)))
19338 sfk = sfk_constructor;
19340 if (ctor_dtor_or_conv_p && sfk != sfk_none)
19341 *ctor_dtor_or_conv_p = -1;
19344 declarator = make_id_declarator (qualifying_scope,
19345 unqualified_name,
19346 sfk);
19347 declarator->std_attributes = attrs;
19348 declarator->id_loc = token->location;
19349 declarator->parameter_pack_p = pack_expansion_p;
19351 if (pack_expansion_p)
19352 maybe_warn_variadic_templates ();
19355 handle_declarator:;
19356 scope = get_scope_of_declarator (declarator);
19357 if (scope)
19359 /* Any names that appear after the declarator-id for a
19360 member are looked up in the containing scope. */
19361 if (at_function_scope_p ())
19363 /* But declarations with qualified-ids can't appear in a
19364 function. */
19365 cp_parser_error (parser, "qualified-id in declaration");
19366 declarator = cp_error_declarator;
19367 break;
19369 pushed_scope = push_scope (scope);
19371 parser->in_declarator_p = true;
19372 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
19373 || (declarator && declarator->kind == cdk_id))
19374 /* Default args are only allowed on function
19375 declarations. */
19376 parser->default_arg_ok_p = saved_default_arg_ok_p;
19377 else
19378 parser->default_arg_ok_p = false;
19380 first = false;
19382 /* We're done. */
19383 else
19384 break;
19387 /* For an abstract declarator, we might wind up with nothing at this
19388 point. That's an error; the declarator is not optional. */
19389 if (!declarator)
19390 cp_parser_error (parser, "expected declarator");
19392 /* If we entered a scope, we must exit it now. */
19393 if (pushed_scope)
19394 pop_scope (pushed_scope);
19396 parser->default_arg_ok_p = saved_default_arg_ok_p;
19397 parser->in_declarator_p = saved_in_declarator_p;
19399 return declarator;
19402 /* Parse a ptr-operator.
19404 ptr-operator:
19405 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19406 * cv-qualifier-seq [opt]
19408 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
19409 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19411 GNU Extension:
19413 ptr-operator:
19414 & cv-qualifier-seq [opt]
19416 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
19417 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
19418 an rvalue reference. In the case of a pointer-to-member, *TYPE is
19419 filled in with the TYPE containing the member. *CV_QUALS is
19420 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
19421 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
19422 Note that the tree codes returned by this function have nothing
19423 to do with the types of trees that will be eventually be created
19424 to represent the pointer or reference type being parsed. They are
19425 just constants with suggestive names. */
19426 static enum tree_code
19427 cp_parser_ptr_operator (cp_parser* parser,
19428 tree* type,
19429 cp_cv_quals *cv_quals,
19430 tree *attributes)
19432 enum tree_code code = ERROR_MARK;
19433 cp_token *token;
19434 tree attrs = NULL_TREE;
19436 /* Assume that it's not a pointer-to-member. */
19437 *type = NULL_TREE;
19438 /* And that there are no cv-qualifiers. */
19439 *cv_quals = TYPE_UNQUALIFIED;
19441 /* Peek at the next token. */
19442 token = cp_lexer_peek_token (parser->lexer);
19444 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
19445 if (token->type == CPP_MULT)
19446 code = INDIRECT_REF;
19447 else if (token->type == CPP_AND)
19448 code = ADDR_EXPR;
19449 else if ((cxx_dialect != cxx98) &&
19450 token->type == CPP_AND_AND) /* C++0x only */
19451 code = NON_LVALUE_EXPR;
19453 if (code != ERROR_MARK)
19455 /* Consume the `*', `&' or `&&'. */
19456 cp_lexer_consume_token (parser->lexer);
19458 /* A `*' can be followed by a cv-qualifier-seq, and so can a
19459 `&', if we are allowing GNU extensions. (The only qualifier
19460 that can legally appear after `&' is `restrict', but that is
19461 enforced during semantic analysis. */
19462 if (code == INDIRECT_REF
19463 || cp_parser_allow_gnu_extensions_p (parser))
19464 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19466 attrs = cp_parser_std_attribute_spec_seq (parser);
19467 if (attributes != NULL)
19468 *attributes = attrs;
19470 else
19472 /* Try the pointer-to-member case. */
19473 cp_parser_parse_tentatively (parser);
19474 /* Look for the optional `::' operator. */
19475 cp_parser_global_scope_opt (parser,
19476 /*current_scope_valid_p=*/false);
19477 /* Look for the nested-name specifier. */
19478 token = cp_lexer_peek_token (parser->lexer);
19479 cp_parser_nested_name_specifier (parser,
19480 /*typename_keyword_p=*/false,
19481 /*check_dependency_p=*/true,
19482 /*type_p=*/false,
19483 /*is_declaration=*/false);
19484 /* If we found it, and the next token is a `*', then we are
19485 indeed looking at a pointer-to-member operator. */
19486 if (!cp_parser_error_occurred (parser)
19487 && cp_parser_require (parser, CPP_MULT, RT_MULT))
19489 /* Indicate that the `*' operator was used. */
19490 code = INDIRECT_REF;
19492 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
19493 error_at (token->location, "%qD is a namespace", parser->scope);
19494 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
19495 error_at (token->location, "cannot form pointer to member of "
19496 "non-class %q#T", parser->scope);
19497 else
19499 /* The type of which the member is a member is given by the
19500 current SCOPE. */
19501 *type = parser->scope;
19502 /* The next name will not be qualified. */
19503 parser->scope = NULL_TREE;
19504 parser->qualifying_scope = NULL_TREE;
19505 parser->object_scope = NULL_TREE;
19506 /* Look for optional c++11 attributes. */
19507 attrs = cp_parser_std_attribute_spec_seq (parser);
19508 if (attributes != NULL)
19509 *attributes = attrs;
19510 /* Look for the optional cv-qualifier-seq. */
19511 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19514 /* If that didn't work we don't have a ptr-operator. */
19515 if (!cp_parser_parse_definitely (parser))
19516 cp_parser_error (parser, "expected ptr-operator");
19519 return code;
19522 /* Parse an (optional) cv-qualifier-seq.
19524 cv-qualifier-seq:
19525 cv-qualifier cv-qualifier-seq [opt]
19527 cv-qualifier:
19528 const
19529 volatile
19531 GNU Extension:
19533 cv-qualifier:
19534 __restrict__
19536 Returns a bitmask representing the cv-qualifiers. */
19538 static cp_cv_quals
19539 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
19541 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
19543 while (true)
19545 cp_token *token;
19546 cp_cv_quals cv_qualifier;
19548 /* Peek at the next token. */
19549 token = cp_lexer_peek_token (parser->lexer);
19550 /* See if it's a cv-qualifier. */
19551 switch (token->keyword)
19553 case RID_CONST:
19554 cv_qualifier = TYPE_QUAL_CONST;
19555 break;
19557 case RID_VOLATILE:
19558 cv_qualifier = TYPE_QUAL_VOLATILE;
19559 break;
19561 case RID_RESTRICT:
19562 cv_qualifier = TYPE_QUAL_RESTRICT;
19563 break;
19565 default:
19566 cv_qualifier = TYPE_UNQUALIFIED;
19567 break;
19570 if (!cv_qualifier)
19571 break;
19573 if (cv_quals & cv_qualifier)
19575 error_at (token->location, "duplicate cv-qualifier");
19576 cp_lexer_purge_token (parser->lexer);
19578 else
19580 cp_lexer_consume_token (parser->lexer);
19581 cv_quals |= cv_qualifier;
19585 return cv_quals;
19588 /* Parse an (optional) ref-qualifier
19590 ref-qualifier:
19594 Returns cp_ref_qualifier representing ref-qualifier. */
19596 static cp_ref_qualifier
19597 cp_parser_ref_qualifier_opt (cp_parser* parser)
19599 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
19601 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
19602 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
19603 return ref_qual;
19605 while (true)
19607 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
19608 cp_token *token = cp_lexer_peek_token (parser->lexer);
19610 switch (token->type)
19612 case CPP_AND:
19613 curr_ref_qual = REF_QUAL_LVALUE;
19614 break;
19616 case CPP_AND_AND:
19617 curr_ref_qual = REF_QUAL_RVALUE;
19618 break;
19620 default:
19621 curr_ref_qual = REF_QUAL_NONE;
19622 break;
19625 if (!curr_ref_qual)
19626 break;
19627 else if (ref_qual)
19629 error_at (token->location, "multiple ref-qualifiers");
19630 cp_lexer_purge_token (parser->lexer);
19632 else
19634 ref_qual = curr_ref_qual;
19635 cp_lexer_consume_token (parser->lexer);
19639 return ref_qual;
19642 /* Parse an optional tx-qualifier.
19644 tx-qualifier:
19645 transaction_safe
19646 transaction_safe_dynamic */
19648 static tree
19649 cp_parser_tx_qualifier_opt (cp_parser *parser)
19651 cp_token *token = cp_lexer_peek_token (parser->lexer);
19652 if (token->type == CPP_NAME)
19654 tree name = token->u.value;
19655 const char *p = IDENTIFIER_POINTER (name);
19656 const int len = strlen ("transaction_safe");
19657 if (!strncmp (p, "transaction_safe", len))
19659 p += len;
19660 if (*p == '\0'
19661 || !strcmp (p, "_dynamic"))
19663 cp_lexer_consume_token (parser->lexer);
19664 if (!flag_tm)
19666 error ("%E requires %<-fgnu-tm%>", name);
19667 return NULL_TREE;
19669 else
19670 return name;
19674 return NULL_TREE;
19677 /* Parse an (optional) virt-specifier-seq.
19679 virt-specifier-seq:
19680 virt-specifier virt-specifier-seq [opt]
19682 virt-specifier:
19683 override
19684 final
19686 Returns a bitmask representing the virt-specifiers. */
19688 static cp_virt_specifiers
19689 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
19691 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19693 while (true)
19695 cp_token *token;
19696 cp_virt_specifiers virt_specifier;
19698 /* Peek at the next token. */
19699 token = cp_lexer_peek_token (parser->lexer);
19700 /* See if it's a virt-specifier-qualifier. */
19701 if (token->type != CPP_NAME)
19702 break;
19703 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
19705 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19706 virt_specifier = VIRT_SPEC_OVERRIDE;
19708 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
19710 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19711 virt_specifier = VIRT_SPEC_FINAL;
19713 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
19715 virt_specifier = VIRT_SPEC_FINAL;
19717 else
19718 break;
19720 if (virt_specifiers & virt_specifier)
19722 error_at (token->location, "duplicate virt-specifier");
19723 cp_lexer_purge_token (parser->lexer);
19725 else
19727 cp_lexer_consume_token (parser->lexer);
19728 virt_specifiers |= virt_specifier;
19731 return virt_specifiers;
19734 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
19735 is in scope even though it isn't real. */
19737 void
19738 inject_this_parameter (tree ctype, cp_cv_quals quals)
19740 tree this_parm;
19742 if (current_class_ptr)
19744 /* We don't clear this between NSDMIs. Is it already what we want? */
19745 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
19746 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
19747 && cp_type_quals (type) == quals)
19748 return;
19751 this_parm = build_this_parm (ctype, quals);
19752 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
19753 current_class_ptr = NULL_TREE;
19754 current_class_ref
19755 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
19756 current_class_ptr = this_parm;
19759 /* Return true iff our current scope is a non-static data member
19760 initializer. */
19762 bool
19763 parsing_nsdmi (void)
19765 /* We recognize NSDMI context by the context-less 'this' pointer set up
19766 by the function above. */
19767 if (current_class_ptr
19768 && TREE_CODE (current_class_ptr) == PARM_DECL
19769 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
19770 return true;
19771 return false;
19774 /* Parse a late-specified return type, if any. This is not a separate
19775 non-terminal, but part of a function declarator, which looks like
19777 -> trailing-type-specifier-seq abstract-declarator(opt)
19779 Returns the type indicated by the type-id.
19781 In addition to this, parse any queued up omp declare simd
19782 clauses and Cilk Plus SIMD-enabled function's vector attributes.
19784 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
19785 function. */
19787 static tree
19788 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
19789 tree& requires_clause, cp_cv_quals quals)
19791 cp_token *token;
19792 tree type = NULL_TREE;
19793 bool declare_simd_p = (parser->omp_declare_simd
19794 && declarator
19795 && declarator->kind == cdk_id);
19797 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
19798 && declarator && declarator->kind == cdk_id);
19800 bool oacc_routine_p = (parser->oacc_routine
19801 && declarator
19802 && declarator->kind == cdk_id);
19804 /* Peek at the next token. */
19805 token = cp_lexer_peek_token (parser->lexer);
19806 /* A late-specified return type is indicated by an initial '->'. */
19807 if (token->type != CPP_DEREF
19808 && token->keyword != RID_REQUIRES
19809 && !(token->type == CPP_NAME
19810 && token->u.value == ridpointers[RID_REQUIRES])
19811 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
19812 return NULL_TREE;
19814 tree save_ccp = current_class_ptr;
19815 tree save_ccr = current_class_ref;
19816 if (quals >= 0)
19818 /* DR 1207: 'this' is in scope in the trailing return type. */
19819 inject_this_parameter (current_class_type, quals);
19822 if (token->type == CPP_DEREF)
19824 /* Consume the ->. */
19825 cp_lexer_consume_token (parser->lexer);
19827 type = cp_parser_trailing_type_id (parser);
19830 /* Function declarations may be followed by a trailing
19831 requires-clause. */
19832 requires_clause = cp_parser_requires_clause_opt (parser);
19834 if (cilk_simd_fn_vector_p)
19835 declarator->attributes
19836 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
19837 declarator->attributes);
19838 if (declare_simd_p)
19839 declarator->attributes
19840 = cp_parser_late_parsing_omp_declare_simd (parser,
19841 declarator->attributes);
19842 if (oacc_routine_p)
19843 declarator->attributes
19844 = cp_parser_late_parsing_oacc_routine (parser,
19845 declarator->attributes);
19847 if (quals >= 0)
19849 current_class_ptr = save_ccp;
19850 current_class_ref = save_ccr;
19853 return type;
19856 /* Parse a declarator-id.
19858 declarator-id:
19859 id-expression
19860 :: [opt] nested-name-specifier [opt] type-name
19862 In the `id-expression' case, the value returned is as for
19863 cp_parser_id_expression if the id-expression was an unqualified-id.
19864 If the id-expression was a qualified-id, then a SCOPE_REF is
19865 returned. The first operand is the scope (either a NAMESPACE_DECL
19866 or TREE_TYPE), but the second is still just a representation of an
19867 unqualified-id. */
19869 static tree
19870 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
19872 tree id;
19873 /* The expression must be an id-expression. Assume that qualified
19874 names are the names of types so that:
19876 template <class T>
19877 int S<T>::R::i = 3;
19879 will work; we must treat `S<T>::R' as the name of a type.
19880 Similarly, assume that qualified names are templates, where
19881 required, so that:
19883 template <class T>
19884 int S<T>::R<T>::i = 3;
19886 will work, too. */
19887 id = cp_parser_id_expression (parser,
19888 /*template_keyword_p=*/false,
19889 /*check_dependency_p=*/false,
19890 /*template_p=*/NULL,
19891 /*declarator_p=*/true,
19892 optional_p);
19893 if (id && BASELINK_P (id))
19894 id = BASELINK_FUNCTIONS (id);
19895 return id;
19898 /* Parse a type-id.
19900 type-id:
19901 type-specifier-seq abstract-declarator [opt]
19903 Returns the TYPE specified. */
19905 static tree
19906 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
19907 bool is_trailing_return)
19909 cp_decl_specifier_seq type_specifier_seq;
19910 cp_declarator *abstract_declarator;
19912 /* Parse the type-specifier-seq. */
19913 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
19914 is_trailing_return,
19915 &type_specifier_seq);
19916 if (type_specifier_seq.type == error_mark_node)
19917 return error_mark_node;
19919 /* There might or might not be an abstract declarator. */
19920 cp_parser_parse_tentatively (parser);
19921 /* Look for the declarator. */
19922 abstract_declarator
19923 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
19924 /*parenthesized_p=*/NULL,
19925 /*member_p=*/false,
19926 /*friend_p=*/false);
19927 /* Check to see if there really was a declarator. */
19928 if (!cp_parser_parse_definitely (parser))
19929 abstract_declarator = NULL;
19931 if (type_specifier_seq.type
19932 /* The concepts TS allows 'auto' as a type-id. */
19933 && (!flag_concepts || parser->in_type_id_in_expr_p)
19934 /* None of the valid uses of 'auto' in C++14 involve the type-id
19935 nonterminal, but it is valid in a trailing-return-type. */
19936 && !(cxx_dialect >= cxx14 && is_trailing_return)
19937 && type_uses_auto (type_specifier_seq.type))
19939 /* A type-id with type 'auto' is only ok if the abstract declarator
19940 is a function declarator with a late-specified return type.
19942 A type-id with 'auto' is also valid in a trailing-return-type
19943 in a compound-requirement. */
19944 if (abstract_declarator
19945 && abstract_declarator->kind == cdk_function
19946 && abstract_declarator->u.function.late_return_type)
19947 /* OK */;
19948 else if (parser->in_result_type_constraint_p)
19949 /* OK */;
19950 else
19952 error ("invalid use of %<auto%>");
19953 return error_mark_node;
19957 return groktypename (&type_specifier_seq, abstract_declarator,
19958 is_template_arg);
19961 static tree
19962 cp_parser_type_id (cp_parser *parser)
19964 return cp_parser_type_id_1 (parser, false, false);
19967 static tree
19968 cp_parser_template_type_arg (cp_parser *parser)
19970 tree r;
19971 const char *saved_message = parser->type_definition_forbidden_message;
19972 parser->type_definition_forbidden_message
19973 = G_("types may not be defined in template arguments");
19974 r = cp_parser_type_id_1 (parser, true, false);
19975 parser->type_definition_forbidden_message = saved_message;
19976 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
19978 error ("invalid use of %<auto%> in template argument");
19979 r = error_mark_node;
19981 return r;
19984 static tree
19985 cp_parser_trailing_type_id (cp_parser *parser)
19987 return cp_parser_type_id_1 (parser, false, true);
19990 /* Parse a type-specifier-seq.
19992 type-specifier-seq:
19993 type-specifier type-specifier-seq [opt]
19995 GNU extension:
19997 type-specifier-seq:
19998 attributes type-specifier-seq [opt]
20000 If IS_DECLARATION is true, we are at the start of a "condition" or
20001 exception-declaration, so we might be followed by a declarator-id.
20003 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20004 i.e. we've just seen "->".
20006 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20008 static void
20009 cp_parser_type_specifier_seq (cp_parser* parser,
20010 bool is_declaration,
20011 bool is_trailing_return,
20012 cp_decl_specifier_seq *type_specifier_seq)
20014 bool seen_type_specifier = false;
20015 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20016 cp_token *start_token = NULL;
20018 /* Clear the TYPE_SPECIFIER_SEQ. */
20019 clear_decl_specs (type_specifier_seq);
20021 /* In the context of a trailing return type, enum E { } is an
20022 elaborated-type-specifier followed by a function-body, not an
20023 enum-specifier. */
20024 if (is_trailing_return)
20025 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20027 /* Parse the type-specifiers and attributes. */
20028 while (true)
20030 tree type_specifier;
20031 bool is_cv_qualifier;
20033 /* Check for attributes first. */
20034 if (cp_next_tokens_can_be_attribute_p (parser))
20036 type_specifier_seq->attributes =
20037 chainon (type_specifier_seq->attributes,
20038 cp_parser_attributes_opt (parser));
20039 continue;
20042 /* record the token of the beginning of the type specifier seq,
20043 for error reporting purposes*/
20044 if (!start_token)
20045 start_token = cp_lexer_peek_token (parser->lexer);
20047 /* Look for the type-specifier. */
20048 type_specifier = cp_parser_type_specifier (parser,
20049 flags,
20050 type_specifier_seq,
20051 /*is_declaration=*/false,
20052 NULL,
20053 &is_cv_qualifier);
20054 if (!type_specifier)
20056 /* If the first type-specifier could not be found, this is not a
20057 type-specifier-seq at all. */
20058 if (!seen_type_specifier)
20060 /* Set in_declarator_p to avoid skipping to the semicolon. */
20061 int in_decl = parser->in_declarator_p;
20062 parser->in_declarator_p = true;
20064 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20065 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20066 cp_parser_error (parser, "expected type-specifier");
20068 parser->in_declarator_p = in_decl;
20070 type_specifier_seq->type = error_mark_node;
20071 return;
20073 /* If subsequent type-specifiers could not be found, the
20074 type-specifier-seq is complete. */
20075 break;
20078 seen_type_specifier = true;
20079 /* The standard says that a condition can be:
20081 type-specifier-seq declarator = assignment-expression
20083 However, given:
20085 struct S {};
20086 if (int S = ...)
20088 we should treat the "S" as a declarator, not as a
20089 type-specifier. The standard doesn't say that explicitly for
20090 type-specifier-seq, but it does say that for
20091 decl-specifier-seq in an ordinary declaration. Perhaps it
20092 would be clearer just to allow a decl-specifier-seq here, and
20093 then add a semantic restriction that if any decl-specifiers
20094 that are not type-specifiers appear, the program is invalid. */
20095 if (is_declaration && !is_cv_qualifier)
20096 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20100 /* Return whether the function currently being declared has an associated
20101 template parameter list. */
20103 static bool
20104 function_being_declared_is_template_p (cp_parser* parser)
20106 if (!current_template_parms || processing_template_parmlist)
20107 return false;
20109 if (parser->implicit_template_scope)
20110 return true;
20112 if (at_class_scope_p ()
20113 && TYPE_BEING_DEFINED (current_class_type))
20114 return parser->num_template_parameter_lists != 0;
20116 return ((int) parser->num_template_parameter_lists > template_class_depth
20117 (current_class_type));
20120 /* Parse a parameter-declaration-clause.
20122 parameter-declaration-clause:
20123 parameter-declaration-list [opt] ... [opt]
20124 parameter-declaration-list , ...
20126 Returns a representation for the parameter declarations. A return
20127 value of NULL indicates a parameter-declaration-clause consisting
20128 only of an ellipsis. */
20130 static tree
20131 cp_parser_parameter_declaration_clause (cp_parser* parser)
20133 tree parameters;
20134 cp_token *token;
20135 bool ellipsis_p;
20136 bool is_error;
20138 struct cleanup {
20139 cp_parser* parser;
20140 int auto_is_implicit_function_template_parm_p;
20141 ~cleanup() {
20142 parser->auto_is_implicit_function_template_parm_p
20143 = auto_is_implicit_function_template_parm_p;
20145 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
20147 (void) cleanup;
20149 if (!processing_specialization
20150 && !processing_template_parmlist
20151 && !processing_explicit_instantiation)
20152 if (!current_function_decl
20153 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
20154 parser->auto_is_implicit_function_template_parm_p = true;
20156 /* Peek at the next token. */
20157 token = cp_lexer_peek_token (parser->lexer);
20158 /* Check for trivial parameter-declaration-clauses. */
20159 if (token->type == CPP_ELLIPSIS)
20161 /* Consume the `...' token. */
20162 cp_lexer_consume_token (parser->lexer);
20163 return NULL_TREE;
20165 else if (token->type == CPP_CLOSE_PAREN)
20166 /* There are no parameters. */
20168 #ifndef NO_IMPLICIT_EXTERN_C
20169 if (in_system_header_at (input_location)
20170 && current_class_type == NULL
20171 && current_lang_name == lang_name_c)
20172 return NULL_TREE;
20173 else
20174 #endif
20175 return void_list_node;
20177 /* Check for `(void)', too, which is a special case. */
20178 else if (token->keyword == RID_VOID
20179 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20180 == CPP_CLOSE_PAREN))
20182 /* Consume the `void' token. */
20183 cp_lexer_consume_token (parser->lexer);
20184 /* There are no parameters. */
20185 return void_list_node;
20188 /* Parse the parameter-declaration-list. */
20189 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
20190 /* If a parse error occurred while parsing the
20191 parameter-declaration-list, then the entire
20192 parameter-declaration-clause is erroneous. */
20193 if (is_error)
20194 return NULL;
20196 /* Peek at the next token. */
20197 token = cp_lexer_peek_token (parser->lexer);
20198 /* If it's a `,', the clause should terminate with an ellipsis. */
20199 if (token->type == CPP_COMMA)
20201 /* Consume the `,'. */
20202 cp_lexer_consume_token (parser->lexer);
20203 /* Expect an ellipsis. */
20204 ellipsis_p
20205 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
20207 /* It might also be `...' if the optional trailing `,' was
20208 omitted. */
20209 else if (token->type == CPP_ELLIPSIS)
20211 /* Consume the `...' token. */
20212 cp_lexer_consume_token (parser->lexer);
20213 /* And remember that we saw it. */
20214 ellipsis_p = true;
20216 else
20217 ellipsis_p = false;
20219 /* Finish the parameter list. */
20220 if (!ellipsis_p)
20221 parameters = chainon (parameters, void_list_node);
20223 return parameters;
20226 /* Parse a parameter-declaration-list.
20228 parameter-declaration-list:
20229 parameter-declaration
20230 parameter-declaration-list , parameter-declaration
20232 Returns a representation of the parameter-declaration-list, as for
20233 cp_parser_parameter_declaration_clause. However, the
20234 `void_list_node' is never appended to the list. Upon return,
20235 *IS_ERROR will be true iff an error occurred. */
20237 static tree
20238 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
20240 tree parameters = NULL_TREE;
20241 tree *tail = &parameters;
20242 bool saved_in_unbraced_linkage_specification_p;
20243 int index = 0;
20245 /* Assume all will go well. */
20246 *is_error = false;
20247 /* The special considerations that apply to a function within an
20248 unbraced linkage specifications do not apply to the parameters
20249 to the function. */
20250 saved_in_unbraced_linkage_specification_p
20251 = parser->in_unbraced_linkage_specification_p;
20252 parser->in_unbraced_linkage_specification_p = false;
20254 /* Look for more parameters. */
20255 while (true)
20257 cp_parameter_declarator *parameter;
20258 tree decl = error_mark_node;
20259 bool parenthesized_p = false;
20260 int template_parm_idx = (function_being_declared_is_template_p (parser)?
20261 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
20262 (current_template_parms)) : 0);
20264 /* Parse the parameter. */
20265 parameter
20266 = cp_parser_parameter_declaration (parser,
20267 /*template_parm_p=*/false,
20268 &parenthesized_p);
20270 /* We don't know yet if the enclosing context is deprecated, so wait
20271 and warn in grokparms if appropriate. */
20272 deprecated_state = DEPRECATED_SUPPRESS;
20274 if (parameter)
20276 /* If a function parameter pack was specified and an implicit template
20277 parameter was introduced during cp_parser_parameter_declaration,
20278 change any implicit parameters introduced into packs. */
20279 if (parser->implicit_template_parms
20280 && parameter->declarator
20281 && parameter->declarator->parameter_pack_p)
20283 int latest_template_parm_idx = TREE_VEC_LENGTH
20284 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
20286 if (latest_template_parm_idx != template_parm_idx)
20287 parameter->decl_specifiers.type = convert_generic_types_to_packs
20288 (parameter->decl_specifiers.type,
20289 template_parm_idx, latest_template_parm_idx);
20292 decl = grokdeclarator (parameter->declarator,
20293 &parameter->decl_specifiers,
20294 PARM,
20295 parameter->default_argument != NULL_TREE,
20296 &parameter->decl_specifiers.attributes);
20299 deprecated_state = DEPRECATED_NORMAL;
20301 /* If a parse error occurred parsing the parameter declaration,
20302 then the entire parameter-declaration-list is erroneous. */
20303 if (decl == error_mark_node)
20305 *is_error = true;
20306 parameters = error_mark_node;
20307 break;
20310 if (parameter->decl_specifiers.attributes)
20311 cplus_decl_attributes (&decl,
20312 parameter->decl_specifiers.attributes,
20314 if (DECL_NAME (decl))
20315 decl = pushdecl (decl);
20317 if (decl != error_mark_node)
20319 retrofit_lang_decl (decl);
20320 DECL_PARM_INDEX (decl) = ++index;
20321 DECL_PARM_LEVEL (decl) = function_parm_depth ();
20324 /* Add the new parameter to the list. */
20325 *tail = build_tree_list (parameter->default_argument, decl);
20326 tail = &TREE_CHAIN (*tail);
20328 /* Peek at the next token. */
20329 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
20330 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
20331 /* These are for Objective-C++ */
20332 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20333 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20334 /* The parameter-declaration-list is complete. */
20335 break;
20336 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20338 cp_token *token;
20340 /* Peek at the next token. */
20341 token = cp_lexer_peek_nth_token (parser->lexer, 2);
20342 /* If it's an ellipsis, then the list is complete. */
20343 if (token->type == CPP_ELLIPSIS)
20344 break;
20345 /* Otherwise, there must be more parameters. Consume the
20346 `,'. */
20347 cp_lexer_consume_token (parser->lexer);
20348 /* When parsing something like:
20350 int i(float f, double d)
20352 we can tell after seeing the declaration for "f" that we
20353 are not looking at an initialization of a variable "i",
20354 but rather at the declaration of a function "i".
20356 Due to the fact that the parsing of template arguments
20357 (as specified to a template-id) requires backtracking we
20358 cannot use this technique when inside a template argument
20359 list. */
20360 if (!parser->in_template_argument_list_p
20361 && !parser->in_type_id_in_expr_p
20362 && cp_parser_uncommitted_to_tentative_parse_p (parser)
20363 /* However, a parameter-declaration of the form
20364 "float(f)" (which is a valid declaration of a
20365 parameter "f") can also be interpreted as an
20366 expression (the conversion of "f" to "float"). */
20367 && !parenthesized_p)
20368 cp_parser_commit_to_tentative_parse (parser);
20370 else
20372 cp_parser_error (parser, "expected %<,%> or %<...%>");
20373 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20374 cp_parser_skip_to_closing_parenthesis (parser,
20375 /*recovering=*/true,
20376 /*or_comma=*/false,
20377 /*consume_paren=*/false);
20378 break;
20382 parser->in_unbraced_linkage_specification_p
20383 = saved_in_unbraced_linkage_specification_p;
20385 /* Reset implicit_template_scope if we are about to leave the function
20386 parameter list that introduced it. Note that for out-of-line member
20387 definitions, there will be one or more class scopes before we get to
20388 the template parameter scope. */
20390 if (cp_binding_level *its = parser->implicit_template_scope)
20391 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
20393 while (maybe_its->kind == sk_class)
20394 maybe_its = maybe_its->level_chain;
20395 if (maybe_its == its)
20397 parser->implicit_template_parms = 0;
20398 parser->implicit_template_scope = 0;
20402 return parameters;
20405 /* Parse a parameter declaration.
20407 parameter-declaration:
20408 decl-specifier-seq ... [opt] declarator
20409 decl-specifier-seq declarator = assignment-expression
20410 decl-specifier-seq ... [opt] abstract-declarator [opt]
20411 decl-specifier-seq abstract-declarator [opt] = assignment-expression
20413 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
20414 declares a template parameter. (In that case, a non-nested `>'
20415 token encountered during the parsing of the assignment-expression
20416 is not interpreted as a greater-than operator.)
20418 Returns a representation of the parameter, or NULL if an error
20419 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
20420 true iff the declarator is of the form "(p)". */
20422 static cp_parameter_declarator *
20423 cp_parser_parameter_declaration (cp_parser *parser,
20424 bool template_parm_p,
20425 bool *parenthesized_p)
20427 int declares_class_or_enum;
20428 cp_decl_specifier_seq decl_specifiers;
20429 cp_declarator *declarator;
20430 tree default_argument;
20431 cp_token *token = NULL, *declarator_token_start = NULL;
20432 const char *saved_message;
20433 bool template_parameter_pack_p = false;
20435 /* In a template parameter, `>' is not an operator.
20437 [temp.param]
20439 When parsing a default template-argument for a non-type
20440 template-parameter, the first non-nested `>' is taken as the end
20441 of the template parameter-list rather than a greater-than
20442 operator. */
20444 /* Type definitions may not appear in parameter types. */
20445 saved_message = parser->type_definition_forbidden_message;
20446 parser->type_definition_forbidden_message
20447 = G_("types may not be defined in parameter types");
20449 /* Parse the declaration-specifiers. */
20450 cp_parser_decl_specifier_seq (parser,
20451 CP_PARSER_FLAGS_NONE,
20452 &decl_specifiers,
20453 &declares_class_or_enum);
20455 /* Complain about missing 'typename' or other invalid type names. */
20456 if (!decl_specifiers.any_type_specifiers_p
20457 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20458 decl_specifiers.type = error_mark_node;
20460 /* If an error occurred, there's no reason to attempt to parse the
20461 rest of the declaration. */
20462 if (cp_parser_error_occurred (parser))
20464 parser->type_definition_forbidden_message = saved_message;
20465 return NULL;
20468 /* Peek at the next token. */
20469 token = cp_lexer_peek_token (parser->lexer);
20471 /* If the next token is a `)', `,', `=', `>', or `...', then there
20472 is no declarator. However, when variadic templates are enabled,
20473 there may be a declarator following `...'. */
20474 if (token->type == CPP_CLOSE_PAREN
20475 || token->type == CPP_COMMA
20476 || token->type == CPP_EQ
20477 || token->type == CPP_GREATER)
20479 declarator = NULL;
20480 if (parenthesized_p)
20481 *parenthesized_p = false;
20483 /* Otherwise, there should be a declarator. */
20484 else
20486 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20487 parser->default_arg_ok_p = false;
20489 /* After seeing a decl-specifier-seq, if the next token is not a
20490 "(", there is no possibility that the code is a valid
20491 expression. Therefore, if parsing tentatively, we commit at
20492 this point. */
20493 if (!parser->in_template_argument_list_p
20494 /* In an expression context, having seen:
20496 (int((char ...
20498 we cannot be sure whether we are looking at a
20499 function-type (taking a "char" as a parameter) or a cast
20500 of some object of type "char" to "int". */
20501 && !parser->in_type_id_in_expr_p
20502 && cp_parser_uncommitted_to_tentative_parse_p (parser)
20503 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20504 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
20505 cp_parser_commit_to_tentative_parse (parser);
20506 /* Parse the declarator. */
20507 declarator_token_start = token;
20508 declarator = cp_parser_declarator (parser,
20509 CP_PARSER_DECLARATOR_EITHER,
20510 /*ctor_dtor_or_conv_p=*/NULL,
20511 parenthesized_p,
20512 /*member_p=*/false,
20513 /*friend_p=*/false);
20514 parser->default_arg_ok_p = saved_default_arg_ok_p;
20515 /* After the declarator, allow more attributes. */
20516 decl_specifiers.attributes
20517 = chainon (decl_specifiers.attributes,
20518 cp_parser_attributes_opt (parser));
20520 /* If the declarator is a template parameter pack, remember that and
20521 clear the flag in the declarator itself so we don't get errors
20522 from grokdeclarator. */
20523 if (template_parm_p && declarator && declarator->parameter_pack_p)
20525 declarator->parameter_pack_p = false;
20526 template_parameter_pack_p = true;
20530 /* If the next token is an ellipsis, and we have not seen a declarator
20531 name, and if either the type of the declarator contains parameter
20532 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
20533 for, eg, abbreviated integral type names), then we actually have a
20534 parameter pack expansion expression. Otherwise, leave the ellipsis
20535 for a C-style variadic function. */
20536 token = cp_lexer_peek_token (parser->lexer);
20537 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20539 tree type = decl_specifiers.type;
20541 if (type && DECL_P (type))
20542 type = TREE_TYPE (type);
20544 if (((type
20545 && TREE_CODE (type) != TYPE_PACK_EXPANSION
20546 && (template_parm_p || uses_parameter_packs (type)))
20547 || (!type && template_parm_p))
20548 && declarator_can_be_parameter_pack (declarator))
20550 /* Consume the `...'. */
20551 cp_lexer_consume_token (parser->lexer);
20552 maybe_warn_variadic_templates ();
20554 /* Build a pack expansion type */
20555 if (template_parm_p)
20556 template_parameter_pack_p = true;
20557 else if (declarator)
20558 declarator->parameter_pack_p = true;
20559 else
20560 decl_specifiers.type = make_pack_expansion (type);
20564 /* The restriction on defining new types applies only to the type
20565 of the parameter, not to the default argument. */
20566 parser->type_definition_forbidden_message = saved_message;
20568 /* If the next token is `=', then process a default argument. */
20569 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20571 tree type = decl_specifiers.type;
20572 token = cp_lexer_peek_token (parser->lexer);
20573 /* If we are defining a class, then the tokens that make up the
20574 default argument must be saved and processed later. */
20575 if (!template_parm_p && at_class_scope_p ()
20576 && TYPE_BEING_DEFINED (current_class_type)
20577 && !LAMBDA_TYPE_P (current_class_type))
20578 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
20580 // A constrained-type-specifier may declare a type template-parameter.
20581 else if (declares_constrained_type_template_parameter (type))
20582 default_argument
20583 = cp_parser_default_type_template_argument (parser);
20585 // A constrained-type-specifier may declare a template-template-parameter.
20586 else if (declares_constrained_template_template_parameter (type))
20587 default_argument
20588 = cp_parser_default_template_template_argument (parser);
20590 /* Outside of a class definition, we can just parse the
20591 assignment-expression. */
20592 else
20593 default_argument
20594 = cp_parser_default_argument (parser, template_parm_p);
20596 if (!parser->default_arg_ok_p)
20598 permerror (token->location,
20599 "default arguments are only "
20600 "permitted for function parameters");
20602 else if ((declarator && declarator->parameter_pack_p)
20603 || template_parameter_pack_p
20604 || (decl_specifiers.type
20605 && PACK_EXPANSION_P (decl_specifiers.type)))
20607 /* Find the name of the parameter pack. */
20608 cp_declarator *id_declarator = declarator;
20609 while (id_declarator && id_declarator->kind != cdk_id)
20610 id_declarator = id_declarator->declarator;
20612 if (id_declarator && id_declarator->kind == cdk_id)
20613 error_at (declarator_token_start->location,
20614 template_parm_p
20615 ? G_("template parameter pack %qD "
20616 "cannot have a default argument")
20617 : G_("parameter pack %qD cannot have "
20618 "a default argument"),
20619 id_declarator->u.id.unqualified_name);
20620 else
20621 error_at (declarator_token_start->location,
20622 template_parm_p
20623 ? G_("template parameter pack cannot have "
20624 "a default argument")
20625 : G_("parameter pack cannot have a "
20626 "default argument"));
20628 default_argument = NULL_TREE;
20631 else
20632 default_argument = NULL_TREE;
20634 return make_parameter_declarator (&decl_specifiers,
20635 declarator,
20636 default_argument,
20637 template_parameter_pack_p);
20640 /* Parse a default argument and return it.
20642 TEMPLATE_PARM_P is true if this is a default argument for a
20643 non-type template parameter. */
20644 static tree
20645 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
20647 tree default_argument = NULL_TREE;
20648 bool saved_greater_than_is_operator_p;
20649 bool saved_local_variables_forbidden_p;
20650 bool non_constant_p, is_direct_init;
20652 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
20653 set correctly. */
20654 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
20655 parser->greater_than_is_operator_p = !template_parm_p;
20656 /* Local variable names (and the `this' keyword) may not
20657 appear in a default argument. */
20658 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20659 parser->local_variables_forbidden_p = true;
20660 /* Parse the assignment-expression. */
20661 if (template_parm_p)
20662 push_deferring_access_checks (dk_no_deferred);
20663 tree saved_class_ptr = NULL_TREE;
20664 tree saved_class_ref = NULL_TREE;
20665 /* The "this" pointer is not valid in a default argument. */
20666 if (cfun)
20668 saved_class_ptr = current_class_ptr;
20669 cp_function_chain->x_current_class_ptr = NULL_TREE;
20670 saved_class_ref = current_class_ref;
20671 cp_function_chain->x_current_class_ref = NULL_TREE;
20673 default_argument
20674 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
20675 /* Restore the "this" pointer. */
20676 if (cfun)
20678 cp_function_chain->x_current_class_ptr = saved_class_ptr;
20679 cp_function_chain->x_current_class_ref = saved_class_ref;
20681 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
20682 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20683 if (template_parm_p)
20684 pop_deferring_access_checks ();
20685 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
20686 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20688 return default_argument;
20691 /* Parse a function-body.
20693 function-body:
20694 compound_statement */
20696 static void
20697 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
20699 cp_parser_compound_statement (parser, NULL, (in_function_try_block
20700 ? BCS_TRY_BLOCK : BCS_NORMAL),
20701 true);
20704 /* Parse a ctor-initializer-opt followed by a function-body. Return
20705 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
20706 is true we are parsing a function-try-block. */
20708 static bool
20709 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
20710 bool in_function_try_block)
20712 tree body, list;
20713 bool ctor_initializer_p;
20714 const bool check_body_p =
20715 DECL_CONSTRUCTOR_P (current_function_decl)
20716 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
20717 tree last = NULL;
20719 /* Begin the function body. */
20720 body = begin_function_body ();
20721 /* Parse the optional ctor-initializer. */
20722 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
20724 /* If we're parsing a constexpr constructor definition, we need
20725 to check that the constructor body is indeed empty. However,
20726 before we get to cp_parser_function_body lot of junk has been
20727 generated, so we can't just check that we have an empty block.
20728 Rather we take a snapshot of the outermost block, and check whether
20729 cp_parser_function_body changed its state. */
20730 if (check_body_p)
20732 list = cur_stmt_list;
20733 if (STATEMENT_LIST_TAIL (list))
20734 last = STATEMENT_LIST_TAIL (list)->stmt;
20736 /* Parse the function-body. */
20737 cp_parser_function_body (parser, in_function_try_block);
20738 if (check_body_p)
20739 check_constexpr_ctor_body (last, list, /*complain=*/true);
20740 /* Finish the function body. */
20741 finish_function_body (body);
20743 return ctor_initializer_p;
20746 /* Parse an initializer.
20748 initializer:
20749 = initializer-clause
20750 ( expression-list )
20752 Returns an expression representing the initializer. If no
20753 initializer is present, NULL_TREE is returned.
20755 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
20756 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
20757 set to TRUE if there is no initializer present. If there is an
20758 initializer, and it is not a constant-expression, *NON_CONSTANT_P
20759 is set to true; otherwise it is set to false. */
20761 static tree
20762 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
20763 bool* non_constant_p)
20765 cp_token *token;
20766 tree init;
20768 /* Peek at the next token. */
20769 token = cp_lexer_peek_token (parser->lexer);
20771 /* Let our caller know whether or not this initializer was
20772 parenthesized. */
20773 *is_direct_init = (token->type != CPP_EQ);
20774 /* Assume that the initializer is constant. */
20775 *non_constant_p = false;
20777 if (token->type == CPP_EQ)
20779 /* Consume the `='. */
20780 cp_lexer_consume_token (parser->lexer);
20781 /* Parse the initializer-clause. */
20782 init = cp_parser_initializer_clause (parser, non_constant_p);
20784 else if (token->type == CPP_OPEN_PAREN)
20786 vec<tree, va_gc> *vec;
20787 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20788 /*cast_p=*/false,
20789 /*allow_expansion_p=*/true,
20790 non_constant_p);
20791 if (vec == NULL)
20792 return error_mark_node;
20793 init = build_tree_list_vec (vec);
20794 release_tree_vector (vec);
20796 else if (token->type == CPP_OPEN_BRACE)
20798 cp_lexer_set_source_position (parser->lexer);
20799 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20800 init = cp_parser_braced_list (parser, non_constant_p);
20801 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
20803 else
20805 /* Anything else is an error. */
20806 cp_parser_error (parser, "expected initializer");
20807 init = error_mark_node;
20810 if (check_for_bare_parameter_packs (init))
20811 init = error_mark_node;
20813 return init;
20816 /* Parse an initializer-clause.
20818 initializer-clause:
20819 assignment-expression
20820 braced-init-list
20822 Returns an expression representing the initializer.
20824 If the `assignment-expression' production is used the value
20825 returned is simply a representation for the expression.
20827 Otherwise, calls cp_parser_braced_list. */
20829 static cp_expr
20830 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
20832 cp_expr initializer;
20834 /* Assume the expression is constant. */
20835 *non_constant_p = false;
20837 /* If it is not a `{', then we are looking at an
20838 assignment-expression. */
20839 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
20841 initializer
20842 = cp_parser_constant_expression (parser,
20843 /*allow_non_constant_p=*/true,
20844 non_constant_p);
20846 else
20847 initializer = cp_parser_braced_list (parser, non_constant_p);
20849 return initializer;
20852 /* Parse a brace-enclosed initializer list.
20854 braced-init-list:
20855 { initializer-list , [opt] }
20858 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
20859 the elements of the initializer-list (or NULL, if the last
20860 production is used). The TREE_TYPE for the CONSTRUCTOR will be
20861 NULL_TREE. There is no way to detect whether or not the optional
20862 trailing `,' was provided. NON_CONSTANT_P is as for
20863 cp_parser_initializer. */
20865 static cp_expr
20866 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
20868 tree initializer;
20869 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
20871 /* Consume the `{' token. */
20872 cp_lexer_consume_token (parser->lexer);
20873 /* Create a CONSTRUCTOR to represent the braced-initializer. */
20874 initializer = make_node (CONSTRUCTOR);
20875 /* If it's not a `}', then there is a non-trivial initializer. */
20876 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
20878 /* Parse the initializer list. */
20879 CONSTRUCTOR_ELTS (initializer)
20880 = cp_parser_initializer_list (parser, non_constant_p);
20881 /* A trailing `,' token is allowed. */
20882 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20883 cp_lexer_consume_token (parser->lexer);
20885 else
20886 *non_constant_p = false;
20887 /* Now, there should be a trailing `}'. */
20888 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
20889 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20890 TREE_TYPE (initializer) = init_list_type_node;
20892 cp_expr result (initializer);
20893 /* Build a location of the form:
20894 { ... }
20895 ^~~~~~~
20896 with caret==start at the open brace, finish at the close brace. */
20897 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
20898 result.set_location (combined_loc);
20899 return result;
20902 /* Consume tokens up to, and including, the next non-nested closing `]'.
20903 Returns true iff we found a closing `]'. */
20905 static bool
20906 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
20908 unsigned square_depth = 0;
20910 while (true)
20912 cp_token * token = cp_lexer_peek_token (parser->lexer);
20914 switch (token->type)
20916 case CPP_EOF:
20917 case CPP_PRAGMA_EOL:
20918 /* If we've run out of tokens, then there is no closing `]'. */
20919 return false;
20921 case CPP_OPEN_SQUARE:
20922 ++square_depth;
20923 break;
20925 case CPP_CLOSE_SQUARE:
20926 if (!square_depth--)
20928 cp_lexer_consume_token (parser->lexer);
20929 return true;
20931 break;
20933 default:
20934 break;
20937 /* Consume the token. */
20938 cp_lexer_consume_token (parser->lexer);
20942 /* Return true if we are looking at an array-designator, false otherwise. */
20944 static bool
20945 cp_parser_array_designator_p (cp_parser *parser)
20947 /* Consume the `['. */
20948 cp_lexer_consume_token (parser->lexer);
20950 cp_lexer_save_tokens (parser->lexer);
20952 /* Skip tokens until the next token is a closing square bracket.
20953 If we find the closing `]', and the next token is a `=', then
20954 we are looking at an array designator. */
20955 bool array_designator_p
20956 = (cp_parser_skip_to_closing_square_bracket (parser)
20957 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
20959 /* Roll back the tokens we skipped. */
20960 cp_lexer_rollback_tokens (parser->lexer);
20962 return array_designator_p;
20965 /* Parse an initializer-list.
20967 initializer-list:
20968 initializer-clause ... [opt]
20969 initializer-list , initializer-clause ... [opt]
20971 GNU Extension:
20973 initializer-list:
20974 designation initializer-clause ...[opt]
20975 initializer-list , designation initializer-clause ...[opt]
20977 designation:
20978 . identifier =
20979 identifier :
20980 [ constant-expression ] =
20982 Returns a vec of constructor_elt. The VALUE of each elt is an expression
20983 for the initializer. If the INDEX of the elt is non-NULL, it is the
20984 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
20985 as for cp_parser_initializer. */
20987 static vec<constructor_elt, va_gc> *
20988 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
20990 vec<constructor_elt, va_gc> *v = NULL;
20992 /* Assume all of the expressions are constant. */
20993 *non_constant_p = false;
20995 /* Parse the rest of the list. */
20996 while (true)
20998 cp_token *token;
20999 tree designator;
21000 tree initializer;
21001 bool clause_non_constant_p;
21003 /* If the next token is an identifier and the following one is a
21004 colon, we are looking at the GNU designated-initializer
21005 syntax. */
21006 if (cp_parser_allow_gnu_extensions_p (parser)
21007 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21008 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
21010 /* Warn the user that they are using an extension. */
21011 pedwarn (input_location, OPT_Wpedantic,
21012 "ISO C++ does not allow designated initializers");
21013 /* Consume the identifier. */
21014 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21015 /* Consume the `:'. */
21016 cp_lexer_consume_token (parser->lexer);
21018 /* Also handle the C99 syntax, '. id ='. */
21019 else if (cp_parser_allow_gnu_extensions_p (parser)
21020 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21021 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21022 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
21024 /* Warn the user that they are using an extension. */
21025 pedwarn (input_location, OPT_Wpedantic,
21026 "ISO C++ does not allow C99 designated initializers");
21027 /* Consume the `.'. */
21028 cp_lexer_consume_token (parser->lexer);
21029 /* Consume the identifier. */
21030 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21031 /* Consume the `='. */
21032 cp_lexer_consume_token (parser->lexer);
21034 /* Also handle C99 array designators, '[ const ] ='. */
21035 else if (cp_parser_allow_gnu_extensions_p (parser)
21036 && !c_dialect_objc ()
21037 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21039 /* In C++11, [ could start a lambda-introducer. */
21040 bool non_const = false;
21042 cp_parser_parse_tentatively (parser);
21044 if (!cp_parser_array_designator_p (parser))
21046 cp_parser_simulate_error (parser);
21047 designator = NULL_TREE;
21049 else
21051 designator = cp_parser_constant_expression (parser, true,
21052 &non_const);
21053 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21054 cp_parser_require (parser, CPP_EQ, RT_EQ);
21057 if (!cp_parser_parse_definitely (parser))
21058 designator = NULL_TREE;
21059 else if (non_const)
21060 require_potential_rvalue_constant_expression (designator);
21062 else
21063 designator = NULL_TREE;
21065 /* Parse the initializer. */
21066 initializer = cp_parser_initializer_clause (parser,
21067 &clause_non_constant_p);
21068 /* If any clause is non-constant, so is the entire initializer. */
21069 if (clause_non_constant_p)
21070 *non_constant_p = true;
21072 /* If we have an ellipsis, this is an initializer pack
21073 expansion. */
21074 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21076 /* Consume the `...'. */
21077 cp_lexer_consume_token (parser->lexer);
21079 /* Turn the initializer into an initializer expansion. */
21080 initializer = make_pack_expansion (initializer);
21083 /* Add it to the vector. */
21084 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21086 /* If the next token is not a comma, we have reached the end of
21087 the list. */
21088 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21089 break;
21091 /* Peek at the next token. */
21092 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21093 /* If the next token is a `}', then we're still done. An
21094 initializer-clause can have a trailing `,' after the
21095 initializer-list and before the closing `}'. */
21096 if (token->type == CPP_CLOSE_BRACE)
21097 break;
21099 /* Consume the `,' token. */
21100 cp_lexer_consume_token (parser->lexer);
21103 return v;
21106 /* Classes [gram.class] */
21108 /* Parse a class-name.
21110 class-name:
21111 identifier
21112 template-id
21114 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21115 to indicate that names looked up in dependent types should be
21116 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21117 keyword has been used to indicate that the name that appears next
21118 is a template. TAG_TYPE indicates the explicit tag given before
21119 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21120 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21121 is the class being defined in a class-head. If ENUM_OK is TRUE,
21122 enum-names are also accepted.
21124 Returns the TYPE_DECL representing the class. */
21126 static tree
21127 cp_parser_class_name (cp_parser *parser,
21128 bool typename_keyword_p,
21129 bool template_keyword_p,
21130 enum tag_types tag_type,
21131 bool check_dependency_p,
21132 bool class_head_p,
21133 bool is_declaration,
21134 bool enum_ok)
21136 tree decl;
21137 tree scope;
21138 bool typename_p;
21139 cp_token *token;
21140 tree identifier = NULL_TREE;
21142 /* All class-names start with an identifier. */
21143 token = cp_lexer_peek_token (parser->lexer);
21144 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
21146 cp_parser_error (parser, "expected class-name");
21147 return error_mark_node;
21150 /* PARSER->SCOPE can be cleared when parsing the template-arguments
21151 to a template-id, so we save it here. */
21152 scope = parser->scope;
21153 if (scope == error_mark_node)
21154 return error_mark_node;
21156 /* Any name names a type if we're following the `typename' keyword
21157 in a qualified name where the enclosing scope is type-dependent. */
21158 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
21159 && dependent_type_p (scope));
21160 /* Handle the common case (an identifier, but not a template-id)
21161 efficiently. */
21162 if (token->type == CPP_NAME
21163 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
21165 cp_token *identifier_token;
21166 bool ambiguous_p;
21168 /* Look for the identifier. */
21169 identifier_token = cp_lexer_peek_token (parser->lexer);
21170 ambiguous_p = identifier_token->error_reported;
21171 identifier = cp_parser_identifier (parser);
21172 /* If the next token isn't an identifier, we are certainly not
21173 looking at a class-name. */
21174 if (identifier == error_mark_node)
21175 decl = error_mark_node;
21176 /* If we know this is a type-name, there's no need to look it
21177 up. */
21178 else if (typename_p)
21179 decl = identifier;
21180 else
21182 tree ambiguous_decls;
21183 /* If we already know that this lookup is ambiguous, then
21184 we've already issued an error message; there's no reason
21185 to check again. */
21186 if (ambiguous_p)
21188 cp_parser_simulate_error (parser);
21189 return error_mark_node;
21191 /* If the next token is a `::', then the name must be a type
21192 name.
21194 [basic.lookup.qual]
21196 During the lookup for a name preceding the :: scope
21197 resolution operator, object, function, and enumerator
21198 names are ignored. */
21199 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21200 tag_type = scope_type;
21201 /* Look up the name. */
21202 decl = cp_parser_lookup_name (parser, identifier,
21203 tag_type,
21204 /*is_template=*/false,
21205 /*is_namespace=*/false,
21206 check_dependency_p,
21207 &ambiguous_decls,
21208 identifier_token->location);
21209 if (ambiguous_decls)
21211 if (cp_parser_parsing_tentatively (parser))
21212 cp_parser_simulate_error (parser);
21213 return error_mark_node;
21217 else
21219 /* Try a template-id. */
21220 decl = cp_parser_template_id (parser, template_keyword_p,
21221 check_dependency_p,
21222 tag_type,
21223 is_declaration);
21224 if (decl == error_mark_node)
21225 return error_mark_node;
21228 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
21230 /* If this is a typename, create a TYPENAME_TYPE. */
21231 if (typename_p && decl != error_mark_node)
21233 decl = make_typename_type (scope, decl, typename_type,
21234 /*complain=*/tf_error);
21235 if (decl != error_mark_node)
21236 decl = TYPE_NAME (decl);
21239 decl = strip_using_decl (decl);
21241 /* Check to see that it is really the name of a class. */
21242 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21243 && identifier_p (TREE_OPERAND (decl, 0))
21244 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21245 /* Situations like this:
21247 template <typename T> struct A {
21248 typename T::template X<int>::I i;
21251 are problematic. Is `T::template X<int>' a class-name? The
21252 standard does not seem to be definitive, but there is no other
21253 valid interpretation of the following `::'. Therefore, those
21254 names are considered class-names. */
21256 decl = make_typename_type (scope, decl, tag_type, tf_error);
21257 if (decl != error_mark_node)
21258 decl = TYPE_NAME (decl);
21260 else if (TREE_CODE (decl) != TYPE_DECL
21261 || TREE_TYPE (decl) == error_mark_node
21262 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
21263 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
21264 /* In Objective-C 2.0, a classname followed by '.' starts a
21265 dot-syntax expression, and it's not a type-name. */
21266 || (c_dialect_objc ()
21267 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
21268 && objc_is_class_name (decl)))
21269 decl = error_mark_node;
21271 if (decl == error_mark_node)
21272 cp_parser_error (parser, "expected class-name");
21273 else if (identifier && !parser->scope)
21274 maybe_note_name_used_in_class (identifier, decl);
21276 return decl;
21279 /* Parse a class-specifier.
21281 class-specifier:
21282 class-head { member-specification [opt] }
21284 Returns the TREE_TYPE representing the class. */
21286 static tree
21287 cp_parser_class_specifier_1 (cp_parser* parser)
21289 tree type;
21290 tree attributes = NULL_TREE;
21291 bool nested_name_specifier_p;
21292 unsigned saved_num_template_parameter_lists;
21293 bool saved_in_function_body;
21294 unsigned char in_statement;
21295 bool in_switch_statement_p;
21296 bool saved_in_unbraced_linkage_specification_p;
21297 tree old_scope = NULL_TREE;
21298 tree scope = NULL_TREE;
21299 cp_token *closing_brace;
21301 push_deferring_access_checks (dk_no_deferred);
21303 /* Parse the class-head. */
21304 type = cp_parser_class_head (parser,
21305 &nested_name_specifier_p);
21306 /* If the class-head was a semantic disaster, skip the entire body
21307 of the class. */
21308 if (!type)
21310 cp_parser_skip_to_end_of_block_or_statement (parser);
21311 pop_deferring_access_checks ();
21312 return error_mark_node;
21315 /* Look for the `{'. */
21316 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
21318 pop_deferring_access_checks ();
21319 return error_mark_node;
21322 cp_ensure_no_omp_declare_simd (parser);
21323 cp_ensure_no_oacc_routine (parser);
21325 /* Issue an error message if type-definitions are forbidden here. */
21326 cp_parser_check_type_definition (parser);
21327 /* Remember that we are defining one more class. */
21328 ++parser->num_classes_being_defined;
21329 /* Inside the class, surrounding template-parameter-lists do not
21330 apply. */
21331 saved_num_template_parameter_lists
21332 = parser->num_template_parameter_lists;
21333 parser->num_template_parameter_lists = 0;
21334 /* We are not in a function body. */
21335 saved_in_function_body = parser->in_function_body;
21336 parser->in_function_body = false;
21337 /* Or in a loop. */
21338 in_statement = parser->in_statement;
21339 parser->in_statement = 0;
21340 /* Or in a switch. */
21341 in_switch_statement_p = parser->in_switch_statement_p;
21342 parser->in_switch_statement_p = false;
21343 /* We are not immediately inside an extern "lang" block. */
21344 saved_in_unbraced_linkage_specification_p
21345 = parser->in_unbraced_linkage_specification_p;
21346 parser->in_unbraced_linkage_specification_p = false;
21348 // Associate constraints with the type.
21349 if (flag_concepts)
21350 type = associate_classtype_constraints (type);
21352 /* Start the class. */
21353 if (nested_name_specifier_p)
21355 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
21356 old_scope = push_inner_scope (scope);
21358 type = begin_class_definition (type);
21360 if (type == error_mark_node)
21361 /* If the type is erroneous, skip the entire body of the class. */
21362 cp_parser_skip_to_closing_brace (parser);
21363 else
21364 /* Parse the member-specification. */
21365 cp_parser_member_specification_opt (parser);
21367 /* Look for the trailing `}'. */
21368 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21369 /* Look for trailing attributes to apply to this class. */
21370 if (cp_parser_allow_gnu_extensions_p (parser))
21371 attributes = cp_parser_gnu_attributes_opt (parser);
21372 if (type != error_mark_node)
21373 type = finish_struct (type, attributes);
21374 if (nested_name_specifier_p)
21375 pop_inner_scope (old_scope, scope);
21377 /* We've finished a type definition. Check for the common syntax
21378 error of forgetting a semicolon after the definition. We need to
21379 be careful, as we can't just check for not-a-semicolon and be done
21380 with it; the user might have typed:
21382 class X { } c = ...;
21383 class X { } *p = ...;
21385 and so forth. Instead, enumerate all the possible tokens that
21386 might follow this production; if we don't see one of them, then
21387 complain and silently insert the semicolon. */
21389 cp_token *token = cp_lexer_peek_token (parser->lexer);
21390 bool want_semicolon = true;
21392 if (cp_next_tokens_can_be_std_attribute_p (parser))
21393 /* Don't try to parse c++11 attributes here. As per the
21394 grammar, that should be a task for
21395 cp_parser_decl_specifier_seq. */
21396 want_semicolon = false;
21398 switch (token->type)
21400 case CPP_NAME:
21401 case CPP_SEMICOLON:
21402 case CPP_MULT:
21403 case CPP_AND:
21404 case CPP_OPEN_PAREN:
21405 case CPP_CLOSE_PAREN:
21406 case CPP_COMMA:
21407 want_semicolon = false;
21408 break;
21410 /* While it's legal for type qualifiers and storage class
21411 specifiers to follow type definitions in the grammar, only
21412 compiler testsuites contain code like that. Assume that if
21413 we see such code, then what we're really seeing is a case
21414 like:
21416 class X { }
21417 const <type> var = ...;
21421 class Y { }
21422 static <type> func (...) ...
21424 i.e. the qualifier or specifier applies to the next
21425 declaration. To do so, however, we need to look ahead one
21426 more token to see if *that* token is a type specifier.
21428 This code could be improved to handle:
21430 class Z { }
21431 static const <type> var = ...; */
21432 case CPP_KEYWORD:
21433 if (keyword_is_decl_specifier (token->keyword))
21435 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
21437 /* Handling user-defined types here would be nice, but very
21438 tricky. */
21439 want_semicolon
21440 = (lookahead->type == CPP_KEYWORD
21441 && keyword_begins_type_specifier (lookahead->keyword));
21443 break;
21444 default:
21445 break;
21448 /* If we don't have a type, then something is very wrong and we
21449 shouldn't try to do anything clever. Likewise for not seeing the
21450 closing brace. */
21451 if (closing_brace && TYPE_P (type) && want_semicolon)
21453 cp_token_position prev
21454 = cp_lexer_previous_token_position (parser->lexer);
21455 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
21456 location_t loc = prev_token->location;
21458 if (CLASSTYPE_DECLARED_CLASS (type))
21459 error_at (loc, "expected %<;%> after class definition");
21460 else if (TREE_CODE (type) == RECORD_TYPE)
21461 error_at (loc, "expected %<;%> after struct definition");
21462 else if (TREE_CODE (type) == UNION_TYPE)
21463 error_at (loc, "expected %<;%> after union definition");
21464 else
21465 gcc_unreachable ();
21467 /* Unget one token and smash it to look as though we encountered
21468 a semicolon in the input stream. */
21469 cp_lexer_set_token_position (parser->lexer, prev);
21470 token = cp_lexer_peek_token (parser->lexer);
21471 token->type = CPP_SEMICOLON;
21472 token->keyword = RID_MAX;
21476 /* If this class is not itself within the scope of another class,
21477 then we need to parse the bodies of all of the queued function
21478 definitions. Note that the queued functions defined in a class
21479 are not always processed immediately following the
21480 class-specifier for that class. Consider:
21482 struct A {
21483 struct B { void f() { sizeof (A); } };
21486 If `f' were processed before the processing of `A' were
21487 completed, there would be no way to compute the size of `A'.
21488 Note that the nesting we are interested in here is lexical --
21489 not the semantic nesting given by TYPE_CONTEXT. In particular,
21490 for:
21492 struct A { struct B; };
21493 struct A::B { void f() { } };
21495 there is no need to delay the parsing of `A::B::f'. */
21496 if (--parser->num_classes_being_defined == 0)
21498 tree decl;
21499 tree class_type = NULL_TREE;
21500 tree pushed_scope = NULL_TREE;
21501 unsigned ix;
21502 cp_default_arg_entry *e;
21503 tree save_ccp, save_ccr;
21505 /* In a first pass, parse default arguments to the functions.
21506 Then, in a second pass, parse the bodies of the functions.
21507 This two-phased approach handles cases like:
21509 struct S {
21510 void f() { g(); }
21511 void g(int i = 3);
21515 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
21517 decl = e->decl;
21518 /* If there are default arguments that have not yet been processed,
21519 take care of them now. */
21520 if (class_type != e->class_type)
21522 if (pushed_scope)
21523 pop_scope (pushed_scope);
21524 class_type = e->class_type;
21525 pushed_scope = push_scope (class_type);
21527 /* Make sure that any template parameters are in scope. */
21528 maybe_begin_member_template_processing (decl);
21529 /* Parse the default argument expressions. */
21530 cp_parser_late_parsing_default_args (parser, decl);
21531 /* Remove any template parameters from the symbol table. */
21532 maybe_end_member_template_processing ();
21534 vec_safe_truncate (unparsed_funs_with_default_args, 0);
21535 /* Now parse any NSDMIs. */
21536 save_ccp = current_class_ptr;
21537 save_ccr = current_class_ref;
21538 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
21540 if (class_type != DECL_CONTEXT (decl))
21542 if (pushed_scope)
21543 pop_scope (pushed_scope);
21544 class_type = DECL_CONTEXT (decl);
21545 pushed_scope = push_scope (class_type);
21547 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
21548 cp_parser_late_parsing_nsdmi (parser, decl);
21550 vec_safe_truncate (unparsed_nsdmis, 0);
21551 current_class_ptr = save_ccp;
21552 current_class_ref = save_ccr;
21553 if (pushed_scope)
21554 pop_scope (pushed_scope);
21556 /* Now do some post-NSDMI bookkeeping. */
21557 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
21558 after_nsdmi_defaulted_late_checks (class_type);
21559 vec_safe_truncate (unparsed_classes, 0);
21560 after_nsdmi_defaulted_late_checks (type);
21562 /* Now parse the body of the functions. */
21563 if (flag_openmp)
21565 /* OpenMP UDRs need to be parsed before all other functions. */
21566 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21567 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
21568 cp_parser_late_parsing_for_member (parser, decl);
21569 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21570 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
21571 cp_parser_late_parsing_for_member (parser, decl);
21573 else
21574 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21575 cp_parser_late_parsing_for_member (parser, decl);
21576 vec_safe_truncate (unparsed_funs_with_definitions, 0);
21578 else
21579 vec_safe_push (unparsed_classes, type);
21581 /* Put back any saved access checks. */
21582 pop_deferring_access_checks ();
21584 /* Restore saved state. */
21585 parser->in_switch_statement_p = in_switch_statement_p;
21586 parser->in_statement = in_statement;
21587 parser->in_function_body = saved_in_function_body;
21588 parser->num_template_parameter_lists
21589 = saved_num_template_parameter_lists;
21590 parser->in_unbraced_linkage_specification_p
21591 = saved_in_unbraced_linkage_specification_p;
21593 return type;
21596 static tree
21597 cp_parser_class_specifier (cp_parser* parser)
21599 tree ret;
21600 timevar_push (TV_PARSE_STRUCT);
21601 ret = cp_parser_class_specifier_1 (parser);
21602 timevar_pop (TV_PARSE_STRUCT);
21603 return ret;
21606 /* Parse a class-head.
21608 class-head:
21609 class-key identifier [opt] base-clause [opt]
21610 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
21611 class-key nested-name-specifier [opt] template-id
21612 base-clause [opt]
21614 class-virt-specifier:
21615 final
21617 GNU Extensions:
21618 class-key attributes identifier [opt] base-clause [opt]
21619 class-key attributes nested-name-specifier identifier base-clause [opt]
21620 class-key attributes nested-name-specifier [opt] template-id
21621 base-clause [opt]
21623 Upon return BASES is initialized to the list of base classes (or
21624 NULL, if there are none) in the same form returned by
21625 cp_parser_base_clause.
21627 Returns the TYPE of the indicated class. Sets
21628 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
21629 involving a nested-name-specifier was used, and FALSE otherwise.
21631 Returns error_mark_node if this is not a class-head.
21633 Returns NULL_TREE if the class-head is syntactically valid, but
21634 semantically invalid in a way that means we should skip the entire
21635 body of the class. */
21637 static tree
21638 cp_parser_class_head (cp_parser* parser,
21639 bool* nested_name_specifier_p)
21641 tree nested_name_specifier;
21642 enum tag_types class_key;
21643 tree id = NULL_TREE;
21644 tree type = NULL_TREE;
21645 tree attributes;
21646 tree bases;
21647 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21648 bool template_id_p = false;
21649 bool qualified_p = false;
21650 bool invalid_nested_name_p = false;
21651 bool invalid_explicit_specialization_p = false;
21652 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
21653 tree pushed_scope = NULL_TREE;
21654 unsigned num_templates;
21655 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
21656 /* Assume no nested-name-specifier will be present. */
21657 *nested_name_specifier_p = false;
21658 /* Assume no template parameter lists will be used in defining the
21659 type. */
21660 num_templates = 0;
21661 parser->colon_corrects_to_scope_p = false;
21663 /* Look for the class-key. */
21664 class_key = cp_parser_class_key (parser);
21665 if (class_key == none_type)
21666 return error_mark_node;
21668 location_t class_head_start_location = input_location;
21670 /* Parse the attributes. */
21671 attributes = cp_parser_attributes_opt (parser);
21673 /* If the next token is `::', that is invalid -- but sometimes
21674 people do try to write:
21676 struct ::S {};
21678 Handle this gracefully by accepting the extra qualifier, and then
21679 issuing an error about it later if this really is a
21680 class-head. If it turns out just to be an elaborated type
21681 specifier, remain silent. */
21682 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
21683 qualified_p = true;
21685 push_deferring_access_checks (dk_no_check);
21687 /* Determine the name of the class. Begin by looking for an
21688 optional nested-name-specifier. */
21689 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
21690 nested_name_specifier
21691 = cp_parser_nested_name_specifier_opt (parser,
21692 /*typename_keyword_p=*/false,
21693 /*check_dependency_p=*/false,
21694 /*type_p=*/true,
21695 /*is_declaration=*/false);
21696 /* If there was a nested-name-specifier, then there *must* be an
21697 identifier. */
21698 if (nested_name_specifier)
21700 type_start_token = cp_lexer_peek_token (parser->lexer);
21701 /* Although the grammar says `identifier', it really means
21702 `class-name' or `template-name'. You are only allowed to
21703 define a class that has already been declared with this
21704 syntax.
21706 The proposed resolution for Core Issue 180 says that wherever
21707 you see `class T::X' you should treat `X' as a type-name.
21709 It is OK to define an inaccessible class; for example:
21711 class A { class B; };
21712 class A::B {};
21714 We do not know if we will see a class-name, or a
21715 template-name. We look for a class-name first, in case the
21716 class-name is a template-id; if we looked for the
21717 template-name first we would stop after the template-name. */
21718 cp_parser_parse_tentatively (parser);
21719 type = cp_parser_class_name (parser,
21720 /*typename_keyword_p=*/false,
21721 /*template_keyword_p=*/false,
21722 class_type,
21723 /*check_dependency_p=*/false,
21724 /*class_head_p=*/true,
21725 /*is_declaration=*/false);
21726 /* If that didn't work, ignore the nested-name-specifier. */
21727 if (!cp_parser_parse_definitely (parser))
21729 invalid_nested_name_p = true;
21730 type_start_token = cp_lexer_peek_token (parser->lexer);
21731 id = cp_parser_identifier (parser);
21732 if (id == error_mark_node)
21733 id = NULL_TREE;
21735 /* If we could not find a corresponding TYPE, treat this
21736 declaration like an unqualified declaration. */
21737 if (type == error_mark_node)
21738 nested_name_specifier = NULL_TREE;
21739 /* Otherwise, count the number of templates used in TYPE and its
21740 containing scopes. */
21741 else
21743 tree scope;
21745 for (scope = TREE_TYPE (type);
21746 scope && TREE_CODE (scope) != NAMESPACE_DECL;
21747 scope = get_containing_scope (scope))
21748 if (TYPE_P (scope)
21749 && CLASS_TYPE_P (scope)
21750 && CLASSTYPE_TEMPLATE_INFO (scope)
21751 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
21752 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
21753 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
21754 ++num_templates;
21757 /* Otherwise, the identifier is optional. */
21758 else
21760 /* We don't know whether what comes next is a template-id,
21761 an identifier, or nothing at all. */
21762 cp_parser_parse_tentatively (parser);
21763 /* Check for a template-id. */
21764 type_start_token = cp_lexer_peek_token (parser->lexer);
21765 id = cp_parser_template_id (parser,
21766 /*template_keyword_p=*/false,
21767 /*check_dependency_p=*/true,
21768 class_key,
21769 /*is_declaration=*/true);
21770 /* If that didn't work, it could still be an identifier. */
21771 if (!cp_parser_parse_definitely (parser))
21773 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21775 type_start_token = cp_lexer_peek_token (parser->lexer);
21776 id = cp_parser_identifier (parser);
21778 else
21779 id = NULL_TREE;
21781 else
21783 template_id_p = true;
21784 ++num_templates;
21788 pop_deferring_access_checks ();
21790 if (id)
21792 cp_parser_check_for_invalid_template_id (parser, id,
21793 class_key,
21794 type_start_token->location);
21796 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
21798 /* If it's not a `:' or a `{' then we can't really be looking at a
21799 class-head, since a class-head only appears as part of a
21800 class-specifier. We have to detect this situation before calling
21801 xref_tag, since that has irreversible side-effects. */
21802 if (!cp_parser_next_token_starts_class_definition_p (parser))
21804 cp_parser_error (parser, "expected %<{%> or %<:%>");
21805 type = error_mark_node;
21806 goto out;
21809 /* At this point, we're going ahead with the class-specifier, even
21810 if some other problem occurs. */
21811 cp_parser_commit_to_tentative_parse (parser);
21812 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
21814 cp_parser_error (parser,
21815 "cannot specify %<override%> for a class");
21816 type = error_mark_node;
21817 goto out;
21819 /* Issue the error about the overly-qualified name now. */
21820 if (qualified_p)
21822 cp_parser_error (parser,
21823 "global qualification of class name is invalid");
21824 type = error_mark_node;
21825 goto out;
21827 else if (invalid_nested_name_p)
21829 cp_parser_error (parser,
21830 "qualified name does not name a class");
21831 type = error_mark_node;
21832 goto out;
21834 else if (nested_name_specifier)
21836 tree scope;
21838 /* Reject typedef-names in class heads. */
21839 if (!DECL_IMPLICIT_TYPEDEF_P (type))
21841 error_at (type_start_token->location,
21842 "invalid class name in declaration of %qD",
21843 type);
21844 type = NULL_TREE;
21845 goto done;
21848 /* Figure out in what scope the declaration is being placed. */
21849 scope = current_scope ();
21850 /* If that scope does not contain the scope in which the
21851 class was originally declared, the program is invalid. */
21852 if (scope && !is_ancestor (scope, nested_name_specifier))
21854 if (at_namespace_scope_p ())
21855 error_at (type_start_token->location,
21856 "declaration of %qD in namespace %qD which does not "
21857 "enclose %qD",
21858 type, scope, nested_name_specifier);
21859 else
21860 error_at (type_start_token->location,
21861 "declaration of %qD in %qD which does not enclose %qD",
21862 type, scope, nested_name_specifier);
21863 type = NULL_TREE;
21864 goto done;
21866 /* [dcl.meaning]
21868 A declarator-id shall not be qualified except for the
21869 definition of a ... nested class outside of its class
21870 ... [or] the definition or explicit instantiation of a
21871 class member of a namespace outside of its namespace. */
21872 if (scope == nested_name_specifier)
21874 permerror (nested_name_specifier_token_start->location,
21875 "extra qualification not allowed");
21876 nested_name_specifier = NULL_TREE;
21877 num_templates = 0;
21880 /* An explicit-specialization must be preceded by "template <>". If
21881 it is not, try to recover gracefully. */
21882 if (at_namespace_scope_p ()
21883 && parser->num_template_parameter_lists == 0
21884 && template_id_p)
21886 /* Build a location of this form:
21887 struct typename <ARGS>
21888 ^~~~~~~~~~~~~~~~~~~~~~
21889 with caret==start at the start token, and
21890 finishing at the end of the type. */
21891 location_t reported_loc
21892 = make_location (class_head_start_location,
21893 class_head_start_location,
21894 get_finish (type_start_token->location));
21895 rich_location richloc (line_table, reported_loc);
21896 richloc.add_fixit_insert (class_head_start_location, "template <> ");
21897 error_at_rich_loc
21898 (&richloc,
21899 "an explicit specialization must be preceded by %<template <>%>");
21900 invalid_explicit_specialization_p = true;
21901 /* Take the same action that would have been taken by
21902 cp_parser_explicit_specialization. */
21903 ++parser->num_template_parameter_lists;
21904 begin_specialization ();
21906 /* There must be no "return" statements between this point and the
21907 end of this function; set "type "to the correct return value and
21908 use "goto done;" to return. */
21909 /* Make sure that the right number of template parameters were
21910 present. */
21911 if (!cp_parser_check_template_parameters (parser, num_templates,
21912 type_start_token->location,
21913 /*declarator=*/NULL))
21915 /* If something went wrong, there is no point in even trying to
21916 process the class-definition. */
21917 type = NULL_TREE;
21918 goto done;
21921 /* Look up the type. */
21922 if (template_id_p)
21924 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
21925 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
21926 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
21928 error_at (type_start_token->location,
21929 "function template %qD redeclared as a class template", id);
21930 type = error_mark_node;
21932 else
21934 type = TREE_TYPE (id);
21935 type = maybe_process_partial_specialization (type);
21937 if (nested_name_specifier)
21938 pushed_scope = push_scope (nested_name_specifier);
21940 else if (nested_name_specifier)
21942 tree class_type;
21944 /* Given:
21946 template <typename T> struct S { struct T };
21947 template <typename T> struct S<T>::T { };
21949 we will get a TYPENAME_TYPE when processing the definition of
21950 `S::T'. We need to resolve it to the actual type before we
21951 try to define it. */
21952 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
21954 class_type = resolve_typename_type (TREE_TYPE (type),
21955 /*only_current_p=*/false);
21956 if (TREE_CODE (class_type) != TYPENAME_TYPE)
21957 type = TYPE_NAME (class_type);
21958 else
21960 cp_parser_error (parser, "could not resolve typename type");
21961 type = error_mark_node;
21965 if (maybe_process_partial_specialization (TREE_TYPE (type))
21966 == error_mark_node)
21968 type = NULL_TREE;
21969 goto done;
21972 class_type = current_class_type;
21973 /* Enter the scope indicated by the nested-name-specifier. */
21974 pushed_scope = push_scope (nested_name_specifier);
21975 /* Get the canonical version of this type. */
21976 type = TYPE_MAIN_DECL (TREE_TYPE (type));
21977 /* Call push_template_decl if it seems like we should be defining a
21978 template either from the template headers or the type we're
21979 defining, so that we diagnose both extra and missing headers. */
21980 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
21981 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
21982 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
21984 type = push_template_decl (type);
21985 if (type == error_mark_node)
21987 type = NULL_TREE;
21988 goto done;
21992 type = TREE_TYPE (type);
21993 *nested_name_specifier_p = true;
21995 else /* The name is not a nested name. */
21997 /* If the class was unnamed, create a dummy name. */
21998 if (!id)
21999 id = make_anon_name ();
22000 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
22001 parser->num_template_parameter_lists);
22004 /* Indicate whether this class was declared as a `class' or as a
22005 `struct'. */
22006 if (TREE_CODE (type) == RECORD_TYPE)
22007 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
22008 cp_parser_check_class_key (class_key, type);
22010 /* If this type was already complete, and we see another definition,
22011 that's an error. */
22012 if (type != error_mark_node && COMPLETE_TYPE_P (type))
22014 error_at (type_start_token->location, "redefinition of %q#T",
22015 type);
22016 inform (location_of (type), "previous definition of %q#T",
22017 type);
22018 type = NULL_TREE;
22019 goto done;
22021 else if (type == error_mark_node)
22022 type = NULL_TREE;
22024 if (type)
22026 /* Apply attributes now, before any use of the class as a template
22027 argument in its base list. */
22028 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
22029 fixup_attribute_variants (type);
22032 /* We will have entered the scope containing the class; the names of
22033 base classes should be looked up in that context. For example:
22035 struct A { struct B {}; struct C; };
22036 struct A::C : B {};
22038 is valid. */
22040 /* Get the list of base-classes, if there is one. */
22041 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22043 /* PR59482: enter the class scope so that base-specifiers are looked
22044 up correctly. */
22045 if (type)
22046 pushclass (type);
22047 bases = cp_parser_base_clause (parser);
22048 /* PR59482: get out of the previously pushed class scope so that the
22049 subsequent pops pop the right thing. */
22050 if (type)
22051 popclass ();
22053 else
22054 bases = NULL_TREE;
22056 /* If we're really defining a class, process the base classes.
22057 If they're invalid, fail. */
22058 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22059 xref_basetypes (type, bases);
22061 done:
22062 /* Leave the scope given by the nested-name-specifier. We will
22063 enter the class scope itself while processing the members. */
22064 if (pushed_scope)
22065 pop_scope (pushed_scope);
22067 if (invalid_explicit_specialization_p)
22069 end_specialization ();
22070 --parser->num_template_parameter_lists;
22073 if (type)
22074 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
22075 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
22076 CLASSTYPE_FINAL (type) = 1;
22077 out:
22078 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22079 return type;
22082 /* Parse a class-key.
22084 class-key:
22085 class
22086 struct
22087 union
22089 Returns the kind of class-key specified, or none_type to indicate
22090 error. */
22092 static enum tag_types
22093 cp_parser_class_key (cp_parser* parser)
22095 cp_token *token;
22096 enum tag_types tag_type;
22098 /* Look for the class-key. */
22099 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
22100 if (!token)
22101 return none_type;
22103 /* Check to see if the TOKEN is a class-key. */
22104 tag_type = cp_parser_token_is_class_key (token);
22105 if (!tag_type)
22106 cp_parser_error (parser, "expected class-key");
22107 return tag_type;
22110 /* Parse a type-parameter-key.
22112 type-parameter-key:
22113 class
22114 typename
22117 static void
22118 cp_parser_type_parameter_key (cp_parser* parser)
22120 /* Look for the type-parameter-key. */
22121 enum tag_types tag_type = none_type;
22122 cp_token *token = cp_lexer_peek_token (parser->lexer);
22123 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
22125 cp_lexer_consume_token (parser->lexer);
22126 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
22127 /* typename is not allowed in a template template parameter
22128 by the standard until C++1Z. */
22129 pedwarn (token->location, OPT_Wpedantic,
22130 "ISO C++ forbids typename key in template template parameter;"
22131 " use -std=c++1z or -std=gnu++1z");
22133 else
22134 cp_parser_error (parser, "expected %<class%> or %<typename%>");
22136 return;
22139 /* Parse an (optional) member-specification.
22141 member-specification:
22142 member-declaration member-specification [opt]
22143 access-specifier : member-specification [opt] */
22145 static void
22146 cp_parser_member_specification_opt (cp_parser* parser)
22148 while (true)
22150 cp_token *token;
22151 enum rid keyword;
22153 /* Peek at the next token. */
22154 token = cp_lexer_peek_token (parser->lexer);
22155 /* If it's a `}', or EOF then we've seen all the members. */
22156 if (token->type == CPP_CLOSE_BRACE
22157 || token->type == CPP_EOF
22158 || token->type == CPP_PRAGMA_EOL)
22159 break;
22161 /* See if this token is a keyword. */
22162 keyword = token->keyword;
22163 switch (keyword)
22165 case RID_PUBLIC:
22166 case RID_PROTECTED:
22167 case RID_PRIVATE:
22168 /* Consume the access-specifier. */
22169 cp_lexer_consume_token (parser->lexer);
22170 /* Remember which access-specifier is active. */
22171 current_access_specifier = token->u.value;
22172 /* Look for the `:'. */
22173 cp_parser_require (parser, CPP_COLON, RT_COLON);
22174 break;
22176 default:
22177 /* Accept #pragmas at class scope. */
22178 if (token->type == CPP_PRAGMA)
22180 cp_parser_pragma (parser, pragma_member, NULL);
22181 break;
22184 /* Otherwise, the next construction must be a
22185 member-declaration. */
22186 cp_parser_member_declaration (parser);
22191 /* Parse a member-declaration.
22193 member-declaration:
22194 decl-specifier-seq [opt] member-declarator-list [opt] ;
22195 function-definition ; [opt]
22196 :: [opt] nested-name-specifier template [opt] unqualified-id ;
22197 using-declaration
22198 template-declaration
22199 alias-declaration
22201 member-declarator-list:
22202 member-declarator
22203 member-declarator-list , member-declarator
22205 member-declarator:
22206 declarator pure-specifier [opt]
22207 declarator constant-initializer [opt]
22208 identifier [opt] : constant-expression
22210 GNU Extensions:
22212 member-declaration:
22213 __extension__ member-declaration
22215 member-declarator:
22216 declarator attributes [opt] pure-specifier [opt]
22217 declarator attributes [opt] constant-initializer [opt]
22218 identifier [opt] attributes [opt] : constant-expression
22220 C++0x Extensions:
22222 member-declaration:
22223 static_assert-declaration */
22225 static void
22226 cp_parser_member_declaration (cp_parser* parser)
22228 cp_decl_specifier_seq decl_specifiers;
22229 tree prefix_attributes;
22230 tree decl;
22231 int declares_class_or_enum;
22232 bool friend_p;
22233 cp_token *token = NULL;
22234 cp_token *decl_spec_token_start = NULL;
22235 cp_token *initializer_token_start = NULL;
22236 int saved_pedantic;
22237 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22239 /* Check for the `__extension__' keyword. */
22240 if (cp_parser_extension_opt (parser, &saved_pedantic))
22242 /* Recurse. */
22243 cp_parser_member_declaration (parser);
22244 /* Restore the old value of the PEDANTIC flag. */
22245 pedantic = saved_pedantic;
22247 return;
22250 /* Check for a template-declaration. */
22251 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22253 /* An explicit specialization here is an error condition, and we
22254 expect the specialization handler to detect and report this. */
22255 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
22256 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
22257 cp_parser_explicit_specialization (parser);
22258 else
22259 cp_parser_template_declaration (parser, /*member_p=*/true);
22261 return;
22263 /* Check for a template introduction. */
22264 else if (cp_parser_template_declaration_after_export (parser, true))
22265 return;
22267 /* Check for a using-declaration. */
22268 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
22270 if (cxx_dialect < cxx11)
22272 /* Parse the using-declaration. */
22273 cp_parser_using_declaration (parser,
22274 /*access_declaration_p=*/false);
22275 return;
22277 else
22279 tree decl;
22280 bool alias_decl_expected;
22281 cp_parser_parse_tentatively (parser);
22282 decl = cp_parser_alias_declaration (parser);
22283 /* Note that if we actually see the '=' token after the
22284 identifier, cp_parser_alias_declaration commits the
22285 tentative parse. In that case, we really expect an
22286 alias-declaration. Otherwise, we expect a using
22287 declaration. */
22288 alias_decl_expected =
22289 !cp_parser_uncommitted_to_tentative_parse_p (parser);
22290 cp_parser_parse_definitely (parser);
22292 if (alias_decl_expected)
22293 finish_member_declaration (decl);
22294 else
22295 cp_parser_using_declaration (parser,
22296 /*access_declaration_p=*/false);
22297 return;
22301 /* Check for @defs. */
22302 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
22304 tree ivar, member;
22305 tree ivar_chains = cp_parser_objc_defs_expression (parser);
22306 ivar = ivar_chains;
22307 while (ivar)
22309 member = ivar;
22310 ivar = TREE_CHAIN (member);
22311 TREE_CHAIN (member) = NULL_TREE;
22312 finish_member_declaration (member);
22314 return;
22317 /* If the next token is `static_assert' we have a static assertion. */
22318 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
22320 cp_parser_static_assert (parser, /*member_p=*/true);
22321 return;
22324 parser->colon_corrects_to_scope_p = false;
22326 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
22327 goto out;
22329 /* Parse the decl-specifier-seq. */
22330 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22331 cp_parser_decl_specifier_seq (parser,
22332 CP_PARSER_FLAGS_OPTIONAL,
22333 &decl_specifiers,
22334 &declares_class_or_enum);
22335 /* Check for an invalid type-name. */
22336 if (!decl_specifiers.any_type_specifiers_p
22337 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22338 goto out;
22339 /* If there is no declarator, then the decl-specifier-seq should
22340 specify a type. */
22341 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22343 /* If there was no decl-specifier-seq, and the next token is a
22344 `;', then we have something like:
22346 struct S { ; };
22348 [class.mem]
22350 Each member-declaration shall declare at least one member
22351 name of the class. */
22352 if (!decl_specifiers.any_specifiers_p)
22354 cp_token *token = cp_lexer_peek_token (parser->lexer);
22355 if (!in_system_header_at (token->location))
22356 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
22358 else
22360 tree type;
22362 /* See if this declaration is a friend. */
22363 friend_p = cp_parser_friend_p (&decl_specifiers);
22364 /* If there were decl-specifiers, check to see if there was
22365 a class-declaration. */
22366 type = check_tag_decl (&decl_specifiers,
22367 /*explicit_type_instantiation_p=*/false);
22368 /* Nested classes have already been added to the class, but
22369 a `friend' needs to be explicitly registered. */
22370 if (friend_p)
22372 /* If the `friend' keyword was present, the friend must
22373 be introduced with a class-key. */
22374 if (!declares_class_or_enum && cxx_dialect < cxx11)
22375 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
22376 "in C++03 a class-key must be used "
22377 "when declaring a friend");
22378 /* In this case:
22380 template <typename T> struct A {
22381 friend struct A<T>::B;
22384 A<T>::B will be represented by a TYPENAME_TYPE, and
22385 therefore not recognized by check_tag_decl. */
22386 if (!type)
22388 type = decl_specifiers.type;
22389 if (type && TREE_CODE (type) == TYPE_DECL)
22390 type = TREE_TYPE (type);
22392 if (!type || !TYPE_P (type))
22393 error_at (decl_spec_token_start->location,
22394 "friend declaration does not name a class or "
22395 "function");
22396 else
22397 make_friend_class (current_class_type, type,
22398 /*complain=*/true);
22400 /* If there is no TYPE, an error message will already have
22401 been issued. */
22402 else if (!type || type == error_mark_node)
22404 /* An anonymous aggregate has to be handled specially; such
22405 a declaration really declares a data member (with a
22406 particular type), as opposed to a nested class. */
22407 else if (ANON_AGGR_TYPE_P (type))
22409 /* C++11 9.5/6. */
22410 if (decl_specifiers.storage_class != sc_none)
22411 error_at (decl_spec_token_start->location,
22412 "a storage class on an anonymous aggregate "
22413 "in class scope is not allowed");
22415 /* Remove constructors and such from TYPE, now that we
22416 know it is an anonymous aggregate. */
22417 fixup_anonymous_aggr (type);
22418 /* And make the corresponding data member. */
22419 decl = build_decl (decl_spec_token_start->location,
22420 FIELD_DECL, NULL_TREE, type);
22421 /* Add it to the class. */
22422 finish_member_declaration (decl);
22424 else
22425 cp_parser_check_access_in_redeclaration
22426 (TYPE_NAME (type),
22427 decl_spec_token_start->location);
22430 else
22432 bool assume_semicolon = false;
22434 /* Clear attributes from the decl_specifiers but keep them
22435 around as prefix attributes that apply them to the entity
22436 being declared. */
22437 prefix_attributes = decl_specifiers.attributes;
22438 decl_specifiers.attributes = NULL_TREE;
22440 /* See if these declarations will be friends. */
22441 friend_p = cp_parser_friend_p (&decl_specifiers);
22443 /* Keep going until we hit the `;' at the end of the
22444 declaration. */
22445 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22447 tree attributes = NULL_TREE;
22448 tree first_attribute;
22450 /* Peek at the next token. */
22451 token = cp_lexer_peek_token (parser->lexer);
22453 /* Check for a bitfield declaration. */
22454 if (token->type == CPP_COLON
22455 || (token->type == CPP_NAME
22456 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
22457 == CPP_COLON))
22459 tree identifier;
22460 tree width;
22462 /* Get the name of the bitfield. Note that we cannot just
22463 check TOKEN here because it may have been invalidated by
22464 the call to cp_lexer_peek_nth_token above. */
22465 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
22466 identifier = cp_parser_identifier (parser);
22467 else
22468 identifier = NULL_TREE;
22470 /* Consume the `:' token. */
22471 cp_lexer_consume_token (parser->lexer);
22472 /* Get the width of the bitfield. */
22473 width
22474 = cp_parser_constant_expression (parser);
22476 /* Look for attributes that apply to the bitfield. */
22477 attributes = cp_parser_attributes_opt (parser);
22478 /* Remember which attributes are prefix attributes and
22479 which are not. */
22480 first_attribute = attributes;
22481 /* Combine the attributes. */
22482 attributes = chainon (prefix_attributes, attributes);
22484 /* Create the bitfield declaration. */
22485 decl = grokbitfield (identifier
22486 ? make_id_declarator (NULL_TREE,
22487 identifier,
22488 sfk_none)
22489 : NULL,
22490 &decl_specifiers,
22491 width,
22492 attributes);
22494 else
22496 cp_declarator *declarator;
22497 tree initializer;
22498 tree asm_specification;
22499 int ctor_dtor_or_conv_p;
22501 /* Parse the declarator. */
22502 declarator
22503 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22504 &ctor_dtor_or_conv_p,
22505 /*parenthesized_p=*/NULL,
22506 /*member_p=*/true,
22507 friend_p);
22509 /* If something went wrong parsing the declarator, make sure
22510 that we at least consume some tokens. */
22511 if (declarator == cp_error_declarator)
22513 /* Skip to the end of the statement. */
22514 cp_parser_skip_to_end_of_statement (parser);
22515 /* If the next token is not a semicolon, that is
22516 probably because we just skipped over the body of
22517 a function. So, we consume a semicolon if
22518 present, but do not issue an error message if it
22519 is not present. */
22520 if (cp_lexer_next_token_is (parser->lexer,
22521 CPP_SEMICOLON))
22522 cp_lexer_consume_token (parser->lexer);
22523 goto out;
22526 if (declares_class_or_enum & 2)
22527 cp_parser_check_for_definition_in_return_type
22528 (declarator, decl_specifiers.type,
22529 decl_specifiers.locations[ds_type_spec]);
22531 /* Look for an asm-specification. */
22532 asm_specification = cp_parser_asm_specification_opt (parser);
22533 /* Look for attributes that apply to the declaration. */
22534 attributes = cp_parser_attributes_opt (parser);
22535 /* Remember which attributes are prefix attributes and
22536 which are not. */
22537 first_attribute = attributes;
22538 /* Combine the attributes. */
22539 attributes = chainon (prefix_attributes, attributes);
22541 /* If it's an `=', then we have a constant-initializer or a
22542 pure-specifier. It is not correct to parse the
22543 initializer before registering the member declaration
22544 since the member declaration should be in scope while
22545 its initializer is processed. However, the rest of the
22546 front end does not yet provide an interface that allows
22547 us to handle this correctly. */
22548 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22550 /* In [class.mem]:
22552 A pure-specifier shall be used only in the declaration of
22553 a virtual function.
22555 A member-declarator can contain a constant-initializer
22556 only if it declares a static member of integral or
22557 enumeration type.
22559 Therefore, if the DECLARATOR is for a function, we look
22560 for a pure-specifier; otherwise, we look for a
22561 constant-initializer. When we call `grokfield', it will
22562 perform more stringent semantics checks. */
22563 initializer_token_start = cp_lexer_peek_token (parser->lexer);
22564 if (function_declarator_p (declarator)
22565 || (decl_specifiers.type
22566 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
22567 && declarator->kind == cdk_id
22568 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
22569 == FUNCTION_TYPE)))
22570 initializer = cp_parser_pure_specifier (parser);
22571 else if (decl_specifiers.storage_class != sc_static)
22572 initializer = cp_parser_save_nsdmi (parser);
22573 else if (cxx_dialect >= cxx11)
22575 bool nonconst;
22576 /* Don't require a constant rvalue in C++11, since we
22577 might want a reference constant. We'll enforce
22578 constancy later. */
22579 cp_lexer_consume_token (parser->lexer);
22580 /* Parse the initializer. */
22581 initializer = cp_parser_initializer_clause (parser,
22582 &nonconst);
22584 else
22585 /* Parse the initializer. */
22586 initializer = cp_parser_constant_initializer (parser);
22588 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
22589 && !function_declarator_p (declarator))
22591 bool x;
22592 if (decl_specifiers.storage_class != sc_static)
22593 initializer = cp_parser_save_nsdmi (parser);
22594 else
22595 initializer = cp_parser_initializer (parser, &x, &x);
22597 /* Otherwise, there is no initializer. */
22598 else
22599 initializer = NULL_TREE;
22601 /* See if we are probably looking at a function
22602 definition. We are certainly not looking at a
22603 member-declarator. Calling `grokfield' has
22604 side-effects, so we must not do it unless we are sure
22605 that we are looking at a member-declarator. */
22606 if (cp_parser_token_starts_function_definition_p
22607 (cp_lexer_peek_token (parser->lexer)))
22609 /* The grammar does not allow a pure-specifier to be
22610 used when a member function is defined. (It is
22611 possible that this fact is an oversight in the
22612 standard, since a pure function may be defined
22613 outside of the class-specifier. */
22614 if (initializer && initializer_token_start)
22615 error_at (initializer_token_start->location,
22616 "pure-specifier on function-definition");
22617 decl = cp_parser_save_member_function_body (parser,
22618 &decl_specifiers,
22619 declarator,
22620 attributes);
22621 if (parser->fully_implicit_function_template_p)
22622 decl = finish_fully_implicit_template (parser, decl);
22623 /* If the member was not a friend, declare it here. */
22624 if (!friend_p)
22625 finish_member_declaration (decl);
22626 /* Peek at the next token. */
22627 token = cp_lexer_peek_token (parser->lexer);
22628 /* If the next token is a semicolon, consume it. */
22629 if (token->type == CPP_SEMICOLON)
22630 cp_lexer_consume_token (parser->lexer);
22631 goto out;
22633 else
22634 if (declarator->kind == cdk_function)
22635 declarator->id_loc = token->location;
22636 /* Create the declaration. */
22637 decl = grokfield (declarator, &decl_specifiers,
22638 initializer, /*init_const_expr_p=*/true,
22639 asm_specification, attributes);
22640 if (parser->fully_implicit_function_template_p)
22642 if (friend_p)
22643 finish_fully_implicit_template (parser, 0);
22644 else
22645 decl = finish_fully_implicit_template (parser, decl);
22649 cp_finalize_omp_declare_simd (parser, decl);
22650 cp_finalize_oacc_routine (parser, decl, false);
22652 /* Reset PREFIX_ATTRIBUTES. */
22653 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22654 attributes = TREE_CHAIN (attributes);
22655 if (attributes)
22656 TREE_CHAIN (attributes) = NULL_TREE;
22658 /* If there is any qualification still in effect, clear it
22659 now; we will be starting fresh with the next declarator. */
22660 parser->scope = NULL_TREE;
22661 parser->qualifying_scope = NULL_TREE;
22662 parser->object_scope = NULL_TREE;
22663 /* If it's a `,', then there are more declarators. */
22664 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22666 cp_lexer_consume_token (parser->lexer);
22667 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22669 cp_token *token = cp_lexer_previous_token (parser->lexer);
22670 error_at (token->location,
22671 "stray %<,%> at end of member declaration");
22674 /* If the next token isn't a `;', then we have a parse error. */
22675 else if (cp_lexer_next_token_is_not (parser->lexer,
22676 CPP_SEMICOLON))
22678 /* The next token might be a ways away from where the
22679 actual semicolon is missing. Find the previous token
22680 and use that for our error position. */
22681 cp_token *token = cp_lexer_previous_token (parser->lexer);
22682 error_at (token->location,
22683 "expected %<;%> at end of member declaration");
22685 /* Assume that the user meant to provide a semicolon. If
22686 we were to cp_parser_skip_to_end_of_statement, we might
22687 skip to a semicolon inside a member function definition
22688 and issue nonsensical error messages. */
22689 assume_semicolon = true;
22692 if (decl)
22694 /* Add DECL to the list of members. */
22695 if (!friend_p
22696 /* Explicitly include, eg, NSDMIs, for better error
22697 recovery (c++/58650). */
22698 || !DECL_DECLARES_FUNCTION_P (decl))
22699 finish_member_declaration (decl);
22701 if (TREE_CODE (decl) == FUNCTION_DECL)
22702 cp_parser_save_default_args (parser, decl);
22703 else if (TREE_CODE (decl) == FIELD_DECL
22704 && !DECL_C_BIT_FIELD (decl)
22705 && DECL_INITIAL (decl))
22706 /* Add DECL to the queue of NSDMI to be parsed later. */
22707 vec_safe_push (unparsed_nsdmis, decl);
22710 if (assume_semicolon)
22711 goto out;
22715 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22716 out:
22717 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22720 /* Parse a pure-specifier.
22722 pure-specifier:
22725 Returns INTEGER_ZERO_NODE if a pure specifier is found.
22726 Otherwise, ERROR_MARK_NODE is returned. */
22728 static tree
22729 cp_parser_pure_specifier (cp_parser* parser)
22731 cp_token *token;
22733 /* Look for the `=' token. */
22734 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22735 return error_mark_node;
22736 /* Look for the `0' token. */
22737 token = cp_lexer_peek_token (parser->lexer);
22739 if (token->type == CPP_EOF
22740 || token->type == CPP_PRAGMA_EOL)
22741 return error_mark_node;
22743 cp_lexer_consume_token (parser->lexer);
22745 /* Accept = default or = delete in c++0x mode. */
22746 if (token->keyword == RID_DEFAULT
22747 || token->keyword == RID_DELETE)
22749 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
22750 return token->u.value;
22753 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
22754 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
22756 cp_parser_error (parser,
22757 "invalid pure specifier (only %<= 0%> is allowed)");
22758 cp_parser_skip_to_end_of_statement (parser);
22759 return error_mark_node;
22761 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
22763 error_at (token->location, "templates may not be %<virtual%>");
22764 return error_mark_node;
22767 return integer_zero_node;
22770 /* Parse a constant-initializer.
22772 constant-initializer:
22773 = constant-expression
22775 Returns a representation of the constant-expression. */
22777 static tree
22778 cp_parser_constant_initializer (cp_parser* parser)
22780 /* Look for the `=' token. */
22781 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22782 return error_mark_node;
22784 /* It is invalid to write:
22786 struct S { static const int i = { 7 }; };
22789 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22791 cp_parser_error (parser,
22792 "a brace-enclosed initializer is not allowed here");
22793 /* Consume the opening brace. */
22794 cp_lexer_consume_token (parser->lexer);
22795 /* Skip the initializer. */
22796 cp_parser_skip_to_closing_brace (parser);
22797 /* Look for the trailing `}'. */
22798 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22800 return error_mark_node;
22803 return cp_parser_constant_expression (parser);
22806 /* Derived classes [gram.class.derived] */
22808 /* Parse a base-clause.
22810 base-clause:
22811 : base-specifier-list
22813 base-specifier-list:
22814 base-specifier ... [opt]
22815 base-specifier-list , base-specifier ... [opt]
22817 Returns a TREE_LIST representing the base-classes, in the order in
22818 which they were declared. The representation of each node is as
22819 described by cp_parser_base_specifier.
22821 In the case that no bases are specified, this function will return
22822 NULL_TREE, not ERROR_MARK_NODE. */
22824 static tree
22825 cp_parser_base_clause (cp_parser* parser)
22827 tree bases = NULL_TREE;
22829 /* Look for the `:' that begins the list. */
22830 cp_parser_require (parser, CPP_COLON, RT_COLON);
22832 /* Scan the base-specifier-list. */
22833 while (true)
22835 cp_token *token;
22836 tree base;
22837 bool pack_expansion_p = false;
22839 /* Look for the base-specifier. */
22840 base = cp_parser_base_specifier (parser);
22841 /* Look for the (optional) ellipsis. */
22842 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22844 /* Consume the `...'. */
22845 cp_lexer_consume_token (parser->lexer);
22847 pack_expansion_p = true;
22850 /* Add BASE to the front of the list. */
22851 if (base && base != error_mark_node)
22853 if (pack_expansion_p)
22854 /* Make this a pack expansion type. */
22855 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
22857 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
22859 TREE_CHAIN (base) = bases;
22860 bases = base;
22863 /* Peek at the next token. */
22864 token = cp_lexer_peek_token (parser->lexer);
22865 /* If it's not a comma, then the list is complete. */
22866 if (token->type != CPP_COMMA)
22867 break;
22868 /* Consume the `,'. */
22869 cp_lexer_consume_token (parser->lexer);
22872 /* PARSER->SCOPE may still be non-NULL at this point, if the last
22873 base class had a qualified name. However, the next name that
22874 appears is certainly not qualified. */
22875 parser->scope = NULL_TREE;
22876 parser->qualifying_scope = NULL_TREE;
22877 parser->object_scope = NULL_TREE;
22879 return nreverse (bases);
22882 /* Parse a base-specifier.
22884 base-specifier:
22885 :: [opt] nested-name-specifier [opt] class-name
22886 virtual access-specifier [opt] :: [opt] nested-name-specifier
22887 [opt] class-name
22888 access-specifier virtual [opt] :: [opt] nested-name-specifier
22889 [opt] class-name
22891 Returns a TREE_LIST. The TREE_PURPOSE will be one of
22892 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
22893 indicate the specifiers provided. The TREE_VALUE will be a TYPE
22894 (or the ERROR_MARK_NODE) indicating the type that was specified. */
22896 static tree
22897 cp_parser_base_specifier (cp_parser* parser)
22899 cp_token *token;
22900 bool done = false;
22901 bool virtual_p = false;
22902 bool duplicate_virtual_error_issued_p = false;
22903 bool duplicate_access_error_issued_p = false;
22904 bool class_scope_p, template_p;
22905 tree access = access_default_node;
22906 tree type;
22908 /* Process the optional `virtual' and `access-specifier'. */
22909 while (!done)
22911 /* Peek at the next token. */
22912 token = cp_lexer_peek_token (parser->lexer);
22913 /* Process `virtual'. */
22914 switch (token->keyword)
22916 case RID_VIRTUAL:
22917 /* If `virtual' appears more than once, issue an error. */
22918 if (virtual_p && !duplicate_virtual_error_issued_p)
22920 cp_parser_error (parser,
22921 "%<virtual%> specified more than once in base-specified");
22922 duplicate_virtual_error_issued_p = true;
22925 virtual_p = true;
22927 /* Consume the `virtual' token. */
22928 cp_lexer_consume_token (parser->lexer);
22930 break;
22932 case RID_PUBLIC:
22933 case RID_PROTECTED:
22934 case RID_PRIVATE:
22935 /* If more than one access specifier appears, issue an
22936 error. */
22937 if (access != access_default_node
22938 && !duplicate_access_error_issued_p)
22940 cp_parser_error (parser,
22941 "more than one access specifier in base-specified");
22942 duplicate_access_error_issued_p = true;
22945 access = ridpointers[(int) token->keyword];
22947 /* Consume the access-specifier. */
22948 cp_lexer_consume_token (parser->lexer);
22950 break;
22952 default:
22953 done = true;
22954 break;
22957 /* It is not uncommon to see programs mechanically, erroneously, use
22958 the 'typename' keyword to denote (dependent) qualified types
22959 as base classes. */
22960 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
22962 token = cp_lexer_peek_token (parser->lexer);
22963 if (!processing_template_decl)
22964 error_at (token->location,
22965 "keyword %<typename%> not allowed outside of templates");
22966 else
22967 error_at (token->location,
22968 "keyword %<typename%> not allowed in this context "
22969 "(the base class is implicitly a type)");
22970 cp_lexer_consume_token (parser->lexer);
22973 /* Look for the optional `::' operator. */
22974 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
22975 /* Look for the nested-name-specifier. The simplest way to
22976 implement:
22978 [temp.res]
22980 The keyword `typename' is not permitted in a base-specifier or
22981 mem-initializer; in these contexts a qualified name that
22982 depends on a template-parameter is implicitly assumed to be a
22983 type name.
22985 is to pretend that we have seen the `typename' keyword at this
22986 point. */
22987 cp_parser_nested_name_specifier_opt (parser,
22988 /*typename_keyword_p=*/true,
22989 /*check_dependency_p=*/true,
22990 typename_type,
22991 /*is_declaration=*/true);
22992 /* If the base class is given by a qualified name, assume that names
22993 we see are type names or templates, as appropriate. */
22994 class_scope_p = (parser->scope && TYPE_P (parser->scope));
22995 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
22997 if (!parser->scope
22998 && cp_lexer_next_token_is_decltype (parser->lexer))
22999 /* DR 950 allows decltype as a base-specifier. */
23000 type = cp_parser_decltype (parser);
23001 else
23003 /* Otherwise, look for the class-name. */
23004 type = cp_parser_class_name (parser,
23005 class_scope_p,
23006 template_p,
23007 typename_type,
23008 /*check_dependency_p=*/true,
23009 /*class_head_p=*/false,
23010 /*is_declaration=*/true);
23011 type = TREE_TYPE (type);
23014 if (type == error_mark_node)
23015 return error_mark_node;
23017 return finish_base_specifier (type, access, virtual_p);
23020 /* Exception handling [gram.exception] */
23022 /* Parse an (optional) noexcept-specification.
23024 noexcept-specification:
23025 noexcept ( constant-expression ) [opt]
23027 If no noexcept-specification is present, returns NULL_TREE.
23028 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23029 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23030 there are no parentheses. CONSUMED_EXPR will be set accordingly.
23031 Otherwise, returns a noexcept specification unless RETURN_COND is true,
23032 in which case a boolean condition is returned instead. */
23034 static tree
23035 cp_parser_noexcept_specification_opt (cp_parser* parser,
23036 bool require_constexpr,
23037 bool* consumed_expr,
23038 bool return_cond)
23040 cp_token *token;
23041 const char *saved_message;
23043 /* Peek at the next token. */
23044 token = cp_lexer_peek_token (parser->lexer);
23046 /* Is it a noexcept-specification? */
23047 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
23049 tree expr;
23050 cp_lexer_consume_token (parser->lexer);
23052 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
23054 cp_lexer_consume_token (parser->lexer);
23056 if (require_constexpr)
23058 /* Types may not be defined in an exception-specification. */
23059 saved_message = parser->type_definition_forbidden_message;
23060 parser->type_definition_forbidden_message
23061 = G_("types may not be defined in an exception-specification");
23063 expr = cp_parser_constant_expression (parser);
23065 /* Restore the saved message. */
23066 parser->type_definition_forbidden_message = saved_message;
23068 else
23070 expr = cp_parser_expression (parser);
23071 *consumed_expr = true;
23074 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23076 else
23078 expr = boolean_true_node;
23079 if (!require_constexpr)
23080 *consumed_expr = false;
23083 /* We cannot build a noexcept-spec right away because this will check
23084 that expr is a constexpr. */
23085 if (!return_cond)
23086 return build_noexcept_spec (expr, tf_warning_or_error);
23087 else
23088 return expr;
23090 else
23091 return NULL_TREE;
23094 /* Parse an (optional) exception-specification.
23096 exception-specification:
23097 throw ( type-id-list [opt] )
23099 Returns a TREE_LIST representing the exception-specification. The
23100 TREE_VALUE of each node is a type. */
23102 static tree
23103 cp_parser_exception_specification_opt (cp_parser* parser)
23105 cp_token *token;
23106 tree type_id_list;
23107 const char *saved_message;
23109 /* Peek at the next token. */
23110 token = cp_lexer_peek_token (parser->lexer);
23112 /* Is it a noexcept-specification? */
23113 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
23114 false);
23115 if (type_id_list != NULL_TREE)
23116 return type_id_list;
23118 /* If it's not `throw', then there's no exception-specification. */
23119 if (!cp_parser_is_keyword (token, RID_THROW))
23120 return NULL_TREE;
23122 #if 0
23123 /* Enable this once a lot of code has transitioned to noexcept? */
23124 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
23125 warning (OPT_Wdeprecated, "dynamic exception specifications are "
23126 "deprecated in C++0x; use %<noexcept%> instead");
23127 #endif
23129 /* Consume the `throw'. */
23130 cp_lexer_consume_token (parser->lexer);
23132 /* Look for the `('. */
23133 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23135 /* Peek at the next token. */
23136 token = cp_lexer_peek_token (parser->lexer);
23137 /* If it's not a `)', then there is a type-id-list. */
23138 if (token->type != CPP_CLOSE_PAREN)
23140 /* Types may not be defined in an exception-specification. */
23141 saved_message = parser->type_definition_forbidden_message;
23142 parser->type_definition_forbidden_message
23143 = G_("types may not be defined in an exception-specification");
23144 /* Parse the type-id-list. */
23145 type_id_list = cp_parser_type_id_list (parser);
23146 /* Restore the saved message. */
23147 parser->type_definition_forbidden_message = saved_message;
23149 else
23150 type_id_list = empty_except_spec;
23152 /* Look for the `)'. */
23153 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23155 return type_id_list;
23158 /* Parse an (optional) type-id-list.
23160 type-id-list:
23161 type-id ... [opt]
23162 type-id-list , type-id ... [opt]
23164 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
23165 in the order that the types were presented. */
23167 static tree
23168 cp_parser_type_id_list (cp_parser* parser)
23170 tree types = NULL_TREE;
23172 while (true)
23174 cp_token *token;
23175 tree type;
23177 token = cp_lexer_peek_token (parser->lexer);
23179 /* Get the next type-id. */
23180 type = cp_parser_type_id (parser);
23181 /* Check for invalid 'auto'. */
23182 if (flag_concepts && type_uses_auto (type))
23184 error_at (token->location,
23185 "invalid use of %<auto%> in exception-specification");
23186 type = error_mark_node;
23188 /* Parse the optional ellipsis. */
23189 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23191 /* Consume the `...'. */
23192 cp_lexer_consume_token (parser->lexer);
23194 /* Turn the type into a pack expansion expression. */
23195 type = make_pack_expansion (type);
23197 /* Add it to the list. */
23198 types = add_exception_specifier (types, type, /*complain=*/1);
23199 /* Peek at the next token. */
23200 token = cp_lexer_peek_token (parser->lexer);
23201 /* If it is not a `,', we are done. */
23202 if (token->type != CPP_COMMA)
23203 break;
23204 /* Consume the `,'. */
23205 cp_lexer_consume_token (parser->lexer);
23208 return nreverse (types);
23211 /* Parse a try-block.
23213 try-block:
23214 try compound-statement handler-seq */
23216 static tree
23217 cp_parser_try_block (cp_parser* parser)
23219 tree try_block;
23221 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
23222 if (parser->in_function_body
23223 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
23224 error ("%<try%> in %<constexpr%> function");
23226 try_block = begin_try_block ();
23227 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
23228 finish_try_block (try_block);
23229 cp_parser_handler_seq (parser);
23230 finish_handler_sequence (try_block);
23232 return try_block;
23235 /* Parse a function-try-block.
23237 function-try-block:
23238 try ctor-initializer [opt] function-body handler-seq */
23240 static bool
23241 cp_parser_function_try_block (cp_parser* parser)
23243 tree compound_stmt;
23244 tree try_block;
23245 bool ctor_initializer_p;
23247 /* Look for the `try' keyword. */
23248 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
23249 return false;
23250 /* Let the rest of the front end know where we are. */
23251 try_block = begin_function_try_block (&compound_stmt);
23252 /* Parse the function-body. */
23253 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23254 (parser, /*in_function_try_block=*/true);
23255 /* We're done with the `try' part. */
23256 finish_function_try_block (try_block);
23257 /* Parse the handlers. */
23258 cp_parser_handler_seq (parser);
23259 /* We're done with the handlers. */
23260 finish_function_handler_sequence (try_block, compound_stmt);
23262 return ctor_initializer_p;
23265 /* Parse a handler-seq.
23267 handler-seq:
23268 handler handler-seq [opt] */
23270 static void
23271 cp_parser_handler_seq (cp_parser* parser)
23273 while (true)
23275 cp_token *token;
23277 /* Parse the handler. */
23278 cp_parser_handler (parser);
23279 /* Peek at the next token. */
23280 token = cp_lexer_peek_token (parser->lexer);
23281 /* If it's not `catch' then there are no more handlers. */
23282 if (!cp_parser_is_keyword (token, RID_CATCH))
23283 break;
23287 /* Parse a handler.
23289 handler:
23290 catch ( exception-declaration ) compound-statement */
23292 static void
23293 cp_parser_handler (cp_parser* parser)
23295 tree handler;
23296 tree declaration;
23298 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
23299 handler = begin_handler ();
23300 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23301 declaration = cp_parser_exception_declaration (parser);
23302 finish_handler_parms (declaration, handler);
23303 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23304 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
23305 finish_handler (handler);
23308 /* Parse an exception-declaration.
23310 exception-declaration:
23311 type-specifier-seq declarator
23312 type-specifier-seq abstract-declarator
23313 type-specifier-seq
23316 Returns a VAR_DECL for the declaration, or NULL_TREE if the
23317 ellipsis variant is used. */
23319 static tree
23320 cp_parser_exception_declaration (cp_parser* parser)
23322 cp_decl_specifier_seq type_specifiers;
23323 cp_declarator *declarator;
23324 const char *saved_message;
23326 /* If it's an ellipsis, it's easy to handle. */
23327 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23329 /* Consume the `...' token. */
23330 cp_lexer_consume_token (parser->lexer);
23331 return NULL_TREE;
23334 /* Types may not be defined in exception-declarations. */
23335 saved_message = parser->type_definition_forbidden_message;
23336 parser->type_definition_forbidden_message
23337 = G_("types may not be defined in exception-declarations");
23339 /* Parse the type-specifier-seq. */
23340 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
23341 /*is_trailing_return=*/false,
23342 &type_specifiers);
23343 /* If it's a `)', then there is no declarator. */
23344 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23345 declarator = NULL;
23346 else
23347 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
23348 /*ctor_dtor_or_conv_p=*/NULL,
23349 /*parenthesized_p=*/NULL,
23350 /*member_p=*/false,
23351 /*friend_p=*/false);
23353 /* Restore the saved message. */
23354 parser->type_definition_forbidden_message = saved_message;
23356 if (!type_specifiers.any_specifiers_p)
23357 return error_mark_node;
23359 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
23362 /* Parse a throw-expression.
23364 throw-expression:
23365 throw assignment-expression [opt]
23367 Returns a THROW_EXPR representing the throw-expression. */
23369 static tree
23370 cp_parser_throw_expression (cp_parser* parser)
23372 tree expression;
23373 cp_token* token;
23375 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
23376 token = cp_lexer_peek_token (parser->lexer);
23377 /* Figure out whether or not there is an assignment-expression
23378 following the "throw" keyword. */
23379 if (token->type == CPP_COMMA
23380 || token->type == CPP_SEMICOLON
23381 || token->type == CPP_CLOSE_PAREN
23382 || token->type == CPP_CLOSE_SQUARE
23383 || token->type == CPP_CLOSE_BRACE
23384 || token->type == CPP_COLON)
23385 expression = NULL_TREE;
23386 else
23387 expression = cp_parser_assignment_expression (parser);
23389 return build_throw (expression);
23392 /* GNU Extensions */
23394 /* Parse an (optional) asm-specification.
23396 asm-specification:
23397 asm ( string-literal )
23399 If the asm-specification is present, returns a STRING_CST
23400 corresponding to the string-literal. Otherwise, returns
23401 NULL_TREE. */
23403 static tree
23404 cp_parser_asm_specification_opt (cp_parser* parser)
23406 cp_token *token;
23407 tree asm_specification;
23409 /* Peek at the next token. */
23410 token = cp_lexer_peek_token (parser->lexer);
23411 /* If the next token isn't the `asm' keyword, then there's no
23412 asm-specification. */
23413 if (!cp_parser_is_keyword (token, RID_ASM))
23414 return NULL_TREE;
23416 /* Consume the `asm' token. */
23417 cp_lexer_consume_token (parser->lexer);
23418 /* Look for the `('. */
23419 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23421 /* Look for the string-literal. */
23422 asm_specification = cp_parser_string_literal (parser, false, false);
23424 /* Look for the `)'. */
23425 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23427 return asm_specification;
23430 /* Parse an asm-operand-list.
23432 asm-operand-list:
23433 asm-operand
23434 asm-operand-list , asm-operand
23436 asm-operand:
23437 string-literal ( expression )
23438 [ string-literal ] string-literal ( expression )
23440 Returns a TREE_LIST representing the operands. The TREE_VALUE of
23441 each node is the expression. The TREE_PURPOSE is itself a
23442 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
23443 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
23444 is a STRING_CST for the string literal before the parenthesis. Returns
23445 ERROR_MARK_NODE if any of the operands are invalid. */
23447 static tree
23448 cp_parser_asm_operand_list (cp_parser* parser)
23450 tree asm_operands = NULL_TREE;
23451 bool invalid_operands = false;
23453 while (true)
23455 tree string_literal;
23456 tree expression;
23457 tree name;
23459 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
23461 /* Consume the `[' token. */
23462 cp_lexer_consume_token (parser->lexer);
23463 /* Read the operand name. */
23464 name = cp_parser_identifier (parser);
23465 if (name != error_mark_node)
23466 name = build_string (IDENTIFIER_LENGTH (name),
23467 IDENTIFIER_POINTER (name));
23468 /* Look for the closing `]'. */
23469 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23471 else
23472 name = NULL_TREE;
23473 /* Look for the string-literal. */
23474 string_literal = cp_parser_string_literal (parser, false, false);
23476 /* Look for the `('. */
23477 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23478 /* Parse the expression. */
23479 expression = cp_parser_expression (parser);
23480 /* Look for the `)'. */
23481 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23483 if (name == error_mark_node
23484 || string_literal == error_mark_node
23485 || expression == error_mark_node)
23486 invalid_operands = true;
23488 /* Add this operand to the list. */
23489 asm_operands = tree_cons (build_tree_list (name, string_literal),
23490 expression,
23491 asm_operands);
23492 /* If the next token is not a `,', there are no more
23493 operands. */
23494 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23495 break;
23496 /* Consume the `,'. */
23497 cp_lexer_consume_token (parser->lexer);
23500 return invalid_operands ? error_mark_node : nreverse (asm_operands);
23503 /* Parse an asm-clobber-list.
23505 asm-clobber-list:
23506 string-literal
23507 asm-clobber-list , string-literal
23509 Returns a TREE_LIST, indicating the clobbers in the order that they
23510 appeared. The TREE_VALUE of each node is a STRING_CST. */
23512 static tree
23513 cp_parser_asm_clobber_list (cp_parser* parser)
23515 tree clobbers = NULL_TREE;
23517 while (true)
23519 tree string_literal;
23521 /* Look for the string literal. */
23522 string_literal = cp_parser_string_literal (parser, false, false);
23523 /* Add it to the list. */
23524 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
23525 /* If the next token is not a `,', then the list is
23526 complete. */
23527 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23528 break;
23529 /* Consume the `,' token. */
23530 cp_lexer_consume_token (parser->lexer);
23533 return clobbers;
23536 /* Parse an asm-label-list.
23538 asm-label-list:
23539 identifier
23540 asm-label-list , identifier
23542 Returns a TREE_LIST, indicating the labels in the order that they
23543 appeared. The TREE_VALUE of each node is a label. */
23545 static tree
23546 cp_parser_asm_label_list (cp_parser* parser)
23548 tree labels = NULL_TREE;
23550 while (true)
23552 tree identifier, label, name;
23554 /* Look for the identifier. */
23555 identifier = cp_parser_identifier (parser);
23556 if (!error_operand_p (identifier))
23558 label = lookup_label (identifier);
23559 if (TREE_CODE (label) == LABEL_DECL)
23561 TREE_USED (label) = 1;
23562 check_goto (label);
23563 name = build_string (IDENTIFIER_LENGTH (identifier),
23564 IDENTIFIER_POINTER (identifier));
23565 labels = tree_cons (name, label, labels);
23568 /* If the next token is not a `,', then the list is
23569 complete. */
23570 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23571 break;
23572 /* Consume the `,' token. */
23573 cp_lexer_consume_token (parser->lexer);
23576 return nreverse (labels);
23579 /* Return TRUE iff the next tokens in the stream are possibly the
23580 beginning of a GNU extension attribute. */
23582 static bool
23583 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
23585 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
23588 /* Return TRUE iff the next tokens in the stream are possibly the
23589 beginning of a standard C++-11 attribute specifier. */
23591 static bool
23592 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
23594 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
23597 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23598 beginning of a standard C++-11 attribute specifier. */
23600 static bool
23601 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
23603 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23605 return (cxx_dialect >= cxx11
23606 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
23607 || (token->type == CPP_OPEN_SQUARE
23608 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
23609 && token->type == CPP_OPEN_SQUARE)));
23612 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23613 beginning of a GNU extension attribute. */
23615 static bool
23616 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
23618 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23620 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
23623 /* Return true iff the next tokens can be the beginning of either a
23624 GNU attribute list, or a standard C++11 attribute sequence. */
23626 static bool
23627 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
23629 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
23630 || cp_next_tokens_can_be_std_attribute_p (parser));
23633 /* Return true iff the next Nth tokens can be the beginning of either
23634 a GNU attribute list, or a standard C++11 attribute sequence. */
23636 static bool
23637 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
23639 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
23640 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
23643 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
23644 of GNU attributes, or return NULL. */
23646 static tree
23647 cp_parser_attributes_opt (cp_parser *parser)
23649 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
23650 return cp_parser_gnu_attributes_opt (parser);
23651 return cp_parser_std_attribute_spec_seq (parser);
23654 #define CILK_SIMD_FN_CLAUSE_MASK \
23655 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
23656 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
23657 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
23658 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
23659 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
23661 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
23662 vector [(<clauses>)] */
23664 static void
23665 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
23667 bool first_p = parser->cilk_simd_fn_info == NULL;
23668 cp_token *token = v_token;
23669 if (first_p)
23671 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
23672 parser->cilk_simd_fn_info->error_seen = false;
23673 parser->cilk_simd_fn_info->fndecl_seen = false;
23674 parser->cilk_simd_fn_info->tokens = vNULL;
23676 int paren_scope = 0;
23677 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23679 cp_lexer_consume_token (parser->lexer);
23680 v_token = cp_lexer_peek_token (parser->lexer);
23681 paren_scope++;
23683 while (paren_scope > 0)
23685 token = cp_lexer_peek_token (parser->lexer);
23686 if (token->type == CPP_OPEN_PAREN)
23687 paren_scope++;
23688 else if (token->type == CPP_CLOSE_PAREN)
23689 paren_scope--;
23690 /* Do not push the last ')' */
23691 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
23692 cp_lexer_consume_token (parser->lexer);
23695 token->type = CPP_PRAGMA_EOL;
23696 parser->lexer->next_token = token;
23697 cp_lexer_consume_token (parser->lexer);
23699 struct cp_token_cache *cp
23700 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
23701 parser->cilk_simd_fn_info->tokens.safe_push (cp);
23704 /* Parse an (optional) series of attributes.
23706 attributes:
23707 attributes attribute
23709 attribute:
23710 __attribute__ (( attribute-list [opt] ))
23712 The return value is as for cp_parser_gnu_attribute_list. */
23714 static tree
23715 cp_parser_gnu_attributes_opt (cp_parser* parser)
23717 tree attributes = NULL_TREE;
23719 while (true)
23721 cp_token *token;
23722 tree attribute_list;
23723 bool ok = true;
23725 /* Peek at the next token. */
23726 token = cp_lexer_peek_token (parser->lexer);
23727 /* If it's not `__attribute__', then we're done. */
23728 if (token->keyword != RID_ATTRIBUTE)
23729 break;
23731 /* Consume the `__attribute__' keyword. */
23732 cp_lexer_consume_token (parser->lexer);
23733 /* Look for the two `(' tokens. */
23734 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23735 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23737 /* Peek at the next token. */
23738 token = cp_lexer_peek_token (parser->lexer);
23739 if (token->type != CPP_CLOSE_PAREN)
23740 /* Parse the attribute-list. */
23741 attribute_list = cp_parser_gnu_attribute_list (parser);
23742 else
23743 /* If the next token is a `)', then there is no attribute
23744 list. */
23745 attribute_list = NULL;
23747 /* Look for the two `)' tokens. */
23748 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23749 ok = false;
23750 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23751 ok = false;
23752 if (!ok)
23753 cp_parser_skip_to_end_of_statement (parser);
23755 /* Add these new attributes to the list. */
23756 attributes = chainon (attributes, attribute_list);
23759 return attributes;
23762 /* Parse a GNU attribute-list.
23764 attribute-list:
23765 attribute
23766 attribute-list , attribute
23768 attribute:
23769 identifier
23770 identifier ( identifier )
23771 identifier ( identifier , expression-list )
23772 identifier ( expression-list )
23774 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
23775 to an attribute. The TREE_PURPOSE of each node is the identifier
23776 indicating which attribute is in use. The TREE_VALUE represents
23777 the arguments, if any. */
23779 static tree
23780 cp_parser_gnu_attribute_list (cp_parser* parser)
23782 tree attribute_list = NULL_TREE;
23783 bool save_translate_strings_p = parser->translate_strings_p;
23785 parser->translate_strings_p = false;
23786 while (true)
23788 cp_token *token;
23789 tree identifier;
23790 tree attribute;
23792 /* Look for the identifier. We also allow keywords here; for
23793 example `__attribute__ ((const))' is legal. */
23794 token = cp_lexer_peek_token (parser->lexer);
23795 if (token->type == CPP_NAME
23796 || token->type == CPP_KEYWORD)
23798 tree arguments = NULL_TREE;
23800 /* Consume the token, but save it since we need it for the
23801 SIMD enabled function parsing. */
23802 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
23804 /* Save away the identifier that indicates which attribute
23805 this is. */
23806 identifier = (token->type == CPP_KEYWORD)
23807 /* For keywords, use the canonical spelling, not the
23808 parsed identifier. */
23809 ? ridpointers[(int) token->keyword]
23810 : id_token->u.value;
23812 attribute = build_tree_list (identifier, NULL_TREE);
23814 /* Peek at the next token. */
23815 token = cp_lexer_peek_token (parser->lexer);
23816 /* If it's an `(', then parse the attribute arguments. */
23817 if (token->type == CPP_OPEN_PAREN)
23819 vec<tree, va_gc> *vec;
23820 int attr_flag = (attribute_takes_identifier_p (identifier)
23821 ? id_attr : normal_attr);
23822 if (is_cilkplus_vector_p (identifier))
23824 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23825 continue;
23827 else
23828 vec = cp_parser_parenthesized_expression_list
23829 (parser, attr_flag, /*cast_p=*/false,
23830 /*allow_expansion_p=*/false,
23831 /*non_constant_p=*/NULL);
23832 if (vec == NULL)
23833 arguments = error_mark_node;
23834 else
23836 arguments = build_tree_list_vec (vec);
23837 release_tree_vector (vec);
23839 /* Save the arguments away. */
23840 TREE_VALUE (attribute) = arguments;
23842 else if (is_cilkplus_vector_p (identifier))
23844 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23845 continue;
23848 if (arguments != error_mark_node)
23850 /* Add this attribute to the list. */
23851 TREE_CHAIN (attribute) = attribute_list;
23852 attribute_list = attribute;
23855 token = cp_lexer_peek_token (parser->lexer);
23857 /* Now, look for more attributes. If the next token isn't a
23858 `,', we're done. */
23859 if (token->type != CPP_COMMA)
23860 break;
23862 /* Consume the comma and keep going. */
23863 cp_lexer_consume_token (parser->lexer);
23865 parser->translate_strings_p = save_translate_strings_p;
23867 /* We built up the list in reverse order. */
23868 return nreverse (attribute_list);
23871 /* Parse a standard C++11 attribute.
23873 The returned representation is a TREE_LIST which TREE_PURPOSE is
23874 the scoped name of the attribute, and the TREE_VALUE is its
23875 arguments list.
23877 Note that the scoped name of the attribute is itself a TREE_LIST
23878 which TREE_PURPOSE is the namespace of the attribute, and
23879 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
23880 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
23881 and which TREE_PURPOSE is directly the attribute name.
23883 Clients of the attribute code should use get_attribute_namespace
23884 and get_attribute_name to get the actual namespace and name of
23885 attributes, regardless of their being GNU or C++11 attributes.
23887 attribute:
23888 attribute-token attribute-argument-clause [opt]
23890 attribute-token:
23891 identifier
23892 attribute-scoped-token
23894 attribute-scoped-token:
23895 attribute-namespace :: identifier
23897 attribute-namespace:
23898 identifier
23900 attribute-argument-clause:
23901 ( balanced-token-seq )
23903 balanced-token-seq:
23904 balanced-token [opt]
23905 balanced-token-seq balanced-token
23907 balanced-token:
23908 ( balanced-token-seq )
23909 [ balanced-token-seq ]
23910 { balanced-token-seq }. */
23912 static tree
23913 cp_parser_std_attribute (cp_parser *parser)
23915 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
23916 cp_token *token;
23918 /* First, parse name of the attribute, a.k.a attribute-token. */
23920 token = cp_lexer_peek_token (parser->lexer);
23921 if (token->type == CPP_NAME)
23922 attr_id = token->u.value;
23923 else if (token->type == CPP_KEYWORD)
23924 attr_id = ridpointers[(int) token->keyword];
23925 else if (token->flags & NAMED_OP)
23926 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
23928 if (attr_id == NULL_TREE)
23929 return NULL_TREE;
23931 cp_lexer_consume_token (parser->lexer);
23933 token = cp_lexer_peek_token (parser->lexer);
23934 if (token->type == CPP_SCOPE)
23936 /* We are seeing a scoped attribute token. */
23938 cp_lexer_consume_token (parser->lexer);
23939 attr_ns = attr_id;
23941 token = cp_lexer_consume_token (parser->lexer);
23942 if (token->type == CPP_NAME)
23943 attr_id = token->u.value;
23944 else if (token->type == CPP_KEYWORD)
23945 attr_id = ridpointers[(int) token->keyword];
23946 else
23948 error_at (token->location,
23949 "expected an identifier for the attribute name");
23950 return error_mark_node;
23952 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
23953 NULL_TREE);
23954 token = cp_lexer_peek_token (parser->lexer);
23956 else
23958 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
23959 NULL_TREE);
23960 /* C++11 noreturn attribute is equivalent to GNU's. */
23961 if (is_attribute_p ("noreturn", attr_id))
23962 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23963 /* C++14 deprecated attribute is equivalent to GNU's. */
23964 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
23966 if (cxx_dialect == cxx11)
23967 pedwarn (token->location, OPT_Wpedantic,
23968 "%<deprecated%> is a C++14 feature;"
23969 " use %<gnu::deprecated%>");
23970 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23972 /* Transactional Memory TS optimize_for_synchronized attribute is
23973 equivalent to GNU transaction_callable. */
23974 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
23975 TREE_PURPOSE (attribute)
23976 = get_identifier ("transaction_callable");
23977 /* Transactional Memory attributes are GNU attributes. */
23978 else if (tm_attr_to_mask (attr_id))
23979 TREE_PURPOSE (attribute) = attr_id;
23982 /* Now parse the optional argument clause of the attribute. */
23984 if (token->type != CPP_OPEN_PAREN)
23985 return attribute;
23988 vec<tree, va_gc> *vec;
23989 int attr_flag = normal_attr;
23991 if (attr_ns == get_identifier ("gnu")
23992 && attribute_takes_identifier_p (attr_id))
23993 /* A GNU attribute that takes an identifier in parameter. */
23994 attr_flag = id_attr;
23996 vec = cp_parser_parenthesized_expression_list
23997 (parser, attr_flag, /*cast_p=*/false,
23998 /*allow_expansion_p=*/true,
23999 /*non_constant_p=*/NULL);
24000 if (vec == NULL)
24001 arguments = error_mark_node;
24002 else
24004 arguments = build_tree_list_vec (vec);
24005 release_tree_vector (vec);
24008 if (arguments == error_mark_node)
24009 attribute = error_mark_node;
24010 else
24011 TREE_VALUE (attribute) = arguments;
24014 return attribute;
24017 /* Check that the attribute ATTRIBUTE appears at most once in the
24018 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
24019 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
24020 isn't implemented yet in GCC. */
24022 static void
24023 cp_parser_check_std_attribute (tree attributes, tree attribute)
24025 if (attributes)
24027 tree name = get_attribute_name (attribute);
24028 if (is_attribute_p ("noreturn", name)
24029 && lookup_attribute ("noreturn", attributes))
24030 error ("attribute noreturn can appear at most once "
24031 "in an attribute-list");
24032 else if (is_attribute_p ("deprecated", name)
24033 && lookup_attribute ("deprecated", attributes))
24034 error ("attribute deprecated can appear at most once "
24035 "in an attribute-list");
24039 /* Parse a list of standard C++-11 attributes.
24041 attribute-list:
24042 attribute [opt]
24043 attribute-list , attribute[opt]
24044 attribute ...
24045 attribute-list , attribute ...
24048 static tree
24049 cp_parser_std_attribute_list (cp_parser *parser)
24051 tree attributes = NULL_TREE, attribute = NULL_TREE;
24052 cp_token *token = NULL;
24054 while (true)
24056 attribute = cp_parser_std_attribute (parser);
24057 if (attribute == error_mark_node)
24058 break;
24059 if (attribute != NULL_TREE)
24061 cp_parser_check_std_attribute (attributes, attribute);
24062 TREE_CHAIN (attribute) = attributes;
24063 attributes = attribute;
24065 token = cp_lexer_peek_token (parser->lexer);
24066 if (token->type == CPP_ELLIPSIS)
24068 cp_lexer_consume_token (parser->lexer);
24069 TREE_VALUE (attribute)
24070 = make_pack_expansion (TREE_VALUE (attribute));
24071 token = cp_lexer_peek_token (parser->lexer);
24073 if (token->type != CPP_COMMA)
24074 break;
24075 cp_lexer_consume_token (parser->lexer);
24077 attributes = nreverse (attributes);
24078 return attributes;
24081 /* Parse a standard C++-11 attribute specifier.
24083 attribute-specifier:
24084 [ [ attribute-list ] ]
24085 alignment-specifier
24087 alignment-specifier:
24088 alignas ( type-id ... [opt] )
24089 alignas ( alignment-expression ... [opt] ). */
24091 static tree
24092 cp_parser_std_attribute_spec (cp_parser *parser)
24094 tree attributes = NULL_TREE;
24095 cp_token *token = cp_lexer_peek_token (parser->lexer);
24097 if (token->type == CPP_OPEN_SQUARE
24098 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
24100 cp_lexer_consume_token (parser->lexer);
24101 cp_lexer_consume_token (parser->lexer);
24103 attributes = cp_parser_std_attribute_list (parser);
24105 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
24106 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24107 cp_parser_skip_to_end_of_statement (parser);
24108 else
24109 /* Warn about parsing c++11 attribute in non-c++1 mode, only
24110 when we are sure that we have actually parsed them. */
24111 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24113 else
24115 tree alignas_expr;
24117 /* Look for an alignment-specifier. */
24119 token = cp_lexer_peek_token (parser->lexer);
24121 if (token->type != CPP_KEYWORD
24122 || token->keyword != RID_ALIGNAS)
24123 return NULL_TREE;
24125 cp_lexer_consume_token (parser->lexer);
24126 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24128 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
24130 cp_parser_error (parser, "expected %<(%>");
24131 return error_mark_node;
24134 cp_parser_parse_tentatively (parser);
24135 alignas_expr = cp_parser_type_id (parser);
24137 if (!cp_parser_parse_definitely (parser))
24139 gcc_assert (alignas_expr == error_mark_node
24140 || alignas_expr == NULL_TREE);
24142 alignas_expr =
24143 cp_parser_assignment_expression (parser);
24144 if (alignas_expr == error_mark_node)
24145 cp_parser_skip_to_end_of_statement (parser);
24146 if (alignas_expr == NULL_TREE
24147 || alignas_expr == error_mark_node)
24148 return alignas_expr;
24151 alignas_expr = cxx_alignas_expr (alignas_expr);
24152 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
24154 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24156 cp_lexer_consume_token (parser->lexer);
24157 alignas_expr = make_pack_expansion (alignas_expr);
24160 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
24162 cp_parser_error (parser, "expected %<)%>");
24163 return error_mark_node;
24166 /* Build the C++-11 representation of an 'aligned'
24167 attribute. */
24168 attributes =
24169 build_tree_list (build_tree_list (get_identifier ("gnu"),
24170 get_identifier ("aligned")),
24171 alignas_expr);
24174 return attributes;
24177 /* Parse a standard C++-11 attribute-specifier-seq.
24179 attribute-specifier-seq:
24180 attribute-specifier-seq [opt] attribute-specifier
24183 static tree
24184 cp_parser_std_attribute_spec_seq (cp_parser *parser)
24186 tree attr_specs = NULL_TREE;
24187 tree attr_last = NULL_TREE;
24189 while (true)
24191 tree attr_spec = cp_parser_std_attribute_spec (parser);
24192 if (attr_spec == NULL_TREE)
24193 break;
24194 if (attr_spec == error_mark_node)
24195 return error_mark_node;
24197 if (attr_last)
24198 TREE_CHAIN (attr_last) = attr_spec;
24199 else
24200 attr_specs = attr_last = attr_spec;
24201 attr_last = tree_last (attr_last);
24204 return attr_specs;
24207 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
24208 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
24209 current value of the PEDANTIC flag, regardless of whether or not
24210 the `__extension__' keyword is present. The caller is responsible
24211 for restoring the value of the PEDANTIC flag. */
24213 static bool
24214 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
24216 /* Save the old value of the PEDANTIC flag. */
24217 *saved_pedantic = pedantic;
24219 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
24221 /* Consume the `__extension__' token. */
24222 cp_lexer_consume_token (parser->lexer);
24223 /* We're not being pedantic while the `__extension__' keyword is
24224 in effect. */
24225 pedantic = 0;
24227 return true;
24230 return false;
24233 /* Parse a label declaration.
24235 label-declaration:
24236 __label__ label-declarator-seq ;
24238 label-declarator-seq:
24239 identifier , label-declarator-seq
24240 identifier */
24242 static void
24243 cp_parser_label_declaration (cp_parser* parser)
24245 /* Look for the `__label__' keyword. */
24246 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
24248 while (true)
24250 tree identifier;
24252 /* Look for an identifier. */
24253 identifier = cp_parser_identifier (parser);
24254 /* If we failed, stop. */
24255 if (identifier == error_mark_node)
24256 break;
24257 /* Declare it as a label. */
24258 finish_label_decl (identifier);
24259 /* If the next token is a `;', stop. */
24260 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24261 break;
24262 /* Look for the `,' separating the label declarations. */
24263 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
24266 /* Look for the final `;'. */
24267 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24270 // -------------------------------------------------------------------------- //
24271 // Requires Clause
24273 // Parse a requires clause.
24275 // requires-clause:
24276 // 'requires' logical-or-expression
24278 // The required logical-or-expression must be a constant expression. Note
24279 // that we don't check that the expression is constepxr here. We defer until
24280 // we analyze constraints and then, we only check atomic constraints.
24281 static tree
24282 cp_parser_requires_clause (cp_parser *parser)
24284 // Parse the requires clause so that it is not automatically folded.
24285 ++processing_template_decl;
24286 tree expr = cp_parser_binary_expression (parser, false, false,
24287 PREC_NOT_OPERATOR, NULL);
24288 if (check_for_bare_parameter_packs (expr))
24289 expr = error_mark_node;
24290 --processing_template_decl;
24291 return expr;
24294 // Optionally parse a requires clause:
24295 static tree
24296 cp_parser_requires_clause_opt (cp_parser *parser)
24298 cp_token *tok = cp_lexer_peek_token (parser->lexer);
24299 if (tok->keyword != RID_REQUIRES)
24301 if (!flag_concepts && tok->type == CPP_NAME
24302 && tok->u.value == ridpointers[RID_REQUIRES])
24304 error_at (cp_lexer_peek_token (parser->lexer)->location,
24305 "%<requires%> only available with -fconcepts");
24306 /* Parse and discard the requires-clause. */
24307 cp_lexer_consume_token (parser->lexer);
24308 cp_parser_requires_clause (parser);
24310 return NULL_TREE;
24312 cp_lexer_consume_token (parser->lexer);
24313 return cp_parser_requires_clause (parser);
24317 /*---------------------------------------------------------------------------
24318 Requires expressions
24319 ---------------------------------------------------------------------------*/
24321 /* Parse a requires expression
24323 requirement-expression:
24324 'requires' requirement-parameter-list [opt] requirement-body */
24325 static tree
24326 cp_parser_requires_expression (cp_parser *parser)
24328 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
24329 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
24331 /* A requires-expression shall appear only within a concept
24332 definition or a requires-clause.
24334 TODO: Implement this diagnostic correctly. */
24335 if (!processing_template_decl)
24337 error_at (loc, "a requires expression cannot appear outside a template");
24338 cp_parser_skip_to_end_of_statement (parser);
24339 return error_mark_node;
24342 tree parms, reqs;
24344 /* Local parameters are delared as variables within the scope
24345 of the expression. They are not visible past the end of
24346 the expression. Expressions within the requires-expression
24347 are unevaluated. */
24348 struct scope_sentinel
24350 scope_sentinel ()
24352 ++cp_unevaluated_operand;
24353 begin_scope (sk_block, NULL_TREE);
24356 ~scope_sentinel ()
24358 pop_bindings_and_leave_scope ();
24359 --cp_unevaluated_operand;
24361 } s;
24363 /* Parse the optional parameter list. */
24364 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24366 parms = cp_parser_requirement_parameter_list (parser);
24367 if (parms == error_mark_node)
24368 return error_mark_node;
24370 else
24371 parms = NULL_TREE;
24373 /* Parse the requirement body. */
24374 reqs = cp_parser_requirement_body (parser);
24375 if (reqs == error_mark_node)
24376 return error_mark_node;
24379 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
24380 the parm chain. */
24381 grokparms (parms, &parms);
24382 return finish_requires_expr (parms, reqs);
24385 /* Parse a parameterized requirement.
24387 requirement-parameter-list:
24388 '(' parameter-declaration-clause ')' */
24389 static tree
24390 cp_parser_requirement_parameter_list (cp_parser *parser)
24392 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24393 return error_mark_node;
24395 tree parms = cp_parser_parameter_declaration_clause (parser);
24397 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24398 return error_mark_node;
24400 return parms;
24403 /* Parse the body of a requirement.
24405 requirement-body:
24406 '{' requirement-list '}' */
24407 static tree
24408 cp_parser_requirement_body (cp_parser *parser)
24410 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24411 return error_mark_node;
24413 tree reqs = cp_parser_requirement_list (parser);
24415 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24416 return error_mark_node;
24418 return reqs;
24421 /* Parse a list of requirements.
24423 requirement-list:
24424 requirement
24425 requirement-list ';' requirement[opt] */
24426 static tree
24427 cp_parser_requirement_list (cp_parser *parser)
24429 tree result = NULL_TREE;
24430 while (true)
24432 tree req = cp_parser_requirement (parser);
24433 if (req == error_mark_node)
24434 return error_mark_node;
24436 result = tree_cons (NULL_TREE, req, result);
24438 /* If we see a semi-colon, consume it. */
24439 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24440 cp_lexer_consume_token (parser->lexer);
24442 /* Stop processing at the end of the list. */
24443 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24444 break;
24447 /* Reverse the order of requirements so they are analyzed in
24448 declaration order. */
24449 return nreverse (result);
24452 /* Parse a syntactic requirement or type requirement.
24454 requirement:
24455 simple-requirement
24456 compound-requirement
24457 type-requirement
24458 nested-requirement */
24459 static tree
24460 cp_parser_requirement (cp_parser *parser)
24462 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24463 return cp_parser_compound_requirement (parser);
24464 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24465 return cp_parser_type_requirement (parser);
24466 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
24467 return cp_parser_nested_requirement (parser);
24468 else
24469 return cp_parser_simple_requirement (parser);
24472 /* Parse a simple requirement.
24474 simple-requirement:
24475 expression ';' */
24476 static tree
24477 cp_parser_simple_requirement (cp_parser *parser)
24479 tree expr = cp_parser_expression (parser, NULL, false, false);
24480 if (!expr || expr == error_mark_node)
24481 return error_mark_node;
24483 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24484 return error_mark_node;
24486 return finish_simple_requirement (expr);
24489 /* Parse a type requirement
24491 type-requirement
24492 nested-name-specifier [opt] required-type-name ';'
24494 required-type-name:
24495 type-name
24496 'template' [opt] simple-template-id */
24497 static tree
24498 cp_parser_type_requirement (cp_parser *parser)
24500 cp_lexer_consume_token (parser->lexer);
24502 // Save the scope before parsing name specifiers.
24503 tree saved_scope = parser->scope;
24504 tree saved_object_scope = parser->object_scope;
24505 tree saved_qualifying_scope = parser->qualifying_scope;
24506 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
24507 cp_parser_nested_name_specifier_opt (parser,
24508 /*typename_keyword_p=*/true,
24509 /*check_dependency_p=*/false,
24510 /*type_p=*/true,
24511 /*is_declaration=*/false);
24513 tree type;
24514 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24516 cp_lexer_consume_token (parser->lexer);
24517 type = cp_parser_template_id (parser,
24518 /*template_keyword_p=*/true,
24519 /*check_dependency=*/false,
24520 /*tag_type=*/none_type,
24521 /*is_declaration=*/false);
24522 type = make_typename_type (parser->scope, type, typename_type,
24523 /*complain=*/tf_error);
24525 else
24526 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
24528 if (TREE_CODE (type) == TYPE_DECL)
24529 type = TREE_TYPE (type);
24531 parser->scope = saved_scope;
24532 parser->object_scope = saved_object_scope;
24533 parser->qualifying_scope = saved_qualifying_scope;
24535 if (type == error_mark_node)
24536 cp_parser_skip_to_end_of_statement (parser);
24538 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24539 return error_mark_node;
24540 if (type == error_mark_node)
24541 return error_mark_node;
24543 return finish_type_requirement (type);
24546 /* Parse a compound requirement
24548 compound-requirement:
24549 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
24550 static tree
24551 cp_parser_compound_requirement (cp_parser *parser)
24553 /* Parse an expression enclosed in '{ }'s. */
24554 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24555 return error_mark_node;
24557 tree expr = cp_parser_expression (parser, NULL, false, false);
24558 if (!expr || expr == error_mark_node)
24559 return error_mark_node;
24561 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24562 return error_mark_node;
24564 /* Parse the optional noexcept. */
24565 bool noexcept_p = false;
24566 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
24568 cp_lexer_consume_token (parser->lexer);
24569 noexcept_p = true;
24572 /* Parse the optional trailing return type. */
24573 tree type = NULL_TREE;
24574 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
24576 cp_lexer_consume_token (parser->lexer);
24577 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
24578 parser->in_result_type_constraint_p = true;
24579 type = cp_parser_trailing_type_id (parser);
24580 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
24581 if (type == error_mark_node)
24582 return error_mark_node;
24585 return finish_compound_requirement (expr, type, noexcept_p);
24588 /* Parse a nested requirement. This is the same as a requires clause.
24590 nested-requirement:
24591 requires-clause */
24592 static tree
24593 cp_parser_nested_requirement (cp_parser *parser)
24595 cp_lexer_consume_token (parser->lexer);
24596 tree req = cp_parser_requires_clause (parser);
24597 if (req == error_mark_node)
24598 return error_mark_node;
24599 return finish_nested_requirement (req);
24602 /* Support Functions */
24604 /* Return the appropriate prefer_type argument for lookup_name_real based on
24605 tag_type and template_mem_access. */
24607 static inline int
24608 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
24610 /* DR 141: When looking in the current enclosing context for a template-name
24611 after -> or ., only consider class templates. */
24612 if (template_mem_access)
24613 return 2;
24614 switch (tag_type)
24616 case none_type: return 0; // No preference.
24617 case scope_type: return 1; // Type or namespace.
24618 default: return 2; // Type only.
24622 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
24623 NAME should have one of the representations used for an
24624 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
24625 is returned. If PARSER->SCOPE is a dependent type, then a
24626 SCOPE_REF is returned.
24628 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
24629 returned; the name was already resolved when the TEMPLATE_ID_EXPR
24630 was formed. Abstractly, such entities should not be passed to this
24631 function, because they do not need to be looked up, but it is
24632 simpler to check for this special case here, rather than at the
24633 call-sites.
24635 In cases not explicitly covered above, this function returns a
24636 DECL, OVERLOAD, or baselink representing the result of the lookup.
24637 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
24638 is returned.
24640 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
24641 (e.g., "struct") that was used. In that case bindings that do not
24642 refer to types are ignored.
24644 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
24645 ignored.
24647 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
24648 are ignored.
24650 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
24651 types.
24653 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
24654 TREE_LIST of candidates if name-lookup results in an ambiguity, and
24655 NULL_TREE otherwise. */
24657 static cp_expr
24658 cp_parser_lookup_name (cp_parser *parser, tree name,
24659 enum tag_types tag_type,
24660 bool is_template,
24661 bool is_namespace,
24662 bool check_dependency,
24663 tree *ambiguous_decls,
24664 location_t name_location)
24666 tree decl;
24667 tree object_type = parser->context->object_type;
24669 /* Assume that the lookup will be unambiguous. */
24670 if (ambiguous_decls)
24671 *ambiguous_decls = NULL_TREE;
24673 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
24674 no longer valid. Note that if we are parsing tentatively, and
24675 the parse fails, OBJECT_TYPE will be automatically restored. */
24676 parser->context->object_type = NULL_TREE;
24678 if (name == error_mark_node)
24679 return error_mark_node;
24681 /* A template-id has already been resolved; there is no lookup to
24682 do. */
24683 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
24684 return name;
24685 if (BASELINK_P (name))
24687 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
24688 == TEMPLATE_ID_EXPR);
24689 return name;
24692 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
24693 it should already have been checked to make sure that the name
24694 used matches the type being destroyed. */
24695 if (TREE_CODE (name) == BIT_NOT_EXPR)
24697 tree type;
24699 /* Figure out to which type this destructor applies. */
24700 if (parser->scope)
24701 type = parser->scope;
24702 else if (object_type)
24703 type = object_type;
24704 else
24705 type = current_class_type;
24706 /* If that's not a class type, there is no destructor. */
24707 if (!type || !CLASS_TYPE_P (type))
24708 return error_mark_node;
24709 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
24710 lazily_declare_fn (sfk_destructor, type);
24711 if (!CLASSTYPE_DESTRUCTORS (type))
24712 return error_mark_node;
24713 /* If it was a class type, return the destructor. */
24714 return CLASSTYPE_DESTRUCTORS (type);
24717 /* By this point, the NAME should be an ordinary identifier. If
24718 the id-expression was a qualified name, the qualifying scope is
24719 stored in PARSER->SCOPE at this point. */
24720 gcc_assert (identifier_p (name));
24722 /* Perform the lookup. */
24723 if (parser->scope)
24725 bool dependent_p;
24727 if (parser->scope == error_mark_node)
24728 return error_mark_node;
24730 /* If the SCOPE is dependent, the lookup must be deferred until
24731 the template is instantiated -- unless we are explicitly
24732 looking up names in uninstantiated templates. Even then, we
24733 cannot look up the name if the scope is not a class type; it
24734 might, for example, be a template type parameter. */
24735 dependent_p = (TYPE_P (parser->scope)
24736 && dependent_scope_p (parser->scope));
24737 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
24738 && dependent_p)
24739 /* Defer lookup. */
24740 decl = error_mark_node;
24741 else
24743 tree pushed_scope = NULL_TREE;
24745 /* If PARSER->SCOPE is a dependent type, then it must be a
24746 class type, and we must not be checking dependencies;
24747 otherwise, we would have processed this lookup above. So
24748 that PARSER->SCOPE is not considered a dependent base by
24749 lookup_member, we must enter the scope here. */
24750 if (dependent_p)
24751 pushed_scope = push_scope (parser->scope);
24753 /* If the PARSER->SCOPE is a template specialization, it
24754 may be instantiated during name lookup. In that case,
24755 errors may be issued. Even if we rollback the current
24756 tentative parse, those errors are valid. */
24757 decl = lookup_qualified_name (parser->scope, name,
24758 prefer_type_arg (tag_type),
24759 /*complain=*/true);
24761 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
24762 lookup result and the nested-name-specifier nominates a class C:
24763 * if the name specified after the nested-name-specifier, when
24764 looked up in C, is the injected-class-name of C (Clause 9), or
24765 * if the name specified after the nested-name-specifier is the
24766 same as the identifier or the simple-template-id's template-
24767 name in the last component of the nested-name-specifier,
24768 the name is instead considered to name the constructor of
24769 class C. [ Note: for example, the constructor is not an
24770 acceptable lookup result in an elaborated-type-specifier so
24771 the constructor would not be used in place of the
24772 injected-class-name. --end note ] Such a constructor name
24773 shall be used only in the declarator-id of a declaration that
24774 names a constructor or in a using-declaration. */
24775 if (tag_type == none_type
24776 && DECL_SELF_REFERENCE_P (decl)
24777 && same_type_p (DECL_CONTEXT (decl), parser->scope))
24778 decl = lookup_qualified_name (parser->scope, ctor_identifier,
24779 prefer_type_arg (tag_type),
24780 /*complain=*/true);
24782 /* If we have a single function from a using decl, pull it out. */
24783 if (TREE_CODE (decl) == OVERLOAD
24784 && !really_overloaded_fn (decl))
24785 decl = OVL_FUNCTION (decl);
24787 if (pushed_scope)
24788 pop_scope (pushed_scope);
24791 /* If the scope is a dependent type and either we deferred lookup or
24792 we did lookup but didn't find the name, rememeber the name. */
24793 if (decl == error_mark_node && TYPE_P (parser->scope)
24794 && dependent_type_p (parser->scope))
24796 if (tag_type)
24798 tree type;
24800 /* The resolution to Core Issue 180 says that `struct
24801 A::B' should be considered a type-name, even if `A'
24802 is dependent. */
24803 type = make_typename_type (parser->scope, name, tag_type,
24804 /*complain=*/tf_error);
24805 if (type != error_mark_node)
24806 decl = TYPE_NAME (type);
24808 else if (is_template
24809 && (cp_parser_next_token_ends_template_argument_p (parser)
24810 || cp_lexer_next_token_is (parser->lexer,
24811 CPP_CLOSE_PAREN)))
24812 decl = make_unbound_class_template (parser->scope,
24813 name, NULL_TREE,
24814 /*complain=*/tf_error);
24815 else
24816 decl = build_qualified_name (/*type=*/NULL_TREE,
24817 parser->scope, name,
24818 is_template);
24820 parser->qualifying_scope = parser->scope;
24821 parser->object_scope = NULL_TREE;
24823 else if (object_type)
24825 /* Look up the name in the scope of the OBJECT_TYPE, unless the
24826 OBJECT_TYPE is not a class. */
24827 if (CLASS_TYPE_P (object_type))
24828 /* If the OBJECT_TYPE is a template specialization, it may
24829 be instantiated during name lookup. In that case, errors
24830 may be issued. Even if we rollback the current tentative
24831 parse, those errors are valid. */
24832 decl = lookup_member (object_type,
24833 name,
24834 /*protect=*/0,
24835 prefer_type_arg (tag_type),
24836 tf_warning_or_error);
24837 else
24838 decl = NULL_TREE;
24840 if (!decl)
24841 /* Look it up in the enclosing context. DR 141: When looking for a
24842 template-name after -> or ., only consider class templates. */
24843 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
24844 /*nonclass=*/0,
24845 /*block_p=*/true, is_namespace, 0);
24846 if (object_type == unknown_type_node)
24847 /* The object is type-dependent, so we can't look anything up; we used
24848 this to get the DR 141 behavior. */
24849 object_type = NULL_TREE;
24850 parser->object_scope = object_type;
24851 parser->qualifying_scope = NULL_TREE;
24853 else
24855 decl = lookup_name_real (name, prefer_type_arg (tag_type),
24856 /*nonclass=*/0,
24857 /*block_p=*/true, is_namespace, 0);
24858 parser->qualifying_scope = NULL_TREE;
24859 parser->object_scope = NULL_TREE;
24862 /* If the lookup failed, let our caller know. */
24863 if (!decl || decl == error_mark_node)
24864 return error_mark_node;
24866 /* Pull out the template from an injected-class-name (or multiple). */
24867 if (is_template)
24868 decl = maybe_get_template_decl_from_type_decl (decl);
24870 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
24871 if (TREE_CODE (decl) == TREE_LIST)
24873 if (ambiguous_decls)
24874 *ambiguous_decls = decl;
24875 /* The error message we have to print is too complicated for
24876 cp_parser_error, so we incorporate its actions directly. */
24877 if (!cp_parser_simulate_error (parser))
24879 error_at (name_location, "reference to %qD is ambiguous",
24880 name);
24881 print_candidates (decl);
24883 return error_mark_node;
24886 gcc_assert (DECL_P (decl)
24887 || TREE_CODE (decl) == OVERLOAD
24888 || TREE_CODE (decl) == SCOPE_REF
24889 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
24890 || BASELINK_P (decl));
24892 /* If we have resolved the name of a member declaration, check to
24893 see if the declaration is accessible. When the name resolves to
24894 set of overloaded functions, accessibility is checked when
24895 overload resolution is done.
24897 During an explicit instantiation, access is not checked at all,
24898 as per [temp.explicit]. */
24899 if (DECL_P (decl))
24900 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
24902 maybe_record_typedef_use (decl);
24904 return cp_expr (decl, name_location);
24907 /* Like cp_parser_lookup_name, but for use in the typical case where
24908 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
24909 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
24911 static tree
24912 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
24914 return cp_parser_lookup_name (parser, name,
24915 none_type,
24916 /*is_template=*/false,
24917 /*is_namespace=*/false,
24918 /*check_dependency=*/true,
24919 /*ambiguous_decls=*/NULL,
24920 location);
24923 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
24924 the current context, return the TYPE_DECL. If TAG_NAME_P is
24925 true, the DECL indicates the class being defined in a class-head,
24926 or declared in an elaborated-type-specifier.
24928 Otherwise, return DECL. */
24930 static tree
24931 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
24933 /* If the TEMPLATE_DECL is being declared as part of a class-head,
24934 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
24936 struct A {
24937 template <typename T> struct B;
24940 template <typename T> struct A::B {};
24942 Similarly, in an elaborated-type-specifier:
24944 namespace N { struct X{}; }
24946 struct A {
24947 template <typename T> friend struct N::X;
24950 However, if the DECL refers to a class type, and we are in
24951 the scope of the class, then the name lookup automatically
24952 finds the TYPE_DECL created by build_self_reference rather
24953 than a TEMPLATE_DECL. For example, in:
24955 template <class T> struct S {
24956 S s;
24959 there is no need to handle such case. */
24961 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
24962 return DECL_TEMPLATE_RESULT (decl);
24964 return decl;
24967 /* If too many, or too few, template-parameter lists apply to the
24968 declarator, issue an error message. Returns TRUE if all went well,
24969 and FALSE otherwise. */
24971 static bool
24972 cp_parser_check_declarator_template_parameters (cp_parser* parser,
24973 cp_declarator *declarator,
24974 location_t declarator_location)
24976 switch (declarator->kind)
24978 case cdk_id:
24980 unsigned num_templates = 0;
24981 tree scope = declarator->u.id.qualifying_scope;
24983 if (scope)
24984 num_templates = num_template_headers_for_class (scope);
24985 else if (TREE_CODE (declarator->u.id.unqualified_name)
24986 == TEMPLATE_ID_EXPR)
24987 /* If the DECLARATOR has the form `X<y>' then it uses one
24988 additional level of template parameters. */
24989 ++num_templates;
24991 return cp_parser_check_template_parameters
24992 (parser, num_templates, declarator_location, declarator);
24995 case cdk_function:
24996 case cdk_array:
24997 case cdk_pointer:
24998 case cdk_reference:
24999 case cdk_ptrmem:
25000 return (cp_parser_check_declarator_template_parameters
25001 (parser, declarator->declarator, declarator_location));
25003 case cdk_error:
25004 return true;
25006 default:
25007 gcc_unreachable ();
25009 return false;
25012 /* NUM_TEMPLATES were used in the current declaration. If that is
25013 invalid, return FALSE and issue an error messages. Otherwise,
25014 return TRUE. If DECLARATOR is non-NULL, then we are checking a
25015 declarator and we can print more accurate diagnostics. */
25017 static bool
25018 cp_parser_check_template_parameters (cp_parser* parser,
25019 unsigned num_templates,
25020 location_t location,
25021 cp_declarator *declarator)
25023 /* If there are the same number of template classes and parameter
25024 lists, that's OK. */
25025 if (parser->num_template_parameter_lists == num_templates)
25026 return true;
25027 /* If there are more, but only one more, then we are referring to a
25028 member template. That's OK too. */
25029 if (parser->num_template_parameter_lists == num_templates + 1)
25030 return true;
25031 /* If there are more template classes than parameter lists, we have
25032 something like:
25034 template <class T> void S<T>::R<T>::f (); */
25035 if (parser->num_template_parameter_lists < num_templates)
25037 if (declarator && !current_function_decl)
25038 error_at (location, "specializing member %<%T::%E%> "
25039 "requires %<template<>%> syntax",
25040 declarator->u.id.qualifying_scope,
25041 declarator->u.id.unqualified_name);
25042 else if (declarator)
25043 error_at (location, "invalid declaration of %<%T::%E%>",
25044 declarator->u.id.qualifying_scope,
25045 declarator->u.id.unqualified_name);
25046 else
25047 error_at (location, "too few template-parameter-lists");
25048 return false;
25050 /* Otherwise, there are too many template parameter lists. We have
25051 something like:
25053 template <class T> template <class U> void S::f(); */
25054 error_at (location, "too many template-parameter-lists");
25055 return false;
25058 /* Parse an optional `::' token indicating that the following name is
25059 from the global namespace. If so, PARSER->SCOPE is set to the
25060 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
25061 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
25062 Returns the new value of PARSER->SCOPE, if the `::' token is
25063 present, and NULL_TREE otherwise. */
25065 static tree
25066 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
25068 cp_token *token;
25070 /* Peek at the next token. */
25071 token = cp_lexer_peek_token (parser->lexer);
25072 /* If we're looking at a `::' token then we're starting from the
25073 global namespace, not our current location. */
25074 if (token->type == CPP_SCOPE)
25076 /* Consume the `::' token. */
25077 cp_lexer_consume_token (parser->lexer);
25078 /* Set the SCOPE so that we know where to start the lookup. */
25079 parser->scope = global_namespace;
25080 parser->qualifying_scope = global_namespace;
25081 parser->object_scope = NULL_TREE;
25083 return parser->scope;
25085 else if (!current_scope_valid_p)
25087 parser->scope = NULL_TREE;
25088 parser->qualifying_scope = NULL_TREE;
25089 parser->object_scope = NULL_TREE;
25092 return NULL_TREE;
25095 /* Returns TRUE if the upcoming token sequence is the start of a
25096 constructor declarator. If FRIEND_P is true, the declarator is
25097 preceded by the `friend' specifier. */
25099 static bool
25100 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
25102 bool constructor_p;
25103 bool outside_class_specifier_p;
25104 tree nested_name_specifier;
25105 cp_token *next_token;
25107 /* The common case is that this is not a constructor declarator, so
25108 try to avoid doing lots of work if at all possible. It's not
25109 valid declare a constructor at function scope. */
25110 if (parser->in_function_body)
25111 return false;
25112 /* And only certain tokens can begin a constructor declarator. */
25113 next_token = cp_lexer_peek_token (parser->lexer);
25114 if (next_token->type != CPP_NAME
25115 && next_token->type != CPP_SCOPE
25116 && next_token->type != CPP_NESTED_NAME_SPECIFIER
25117 && next_token->type != CPP_TEMPLATE_ID)
25118 return false;
25120 /* Parse tentatively; we are going to roll back all of the tokens
25121 consumed here. */
25122 cp_parser_parse_tentatively (parser);
25123 /* Assume that we are looking at a constructor declarator. */
25124 constructor_p = true;
25126 /* Look for the optional `::' operator. */
25127 cp_parser_global_scope_opt (parser,
25128 /*current_scope_valid_p=*/false);
25129 /* Look for the nested-name-specifier. */
25130 nested_name_specifier
25131 = (cp_parser_nested_name_specifier_opt (parser,
25132 /*typename_keyword_p=*/false,
25133 /*check_dependency_p=*/false,
25134 /*type_p=*/false,
25135 /*is_declaration=*/false));
25137 outside_class_specifier_p = (!at_class_scope_p ()
25138 || !TYPE_BEING_DEFINED (current_class_type)
25139 || friend_p);
25141 /* Outside of a class-specifier, there must be a
25142 nested-name-specifier. */
25143 if (!nested_name_specifier && outside_class_specifier_p)
25144 constructor_p = false;
25145 else if (nested_name_specifier == error_mark_node)
25146 constructor_p = false;
25148 /* If we have a class scope, this is easy; DR 147 says that S::S always
25149 names the constructor, and no other qualified name could. */
25150 if (constructor_p && nested_name_specifier
25151 && CLASS_TYPE_P (nested_name_specifier))
25153 tree id = cp_parser_unqualified_id (parser,
25154 /*template_keyword_p=*/false,
25155 /*check_dependency_p=*/false,
25156 /*declarator_p=*/true,
25157 /*optional_p=*/false);
25158 if (is_overloaded_fn (id))
25159 id = DECL_NAME (get_first_fn (id));
25160 if (!constructor_name_p (id, nested_name_specifier))
25161 constructor_p = false;
25163 /* If we still think that this might be a constructor-declarator,
25164 look for a class-name. */
25165 else if (constructor_p)
25167 /* If we have:
25169 template <typename T> struct S {
25170 S();
25173 we must recognize that the nested `S' names a class. */
25174 tree type_decl;
25175 type_decl = cp_parser_class_name (parser,
25176 /*typename_keyword_p=*/false,
25177 /*template_keyword_p=*/false,
25178 none_type,
25179 /*check_dependency_p=*/false,
25180 /*class_head_p=*/false,
25181 /*is_declaration=*/false);
25182 /* If there was no class-name, then this is not a constructor.
25183 Otherwise, if we are in a class-specifier and we aren't
25184 handling a friend declaration, check that its type matches
25185 current_class_type (c++/38313). Note: error_mark_node
25186 is left alone for error recovery purposes. */
25187 constructor_p = (!cp_parser_error_occurred (parser)
25188 && (outside_class_specifier_p
25189 || type_decl == error_mark_node
25190 || same_type_p (current_class_type,
25191 TREE_TYPE (type_decl))));
25193 /* If we're still considering a constructor, we have to see a `(',
25194 to begin the parameter-declaration-clause, followed by either a
25195 `)', an `...', or a decl-specifier. We need to check for a
25196 type-specifier to avoid being fooled into thinking that:
25198 S (f) (int);
25200 is a constructor. (It is actually a function named `f' that
25201 takes one parameter (of type `int') and returns a value of type
25202 `S'. */
25203 if (constructor_p
25204 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25205 constructor_p = false;
25207 if (constructor_p
25208 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
25209 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
25210 /* A parameter declaration begins with a decl-specifier,
25211 which is either the "attribute" keyword, a storage class
25212 specifier, or (usually) a type-specifier. */
25213 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
25215 tree type;
25216 tree pushed_scope = NULL_TREE;
25217 unsigned saved_num_template_parameter_lists;
25219 /* Names appearing in the type-specifier should be looked up
25220 in the scope of the class. */
25221 if (current_class_type)
25222 type = NULL_TREE;
25223 else
25225 type = TREE_TYPE (type_decl);
25226 if (TREE_CODE (type) == TYPENAME_TYPE)
25228 type = resolve_typename_type (type,
25229 /*only_current_p=*/false);
25230 if (TREE_CODE (type) == TYPENAME_TYPE)
25232 cp_parser_abort_tentative_parse (parser);
25233 return false;
25236 pushed_scope = push_scope (type);
25239 /* Inside the constructor parameter list, surrounding
25240 template-parameter-lists do not apply. */
25241 saved_num_template_parameter_lists
25242 = parser->num_template_parameter_lists;
25243 parser->num_template_parameter_lists = 0;
25245 /* Look for the type-specifier. */
25246 cp_parser_type_specifier (parser,
25247 CP_PARSER_FLAGS_NONE,
25248 /*decl_specs=*/NULL,
25249 /*is_declarator=*/true,
25250 /*declares_class_or_enum=*/NULL,
25251 /*is_cv_qualifier=*/NULL);
25253 parser->num_template_parameter_lists
25254 = saved_num_template_parameter_lists;
25256 /* Leave the scope of the class. */
25257 if (pushed_scope)
25258 pop_scope (pushed_scope);
25260 constructor_p = !cp_parser_error_occurred (parser);
25264 /* We did not really want to consume any tokens. */
25265 cp_parser_abort_tentative_parse (parser);
25267 return constructor_p;
25270 /* Parse the definition of the function given by the DECL_SPECIFIERS,
25271 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
25272 they must be performed once we are in the scope of the function.
25274 Returns the function defined. */
25276 static tree
25277 cp_parser_function_definition_from_specifiers_and_declarator
25278 (cp_parser* parser,
25279 cp_decl_specifier_seq *decl_specifiers,
25280 tree attributes,
25281 const cp_declarator *declarator)
25283 tree fn;
25284 bool success_p;
25286 /* Begin the function-definition. */
25287 success_p = start_function (decl_specifiers, declarator, attributes);
25289 /* The things we're about to see are not directly qualified by any
25290 template headers we've seen thus far. */
25291 reset_specialization ();
25293 /* If there were names looked up in the decl-specifier-seq that we
25294 did not check, check them now. We must wait until we are in the
25295 scope of the function to perform the checks, since the function
25296 might be a friend. */
25297 perform_deferred_access_checks (tf_warning_or_error);
25299 if (success_p)
25301 cp_finalize_omp_declare_simd (parser, current_function_decl);
25302 parser->omp_declare_simd = NULL;
25303 cp_finalize_oacc_routine (parser, current_function_decl, true);
25304 parser->oacc_routine = NULL;
25307 if (!success_p)
25309 /* Skip the entire function. */
25310 cp_parser_skip_to_end_of_block_or_statement (parser);
25311 fn = error_mark_node;
25313 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
25315 /* Seen already, skip it. An error message has already been output. */
25316 cp_parser_skip_to_end_of_block_or_statement (parser);
25317 fn = current_function_decl;
25318 current_function_decl = NULL_TREE;
25319 /* If this is a function from a class, pop the nested class. */
25320 if (current_class_name)
25321 pop_nested_class ();
25323 else
25325 timevar_id_t tv;
25326 if (DECL_DECLARED_INLINE_P (current_function_decl))
25327 tv = TV_PARSE_INLINE;
25328 else
25329 tv = TV_PARSE_FUNC;
25330 timevar_push (tv);
25331 fn = cp_parser_function_definition_after_declarator (parser,
25332 /*inline_p=*/false);
25333 timevar_pop (tv);
25336 return fn;
25339 /* Parse the part of a function-definition that follows the
25340 declarator. INLINE_P is TRUE iff this function is an inline
25341 function defined within a class-specifier.
25343 Returns the function defined. */
25345 static tree
25346 cp_parser_function_definition_after_declarator (cp_parser* parser,
25347 bool inline_p)
25349 tree fn;
25350 bool ctor_initializer_p = false;
25351 bool saved_in_unbraced_linkage_specification_p;
25352 bool saved_in_function_body;
25353 unsigned saved_num_template_parameter_lists;
25354 cp_token *token;
25355 bool fully_implicit_function_template_p
25356 = parser->fully_implicit_function_template_p;
25357 parser->fully_implicit_function_template_p = false;
25358 tree implicit_template_parms
25359 = parser->implicit_template_parms;
25360 parser->implicit_template_parms = 0;
25361 cp_binding_level* implicit_template_scope
25362 = parser->implicit_template_scope;
25363 parser->implicit_template_scope = 0;
25365 saved_in_function_body = parser->in_function_body;
25366 parser->in_function_body = true;
25367 /* If the next token is `return', then the code may be trying to
25368 make use of the "named return value" extension that G++ used to
25369 support. */
25370 token = cp_lexer_peek_token (parser->lexer);
25371 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
25373 /* Consume the `return' keyword. */
25374 cp_lexer_consume_token (parser->lexer);
25375 /* Look for the identifier that indicates what value is to be
25376 returned. */
25377 cp_parser_identifier (parser);
25378 /* Issue an error message. */
25379 error_at (token->location,
25380 "named return values are no longer supported");
25381 /* Skip tokens until we reach the start of the function body. */
25382 while (true)
25384 cp_token *token = cp_lexer_peek_token (parser->lexer);
25385 if (token->type == CPP_OPEN_BRACE
25386 || token->type == CPP_EOF
25387 || token->type == CPP_PRAGMA_EOL)
25388 break;
25389 cp_lexer_consume_token (parser->lexer);
25392 /* The `extern' in `extern "C" void f () { ... }' does not apply to
25393 anything declared inside `f'. */
25394 saved_in_unbraced_linkage_specification_p
25395 = parser->in_unbraced_linkage_specification_p;
25396 parser->in_unbraced_linkage_specification_p = false;
25397 /* Inside the function, surrounding template-parameter-lists do not
25398 apply. */
25399 saved_num_template_parameter_lists
25400 = parser->num_template_parameter_lists;
25401 parser->num_template_parameter_lists = 0;
25403 start_lambda_scope (current_function_decl);
25405 /* If the next token is `try', `__transaction_atomic', or
25406 `__transaction_relaxed`, then we are looking at either function-try-block
25407 or function-transaction-block. Note that all of these include the
25408 function-body. */
25409 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
25410 ctor_initializer_p = cp_parser_function_transaction (parser,
25411 RID_TRANSACTION_ATOMIC);
25412 else if (cp_lexer_next_token_is_keyword (parser->lexer,
25413 RID_TRANSACTION_RELAXED))
25414 ctor_initializer_p = cp_parser_function_transaction (parser,
25415 RID_TRANSACTION_RELAXED);
25416 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
25417 ctor_initializer_p = cp_parser_function_try_block (parser);
25418 else
25419 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
25420 (parser, /*in_function_try_block=*/false);
25422 finish_lambda_scope ();
25424 /* Finish the function. */
25425 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
25426 (inline_p ? 2 : 0));
25427 /* Generate code for it, if necessary. */
25428 expand_or_defer_fn (fn);
25429 /* Restore the saved values. */
25430 parser->in_unbraced_linkage_specification_p
25431 = saved_in_unbraced_linkage_specification_p;
25432 parser->num_template_parameter_lists
25433 = saved_num_template_parameter_lists;
25434 parser->in_function_body = saved_in_function_body;
25436 parser->fully_implicit_function_template_p
25437 = fully_implicit_function_template_p;
25438 parser->implicit_template_parms
25439 = implicit_template_parms;
25440 parser->implicit_template_scope
25441 = implicit_template_scope;
25443 if (parser->fully_implicit_function_template_p)
25444 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
25446 return fn;
25449 /* Parse a template-declaration body (following argument list). */
25451 static void
25452 cp_parser_template_declaration_after_parameters (cp_parser* parser,
25453 tree parameter_list,
25454 bool member_p)
25456 tree decl = NULL_TREE;
25457 bool friend_p = false;
25459 /* We just processed one more parameter list. */
25460 ++parser->num_template_parameter_lists;
25462 /* Get the deferred access checks from the parameter list. These
25463 will be checked once we know what is being declared, as for a
25464 member template the checks must be performed in the scope of the
25465 class containing the member. */
25466 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
25468 /* Tentatively parse for a new template parameter list, which can either be
25469 the template keyword or a template introduction. */
25470 if (cp_parser_template_declaration_after_export (parser, member_p))
25471 /* OK */;
25472 else if (cxx_dialect >= cxx11
25473 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25474 decl = cp_parser_alias_declaration (parser);
25475 else
25477 /* There are no access checks when parsing a template, as we do not
25478 know if a specialization will be a friend. */
25479 push_deferring_access_checks (dk_no_check);
25480 cp_token *token = cp_lexer_peek_token (parser->lexer);
25481 decl = cp_parser_single_declaration (parser,
25482 checks,
25483 member_p,
25484 /*explicit_specialization_p=*/false,
25485 &friend_p);
25486 pop_deferring_access_checks ();
25488 /* If this is a member template declaration, let the front
25489 end know. */
25490 if (member_p && !friend_p && decl)
25492 if (TREE_CODE (decl) == TYPE_DECL)
25493 cp_parser_check_access_in_redeclaration (decl, token->location);
25495 decl = finish_member_template_decl (decl);
25497 else if (friend_p && decl
25498 && DECL_DECLARES_TYPE_P (decl))
25499 make_friend_class (current_class_type, TREE_TYPE (decl),
25500 /*complain=*/true);
25502 /* We are done with the current parameter list. */
25503 --parser->num_template_parameter_lists;
25505 pop_deferring_access_checks ();
25507 /* Finish up. */
25508 finish_template_decl (parameter_list);
25510 /* Check the template arguments for a literal operator template. */
25511 if (decl
25512 && DECL_DECLARES_FUNCTION_P (decl)
25513 && UDLIT_OPER_P (DECL_NAME (decl)))
25515 bool ok = true;
25516 if (parameter_list == NULL_TREE)
25517 ok = false;
25518 else
25520 int num_parms = TREE_VEC_LENGTH (parameter_list);
25521 if (num_parms == 1)
25523 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
25524 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25525 if (TREE_TYPE (parm) != char_type_node
25526 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25527 ok = false;
25529 else if (num_parms == 2 && cxx_dialect >= cxx14)
25531 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
25532 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
25533 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
25534 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25535 if (TREE_TYPE (parm) != TREE_TYPE (type)
25536 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25537 ok = false;
25539 else
25540 ok = false;
25542 if (!ok)
25544 if (cxx_dialect >= cxx14)
25545 error ("literal operator template %qD has invalid parameter list."
25546 " Expected non-type template argument pack <char...>"
25547 " or <typename CharT, CharT...>",
25548 decl);
25549 else
25550 error ("literal operator template %qD has invalid parameter list."
25551 " Expected non-type template argument pack <char...>",
25552 decl);
25556 /* Register member declarations. */
25557 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
25558 finish_member_declaration (decl);
25559 /* If DECL is a function template, we must return to parse it later.
25560 (Even though there is no definition, there might be default
25561 arguments that need handling.) */
25562 if (member_p && decl
25563 && DECL_DECLARES_FUNCTION_P (decl))
25564 vec_safe_push (unparsed_funs_with_definitions, decl);
25567 /* Parse a template introduction header for a template-declaration. Returns
25568 false if tentative parse fails. */
25570 static bool
25571 cp_parser_template_introduction (cp_parser* parser, bool member_p)
25573 cp_parser_parse_tentatively (parser);
25575 tree saved_scope = parser->scope;
25576 tree saved_object_scope = parser->object_scope;
25577 tree saved_qualifying_scope = parser->qualifying_scope;
25579 /* Look for the optional `::' operator. */
25580 cp_parser_global_scope_opt (parser,
25581 /*current_scope_valid_p=*/false);
25582 /* Look for the nested-name-specifier. */
25583 cp_parser_nested_name_specifier_opt (parser,
25584 /*typename_keyword_p=*/false,
25585 /*check_dependency_p=*/true,
25586 /*type_p=*/false,
25587 /*is_declaration=*/false);
25589 cp_token *token = cp_lexer_peek_token (parser->lexer);
25590 tree concept_name = cp_parser_identifier (parser);
25592 /* Look up the concept for which we will be matching
25593 template parameters. */
25594 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
25595 token->location);
25596 parser->scope = saved_scope;
25597 parser->object_scope = saved_object_scope;
25598 parser->qualifying_scope = saved_qualifying_scope;
25600 if (concept_name == error_mark_node)
25601 cp_parser_simulate_error (parser);
25603 /* Look for opening brace for introduction. */
25604 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
25606 if (!cp_parser_parse_definitely (parser))
25607 return false;
25609 push_deferring_access_checks (dk_deferred);
25611 /* Build vector of placeholder parameters and grab
25612 matching identifiers. */
25613 tree introduction_list = cp_parser_introduction_list (parser);
25615 /* The introduction-list shall not be empty. */
25616 int nargs = TREE_VEC_LENGTH (introduction_list);
25617 if (nargs == 0)
25619 error ("empty introduction-list");
25620 return true;
25623 /* Look for closing brace for introduction. */
25624 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25625 return true;
25627 if (tmpl_decl == error_mark_node)
25629 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
25630 token->location);
25631 return true;
25634 /* Build and associate the constraint. */
25635 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
25636 if (parms && parms != error_mark_node)
25638 cp_parser_template_declaration_after_parameters (parser, parms,
25639 member_p);
25640 return true;
25643 error_at (token->location, "no matching concept for template-introduction");
25644 return true;
25647 /* Parse a normal template-declaration following the template keyword. */
25649 static void
25650 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
25652 tree parameter_list;
25653 bool need_lang_pop;
25654 location_t location = input_location;
25656 /* Look for the `<' token. */
25657 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
25658 return;
25659 if (at_class_scope_p () && current_function_decl)
25661 /* 14.5.2.2 [temp.mem]
25663 A local class shall not have member templates. */
25664 error_at (location,
25665 "invalid declaration of member template in local class");
25666 cp_parser_skip_to_end_of_block_or_statement (parser);
25667 return;
25669 /* [temp]
25671 A template ... shall not have C linkage. */
25672 if (current_lang_name == lang_name_c)
25674 error_at (location, "template with C linkage");
25675 /* Give it C++ linkage to avoid confusing other parts of the
25676 front end. */
25677 push_lang_context (lang_name_cplusplus);
25678 need_lang_pop = true;
25680 else
25681 need_lang_pop = false;
25683 /* We cannot perform access checks on the template parameter
25684 declarations until we know what is being declared, just as we
25685 cannot check the decl-specifier list. */
25686 push_deferring_access_checks (dk_deferred);
25688 /* If the next token is `>', then we have an invalid
25689 specialization. Rather than complain about an invalid template
25690 parameter, issue an error message here. */
25691 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
25693 cp_parser_error (parser, "invalid explicit specialization");
25694 begin_specialization ();
25695 parameter_list = NULL_TREE;
25697 else
25699 /* Parse the template parameters. */
25700 parameter_list = cp_parser_template_parameter_list (parser);
25703 /* Look for the `>'. */
25704 cp_parser_skip_to_end_of_template_parameter_list (parser);
25706 /* Manage template requirements */
25707 tree reqs = get_shorthand_constraints (current_template_parms);
25708 if (tree r = cp_parser_requires_clause_opt (parser))
25709 reqs = conjoin_constraints (reqs, make_predicate_constraint (r));
25710 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
25712 cp_parser_template_declaration_after_parameters (parser, parameter_list,
25713 member_p);
25715 /* For the erroneous case of a template with C linkage, we pushed an
25716 implicit C++ linkage scope; exit that scope now. */
25717 if (need_lang_pop)
25718 pop_lang_context ();
25721 /* Parse a template-declaration, assuming that the `export' (and
25722 `extern') keywords, if present, has already been scanned. MEMBER_P
25723 is as for cp_parser_template_declaration. */
25725 static bool
25726 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
25728 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25730 cp_lexer_consume_token (parser->lexer);
25731 cp_parser_explicit_template_declaration (parser, member_p);
25732 return true;
25734 else if (flag_concepts)
25735 return cp_parser_template_introduction (parser, member_p);
25737 return false;
25740 /* Perform the deferred access checks from a template-parameter-list.
25741 CHECKS is a TREE_LIST of access checks, as returned by
25742 get_deferred_access_checks. */
25744 static void
25745 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
25747 ++processing_template_parmlist;
25748 perform_access_checks (checks, tf_warning_or_error);
25749 --processing_template_parmlist;
25752 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
25753 `function-definition' sequence that follows a template header.
25754 If MEMBER_P is true, this declaration appears in a class scope.
25756 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
25757 *FRIEND_P is set to TRUE iff the declaration is a friend. */
25759 static tree
25760 cp_parser_single_declaration (cp_parser* parser,
25761 vec<deferred_access_check, va_gc> *checks,
25762 bool member_p,
25763 bool explicit_specialization_p,
25764 bool* friend_p)
25766 int declares_class_or_enum;
25767 tree decl = NULL_TREE;
25768 cp_decl_specifier_seq decl_specifiers;
25769 bool function_definition_p = false;
25770 cp_token *decl_spec_token_start;
25772 /* This function is only used when processing a template
25773 declaration. */
25774 gcc_assert (innermost_scope_kind () == sk_template_parms
25775 || innermost_scope_kind () == sk_template_spec);
25777 /* Defer access checks until we know what is being declared. */
25778 push_deferring_access_checks (dk_deferred);
25780 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
25781 alternative. */
25782 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25783 cp_parser_decl_specifier_seq (parser,
25784 CP_PARSER_FLAGS_OPTIONAL,
25785 &decl_specifiers,
25786 &declares_class_or_enum);
25787 if (friend_p)
25788 *friend_p = cp_parser_friend_p (&decl_specifiers);
25790 /* There are no template typedefs. */
25791 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
25793 error_at (decl_spec_token_start->location,
25794 "template declaration of %<typedef%>");
25795 decl = error_mark_node;
25798 /* Gather up the access checks that occurred the
25799 decl-specifier-seq. */
25800 stop_deferring_access_checks ();
25802 /* Check for the declaration of a template class. */
25803 if (declares_class_or_enum)
25805 if (cp_parser_declares_only_class_p (parser)
25806 || (declares_class_or_enum & 2))
25808 // If this is a declaration, but not a definition, associate
25809 // any constraints with the type declaration. Constraints
25810 // are associated with definitions in cp_parser_class_specifier.
25811 if (declares_class_or_enum == 1)
25812 associate_classtype_constraints (decl_specifiers.type);
25814 decl = shadow_tag (&decl_specifiers);
25816 /* In this case:
25818 struct C {
25819 friend template <typename T> struct A<T>::B;
25822 A<T>::B will be represented by a TYPENAME_TYPE, and
25823 therefore not recognized by shadow_tag. */
25824 if (friend_p && *friend_p
25825 && !decl
25826 && decl_specifiers.type
25827 && TYPE_P (decl_specifiers.type))
25828 decl = decl_specifiers.type;
25830 if (decl && decl != error_mark_node)
25831 decl = TYPE_NAME (decl);
25832 else
25833 decl = error_mark_node;
25835 /* Perform access checks for template parameters. */
25836 cp_parser_perform_template_parameter_access_checks (checks);
25838 /* Give a helpful diagnostic for
25839 template <class T> struct A { } a;
25840 if we aren't already recovering from an error. */
25841 if (!cp_parser_declares_only_class_p (parser)
25842 && !seen_error ())
25844 error_at (cp_lexer_peek_token (parser->lexer)->location,
25845 "a class template declaration must not declare "
25846 "anything else");
25847 cp_parser_skip_to_end_of_block_or_statement (parser);
25848 goto out;
25853 /* Complain about missing 'typename' or other invalid type names. */
25854 if (!decl_specifiers.any_type_specifiers_p
25855 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25857 /* cp_parser_parse_and_diagnose_invalid_type_name calls
25858 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
25859 the rest of this declaration. */
25860 decl = error_mark_node;
25861 goto out;
25864 /* If it's not a template class, try for a template function. If
25865 the next token is a `;', then this declaration does not declare
25866 anything. But, if there were errors in the decl-specifiers, then
25867 the error might well have come from an attempted class-specifier.
25868 In that case, there's no need to warn about a missing declarator. */
25869 if (!decl
25870 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
25871 || decl_specifiers.type != error_mark_node))
25873 decl = cp_parser_init_declarator (parser,
25874 &decl_specifiers,
25875 checks,
25876 /*function_definition_allowed_p=*/true,
25877 member_p,
25878 declares_class_or_enum,
25879 &function_definition_p,
25880 NULL, NULL, NULL);
25882 /* 7.1.1-1 [dcl.stc]
25884 A storage-class-specifier shall not be specified in an explicit
25885 specialization... */
25886 if (decl
25887 && explicit_specialization_p
25888 && decl_specifiers.storage_class != sc_none)
25890 error_at (decl_spec_token_start->location,
25891 "explicit template specialization cannot have a storage class");
25892 decl = error_mark_node;
25895 if (decl && VAR_P (decl))
25896 check_template_variable (decl);
25899 /* Look for a trailing `;' after the declaration. */
25900 if (!function_definition_p
25901 && (decl == error_mark_node
25902 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
25903 cp_parser_skip_to_end_of_block_or_statement (parser);
25905 out:
25906 pop_deferring_access_checks ();
25908 /* Clear any current qualification; whatever comes next is the start
25909 of something new. */
25910 parser->scope = NULL_TREE;
25911 parser->qualifying_scope = NULL_TREE;
25912 parser->object_scope = NULL_TREE;
25914 return decl;
25917 /* Parse a cast-expression that is not the operand of a unary "&". */
25919 static cp_expr
25920 cp_parser_simple_cast_expression (cp_parser *parser)
25922 return cp_parser_cast_expression (parser, /*address_p=*/false,
25923 /*cast_p=*/false, /*decltype*/false, NULL);
25926 /* Parse a functional cast to TYPE. Returns an expression
25927 representing the cast. */
25929 static cp_expr
25930 cp_parser_functional_cast (cp_parser* parser, tree type)
25932 vec<tree, va_gc> *vec;
25933 tree expression_list;
25934 cp_expr cast;
25935 bool nonconst_p;
25937 location_t start_loc = input_location;
25939 if (!type)
25940 type = error_mark_node;
25942 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25944 cp_lexer_set_source_position (parser->lexer);
25945 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25946 expression_list = cp_parser_braced_list (parser, &nonconst_p);
25947 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
25948 if (TREE_CODE (type) == TYPE_DECL)
25949 type = TREE_TYPE (type);
25951 cast = finish_compound_literal (type, expression_list,
25952 tf_warning_or_error);
25953 /* Create a location of the form:
25954 type_name{i, f}
25955 ^~~~~~~~~~~~~~~
25956 with caret == start at the start of the type name,
25957 finishing at the closing brace. */
25958 location_t finish_loc
25959 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
25960 location_t combined_loc = make_location (start_loc, start_loc,
25961 finish_loc);
25962 cast.set_location (combined_loc);
25963 return cast;
25967 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
25968 /*cast_p=*/true,
25969 /*allow_expansion_p=*/true,
25970 /*non_constant_p=*/NULL);
25971 if (vec == NULL)
25972 expression_list = error_mark_node;
25973 else
25975 expression_list = build_tree_list_vec (vec);
25976 release_tree_vector (vec);
25979 cast = build_functional_cast (type, expression_list,
25980 tf_warning_or_error);
25981 /* [expr.const]/1: In an integral constant expression "only type
25982 conversions to integral or enumeration type can be used". */
25983 if (TREE_CODE (type) == TYPE_DECL)
25984 type = TREE_TYPE (type);
25985 if (cast != error_mark_node
25986 && !cast_valid_in_integral_constant_expression_p (type)
25987 && cp_parser_non_integral_constant_expression (parser,
25988 NIC_CONSTRUCTOR))
25989 return error_mark_node;
25991 /* Create a location of the form:
25992 float(i)
25993 ^~~~~~~~
25994 with caret == start at the start of the type name,
25995 finishing at the closing paren. */
25996 location_t finish_loc
25997 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
25998 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
25999 cast.set_location (combined_loc);
26000 return cast;
26003 /* Save the tokens that make up the body of a member function defined
26004 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
26005 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
26006 specifiers applied to the declaration. Returns the FUNCTION_DECL
26007 for the member function. */
26009 static tree
26010 cp_parser_save_member_function_body (cp_parser* parser,
26011 cp_decl_specifier_seq *decl_specifiers,
26012 cp_declarator *declarator,
26013 tree attributes)
26015 cp_token *first;
26016 cp_token *last;
26017 tree fn;
26019 /* Create the FUNCTION_DECL. */
26020 fn = grokmethod (decl_specifiers, declarator, attributes);
26021 cp_finalize_omp_declare_simd (parser, fn);
26022 cp_finalize_oacc_routine (parser, fn, true);
26023 /* If something went badly wrong, bail out now. */
26024 if (fn == error_mark_node)
26026 /* If there's a function-body, skip it. */
26027 if (cp_parser_token_starts_function_definition_p
26028 (cp_lexer_peek_token (parser->lexer)))
26029 cp_parser_skip_to_end_of_block_or_statement (parser);
26030 return error_mark_node;
26033 /* Remember it, if there default args to post process. */
26034 cp_parser_save_default_args (parser, fn);
26036 /* Save away the tokens that make up the body of the
26037 function. */
26038 first = parser->lexer->next_token;
26039 /* Handle function try blocks. */
26040 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26041 cp_lexer_consume_token (parser->lexer);
26042 /* We can have braced-init-list mem-initializers before the fn body. */
26043 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26045 cp_lexer_consume_token (parser->lexer);
26046 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
26048 /* cache_group will stop after an un-nested { } pair, too. */
26049 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26050 break;
26052 /* variadic mem-inits have ... after the ')'. */
26053 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26054 cp_lexer_consume_token (parser->lexer);
26057 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26058 /* Handle function try blocks. */
26059 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
26060 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26061 last = parser->lexer->next_token;
26063 /* Save away the inline definition; we will process it when the
26064 class is complete. */
26065 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
26066 DECL_PENDING_INLINE_P (fn) = 1;
26068 /* We need to know that this was defined in the class, so that
26069 friend templates are handled correctly. */
26070 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
26072 /* Add FN to the queue of functions to be parsed later. */
26073 vec_safe_push (unparsed_funs_with_definitions, fn);
26075 return fn;
26078 /* Save the tokens that make up the in-class initializer for a non-static
26079 data member. Returns a DEFAULT_ARG. */
26081 static tree
26082 cp_parser_save_nsdmi (cp_parser* parser)
26084 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
26087 /* Parse a template-argument-list, as well as the trailing ">" (but
26088 not the opening "<"). See cp_parser_template_argument_list for the
26089 return value. */
26091 static tree
26092 cp_parser_enclosed_template_argument_list (cp_parser* parser)
26094 tree arguments;
26095 tree saved_scope;
26096 tree saved_qualifying_scope;
26097 tree saved_object_scope;
26098 bool saved_greater_than_is_operator_p;
26099 int saved_unevaluated_operand;
26100 int saved_inhibit_evaluation_warnings;
26102 /* [temp.names]
26104 When parsing a template-id, the first non-nested `>' is taken as
26105 the end of the template-argument-list rather than a greater-than
26106 operator. */
26107 saved_greater_than_is_operator_p
26108 = parser->greater_than_is_operator_p;
26109 parser->greater_than_is_operator_p = false;
26110 /* Parsing the argument list may modify SCOPE, so we save it
26111 here. */
26112 saved_scope = parser->scope;
26113 saved_qualifying_scope = parser->qualifying_scope;
26114 saved_object_scope = parser->object_scope;
26115 /* We need to evaluate the template arguments, even though this
26116 template-id may be nested within a "sizeof". */
26117 saved_unevaluated_operand = cp_unevaluated_operand;
26118 cp_unevaluated_operand = 0;
26119 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
26120 c_inhibit_evaluation_warnings = 0;
26121 /* Parse the template-argument-list itself. */
26122 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
26123 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26124 arguments = NULL_TREE;
26125 else
26126 arguments = cp_parser_template_argument_list (parser);
26127 /* Look for the `>' that ends the template-argument-list. If we find
26128 a '>>' instead, it's probably just a typo. */
26129 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26131 if (cxx_dialect != cxx98)
26133 /* In C++0x, a `>>' in a template argument list or cast
26134 expression is considered to be two separate `>'
26135 tokens. So, change the current token to a `>', but don't
26136 consume it: it will be consumed later when the outer
26137 template argument list (or cast expression) is parsed.
26138 Note that this replacement of `>' for `>>' is necessary
26139 even if we are parsing tentatively: in the tentative
26140 case, after calling
26141 cp_parser_enclosed_template_argument_list we will always
26142 throw away all of the template arguments and the first
26143 closing `>', either because the template argument list
26144 was erroneous or because we are replacing those tokens
26145 with a CPP_TEMPLATE_ID token. The second `>' (which will
26146 not have been thrown away) is needed either to close an
26147 outer template argument list or to complete a new-style
26148 cast. */
26149 cp_token *token = cp_lexer_peek_token (parser->lexer);
26150 token->type = CPP_GREATER;
26152 else if (!saved_greater_than_is_operator_p)
26154 /* If we're in a nested template argument list, the '>>' has
26155 to be a typo for '> >'. We emit the error message, but we
26156 continue parsing and we push a '>' as next token, so that
26157 the argument list will be parsed correctly. Note that the
26158 global source location is still on the token before the
26159 '>>', so we need to say explicitly where we want it. */
26160 cp_token *token = cp_lexer_peek_token (parser->lexer);
26161 error_at (token->location, "%<>>%> should be %<> >%> "
26162 "within a nested template argument list");
26164 token->type = CPP_GREATER;
26166 else
26168 /* If this is not a nested template argument list, the '>>'
26169 is a typo for '>'. Emit an error message and continue.
26170 Same deal about the token location, but here we can get it
26171 right by consuming the '>>' before issuing the diagnostic. */
26172 cp_token *token = cp_lexer_consume_token (parser->lexer);
26173 error_at (token->location,
26174 "spurious %<>>%>, use %<>%> to terminate "
26175 "a template argument list");
26178 else
26179 cp_parser_skip_to_end_of_template_parameter_list (parser);
26180 /* The `>' token might be a greater-than operator again now. */
26181 parser->greater_than_is_operator_p
26182 = saved_greater_than_is_operator_p;
26183 /* Restore the SAVED_SCOPE. */
26184 parser->scope = saved_scope;
26185 parser->qualifying_scope = saved_qualifying_scope;
26186 parser->object_scope = saved_object_scope;
26187 cp_unevaluated_operand = saved_unevaluated_operand;
26188 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
26190 return arguments;
26193 /* MEMBER_FUNCTION is a member function, or a friend. If default
26194 arguments, or the body of the function have not yet been parsed,
26195 parse them now. */
26197 static void
26198 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
26200 timevar_push (TV_PARSE_INMETH);
26201 /* If this member is a template, get the underlying
26202 FUNCTION_DECL. */
26203 if (DECL_FUNCTION_TEMPLATE_P (member_function))
26204 member_function = DECL_TEMPLATE_RESULT (member_function);
26206 /* There should not be any class definitions in progress at this
26207 point; the bodies of members are only parsed outside of all class
26208 definitions. */
26209 gcc_assert (parser->num_classes_being_defined == 0);
26210 /* While we're parsing the member functions we might encounter more
26211 classes. We want to handle them right away, but we don't want
26212 them getting mixed up with functions that are currently in the
26213 queue. */
26214 push_unparsed_function_queues (parser);
26216 /* Make sure that any template parameters are in scope. */
26217 maybe_begin_member_template_processing (member_function);
26219 /* If the body of the function has not yet been parsed, parse it
26220 now. */
26221 if (DECL_PENDING_INLINE_P (member_function))
26223 tree function_scope;
26224 cp_token_cache *tokens;
26226 /* The function is no longer pending; we are processing it. */
26227 tokens = DECL_PENDING_INLINE_INFO (member_function);
26228 DECL_PENDING_INLINE_INFO (member_function) = NULL;
26229 DECL_PENDING_INLINE_P (member_function) = 0;
26231 /* If this is a local class, enter the scope of the containing
26232 function. */
26233 function_scope = current_function_decl;
26234 if (function_scope)
26235 push_function_context ();
26237 /* Push the body of the function onto the lexer stack. */
26238 cp_parser_push_lexer_for_tokens (parser, tokens);
26240 /* Let the front end know that we going to be defining this
26241 function. */
26242 start_preparsed_function (member_function, NULL_TREE,
26243 SF_PRE_PARSED | SF_INCLASS_INLINE);
26245 /* Don't do access checking if it is a templated function. */
26246 if (processing_template_decl)
26247 push_deferring_access_checks (dk_no_check);
26249 /* #pragma omp declare reduction needs special parsing. */
26250 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
26252 parser->lexer->in_pragma = true;
26253 cp_parser_omp_declare_reduction_exprs (member_function, parser);
26254 finish_function (/*inline*/2);
26255 cp_check_omp_declare_reduction (member_function);
26257 else
26258 /* Now, parse the body of the function. */
26259 cp_parser_function_definition_after_declarator (parser,
26260 /*inline_p=*/true);
26262 if (processing_template_decl)
26263 pop_deferring_access_checks ();
26265 /* Leave the scope of the containing function. */
26266 if (function_scope)
26267 pop_function_context ();
26268 cp_parser_pop_lexer (parser);
26271 /* Remove any template parameters from the symbol table. */
26272 maybe_end_member_template_processing ();
26274 /* Restore the queue. */
26275 pop_unparsed_function_queues (parser);
26276 timevar_pop (TV_PARSE_INMETH);
26279 /* If DECL contains any default args, remember it on the unparsed
26280 functions queue. */
26282 static void
26283 cp_parser_save_default_args (cp_parser* parser, tree decl)
26285 tree probe;
26287 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
26288 probe;
26289 probe = TREE_CHAIN (probe))
26290 if (TREE_PURPOSE (probe))
26292 cp_default_arg_entry entry = {current_class_type, decl};
26293 vec_safe_push (unparsed_funs_with_default_args, entry);
26294 break;
26298 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
26299 which is either a FIELD_DECL or PARM_DECL. Parse it and return
26300 the result. For a PARM_DECL, PARMTYPE is the corresponding type
26301 from the parameter-type-list. */
26303 static tree
26304 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
26305 tree default_arg, tree parmtype)
26307 cp_token_cache *tokens;
26308 tree parsed_arg;
26309 bool dummy;
26311 if (default_arg == error_mark_node)
26312 return error_mark_node;
26314 /* Push the saved tokens for the default argument onto the parser's
26315 lexer stack. */
26316 tokens = DEFARG_TOKENS (default_arg);
26317 cp_parser_push_lexer_for_tokens (parser, tokens);
26319 start_lambda_scope (decl);
26321 /* Parse the default argument. */
26322 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
26323 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
26324 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26326 finish_lambda_scope ();
26328 if (parsed_arg == error_mark_node)
26329 cp_parser_skip_to_end_of_statement (parser);
26331 if (!processing_template_decl)
26333 /* In a non-template class, check conversions now. In a template,
26334 we'll wait and instantiate these as needed. */
26335 if (TREE_CODE (decl) == PARM_DECL)
26336 parsed_arg = check_default_argument (parmtype, parsed_arg,
26337 tf_warning_or_error);
26338 else
26339 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
26342 /* If the token stream has not been completely used up, then
26343 there was extra junk after the end of the default
26344 argument. */
26345 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26347 if (TREE_CODE (decl) == PARM_DECL)
26348 cp_parser_error (parser, "expected %<,%>");
26349 else
26350 cp_parser_error (parser, "expected %<;%>");
26353 /* Revert to the main lexer. */
26354 cp_parser_pop_lexer (parser);
26356 return parsed_arg;
26359 /* FIELD is a non-static data member with an initializer which we saved for
26360 later; parse it now. */
26362 static void
26363 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
26365 tree def;
26367 maybe_begin_member_template_processing (field);
26369 push_unparsed_function_queues (parser);
26370 def = cp_parser_late_parse_one_default_arg (parser, field,
26371 DECL_INITIAL (field),
26372 NULL_TREE);
26373 pop_unparsed_function_queues (parser);
26375 maybe_end_member_template_processing ();
26377 DECL_INITIAL (field) = def;
26380 /* FN is a FUNCTION_DECL which may contains a parameter with an
26381 unparsed DEFAULT_ARG. Parse the default args now. This function
26382 assumes that the current scope is the scope in which the default
26383 argument should be processed. */
26385 static void
26386 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
26388 bool saved_local_variables_forbidden_p;
26389 tree parm, parmdecl;
26391 /* While we're parsing the default args, we might (due to the
26392 statement expression extension) encounter more classes. We want
26393 to handle them right away, but we don't want them getting mixed
26394 up with default args that are currently in the queue. */
26395 push_unparsed_function_queues (parser);
26397 /* Local variable names (and the `this' keyword) may not appear
26398 in a default argument. */
26399 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
26400 parser->local_variables_forbidden_p = true;
26402 push_defarg_context (fn);
26404 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
26405 parmdecl = DECL_ARGUMENTS (fn);
26406 parm && parm != void_list_node;
26407 parm = TREE_CHAIN (parm),
26408 parmdecl = DECL_CHAIN (parmdecl))
26410 tree default_arg = TREE_PURPOSE (parm);
26411 tree parsed_arg;
26412 vec<tree, va_gc> *insts;
26413 tree copy;
26414 unsigned ix;
26416 if (!default_arg)
26417 continue;
26419 if (TREE_CODE (default_arg) != DEFAULT_ARG)
26420 /* This can happen for a friend declaration for a function
26421 already declared with default arguments. */
26422 continue;
26424 parsed_arg
26425 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
26426 default_arg,
26427 TREE_VALUE (parm));
26428 if (parsed_arg == error_mark_node)
26430 continue;
26433 TREE_PURPOSE (parm) = parsed_arg;
26435 /* Update any instantiations we've already created. */
26436 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
26437 vec_safe_iterate (insts, ix, &copy); ix++)
26438 TREE_PURPOSE (copy) = parsed_arg;
26441 pop_defarg_context ();
26443 /* Make sure no default arg is missing. */
26444 check_default_args (fn);
26446 /* Restore the state of local_variables_forbidden_p. */
26447 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
26449 /* Restore the queue. */
26450 pop_unparsed_function_queues (parser);
26453 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
26455 sizeof ... ( identifier )
26457 where the 'sizeof' token has already been consumed. */
26459 static tree
26460 cp_parser_sizeof_pack (cp_parser *parser)
26462 /* Consume the `...'. */
26463 cp_lexer_consume_token (parser->lexer);
26464 maybe_warn_variadic_templates ();
26466 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
26467 if (paren)
26468 cp_lexer_consume_token (parser->lexer);
26469 else
26470 permerror (cp_lexer_peek_token (parser->lexer)->location,
26471 "%<sizeof...%> argument must be surrounded by parentheses");
26473 cp_token *token = cp_lexer_peek_token (parser->lexer);
26474 tree name = cp_parser_identifier (parser);
26475 if (name == error_mark_node)
26476 return error_mark_node;
26477 /* The name is not qualified. */
26478 parser->scope = NULL_TREE;
26479 parser->qualifying_scope = NULL_TREE;
26480 parser->object_scope = NULL_TREE;
26481 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
26482 if (expr == error_mark_node)
26483 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
26484 token->location);
26485 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
26486 expr = TREE_TYPE (expr);
26487 else if (TREE_CODE (expr) == CONST_DECL)
26488 expr = DECL_INITIAL (expr);
26489 expr = make_pack_expansion (expr);
26490 PACK_EXPANSION_SIZEOF_P (expr) = true;
26492 if (paren)
26493 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26495 return expr;
26498 /* Parse the operand of `sizeof' (or a similar operator). Returns
26499 either a TYPE or an expression, depending on the form of the
26500 input. The KEYWORD indicates which kind of expression we have
26501 encountered. */
26503 static tree
26504 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
26506 tree expr = NULL_TREE;
26507 const char *saved_message;
26508 char *tmp;
26509 bool saved_integral_constant_expression_p;
26510 bool saved_non_integral_constant_expression_p;
26512 /* If it's a `...', then we are computing the length of a parameter
26513 pack. */
26514 if (keyword == RID_SIZEOF
26515 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26516 return cp_parser_sizeof_pack (parser);
26518 /* Types cannot be defined in a `sizeof' expression. Save away the
26519 old message. */
26520 saved_message = parser->type_definition_forbidden_message;
26521 /* And create the new one. */
26522 tmp = concat ("types may not be defined in %<",
26523 IDENTIFIER_POINTER (ridpointers[keyword]),
26524 "%> expressions", NULL);
26525 parser->type_definition_forbidden_message = tmp;
26527 /* The restrictions on constant-expressions do not apply inside
26528 sizeof expressions. */
26529 saved_integral_constant_expression_p
26530 = parser->integral_constant_expression_p;
26531 saved_non_integral_constant_expression_p
26532 = parser->non_integral_constant_expression_p;
26533 parser->integral_constant_expression_p = false;
26535 /* Do not actually evaluate the expression. */
26536 ++cp_unevaluated_operand;
26537 ++c_inhibit_evaluation_warnings;
26538 /* If it's a `(', then we might be looking at the type-id
26539 construction. */
26540 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26542 tree type = NULL_TREE;
26544 /* We can't be sure yet whether we're looking at a type-id or an
26545 expression. */
26546 cp_parser_parse_tentatively (parser);
26547 /* Note: as a GNU Extension, compound literals are considered
26548 postfix-expressions as they are in C99, so they are valid
26549 arguments to sizeof. See comment in cp_parser_cast_expression
26550 for details. */
26551 if (cp_parser_compound_literal_p (parser))
26552 cp_parser_simulate_error (parser);
26553 else
26555 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
26556 parser->in_type_id_in_expr_p = true;
26557 /* Look for the type-id. */
26558 type = cp_parser_type_id (parser);
26559 /* Look for the closing `)'. */
26560 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26561 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
26564 /* If all went well, then we're done. */
26565 if (cp_parser_parse_definitely (parser))
26567 cp_decl_specifier_seq decl_specs;
26569 /* Build a trivial decl-specifier-seq. */
26570 clear_decl_specs (&decl_specs);
26571 decl_specs.type = type;
26573 /* Call grokdeclarator to figure out what type this is. */
26574 expr = grokdeclarator (NULL,
26575 &decl_specs,
26576 TYPENAME,
26577 /*initialized=*/0,
26578 /*attrlist=*/NULL);
26582 /* If the type-id production did not work out, then we must be
26583 looking at the unary-expression production. */
26584 if (!expr)
26585 expr = cp_parser_unary_expression (parser);
26587 /* Go back to evaluating expressions. */
26588 --cp_unevaluated_operand;
26589 --c_inhibit_evaluation_warnings;
26591 /* Free the message we created. */
26592 free (tmp);
26593 /* And restore the old one. */
26594 parser->type_definition_forbidden_message = saved_message;
26595 parser->integral_constant_expression_p
26596 = saved_integral_constant_expression_p;
26597 parser->non_integral_constant_expression_p
26598 = saved_non_integral_constant_expression_p;
26600 return expr;
26603 /* If the current declaration has no declarator, return true. */
26605 static bool
26606 cp_parser_declares_only_class_p (cp_parser *parser)
26608 /* If the next token is a `;' or a `,' then there is no
26609 declarator. */
26610 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26611 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
26614 /* Update the DECL_SPECS to reflect the storage class indicated by
26615 KEYWORD. */
26617 static void
26618 cp_parser_set_storage_class (cp_parser *parser,
26619 cp_decl_specifier_seq *decl_specs,
26620 enum rid keyword,
26621 cp_token *token)
26623 cp_storage_class storage_class;
26625 if (parser->in_unbraced_linkage_specification_p)
26627 error_at (token->location, "invalid use of %qD in linkage specification",
26628 ridpointers[keyword]);
26629 return;
26631 else if (decl_specs->storage_class != sc_none)
26633 decl_specs->conflicting_specifiers_p = true;
26634 return;
26637 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
26638 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
26639 && decl_specs->gnu_thread_keyword_p)
26641 pedwarn (decl_specs->locations[ds_thread], 0,
26642 "%<__thread%> before %qD", ridpointers[keyword]);
26645 switch (keyword)
26647 case RID_AUTO:
26648 storage_class = sc_auto;
26649 break;
26650 case RID_REGISTER:
26651 storage_class = sc_register;
26652 break;
26653 case RID_STATIC:
26654 storage_class = sc_static;
26655 break;
26656 case RID_EXTERN:
26657 storage_class = sc_extern;
26658 break;
26659 case RID_MUTABLE:
26660 storage_class = sc_mutable;
26661 break;
26662 default:
26663 gcc_unreachable ();
26665 decl_specs->storage_class = storage_class;
26666 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
26668 /* A storage class specifier cannot be applied alongside a typedef
26669 specifier. If there is a typedef specifier present then set
26670 conflicting_specifiers_p which will trigger an error later
26671 on in grokdeclarator. */
26672 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
26673 decl_specs->conflicting_specifiers_p = true;
26676 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
26677 is true, the type is a class or enum definition. */
26679 static void
26680 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
26681 tree type_spec,
26682 cp_token *token,
26683 bool type_definition_p)
26685 decl_specs->any_specifiers_p = true;
26687 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
26688 (with, for example, in "typedef int wchar_t;") we remember that
26689 this is what happened. In system headers, we ignore these
26690 declarations so that G++ can work with system headers that are not
26691 C++-safe. */
26692 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
26693 && !type_definition_p
26694 && (type_spec == boolean_type_node
26695 || type_spec == char16_type_node
26696 || type_spec == char32_type_node
26697 || type_spec == wchar_type_node)
26698 && (decl_specs->type
26699 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
26700 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
26701 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
26702 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
26704 decl_specs->redefined_builtin_type = type_spec;
26705 set_and_check_decl_spec_loc (decl_specs,
26706 ds_redefined_builtin_type_spec,
26707 token);
26708 if (!decl_specs->type)
26710 decl_specs->type = type_spec;
26711 decl_specs->type_definition_p = false;
26712 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
26715 else if (decl_specs->type)
26716 decl_specs->multiple_types_p = true;
26717 else
26719 decl_specs->type = type_spec;
26720 decl_specs->type_definition_p = type_definition_p;
26721 decl_specs->redefined_builtin_type = NULL_TREE;
26722 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
26726 /* True iff TOKEN is the GNU keyword __thread. */
26728 static bool
26729 token_is__thread (cp_token *token)
26731 gcc_assert (token->keyword == RID_THREAD);
26732 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
26735 /* Set the location for a declarator specifier and check if it is
26736 duplicated.
26738 DECL_SPECS is the sequence of declarator specifiers onto which to
26739 set the location.
26741 DS is the single declarator specifier to set which location is to
26742 be set onto the existing sequence of declarators.
26744 LOCATION is the location for the declarator specifier to
26745 consider. */
26747 static void
26748 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
26749 cp_decl_spec ds, cp_token *token)
26751 gcc_assert (ds < ds_last);
26753 if (decl_specs == NULL)
26754 return;
26756 source_location location = token->location;
26758 if (decl_specs->locations[ds] == 0)
26760 decl_specs->locations[ds] = location;
26761 if (ds == ds_thread)
26762 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
26764 else
26766 if (ds == ds_long)
26768 if (decl_specs->locations[ds_long_long] != 0)
26769 error_at (location,
26770 "%<long long long%> is too long for GCC");
26771 else
26773 decl_specs->locations[ds_long_long] = location;
26774 pedwarn_cxx98 (location,
26775 OPT_Wlong_long,
26776 "ISO C++ 1998 does not support %<long long%>");
26779 else if (ds == ds_thread)
26781 bool gnu = token_is__thread (token);
26782 if (gnu != decl_specs->gnu_thread_keyword_p)
26783 error_at (location,
26784 "both %<__thread%> and %<thread_local%> specified");
26785 else
26786 error_at (location, "duplicate %qD", token->u.value);
26788 else
26790 static const char *const decl_spec_names[] = {
26791 "signed",
26792 "unsigned",
26793 "short",
26794 "long",
26795 "const",
26796 "volatile",
26797 "restrict",
26798 "inline",
26799 "virtual",
26800 "explicit",
26801 "friend",
26802 "typedef",
26803 "using",
26804 "constexpr",
26805 "__complex"
26807 error_at (location,
26808 "duplicate %qs", decl_spec_names[ds]);
26813 /* Return true iff the declarator specifier DS is present in the
26814 sequence of declarator specifiers DECL_SPECS. */
26816 bool
26817 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
26818 cp_decl_spec ds)
26820 gcc_assert (ds < ds_last);
26822 if (decl_specs == NULL)
26823 return false;
26825 return decl_specs->locations[ds] != 0;
26828 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
26829 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
26831 static bool
26832 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
26834 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
26837 /* Issue an error message indicating that TOKEN_DESC was expected.
26838 If KEYWORD is true, it indicated this function is called by
26839 cp_parser_require_keword and the required token can only be
26840 a indicated keyword. */
26842 static void
26843 cp_parser_required_error (cp_parser *parser,
26844 required_token token_desc,
26845 bool keyword)
26847 switch (token_desc)
26849 case RT_NEW:
26850 cp_parser_error (parser, "expected %<new%>");
26851 return;
26852 case RT_DELETE:
26853 cp_parser_error (parser, "expected %<delete%>");
26854 return;
26855 case RT_RETURN:
26856 cp_parser_error (parser, "expected %<return%>");
26857 return;
26858 case RT_WHILE:
26859 cp_parser_error (parser, "expected %<while%>");
26860 return;
26861 case RT_EXTERN:
26862 cp_parser_error (parser, "expected %<extern%>");
26863 return;
26864 case RT_STATIC_ASSERT:
26865 cp_parser_error (parser, "expected %<static_assert%>");
26866 return;
26867 case RT_DECLTYPE:
26868 cp_parser_error (parser, "expected %<decltype%>");
26869 return;
26870 case RT_OPERATOR:
26871 cp_parser_error (parser, "expected %<operator%>");
26872 return;
26873 case RT_CLASS:
26874 cp_parser_error (parser, "expected %<class%>");
26875 return;
26876 case RT_TEMPLATE:
26877 cp_parser_error (parser, "expected %<template%>");
26878 return;
26879 case RT_NAMESPACE:
26880 cp_parser_error (parser, "expected %<namespace%>");
26881 return;
26882 case RT_USING:
26883 cp_parser_error (parser, "expected %<using%>");
26884 return;
26885 case RT_ASM:
26886 cp_parser_error (parser, "expected %<asm%>");
26887 return;
26888 case RT_TRY:
26889 cp_parser_error (parser, "expected %<try%>");
26890 return;
26891 case RT_CATCH:
26892 cp_parser_error (parser, "expected %<catch%>");
26893 return;
26894 case RT_THROW:
26895 cp_parser_error (parser, "expected %<throw%>");
26896 return;
26897 case RT_LABEL:
26898 cp_parser_error (parser, "expected %<__label__%>");
26899 return;
26900 case RT_AT_TRY:
26901 cp_parser_error (parser, "expected %<@try%>");
26902 return;
26903 case RT_AT_SYNCHRONIZED:
26904 cp_parser_error (parser, "expected %<@synchronized%>");
26905 return;
26906 case RT_AT_THROW:
26907 cp_parser_error (parser, "expected %<@throw%>");
26908 return;
26909 case RT_TRANSACTION_ATOMIC:
26910 cp_parser_error (parser, "expected %<__transaction_atomic%>");
26911 return;
26912 case RT_TRANSACTION_RELAXED:
26913 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
26914 return;
26915 default:
26916 break;
26918 if (!keyword)
26920 switch (token_desc)
26922 case RT_SEMICOLON:
26923 cp_parser_error (parser, "expected %<;%>");
26924 return;
26925 case RT_OPEN_PAREN:
26926 cp_parser_error (parser, "expected %<(%>");
26927 return;
26928 case RT_CLOSE_BRACE:
26929 cp_parser_error (parser, "expected %<}%>");
26930 return;
26931 case RT_OPEN_BRACE:
26932 cp_parser_error (parser, "expected %<{%>");
26933 return;
26934 case RT_CLOSE_SQUARE:
26935 cp_parser_error (parser, "expected %<]%>");
26936 return;
26937 case RT_OPEN_SQUARE:
26938 cp_parser_error (parser, "expected %<[%>");
26939 return;
26940 case RT_COMMA:
26941 cp_parser_error (parser, "expected %<,%>");
26942 return;
26943 case RT_SCOPE:
26944 cp_parser_error (parser, "expected %<::%>");
26945 return;
26946 case RT_LESS:
26947 cp_parser_error (parser, "expected %<<%>");
26948 return;
26949 case RT_GREATER:
26950 cp_parser_error (parser, "expected %<>%>");
26951 return;
26952 case RT_EQ:
26953 cp_parser_error (parser, "expected %<=%>");
26954 return;
26955 case RT_ELLIPSIS:
26956 cp_parser_error (parser, "expected %<...%>");
26957 return;
26958 case RT_MULT:
26959 cp_parser_error (parser, "expected %<*%>");
26960 return;
26961 case RT_COMPL:
26962 cp_parser_error (parser, "expected %<~%>");
26963 return;
26964 case RT_COLON:
26965 cp_parser_error (parser, "expected %<:%>");
26966 return;
26967 case RT_COLON_SCOPE:
26968 cp_parser_error (parser, "expected %<:%> or %<::%>");
26969 return;
26970 case RT_CLOSE_PAREN:
26971 cp_parser_error (parser, "expected %<)%>");
26972 return;
26973 case RT_COMMA_CLOSE_PAREN:
26974 cp_parser_error (parser, "expected %<,%> or %<)%>");
26975 return;
26976 case RT_PRAGMA_EOL:
26977 cp_parser_error (parser, "expected end of line");
26978 return;
26979 case RT_NAME:
26980 cp_parser_error (parser, "expected identifier");
26981 return;
26982 case RT_SELECT:
26983 cp_parser_error (parser, "expected selection-statement");
26984 return;
26985 case RT_INTERATION:
26986 cp_parser_error (parser, "expected iteration-statement");
26987 return;
26988 case RT_JUMP:
26989 cp_parser_error (parser, "expected jump-statement");
26990 return;
26991 case RT_CLASS_KEY:
26992 cp_parser_error (parser, "expected class-key");
26993 return;
26994 case RT_CLASS_TYPENAME_TEMPLATE:
26995 cp_parser_error (parser,
26996 "expected %<class%>, %<typename%>, or %<template%>");
26997 return;
26998 default:
26999 gcc_unreachable ();
27002 else
27003 gcc_unreachable ();
27008 /* If the next token is of the indicated TYPE, consume it. Otherwise,
27009 issue an error message indicating that TOKEN_DESC was expected.
27011 Returns the token consumed, if the token had the appropriate type.
27012 Otherwise, returns NULL. */
27014 static cp_token *
27015 cp_parser_require (cp_parser* parser,
27016 enum cpp_ttype type,
27017 required_token token_desc)
27019 if (cp_lexer_next_token_is (parser->lexer, type))
27020 return cp_lexer_consume_token (parser->lexer);
27021 else
27023 /* Output the MESSAGE -- unless we're parsing tentatively. */
27024 if (!cp_parser_simulate_error (parser))
27025 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
27026 return NULL;
27030 /* An error message is produced if the next token is not '>'.
27031 All further tokens are skipped until the desired token is
27032 found or '{', '}', ';' or an unbalanced ')' or ']'. */
27034 static void
27035 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
27037 /* Current level of '< ... >'. */
27038 unsigned level = 0;
27039 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
27040 unsigned nesting_depth = 0;
27042 /* Are we ready, yet? If not, issue error message. */
27043 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
27044 return;
27046 /* Skip tokens until the desired token is found. */
27047 while (true)
27049 /* Peek at the next token. */
27050 switch (cp_lexer_peek_token (parser->lexer)->type)
27052 case CPP_LESS:
27053 if (!nesting_depth)
27054 ++level;
27055 break;
27057 case CPP_RSHIFT:
27058 if (cxx_dialect == cxx98)
27059 /* C++0x views the `>>' operator as two `>' tokens, but
27060 C++98 does not. */
27061 break;
27062 else if (!nesting_depth && level-- == 0)
27064 /* We've hit a `>>' where the first `>' closes the
27065 template argument list, and the second `>' is
27066 spurious. Just consume the `>>' and stop; we've
27067 already produced at least one error. */
27068 cp_lexer_consume_token (parser->lexer);
27069 return;
27071 /* Fall through for C++0x, so we handle the second `>' in
27072 the `>>'. */
27074 case CPP_GREATER:
27075 if (!nesting_depth && level-- == 0)
27077 /* We've reached the token we want, consume it and stop. */
27078 cp_lexer_consume_token (parser->lexer);
27079 return;
27081 break;
27083 case CPP_OPEN_PAREN:
27084 case CPP_OPEN_SQUARE:
27085 ++nesting_depth;
27086 break;
27088 case CPP_CLOSE_PAREN:
27089 case CPP_CLOSE_SQUARE:
27090 if (nesting_depth-- == 0)
27091 return;
27092 break;
27094 case CPP_EOF:
27095 case CPP_PRAGMA_EOL:
27096 case CPP_SEMICOLON:
27097 case CPP_OPEN_BRACE:
27098 case CPP_CLOSE_BRACE:
27099 /* The '>' was probably forgotten, don't look further. */
27100 return;
27102 default:
27103 break;
27106 /* Consume this token. */
27107 cp_lexer_consume_token (parser->lexer);
27111 /* If the next token is the indicated keyword, consume it. Otherwise,
27112 issue an error message indicating that TOKEN_DESC was expected.
27114 Returns the token consumed, if the token had the appropriate type.
27115 Otherwise, returns NULL. */
27117 static cp_token *
27118 cp_parser_require_keyword (cp_parser* parser,
27119 enum rid keyword,
27120 required_token token_desc)
27122 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
27124 if (token && token->keyword != keyword)
27126 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
27127 return NULL;
27130 return token;
27133 /* Returns TRUE iff TOKEN is a token that can begin the body of a
27134 function-definition. */
27136 static bool
27137 cp_parser_token_starts_function_definition_p (cp_token* token)
27139 return (/* An ordinary function-body begins with an `{'. */
27140 token->type == CPP_OPEN_BRACE
27141 /* A ctor-initializer begins with a `:'. */
27142 || token->type == CPP_COLON
27143 /* A function-try-block begins with `try'. */
27144 || token->keyword == RID_TRY
27145 /* A function-transaction-block begins with `__transaction_atomic'
27146 or `__transaction_relaxed'. */
27147 || token->keyword == RID_TRANSACTION_ATOMIC
27148 || token->keyword == RID_TRANSACTION_RELAXED
27149 /* The named return value extension begins with `return'. */
27150 || token->keyword == RID_RETURN);
27153 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
27154 definition. */
27156 static bool
27157 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
27159 cp_token *token;
27161 token = cp_lexer_peek_token (parser->lexer);
27162 return (token->type == CPP_OPEN_BRACE
27163 || (token->type == CPP_COLON
27164 && !parser->colon_doesnt_start_class_def_p));
27167 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
27168 C++0x) ending a template-argument. */
27170 static bool
27171 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
27173 cp_token *token;
27175 token = cp_lexer_peek_token (parser->lexer);
27176 return (token->type == CPP_COMMA
27177 || token->type == CPP_GREATER
27178 || token->type == CPP_ELLIPSIS
27179 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
27182 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
27183 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
27185 static bool
27186 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
27187 size_t n)
27189 cp_token *token;
27191 token = cp_lexer_peek_nth_token (parser->lexer, n);
27192 if (token->type == CPP_LESS)
27193 return true;
27194 /* Check for the sequence `<::' in the original code. It would be lexed as
27195 `[:', where `[' is a digraph, and there is no whitespace before
27196 `:'. */
27197 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
27199 cp_token *token2;
27200 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
27201 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
27202 return true;
27204 return false;
27207 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
27208 or none_type otherwise. */
27210 static enum tag_types
27211 cp_parser_token_is_class_key (cp_token* token)
27213 switch (token->keyword)
27215 case RID_CLASS:
27216 return class_type;
27217 case RID_STRUCT:
27218 return record_type;
27219 case RID_UNION:
27220 return union_type;
27222 default:
27223 return none_type;
27227 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
27228 or none_type otherwise or if the token is null. */
27230 static enum tag_types
27231 cp_parser_token_is_type_parameter_key (cp_token* token)
27233 if (!token)
27234 return none_type;
27236 switch (token->keyword)
27238 case RID_CLASS:
27239 return class_type;
27240 case RID_TYPENAME:
27241 return typename_type;
27243 default:
27244 return none_type;
27248 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
27250 static void
27251 cp_parser_check_class_key (enum tag_types class_key, tree type)
27253 if (type == error_mark_node)
27254 return;
27255 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
27257 if (permerror (input_location, "%qs tag used in naming %q#T",
27258 class_key == union_type ? "union"
27259 : class_key == record_type ? "struct" : "class",
27260 type))
27261 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
27262 "%q#T was previously declared here", type);
27266 /* Issue an error message if DECL is redeclared with different
27267 access than its original declaration [class.access.spec/3].
27268 This applies to nested classes, nested class templates and
27269 enumerations [class.mem/1]. */
27271 static void
27272 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
27274 if (!decl
27275 || (!CLASS_TYPE_P (TREE_TYPE (decl))
27276 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
27277 return;
27279 if ((TREE_PRIVATE (decl)
27280 != (current_access_specifier == access_private_node))
27281 || (TREE_PROTECTED (decl)
27282 != (current_access_specifier == access_protected_node)))
27283 error_at (location, "%qD redeclared with different access", decl);
27286 /* Look for the `template' keyword, as a syntactic disambiguator.
27287 Return TRUE iff it is present, in which case it will be
27288 consumed. */
27290 static bool
27291 cp_parser_optional_template_keyword (cp_parser *parser)
27293 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27295 /* In C++98 the `template' keyword can only be used within templates;
27296 outside templates the parser can always figure out what is a
27297 template and what is not. In C++11, per the resolution of DR 468,
27298 `template' is allowed in cases where it is not strictly necessary. */
27299 if (!processing_template_decl
27300 && pedantic && cxx_dialect == cxx98)
27302 cp_token *token = cp_lexer_peek_token (parser->lexer);
27303 pedwarn (token->location, OPT_Wpedantic,
27304 "in C++98 %<template%> (as a disambiguator) is only "
27305 "allowed within templates");
27306 /* If this part of the token stream is rescanned, the same
27307 error message would be generated. So, we purge the token
27308 from the stream. */
27309 cp_lexer_purge_token (parser->lexer);
27310 return false;
27312 else
27314 /* Consume the `template' keyword. */
27315 cp_lexer_consume_token (parser->lexer);
27316 return true;
27319 return false;
27322 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
27323 set PARSER->SCOPE, and perform other related actions. */
27325 static void
27326 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
27328 struct tree_check *check_value;
27330 /* Get the stored value. */
27331 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
27332 /* Set the scope from the stored value. */
27333 parser->scope = saved_checks_value (check_value);
27334 parser->qualifying_scope = check_value->qualifying_scope;
27335 parser->object_scope = NULL_TREE;
27338 /* Consume tokens up through a non-nested END token. Returns TRUE if we
27339 encounter the end of a block before what we were looking for. */
27341 static bool
27342 cp_parser_cache_group (cp_parser *parser,
27343 enum cpp_ttype end,
27344 unsigned depth)
27346 while (true)
27348 cp_token *token = cp_lexer_peek_token (parser->lexer);
27350 /* Abort a parenthesized expression if we encounter a semicolon. */
27351 if ((end == CPP_CLOSE_PAREN || depth == 0)
27352 && token->type == CPP_SEMICOLON)
27353 return true;
27354 /* If we've reached the end of the file, stop. */
27355 if (token->type == CPP_EOF
27356 || (end != CPP_PRAGMA_EOL
27357 && token->type == CPP_PRAGMA_EOL))
27358 return true;
27359 if (token->type == CPP_CLOSE_BRACE && depth == 0)
27360 /* We've hit the end of an enclosing block, so there's been some
27361 kind of syntax error. */
27362 return true;
27364 /* Consume the token. */
27365 cp_lexer_consume_token (parser->lexer);
27366 /* See if it starts a new group. */
27367 if (token->type == CPP_OPEN_BRACE)
27369 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
27370 /* In theory this should probably check end == '}', but
27371 cp_parser_save_member_function_body needs it to exit
27372 after either '}' or ')' when called with ')'. */
27373 if (depth == 0)
27374 return false;
27376 else if (token->type == CPP_OPEN_PAREN)
27378 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
27379 if (depth == 0 && end == CPP_CLOSE_PAREN)
27380 return false;
27382 else if (token->type == CPP_PRAGMA)
27383 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
27384 else if (token->type == end)
27385 return false;
27389 /* Like above, for caching a default argument or NSDMI. Both of these are
27390 terminated by a non-nested comma, but it can be unclear whether or not a
27391 comma is nested in a template argument list unless we do more parsing.
27392 In order to handle this ambiguity, when we encounter a ',' after a '<'
27393 we try to parse what follows as a parameter-declaration-list (in the
27394 case of a default argument) or a member-declarator (in the case of an
27395 NSDMI). If that succeeds, then we stop caching. */
27397 static tree
27398 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
27400 unsigned depth = 0;
27401 int maybe_template_id = 0;
27402 cp_token *first_token;
27403 cp_token *token;
27404 tree default_argument;
27406 /* Add tokens until we have processed the entire default
27407 argument. We add the range [first_token, token). */
27408 first_token = cp_lexer_peek_token (parser->lexer);
27409 if (first_token->type == CPP_OPEN_BRACE)
27411 /* For list-initialization, this is straightforward. */
27412 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27413 token = cp_lexer_peek_token (parser->lexer);
27415 else while (true)
27417 bool done = false;
27419 /* Peek at the next token. */
27420 token = cp_lexer_peek_token (parser->lexer);
27421 /* What we do depends on what token we have. */
27422 switch (token->type)
27424 /* In valid code, a default argument must be
27425 immediately followed by a `,' `)', or `...'. */
27426 case CPP_COMMA:
27427 if (depth == 0 && maybe_template_id)
27429 /* If we've seen a '<', we might be in a
27430 template-argument-list. Until Core issue 325 is
27431 resolved, we don't know how this situation ought
27432 to be handled, so try to DTRT. We check whether
27433 what comes after the comma is a valid parameter
27434 declaration list. If it is, then the comma ends
27435 the default argument; otherwise the default
27436 argument continues. */
27437 bool error = false;
27438 cp_token *peek;
27440 /* Set ITALP so cp_parser_parameter_declaration_list
27441 doesn't decide to commit to this parse. */
27442 bool saved_italp = parser->in_template_argument_list_p;
27443 parser->in_template_argument_list_p = true;
27445 cp_parser_parse_tentatively (parser);
27447 if (nsdmi)
27449 /* Parse declarators until we reach a non-comma or
27450 somthing that cannot be an initializer.
27451 Just checking whether we're looking at a single
27452 declarator is insufficient. Consider:
27453 int var = tuple<T,U>::x;
27454 The template parameter 'U' looks exactly like a
27455 declarator. */
27458 int ctor_dtor_or_conv_p;
27459 cp_lexer_consume_token (parser->lexer);
27460 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27461 &ctor_dtor_or_conv_p,
27462 /*parenthesized_p=*/NULL,
27463 /*member_p=*/true,
27464 /*friend_p=*/false);
27465 peek = cp_lexer_peek_token (parser->lexer);
27466 if (cp_parser_error_occurred (parser))
27467 break;
27469 while (peek->type == CPP_COMMA);
27470 /* If we met an '=' or ';' then the original comma
27471 was the end of the NSDMI. Otherwise assume
27472 we're still in the NSDMI. */
27473 error = (peek->type != CPP_EQ
27474 && peek->type != CPP_SEMICOLON);
27476 else
27478 cp_lexer_consume_token (parser->lexer);
27479 begin_scope (sk_function_parms, NULL_TREE);
27480 cp_parser_parameter_declaration_list (parser, &error);
27481 pop_bindings_and_leave_scope ();
27483 if (!cp_parser_error_occurred (parser) && !error)
27484 done = true;
27485 cp_parser_abort_tentative_parse (parser);
27487 parser->in_template_argument_list_p = saved_italp;
27488 break;
27490 case CPP_CLOSE_PAREN:
27491 case CPP_ELLIPSIS:
27492 /* If we run into a non-nested `;', `}', or `]',
27493 then the code is invalid -- but the default
27494 argument is certainly over. */
27495 case CPP_SEMICOLON:
27496 case CPP_CLOSE_BRACE:
27497 case CPP_CLOSE_SQUARE:
27498 if (depth == 0
27499 /* Handle correctly int n = sizeof ... ( p ); */
27500 && token->type != CPP_ELLIPSIS)
27501 done = true;
27502 /* Update DEPTH, if necessary. */
27503 else if (token->type == CPP_CLOSE_PAREN
27504 || token->type == CPP_CLOSE_BRACE
27505 || token->type == CPP_CLOSE_SQUARE)
27506 --depth;
27507 break;
27509 case CPP_OPEN_PAREN:
27510 case CPP_OPEN_SQUARE:
27511 case CPP_OPEN_BRACE:
27512 ++depth;
27513 break;
27515 case CPP_LESS:
27516 if (depth == 0)
27517 /* This might be the comparison operator, or it might
27518 start a template argument list. */
27519 ++maybe_template_id;
27520 break;
27522 case CPP_RSHIFT:
27523 if (cxx_dialect == cxx98)
27524 break;
27525 /* Fall through for C++0x, which treats the `>>'
27526 operator like two `>' tokens in certain
27527 cases. */
27529 case CPP_GREATER:
27530 if (depth == 0)
27532 /* This might be an operator, or it might close a
27533 template argument list. But if a previous '<'
27534 started a template argument list, this will have
27535 closed it, so we can't be in one anymore. */
27536 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
27537 if (maybe_template_id < 0)
27538 maybe_template_id = 0;
27540 break;
27542 /* If we run out of tokens, issue an error message. */
27543 case CPP_EOF:
27544 case CPP_PRAGMA_EOL:
27545 error_at (token->location, "file ends in default argument");
27546 return error_mark_node;
27548 case CPP_NAME:
27549 case CPP_SCOPE:
27550 /* In these cases, we should look for template-ids.
27551 For example, if the default argument is
27552 `X<int, double>()', we need to do name lookup to
27553 figure out whether or not `X' is a template; if
27554 so, the `,' does not end the default argument.
27556 That is not yet done. */
27557 break;
27559 default:
27560 break;
27563 /* If we've reached the end, stop. */
27564 if (done)
27565 break;
27567 /* Add the token to the token block. */
27568 token = cp_lexer_consume_token (parser->lexer);
27571 /* Create a DEFAULT_ARG to represent the unparsed default
27572 argument. */
27573 default_argument = make_node (DEFAULT_ARG);
27574 DEFARG_TOKENS (default_argument)
27575 = cp_token_cache_new (first_token, token);
27576 DEFARG_INSTANTIATIONS (default_argument) = NULL;
27578 return default_argument;
27581 /* Begin parsing tentatively. We always save tokens while parsing
27582 tentatively so that if the tentative parsing fails we can restore the
27583 tokens. */
27585 static void
27586 cp_parser_parse_tentatively (cp_parser* parser)
27588 /* Enter a new parsing context. */
27589 parser->context = cp_parser_context_new (parser->context);
27590 /* Begin saving tokens. */
27591 cp_lexer_save_tokens (parser->lexer);
27592 /* In order to avoid repetitive access control error messages,
27593 access checks are queued up until we are no longer parsing
27594 tentatively. */
27595 push_deferring_access_checks (dk_deferred);
27598 /* Commit to the currently active tentative parse. */
27600 static void
27601 cp_parser_commit_to_tentative_parse (cp_parser* parser)
27603 cp_parser_context *context;
27604 cp_lexer *lexer;
27606 /* Mark all of the levels as committed. */
27607 lexer = parser->lexer;
27608 for (context = parser->context; context->next; context = context->next)
27610 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27611 break;
27612 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27613 while (!cp_lexer_saving_tokens (lexer))
27614 lexer = lexer->next;
27615 cp_lexer_commit_tokens (lexer);
27619 /* Commit to the topmost currently active tentative parse.
27621 Note that this function shouldn't be called when there are
27622 irreversible side-effects while in a tentative state. For
27623 example, we shouldn't create a permanent entry in the symbol
27624 table, or issue an error message that might not apply if the
27625 tentative parse is aborted. */
27627 static void
27628 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
27630 cp_parser_context *context = parser->context;
27631 cp_lexer *lexer = parser->lexer;
27633 if (context)
27635 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27636 return;
27637 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27639 while (!cp_lexer_saving_tokens (lexer))
27640 lexer = lexer->next;
27641 cp_lexer_commit_tokens (lexer);
27645 /* Abort the currently active tentative parse. All consumed tokens
27646 will be rolled back, and no diagnostics will be issued. */
27648 static void
27649 cp_parser_abort_tentative_parse (cp_parser* parser)
27651 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
27652 || errorcount > 0);
27653 cp_parser_simulate_error (parser);
27654 /* Now, pretend that we want to see if the construct was
27655 successfully parsed. */
27656 cp_parser_parse_definitely (parser);
27659 /* Stop parsing tentatively. If a parse error has occurred, restore the
27660 token stream. Otherwise, commit to the tokens we have consumed.
27661 Returns true if no error occurred; false otherwise. */
27663 static bool
27664 cp_parser_parse_definitely (cp_parser* parser)
27666 bool error_occurred;
27667 cp_parser_context *context;
27669 /* Remember whether or not an error occurred, since we are about to
27670 destroy that information. */
27671 error_occurred = cp_parser_error_occurred (parser);
27672 /* Remove the topmost context from the stack. */
27673 context = parser->context;
27674 parser->context = context->next;
27675 /* If no parse errors occurred, commit to the tentative parse. */
27676 if (!error_occurred)
27678 /* Commit to the tokens read tentatively, unless that was
27679 already done. */
27680 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
27681 cp_lexer_commit_tokens (parser->lexer);
27683 pop_to_parent_deferring_access_checks ();
27685 /* Otherwise, if errors occurred, roll back our state so that things
27686 are just as they were before we began the tentative parse. */
27687 else
27689 cp_lexer_rollback_tokens (parser->lexer);
27690 pop_deferring_access_checks ();
27692 /* Add the context to the front of the free list. */
27693 context->next = cp_parser_context_free_list;
27694 cp_parser_context_free_list = context;
27696 return !error_occurred;
27699 /* Returns true if we are parsing tentatively and are not committed to
27700 this tentative parse. */
27702 static bool
27703 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
27705 return (cp_parser_parsing_tentatively (parser)
27706 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
27709 /* Returns nonzero iff an error has occurred during the most recent
27710 tentative parse. */
27712 static bool
27713 cp_parser_error_occurred (cp_parser* parser)
27715 return (cp_parser_parsing_tentatively (parser)
27716 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
27719 /* Returns nonzero if GNU extensions are allowed. */
27721 static bool
27722 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
27724 return parser->allow_gnu_extensions_p;
27727 /* Objective-C++ Productions */
27730 /* Parse an Objective-C expression, which feeds into a primary-expression
27731 above.
27733 objc-expression:
27734 objc-message-expression
27735 objc-string-literal
27736 objc-encode-expression
27737 objc-protocol-expression
27738 objc-selector-expression
27740 Returns a tree representation of the expression. */
27742 static cp_expr
27743 cp_parser_objc_expression (cp_parser* parser)
27745 /* Try to figure out what kind of declaration is present. */
27746 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27748 switch (kwd->type)
27750 case CPP_OPEN_SQUARE:
27751 return cp_parser_objc_message_expression (parser);
27753 case CPP_OBJC_STRING:
27754 kwd = cp_lexer_consume_token (parser->lexer);
27755 return objc_build_string_object (kwd->u.value);
27757 case CPP_KEYWORD:
27758 switch (kwd->keyword)
27760 case RID_AT_ENCODE:
27761 return cp_parser_objc_encode_expression (parser);
27763 case RID_AT_PROTOCOL:
27764 return cp_parser_objc_protocol_expression (parser);
27766 case RID_AT_SELECTOR:
27767 return cp_parser_objc_selector_expression (parser);
27769 default:
27770 break;
27772 default:
27773 error_at (kwd->location,
27774 "misplaced %<@%D%> Objective-C++ construct",
27775 kwd->u.value);
27776 cp_parser_skip_to_end_of_block_or_statement (parser);
27779 return error_mark_node;
27782 /* Parse an Objective-C message expression.
27784 objc-message-expression:
27785 [ objc-message-receiver objc-message-args ]
27787 Returns a representation of an Objective-C message. */
27789 static tree
27790 cp_parser_objc_message_expression (cp_parser* parser)
27792 tree receiver, messageargs;
27794 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27795 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
27796 receiver = cp_parser_objc_message_receiver (parser);
27797 messageargs = cp_parser_objc_message_args (parser);
27798 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
27799 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27801 tree result = objc_build_message_expr (receiver, messageargs);
27803 /* Construct a location e.g.
27804 [self func1:5]
27805 ^~~~~~~~~~~~~~
27806 ranging from the '[' to the ']', with the caret at the start. */
27807 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
27808 protected_set_expr_location (result, combined_loc);
27810 return result;
27813 /* Parse an objc-message-receiver.
27815 objc-message-receiver:
27816 expression
27817 simple-type-specifier
27819 Returns a representation of the type or expression. */
27821 static tree
27822 cp_parser_objc_message_receiver (cp_parser* parser)
27824 tree rcv;
27826 /* An Objective-C message receiver may be either (1) a type
27827 or (2) an expression. */
27828 cp_parser_parse_tentatively (parser);
27829 rcv = cp_parser_expression (parser);
27831 /* If that worked out, fine. */
27832 if (cp_parser_parse_definitely (parser))
27833 return rcv;
27835 cp_parser_parse_tentatively (parser);
27836 rcv = cp_parser_simple_type_specifier (parser,
27837 /*decl_specs=*/NULL,
27838 CP_PARSER_FLAGS_NONE);
27840 if (cp_parser_parse_definitely (parser))
27841 return objc_get_class_reference (rcv);
27843 cp_parser_error (parser, "objective-c++ message receiver expected");
27844 return error_mark_node;
27847 /* Parse the arguments and selectors comprising an Objective-C message.
27849 objc-message-args:
27850 objc-selector
27851 objc-selector-args
27852 objc-selector-args , objc-comma-args
27854 objc-selector-args:
27855 objc-selector [opt] : assignment-expression
27856 objc-selector-args objc-selector [opt] : assignment-expression
27858 objc-comma-args:
27859 assignment-expression
27860 objc-comma-args , assignment-expression
27862 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
27863 selector arguments and TREE_VALUE containing a list of comma
27864 arguments. */
27866 static tree
27867 cp_parser_objc_message_args (cp_parser* parser)
27869 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
27870 bool maybe_unary_selector_p = true;
27871 cp_token *token = cp_lexer_peek_token (parser->lexer);
27873 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
27875 tree selector = NULL_TREE, arg;
27877 if (token->type != CPP_COLON)
27878 selector = cp_parser_objc_selector (parser);
27880 /* Detect if we have a unary selector. */
27881 if (maybe_unary_selector_p
27882 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27883 return build_tree_list (selector, NULL_TREE);
27885 maybe_unary_selector_p = false;
27886 cp_parser_require (parser, CPP_COLON, RT_COLON);
27887 arg = cp_parser_assignment_expression (parser);
27889 sel_args
27890 = chainon (sel_args,
27891 build_tree_list (selector, arg));
27893 token = cp_lexer_peek_token (parser->lexer);
27896 /* Handle non-selector arguments, if any. */
27897 while (token->type == CPP_COMMA)
27899 tree arg;
27901 cp_lexer_consume_token (parser->lexer);
27902 arg = cp_parser_assignment_expression (parser);
27904 addl_args
27905 = chainon (addl_args,
27906 build_tree_list (NULL_TREE, arg));
27908 token = cp_lexer_peek_token (parser->lexer);
27911 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
27913 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
27914 return build_tree_list (error_mark_node, error_mark_node);
27917 return build_tree_list (sel_args, addl_args);
27920 /* Parse an Objective-C encode expression.
27922 objc-encode-expression:
27923 @encode objc-typename
27925 Returns an encoded representation of the type argument. */
27927 static cp_expr
27928 cp_parser_objc_encode_expression (cp_parser* parser)
27930 tree type;
27931 cp_token *token;
27932 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27934 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
27935 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27936 token = cp_lexer_peek_token (parser->lexer);
27937 type = complete_type (cp_parser_type_id (parser));
27938 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27940 if (!type)
27942 error_at (token->location,
27943 "%<@encode%> must specify a type as an argument");
27944 return error_mark_node;
27947 /* This happens if we find @encode(T) (where T is a template
27948 typename or something dependent on a template typename) when
27949 parsing a template. In that case, we can't compile it
27950 immediately, but we rather create an AT_ENCODE_EXPR which will
27951 need to be instantiated when the template is used.
27953 if (dependent_type_p (type))
27955 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
27956 TREE_READONLY (value) = 1;
27957 return value;
27961 /* Build a location of the form:
27962 @encode(int)
27963 ^~~~~~~~~~~~
27964 with caret==start at the @ token, finishing at the close paren. */
27965 location_t combined_loc
27966 = make_location (start_loc, start_loc,
27967 cp_lexer_previous_token (parser->lexer)->location);
27969 return cp_expr (objc_build_encode_expr (type), combined_loc);
27972 /* Parse an Objective-C @defs expression. */
27974 static tree
27975 cp_parser_objc_defs_expression (cp_parser *parser)
27977 tree name;
27979 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
27980 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27981 name = cp_parser_identifier (parser);
27982 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27984 return objc_get_class_ivars (name);
27987 /* Parse an Objective-C protocol expression.
27989 objc-protocol-expression:
27990 @protocol ( identifier )
27992 Returns a representation of the protocol expression. */
27994 static tree
27995 cp_parser_objc_protocol_expression (cp_parser* parser)
27997 tree proto;
27998 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28000 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28001 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28002 proto = cp_parser_identifier (parser);
28003 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28005 /* Build a location of the form:
28006 @protocol(prot)
28007 ^~~~~~~~~~~~~~~
28008 with caret==start at the @ token, finishing at the close paren. */
28009 location_t combined_loc
28010 = make_location (start_loc, start_loc,
28011 cp_lexer_previous_token (parser->lexer)->location);
28012 tree result = objc_build_protocol_expr (proto);
28013 protected_set_expr_location (result, combined_loc);
28014 return result;
28017 /* Parse an Objective-C selector expression.
28019 objc-selector-expression:
28020 @selector ( objc-method-signature )
28022 objc-method-signature:
28023 objc-selector
28024 objc-selector-seq
28026 objc-selector-seq:
28027 objc-selector :
28028 objc-selector-seq objc-selector :
28030 Returns a representation of the method selector. */
28032 static tree
28033 cp_parser_objc_selector_expression (cp_parser* parser)
28035 tree sel_seq = NULL_TREE;
28036 bool maybe_unary_selector_p = true;
28037 cp_token *token;
28038 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28040 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
28041 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28042 token = cp_lexer_peek_token (parser->lexer);
28044 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
28045 || token->type == CPP_SCOPE)
28047 tree selector = NULL_TREE;
28049 if (token->type != CPP_COLON
28050 || token->type == CPP_SCOPE)
28051 selector = cp_parser_objc_selector (parser);
28053 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
28054 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
28056 /* Detect if we have a unary selector. */
28057 if (maybe_unary_selector_p)
28059 sel_seq = selector;
28060 goto finish_selector;
28062 else
28064 cp_parser_error (parser, "expected %<:%>");
28067 maybe_unary_selector_p = false;
28068 token = cp_lexer_consume_token (parser->lexer);
28070 if (token->type == CPP_SCOPE)
28072 sel_seq
28073 = chainon (sel_seq,
28074 build_tree_list (selector, NULL_TREE));
28075 sel_seq
28076 = chainon (sel_seq,
28077 build_tree_list (NULL_TREE, NULL_TREE));
28079 else
28080 sel_seq
28081 = chainon (sel_seq,
28082 build_tree_list (selector, NULL_TREE));
28084 token = cp_lexer_peek_token (parser->lexer);
28087 finish_selector:
28088 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28091 /* Build a location of the form:
28092 @selector(func)
28093 ^~~~~~~~~~~~~~~
28094 with caret==start at the @ token, finishing at the close paren. */
28095 location_t combined_loc
28096 = make_location (loc, loc,
28097 cp_lexer_previous_token (parser->lexer)->location);
28098 tree result = objc_build_selector_expr (combined_loc, sel_seq);
28099 /* TODO: objc_build_selector_expr doesn't always honor the location. */
28100 protected_set_expr_location (result, combined_loc);
28101 return result;
28104 /* Parse a list of identifiers.
28106 objc-identifier-list:
28107 identifier
28108 objc-identifier-list , identifier
28110 Returns a TREE_LIST of identifier nodes. */
28112 static tree
28113 cp_parser_objc_identifier_list (cp_parser* parser)
28115 tree identifier;
28116 tree list;
28117 cp_token *sep;
28119 identifier = cp_parser_identifier (parser);
28120 if (identifier == error_mark_node)
28121 return error_mark_node;
28123 list = build_tree_list (NULL_TREE, identifier);
28124 sep = cp_lexer_peek_token (parser->lexer);
28126 while (sep->type == CPP_COMMA)
28128 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28129 identifier = cp_parser_identifier (parser);
28130 if (identifier == error_mark_node)
28131 return list;
28133 list = chainon (list, build_tree_list (NULL_TREE,
28134 identifier));
28135 sep = cp_lexer_peek_token (parser->lexer);
28138 return list;
28141 /* Parse an Objective-C alias declaration.
28143 objc-alias-declaration:
28144 @compatibility_alias identifier identifier ;
28146 This function registers the alias mapping with the Objective-C front end.
28147 It returns nothing. */
28149 static void
28150 cp_parser_objc_alias_declaration (cp_parser* parser)
28152 tree alias, orig;
28154 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
28155 alias = cp_parser_identifier (parser);
28156 orig = cp_parser_identifier (parser);
28157 objc_declare_alias (alias, orig);
28158 cp_parser_consume_semicolon_at_end_of_statement (parser);
28161 /* Parse an Objective-C class forward-declaration.
28163 objc-class-declaration:
28164 @class objc-identifier-list ;
28166 The function registers the forward declarations with the Objective-C
28167 front end. It returns nothing. */
28169 static void
28170 cp_parser_objc_class_declaration (cp_parser* parser)
28172 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
28173 while (true)
28175 tree id;
28177 id = cp_parser_identifier (parser);
28178 if (id == error_mark_node)
28179 break;
28181 objc_declare_class (id);
28183 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28184 cp_lexer_consume_token (parser->lexer);
28185 else
28186 break;
28188 cp_parser_consume_semicolon_at_end_of_statement (parser);
28191 /* Parse a list of Objective-C protocol references.
28193 objc-protocol-refs-opt:
28194 objc-protocol-refs [opt]
28196 objc-protocol-refs:
28197 < objc-identifier-list >
28199 Returns a TREE_LIST of identifiers, if any. */
28201 static tree
28202 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
28204 tree protorefs = NULL_TREE;
28206 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
28208 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
28209 protorefs = cp_parser_objc_identifier_list (parser);
28210 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
28213 return protorefs;
28216 /* Parse a Objective-C visibility specification. */
28218 static void
28219 cp_parser_objc_visibility_spec (cp_parser* parser)
28221 cp_token *vis = cp_lexer_peek_token (parser->lexer);
28223 switch (vis->keyword)
28225 case RID_AT_PRIVATE:
28226 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
28227 break;
28228 case RID_AT_PROTECTED:
28229 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
28230 break;
28231 case RID_AT_PUBLIC:
28232 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
28233 break;
28234 case RID_AT_PACKAGE:
28235 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
28236 break;
28237 default:
28238 return;
28241 /* Eat '@private'/'@protected'/'@public'. */
28242 cp_lexer_consume_token (parser->lexer);
28245 /* Parse an Objective-C method type. Return 'true' if it is a class
28246 (+) method, and 'false' if it is an instance (-) method. */
28248 static inline bool
28249 cp_parser_objc_method_type (cp_parser* parser)
28251 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
28252 return true;
28253 else
28254 return false;
28257 /* Parse an Objective-C protocol qualifier. */
28259 static tree
28260 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
28262 tree quals = NULL_TREE, node;
28263 cp_token *token = cp_lexer_peek_token (parser->lexer);
28265 node = token->u.value;
28267 while (node && identifier_p (node)
28268 && (node == ridpointers [(int) RID_IN]
28269 || node == ridpointers [(int) RID_OUT]
28270 || node == ridpointers [(int) RID_INOUT]
28271 || node == ridpointers [(int) RID_BYCOPY]
28272 || node == ridpointers [(int) RID_BYREF]
28273 || node == ridpointers [(int) RID_ONEWAY]))
28275 quals = tree_cons (NULL_TREE, node, quals);
28276 cp_lexer_consume_token (parser->lexer);
28277 token = cp_lexer_peek_token (parser->lexer);
28278 node = token->u.value;
28281 return quals;
28284 /* Parse an Objective-C typename. */
28286 static tree
28287 cp_parser_objc_typename (cp_parser* parser)
28289 tree type_name = NULL_TREE;
28291 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28293 tree proto_quals, cp_type = NULL_TREE;
28295 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
28296 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
28298 /* An ObjC type name may consist of just protocol qualifiers, in which
28299 case the type shall default to 'id'. */
28300 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
28302 cp_type = cp_parser_type_id (parser);
28304 /* If the type could not be parsed, an error has already
28305 been produced. For error recovery, behave as if it had
28306 not been specified, which will use the default type
28307 'id'. */
28308 if (cp_type == error_mark_node)
28310 cp_type = NULL_TREE;
28311 /* We need to skip to the closing parenthesis as
28312 cp_parser_type_id() does not seem to do it for
28313 us. */
28314 cp_parser_skip_to_closing_parenthesis (parser,
28315 /*recovering=*/true,
28316 /*or_comma=*/false,
28317 /*consume_paren=*/false);
28321 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28322 type_name = build_tree_list (proto_quals, cp_type);
28325 return type_name;
28328 /* Check to see if TYPE refers to an Objective-C selector name. */
28330 static bool
28331 cp_parser_objc_selector_p (enum cpp_ttype type)
28333 return (type == CPP_NAME || type == CPP_KEYWORD
28334 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
28335 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
28336 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
28337 || type == CPP_XOR || type == CPP_XOR_EQ);
28340 /* Parse an Objective-C selector. */
28342 static tree
28343 cp_parser_objc_selector (cp_parser* parser)
28345 cp_token *token = cp_lexer_consume_token (parser->lexer);
28347 if (!cp_parser_objc_selector_p (token->type))
28349 error_at (token->location, "invalid Objective-C++ selector name");
28350 return error_mark_node;
28353 /* C++ operator names are allowed to appear in ObjC selectors. */
28354 switch (token->type)
28356 case CPP_AND_AND: return get_identifier ("and");
28357 case CPP_AND_EQ: return get_identifier ("and_eq");
28358 case CPP_AND: return get_identifier ("bitand");
28359 case CPP_OR: return get_identifier ("bitor");
28360 case CPP_COMPL: return get_identifier ("compl");
28361 case CPP_NOT: return get_identifier ("not");
28362 case CPP_NOT_EQ: return get_identifier ("not_eq");
28363 case CPP_OR_OR: return get_identifier ("or");
28364 case CPP_OR_EQ: return get_identifier ("or_eq");
28365 case CPP_XOR: return get_identifier ("xor");
28366 case CPP_XOR_EQ: return get_identifier ("xor_eq");
28367 default: return token->u.value;
28371 /* Parse an Objective-C params list. */
28373 static tree
28374 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
28376 tree params = NULL_TREE;
28377 bool maybe_unary_selector_p = true;
28378 cp_token *token = cp_lexer_peek_token (parser->lexer);
28380 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28382 tree selector = NULL_TREE, type_name, identifier;
28383 tree parm_attr = NULL_TREE;
28385 if (token->keyword == RID_ATTRIBUTE)
28386 break;
28388 if (token->type != CPP_COLON)
28389 selector = cp_parser_objc_selector (parser);
28391 /* Detect if we have a unary selector. */
28392 if (maybe_unary_selector_p
28393 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28395 params = selector; /* Might be followed by attributes. */
28396 break;
28399 maybe_unary_selector_p = false;
28400 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28402 /* Something went quite wrong. There should be a colon
28403 here, but there is not. Stop parsing parameters. */
28404 break;
28406 type_name = cp_parser_objc_typename (parser);
28407 /* New ObjC allows attributes on parameters too. */
28408 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
28409 parm_attr = cp_parser_attributes_opt (parser);
28410 identifier = cp_parser_identifier (parser);
28412 params
28413 = chainon (params,
28414 objc_build_keyword_decl (selector,
28415 type_name,
28416 identifier,
28417 parm_attr));
28419 token = cp_lexer_peek_token (parser->lexer);
28422 if (params == NULL_TREE)
28424 cp_parser_error (parser, "objective-c++ method declaration is expected");
28425 return error_mark_node;
28428 /* We allow tail attributes for the method. */
28429 if (token->keyword == RID_ATTRIBUTE)
28431 *attributes = cp_parser_attributes_opt (parser);
28432 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28433 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28434 return params;
28435 cp_parser_error (parser,
28436 "method attributes must be specified at the end");
28437 return error_mark_node;
28440 if (params == NULL_TREE)
28442 cp_parser_error (parser, "objective-c++ method declaration is expected");
28443 return error_mark_node;
28445 return params;
28448 /* Parse the non-keyword Objective-C params. */
28450 static tree
28451 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
28452 tree* attributes)
28454 tree params = make_node (TREE_LIST);
28455 cp_token *token = cp_lexer_peek_token (parser->lexer);
28456 *ellipsisp = false; /* Initially, assume no ellipsis. */
28458 while (token->type == CPP_COMMA)
28460 cp_parameter_declarator *parmdecl;
28461 tree parm;
28463 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28464 token = cp_lexer_peek_token (parser->lexer);
28466 if (token->type == CPP_ELLIPSIS)
28468 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
28469 *ellipsisp = true;
28470 token = cp_lexer_peek_token (parser->lexer);
28471 break;
28474 /* TODO: parse attributes for tail parameters. */
28475 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
28476 parm = grokdeclarator (parmdecl->declarator,
28477 &parmdecl->decl_specifiers,
28478 PARM, /*initialized=*/0,
28479 /*attrlist=*/NULL);
28481 chainon (params, build_tree_list (NULL_TREE, parm));
28482 token = cp_lexer_peek_token (parser->lexer);
28485 /* We allow tail attributes for the method. */
28486 if (token->keyword == RID_ATTRIBUTE)
28488 if (*attributes == NULL_TREE)
28490 *attributes = cp_parser_attributes_opt (parser);
28491 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28492 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28493 return params;
28495 else
28496 /* We have an error, but parse the attributes, so that we can
28497 carry on. */
28498 *attributes = cp_parser_attributes_opt (parser);
28500 cp_parser_error (parser,
28501 "method attributes must be specified at the end");
28502 return error_mark_node;
28505 return params;
28508 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
28510 static void
28511 cp_parser_objc_interstitial_code (cp_parser* parser)
28513 cp_token *token = cp_lexer_peek_token (parser->lexer);
28515 /* If the next token is `extern' and the following token is a string
28516 literal, then we have a linkage specification. */
28517 if (token->keyword == RID_EXTERN
28518 && cp_parser_is_pure_string_literal
28519 (cp_lexer_peek_nth_token (parser->lexer, 2)))
28520 cp_parser_linkage_specification (parser);
28521 /* Handle #pragma, if any. */
28522 else if (token->type == CPP_PRAGMA)
28523 cp_parser_pragma (parser, pragma_objc_icode, NULL);
28524 /* Allow stray semicolons. */
28525 else if (token->type == CPP_SEMICOLON)
28526 cp_lexer_consume_token (parser->lexer);
28527 /* Mark methods as optional or required, when building protocols. */
28528 else if (token->keyword == RID_AT_OPTIONAL)
28530 cp_lexer_consume_token (parser->lexer);
28531 objc_set_method_opt (true);
28533 else if (token->keyword == RID_AT_REQUIRED)
28535 cp_lexer_consume_token (parser->lexer);
28536 objc_set_method_opt (false);
28538 else if (token->keyword == RID_NAMESPACE)
28539 cp_parser_namespace_definition (parser);
28540 /* Other stray characters must generate errors. */
28541 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
28543 cp_lexer_consume_token (parser->lexer);
28544 error ("stray %qs between Objective-C++ methods",
28545 token->type == CPP_OPEN_BRACE ? "{" : "}");
28547 /* Finally, try to parse a block-declaration, or a function-definition. */
28548 else
28549 cp_parser_block_declaration (parser, /*statement_p=*/false);
28552 /* Parse a method signature. */
28554 static tree
28555 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
28557 tree rettype, kwdparms, optparms;
28558 bool ellipsis = false;
28559 bool is_class_method;
28561 is_class_method = cp_parser_objc_method_type (parser);
28562 rettype = cp_parser_objc_typename (parser);
28563 *attributes = NULL_TREE;
28564 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
28565 if (kwdparms == error_mark_node)
28566 return error_mark_node;
28567 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
28568 if (optparms == error_mark_node)
28569 return error_mark_node;
28571 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
28574 static bool
28575 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
28577 tree tattr;
28578 cp_lexer_save_tokens (parser->lexer);
28579 tattr = cp_parser_attributes_opt (parser);
28580 gcc_assert (tattr) ;
28582 /* If the attributes are followed by a method introducer, this is not allowed.
28583 Dump the attributes and flag the situation. */
28584 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
28585 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
28586 return true;
28588 /* Otherwise, the attributes introduce some interstitial code, possibly so
28589 rewind to allow that check. */
28590 cp_lexer_rollback_tokens (parser->lexer);
28591 return false;
28594 /* Parse an Objective-C method prototype list. */
28596 static void
28597 cp_parser_objc_method_prototype_list (cp_parser* parser)
28599 cp_token *token = cp_lexer_peek_token (parser->lexer);
28601 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28603 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28605 tree attributes, sig;
28606 bool is_class_method;
28607 if (token->type == CPP_PLUS)
28608 is_class_method = true;
28609 else
28610 is_class_method = false;
28611 sig = cp_parser_objc_method_signature (parser, &attributes);
28612 if (sig == error_mark_node)
28614 cp_parser_skip_to_end_of_block_or_statement (parser);
28615 token = cp_lexer_peek_token (parser->lexer);
28616 continue;
28618 objc_add_method_declaration (is_class_method, sig, attributes);
28619 cp_parser_consume_semicolon_at_end_of_statement (parser);
28621 else if (token->keyword == RID_AT_PROPERTY)
28622 cp_parser_objc_at_property_declaration (parser);
28623 else if (token->keyword == RID_ATTRIBUTE
28624 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28625 warning_at (cp_lexer_peek_token (parser->lexer)->location,
28626 OPT_Wattributes,
28627 "prefix attributes are ignored for methods");
28628 else
28629 /* Allow for interspersed non-ObjC++ code. */
28630 cp_parser_objc_interstitial_code (parser);
28632 token = cp_lexer_peek_token (parser->lexer);
28635 if (token->type != CPP_EOF)
28636 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28637 else
28638 cp_parser_error (parser, "expected %<@end%>");
28640 objc_finish_interface ();
28643 /* Parse an Objective-C method definition list. */
28645 static void
28646 cp_parser_objc_method_definition_list (cp_parser* parser)
28648 cp_token *token = cp_lexer_peek_token (parser->lexer);
28650 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28652 tree meth;
28654 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28656 cp_token *ptk;
28657 tree sig, attribute;
28658 bool is_class_method;
28659 if (token->type == CPP_PLUS)
28660 is_class_method = true;
28661 else
28662 is_class_method = false;
28663 push_deferring_access_checks (dk_deferred);
28664 sig = cp_parser_objc_method_signature (parser, &attribute);
28665 if (sig == error_mark_node)
28667 cp_parser_skip_to_end_of_block_or_statement (parser);
28668 token = cp_lexer_peek_token (parser->lexer);
28669 continue;
28671 objc_start_method_definition (is_class_method, sig, attribute,
28672 NULL_TREE);
28674 /* For historical reasons, we accept an optional semicolon. */
28675 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28676 cp_lexer_consume_token (parser->lexer);
28678 ptk = cp_lexer_peek_token (parser->lexer);
28679 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
28680 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
28682 perform_deferred_access_checks (tf_warning_or_error);
28683 stop_deferring_access_checks ();
28684 meth = cp_parser_function_definition_after_declarator (parser,
28685 false);
28686 pop_deferring_access_checks ();
28687 objc_finish_method_definition (meth);
28690 /* The following case will be removed once @synthesize is
28691 completely implemented. */
28692 else if (token->keyword == RID_AT_PROPERTY)
28693 cp_parser_objc_at_property_declaration (parser);
28694 else if (token->keyword == RID_AT_SYNTHESIZE)
28695 cp_parser_objc_at_synthesize_declaration (parser);
28696 else if (token->keyword == RID_AT_DYNAMIC)
28697 cp_parser_objc_at_dynamic_declaration (parser);
28698 else if (token->keyword == RID_ATTRIBUTE
28699 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28700 warning_at (token->location, OPT_Wattributes,
28701 "prefix attributes are ignored for methods");
28702 else
28703 /* Allow for interspersed non-ObjC++ code. */
28704 cp_parser_objc_interstitial_code (parser);
28706 token = cp_lexer_peek_token (parser->lexer);
28709 if (token->type != CPP_EOF)
28710 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28711 else
28712 cp_parser_error (parser, "expected %<@end%>");
28714 objc_finish_implementation ();
28717 /* Parse Objective-C ivars. */
28719 static void
28720 cp_parser_objc_class_ivars (cp_parser* parser)
28722 cp_token *token = cp_lexer_peek_token (parser->lexer);
28724 if (token->type != CPP_OPEN_BRACE)
28725 return; /* No ivars specified. */
28727 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
28728 token = cp_lexer_peek_token (parser->lexer);
28730 while (token->type != CPP_CLOSE_BRACE
28731 && token->keyword != RID_AT_END && token->type != CPP_EOF)
28733 cp_decl_specifier_seq declspecs;
28734 int decl_class_or_enum_p;
28735 tree prefix_attributes;
28737 cp_parser_objc_visibility_spec (parser);
28739 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28740 break;
28742 cp_parser_decl_specifier_seq (parser,
28743 CP_PARSER_FLAGS_OPTIONAL,
28744 &declspecs,
28745 &decl_class_or_enum_p);
28747 /* auto, register, static, extern, mutable. */
28748 if (declspecs.storage_class != sc_none)
28750 cp_parser_error (parser, "invalid type for instance variable");
28751 declspecs.storage_class = sc_none;
28754 /* thread_local. */
28755 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
28757 cp_parser_error (parser, "invalid type for instance variable");
28758 declspecs.locations[ds_thread] = 0;
28761 /* typedef. */
28762 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
28764 cp_parser_error (parser, "invalid type for instance variable");
28765 declspecs.locations[ds_typedef] = 0;
28768 prefix_attributes = declspecs.attributes;
28769 declspecs.attributes = NULL_TREE;
28771 /* Keep going until we hit the `;' at the end of the
28772 declaration. */
28773 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28775 tree width = NULL_TREE, attributes, first_attribute, decl;
28776 cp_declarator *declarator = NULL;
28777 int ctor_dtor_or_conv_p;
28779 /* Check for a (possibly unnamed) bitfield declaration. */
28780 token = cp_lexer_peek_token (parser->lexer);
28781 if (token->type == CPP_COLON)
28782 goto eat_colon;
28784 if (token->type == CPP_NAME
28785 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
28786 == CPP_COLON))
28788 /* Get the name of the bitfield. */
28789 declarator = make_id_declarator (NULL_TREE,
28790 cp_parser_identifier (parser),
28791 sfk_none);
28793 eat_colon:
28794 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
28795 /* Get the width of the bitfield. */
28796 width
28797 = cp_parser_constant_expression (parser);
28799 else
28801 /* Parse the declarator. */
28802 declarator
28803 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28804 &ctor_dtor_or_conv_p,
28805 /*parenthesized_p=*/NULL,
28806 /*member_p=*/false,
28807 /*friend_p=*/false);
28810 /* Look for attributes that apply to the ivar. */
28811 attributes = cp_parser_attributes_opt (parser);
28812 /* Remember which attributes are prefix attributes and
28813 which are not. */
28814 first_attribute = attributes;
28815 /* Combine the attributes. */
28816 attributes = chainon (prefix_attributes, attributes);
28818 if (width)
28819 /* Create the bitfield declaration. */
28820 decl = grokbitfield (declarator, &declspecs,
28821 width,
28822 attributes);
28823 else
28824 decl = grokfield (declarator, &declspecs,
28825 NULL_TREE, /*init_const_expr_p=*/false,
28826 NULL_TREE, attributes);
28828 /* Add the instance variable. */
28829 if (decl != error_mark_node && decl != NULL_TREE)
28830 objc_add_instance_variable (decl);
28832 /* Reset PREFIX_ATTRIBUTES. */
28833 while (attributes && TREE_CHAIN (attributes) != first_attribute)
28834 attributes = TREE_CHAIN (attributes);
28835 if (attributes)
28836 TREE_CHAIN (attributes) = NULL_TREE;
28838 token = cp_lexer_peek_token (parser->lexer);
28840 if (token->type == CPP_COMMA)
28842 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28843 continue;
28845 break;
28848 cp_parser_consume_semicolon_at_end_of_statement (parser);
28849 token = cp_lexer_peek_token (parser->lexer);
28852 if (token->keyword == RID_AT_END)
28853 cp_parser_error (parser, "expected %<}%>");
28855 /* Do not consume the RID_AT_END, so it will be read again as terminating
28856 the @interface of @implementation. */
28857 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
28858 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
28860 /* For historical reasons, we accept an optional semicolon. */
28861 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28862 cp_lexer_consume_token (parser->lexer);
28865 /* Parse an Objective-C protocol declaration. */
28867 static void
28868 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
28870 tree proto, protorefs;
28871 cp_token *tok;
28873 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28874 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
28876 tok = cp_lexer_peek_token (parser->lexer);
28877 error_at (tok->location, "identifier expected after %<@protocol%>");
28878 cp_parser_consume_semicolon_at_end_of_statement (parser);
28879 return;
28882 /* See if we have a forward declaration or a definition. */
28883 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
28885 /* Try a forward declaration first. */
28886 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
28888 while (true)
28890 tree id;
28892 id = cp_parser_identifier (parser);
28893 if (id == error_mark_node)
28894 break;
28896 objc_declare_protocol (id, attributes);
28898 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28899 cp_lexer_consume_token (parser->lexer);
28900 else
28901 break;
28903 cp_parser_consume_semicolon_at_end_of_statement (parser);
28906 /* Ok, we got a full-fledged definition (or at least should). */
28907 else
28909 proto = cp_parser_identifier (parser);
28910 protorefs = cp_parser_objc_protocol_refs_opt (parser);
28911 objc_start_protocol (proto, protorefs, attributes);
28912 cp_parser_objc_method_prototype_list (parser);
28916 /* Parse an Objective-C superclass or category. */
28918 static void
28919 cp_parser_objc_superclass_or_category (cp_parser *parser,
28920 bool iface_p,
28921 tree *super,
28922 tree *categ, bool *is_class_extension)
28924 cp_token *next = cp_lexer_peek_token (parser->lexer);
28926 *super = *categ = NULL_TREE;
28927 *is_class_extension = false;
28928 if (next->type == CPP_COLON)
28930 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
28931 *super = cp_parser_identifier (parser);
28933 else if (next->type == CPP_OPEN_PAREN)
28935 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
28937 /* If there is no category name, and this is an @interface, we
28938 have a class extension. */
28939 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28941 *categ = NULL_TREE;
28942 *is_class_extension = true;
28944 else
28945 *categ = cp_parser_identifier (parser);
28947 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28951 /* Parse an Objective-C class interface. */
28953 static void
28954 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
28956 tree name, super, categ, protos;
28957 bool is_class_extension;
28959 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
28960 name = cp_parser_identifier (parser);
28961 if (name == error_mark_node)
28963 /* It's hard to recover because even if valid @interface stuff
28964 is to follow, we can't compile it (or validate it) if we
28965 don't even know which class it refers to. Let's assume this
28966 was a stray '@interface' token in the stream and skip it.
28968 return;
28970 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
28971 &is_class_extension);
28972 protos = cp_parser_objc_protocol_refs_opt (parser);
28974 /* We have either a class or a category on our hands. */
28975 if (categ || is_class_extension)
28976 objc_start_category_interface (name, categ, protos, attributes);
28977 else
28979 objc_start_class_interface (name, super, protos, attributes);
28980 /* Handle instance variable declarations, if any. */
28981 cp_parser_objc_class_ivars (parser);
28982 objc_continue_interface ();
28985 cp_parser_objc_method_prototype_list (parser);
28988 /* Parse an Objective-C class implementation. */
28990 static void
28991 cp_parser_objc_class_implementation (cp_parser* parser)
28993 tree name, super, categ;
28994 bool is_class_extension;
28996 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
28997 name = cp_parser_identifier (parser);
28998 if (name == error_mark_node)
29000 /* It's hard to recover because even if valid @implementation
29001 stuff is to follow, we can't compile it (or validate it) if
29002 we don't even know which class it refers to. Let's assume
29003 this was a stray '@implementation' token in the stream and
29004 skip it.
29006 return;
29008 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
29009 &is_class_extension);
29011 /* We have either a class or a category on our hands. */
29012 if (categ)
29013 objc_start_category_implementation (name, categ);
29014 else
29016 objc_start_class_implementation (name, super);
29017 /* Handle instance variable declarations, if any. */
29018 cp_parser_objc_class_ivars (parser);
29019 objc_continue_implementation ();
29022 cp_parser_objc_method_definition_list (parser);
29025 /* Consume the @end token and finish off the implementation. */
29027 static void
29028 cp_parser_objc_end_implementation (cp_parser* parser)
29030 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29031 objc_finish_implementation ();
29034 /* Parse an Objective-C declaration. */
29036 static void
29037 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
29039 /* Try to figure out what kind of declaration is present. */
29040 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29042 if (attributes)
29043 switch (kwd->keyword)
29045 case RID_AT_ALIAS:
29046 case RID_AT_CLASS:
29047 case RID_AT_END:
29048 error_at (kwd->location, "attributes may not be specified before"
29049 " the %<@%D%> Objective-C++ keyword",
29050 kwd->u.value);
29051 attributes = NULL;
29052 break;
29053 case RID_AT_IMPLEMENTATION:
29054 warning_at (kwd->location, OPT_Wattributes,
29055 "prefix attributes are ignored before %<@%D%>",
29056 kwd->u.value);
29057 attributes = NULL;
29058 default:
29059 break;
29062 switch (kwd->keyword)
29064 case RID_AT_ALIAS:
29065 cp_parser_objc_alias_declaration (parser);
29066 break;
29067 case RID_AT_CLASS:
29068 cp_parser_objc_class_declaration (parser);
29069 break;
29070 case RID_AT_PROTOCOL:
29071 cp_parser_objc_protocol_declaration (parser, attributes);
29072 break;
29073 case RID_AT_INTERFACE:
29074 cp_parser_objc_class_interface (parser, attributes);
29075 break;
29076 case RID_AT_IMPLEMENTATION:
29077 cp_parser_objc_class_implementation (parser);
29078 break;
29079 case RID_AT_END:
29080 cp_parser_objc_end_implementation (parser);
29081 break;
29082 default:
29083 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29084 kwd->u.value);
29085 cp_parser_skip_to_end_of_block_or_statement (parser);
29089 /* Parse an Objective-C try-catch-finally statement.
29091 objc-try-catch-finally-stmt:
29092 @try compound-statement objc-catch-clause-seq [opt]
29093 objc-finally-clause [opt]
29095 objc-catch-clause-seq:
29096 objc-catch-clause objc-catch-clause-seq [opt]
29098 objc-catch-clause:
29099 @catch ( objc-exception-declaration ) compound-statement
29101 objc-finally-clause:
29102 @finally compound-statement
29104 objc-exception-declaration:
29105 parameter-declaration
29106 '...'
29108 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
29110 Returns NULL_TREE.
29112 PS: This function is identical to c_parser_objc_try_catch_finally_statement
29113 for C. Keep them in sync. */
29115 static tree
29116 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
29118 location_t location;
29119 tree stmt;
29121 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
29122 location = cp_lexer_peek_token (parser->lexer)->location;
29123 objc_maybe_warn_exceptions (location);
29124 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
29125 node, lest it get absorbed into the surrounding block. */
29126 stmt = push_stmt_list ();
29127 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29128 objc_begin_try_stmt (location, pop_stmt_list (stmt));
29130 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
29132 cp_parameter_declarator *parm;
29133 tree parameter_declaration = error_mark_node;
29134 bool seen_open_paren = false;
29136 cp_lexer_consume_token (parser->lexer);
29137 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29138 seen_open_paren = true;
29139 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29141 /* We have "@catch (...)" (where the '...' are literally
29142 what is in the code). Skip the '...'.
29143 parameter_declaration is set to NULL_TREE, and
29144 objc_being_catch_clauses() knows that that means
29145 '...'. */
29146 cp_lexer_consume_token (parser->lexer);
29147 parameter_declaration = NULL_TREE;
29149 else
29151 /* We have "@catch (NSException *exception)" or something
29152 like that. Parse the parameter declaration. */
29153 parm = cp_parser_parameter_declaration (parser, false, NULL);
29154 if (parm == NULL)
29155 parameter_declaration = error_mark_node;
29156 else
29157 parameter_declaration = grokdeclarator (parm->declarator,
29158 &parm->decl_specifiers,
29159 PARM, /*initialized=*/0,
29160 /*attrlist=*/NULL);
29162 if (seen_open_paren)
29163 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29164 else
29166 /* If there was no open parenthesis, we are recovering from
29167 an error, and we are trying to figure out what mistake
29168 the user has made. */
29170 /* If there is an immediate closing parenthesis, the user
29171 probably forgot the opening one (ie, they typed "@catch
29172 NSException *e)". Parse the closing parenthesis and keep
29173 going. */
29174 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29175 cp_lexer_consume_token (parser->lexer);
29177 /* If these is no immediate closing parenthesis, the user
29178 probably doesn't know that parenthesis are required at
29179 all (ie, they typed "@catch NSException *e"). So, just
29180 forget about the closing parenthesis and keep going. */
29182 objc_begin_catch_clause (parameter_declaration);
29183 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29184 objc_finish_catch_clause ();
29186 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
29188 cp_lexer_consume_token (parser->lexer);
29189 location = cp_lexer_peek_token (parser->lexer)->location;
29190 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
29191 node, lest it get absorbed into the surrounding block. */
29192 stmt = push_stmt_list ();
29193 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29194 objc_build_finally_clause (location, pop_stmt_list (stmt));
29197 return objc_finish_try_stmt ();
29200 /* Parse an Objective-C synchronized statement.
29202 objc-synchronized-stmt:
29203 @synchronized ( expression ) compound-statement
29205 Returns NULL_TREE. */
29207 static tree
29208 cp_parser_objc_synchronized_statement (cp_parser *parser)
29210 location_t location;
29211 tree lock, stmt;
29213 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
29215 location = cp_lexer_peek_token (parser->lexer)->location;
29216 objc_maybe_warn_exceptions (location);
29217 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29218 lock = cp_parser_expression (parser);
29219 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29221 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
29222 node, lest it get absorbed into the surrounding block. */
29223 stmt = push_stmt_list ();
29224 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29226 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
29229 /* Parse an Objective-C throw statement.
29231 objc-throw-stmt:
29232 @throw assignment-expression [opt] ;
29234 Returns a constructed '@throw' statement. */
29236 static tree
29237 cp_parser_objc_throw_statement (cp_parser *parser)
29239 tree expr = NULL_TREE;
29240 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29242 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
29244 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29245 expr = cp_parser_expression (parser);
29247 cp_parser_consume_semicolon_at_end_of_statement (parser);
29249 return objc_build_throw_stmt (loc, expr);
29252 /* Parse an Objective-C statement. */
29254 static tree
29255 cp_parser_objc_statement (cp_parser * parser)
29257 /* Try to figure out what kind of declaration is present. */
29258 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29260 switch (kwd->keyword)
29262 case RID_AT_TRY:
29263 return cp_parser_objc_try_catch_finally_statement (parser);
29264 case RID_AT_SYNCHRONIZED:
29265 return cp_parser_objc_synchronized_statement (parser);
29266 case RID_AT_THROW:
29267 return cp_parser_objc_throw_statement (parser);
29268 default:
29269 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29270 kwd->u.value);
29271 cp_parser_skip_to_end_of_block_or_statement (parser);
29274 return error_mark_node;
29277 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
29278 look ahead to see if an objc keyword follows the attributes. This
29279 is to detect the use of prefix attributes on ObjC @interface and
29280 @protocol. */
29282 static bool
29283 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
29285 cp_lexer_save_tokens (parser->lexer);
29286 *attrib = cp_parser_attributes_opt (parser);
29287 gcc_assert (*attrib);
29288 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
29290 cp_lexer_commit_tokens (parser->lexer);
29291 return true;
29293 cp_lexer_rollback_tokens (parser->lexer);
29294 return false;
29297 /* This routine is a minimal replacement for
29298 c_parser_struct_declaration () used when parsing the list of
29299 types/names or ObjC++ properties. For example, when parsing the
29300 code
29302 @property (readonly) int a, b, c;
29304 this function is responsible for parsing "int a, int b, int c" and
29305 returning the declarations as CHAIN of DECLs.
29307 TODO: Share this code with cp_parser_objc_class_ivars. It's very
29308 similar parsing. */
29309 static tree
29310 cp_parser_objc_struct_declaration (cp_parser *parser)
29312 tree decls = NULL_TREE;
29313 cp_decl_specifier_seq declspecs;
29314 int decl_class_or_enum_p;
29315 tree prefix_attributes;
29317 cp_parser_decl_specifier_seq (parser,
29318 CP_PARSER_FLAGS_NONE,
29319 &declspecs,
29320 &decl_class_or_enum_p);
29322 if (declspecs.type == error_mark_node)
29323 return error_mark_node;
29325 /* auto, register, static, extern, mutable. */
29326 if (declspecs.storage_class != sc_none)
29328 cp_parser_error (parser, "invalid type for property");
29329 declspecs.storage_class = sc_none;
29332 /* thread_local. */
29333 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29335 cp_parser_error (parser, "invalid type for property");
29336 declspecs.locations[ds_thread] = 0;
29339 /* typedef. */
29340 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29342 cp_parser_error (parser, "invalid type for property");
29343 declspecs.locations[ds_typedef] = 0;
29346 prefix_attributes = declspecs.attributes;
29347 declspecs.attributes = NULL_TREE;
29349 /* Keep going until we hit the `;' at the end of the declaration. */
29350 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29352 tree attributes, first_attribute, decl;
29353 cp_declarator *declarator;
29354 cp_token *token;
29356 /* Parse the declarator. */
29357 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29358 NULL, NULL, false, false);
29360 /* Look for attributes that apply to the ivar. */
29361 attributes = cp_parser_attributes_opt (parser);
29362 /* Remember which attributes are prefix attributes and
29363 which are not. */
29364 first_attribute = attributes;
29365 /* Combine the attributes. */
29366 attributes = chainon (prefix_attributes, attributes);
29368 decl = grokfield (declarator, &declspecs,
29369 NULL_TREE, /*init_const_expr_p=*/false,
29370 NULL_TREE, attributes);
29372 if (decl == error_mark_node || decl == NULL_TREE)
29373 return error_mark_node;
29375 /* Reset PREFIX_ATTRIBUTES. */
29376 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29377 attributes = TREE_CHAIN (attributes);
29378 if (attributes)
29379 TREE_CHAIN (attributes) = NULL_TREE;
29381 DECL_CHAIN (decl) = decls;
29382 decls = decl;
29384 token = cp_lexer_peek_token (parser->lexer);
29385 if (token->type == CPP_COMMA)
29387 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29388 continue;
29390 else
29391 break;
29393 return decls;
29396 /* Parse an Objective-C @property declaration. The syntax is:
29398 objc-property-declaration:
29399 '@property' objc-property-attributes[opt] struct-declaration ;
29401 objc-property-attributes:
29402 '(' objc-property-attribute-list ')'
29404 objc-property-attribute-list:
29405 objc-property-attribute
29406 objc-property-attribute-list, objc-property-attribute
29408 objc-property-attribute
29409 'getter' = identifier
29410 'setter' = identifier
29411 'readonly'
29412 'readwrite'
29413 'assign'
29414 'retain'
29415 'copy'
29416 'nonatomic'
29418 For example:
29419 @property NSString *name;
29420 @property (readonly) id object;
29421 @property (retain, nonatomic, getter=getTheName) id name;
29422 @property int a, b, c;
29424 PS: This function is identical to
29425 c_parser_objc_at_property_declaration for C. Keep them in sync. */
29426 static void
29427 cp_parser_objc_at_property_declaration (cp_parser *parser)
29429 /* The following variables hold the attributes of the properties as
29430 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
29431 seen. When we see an attribute, we set them to 'true' (if they
29432 are boolean properties) or to the identifier (if they have an
29433 argument, ie, for getter and setter). Note that here we only
29434 parse the list of attributes, check the syntax and accumulate the
29435 attributes that we find. objc_add_property_declaration() will
29436 then process the information. */
29437 bool property_assign = false;
29438 bool property_copy = false;
29439 tree property_getter_ident = NULL_TREE;
29440 bool property_nonatomic = false;
29441 bool property_readonly = false;
29442 bool property_readwrite = false;
29443 bool property_retain = false;
29444 tree property_setter_ident = NULL_TREE;
29446 /* 'properties' is the list of properties that we read. Usually a
29447 single one, but maybe more (eg, in "@property int a, b, c;" there
29448 are three). */
29449 tree properties;
29450 location_t loc;
29452 loc = cp_lexer_peek_token (parser->lexer)->location;
29454 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
29456 /* Parse the optional attribute list... */
29457 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29459 /* Eat the '('. */
29460 cp_lexer_consume_token (parser->lexer);
29462 while (true)
29464 bool syntax_error = false;
29465 cp_token *token = cp_lexer_peek_token (parser->lexer);
29466 enum rid keyword;
29468 if (token->type != CPP_NAME)
29470 cp_parser_error (parser, "expected identifier");
29471 break;
29473 keyword = C_RID_CODE (token->u.value);
29474 cp_lexer_consume_token (parser->lexer);
29475 switch (keyword)
29477 case RID_ASSIGN: property_assign = true; break;
29478 case RID_COPY: property_copy = true; break;
29479 case RID_NONATOMIC: property_nonatomic = true; break;
29480 case RID_READONLY: property_readonly = true; break;
29481 case RID_READWRITE: property_readwrite = true; break;
29482 case RID_RETAIN: property_retain = true; break;
29484 case RID_GETTER:
29485 case RID_SETTER:
29486 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29488 if (keyword == RID_GETTER)
29489 cp_parser_error (parser,
29490 "missing %<=%> (after %<getter%> attribute)");
29491 else
29492 cp_parser_error (parser,
29493 "missing %<=%> (after %<setter%> attribute)");
29494 syntax_error = true;
29495 break;
29497 cp_lexer_consume_token (parser->lexer); /* eat the = */
29498 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
29500 cp_parser_error (parser, "expected identifier");
29501 syntax_error = true;
29502 break;
29504 if (keyword == RID_SETTER)
29506 if (property_setter_ident != NULL_TREE)
29508 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
29509 cp_lexer_consume_token (parser->lexer);
29511 else
29512 property_setter_ident = cp_parser_objc_selector (parser);
29513 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29514 cp_parser_error (parser, "setter name must terminate with %<:%>");
29515 else
29516 cp_lexer_consume_token (parser->lexer);
29518 else
29520 if (property_getter_ident != NULL_TREE)
29522 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
29523 cp_lexer_consume_token (parser->lexer);
29525 else
29526 property_getter_ident = cp_parser_objc_selector (parser);
29528 break;
29529 default:
29530 cp_parser_error (parser, "unknown property attribute");
29531 syntax_error = true;
29532 break;
29535 if (syntax_error)
29536 break;
29538 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29539 cp_lexer_consume_token (parser->lexer);
29540 else
29541 break;
29544 /* FIXME: "@property (setter, assign);" will generate a spurious
29545 "error: expected ‘)’ before ‘,’ token". This is because
29546 cp_parser_require, unlike the C counterpart, will produce an
29547 error even if we are in error recovery. */
29548 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29550 cp_parser_skip_to_closing_parenthesis (parser,
29551 /*recovering=*/true,
29552 /*or_comma=*/false,
29553 /*consume_paren=*/true);
29557 /* ... and the property declaration(s). */
29558 properties = cp_parser_objc_struct_declaration (parser);
29560 if (properties == error_mark_node)
29562 cp_parser_skip_to_end_of_statement (parser);
29563 /* If the next token is now a `;', consume it. */
29564 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29565 cp_lexer_consume_token (parser->lexer);
29566 return;
29569 if (properties == NULL_TREE)
29570 cp_parser_error (parser, "expected identifier");
29571 else
29573 /* Comma-separated properties are chained together in
29574 reverse order; add them one by one. */
29575 properties = nreverse (properties);
29577 for (; properties; properties = TREE_CHAIN (properties))
29578 objc_add_property_declaration (loc, copy_node (properties),
29579 property_readonly, property_readwrite,
29580 property_assign, property_retain,
29581 property_copy, property_nonatomic,
29582 property_getter_ident, property_setter_ident);
29585 cp_parser_consume_semicolon_at_end_of_statement (parser);
29588 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
29590 objc-synthesize-declaration:
29591 @synthesize objc-synthesize-identifier-list ;
29593 objc-synthesize-identifier-list:
29594 objc-synthesize-identifier
29595 objc-synthesize-identifier-list, objc-synthesize-identifier
29597 objc-synthesize-identifier
29598 identifier
29599 identifier = identifier
29601 For example:
29602 @synthesize MyProperty;
29603 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
29605 PS: This function is identical to c_parser_objc_at_synthesize_declaration
29606 for C. Keep them in sync.
29608 static void
29609 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
29611 tree list = NULL_TREE;
29612 location_t loc;
29613 loc = cp_lexer_peek_token (parser->lexer)->location;
29615 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
29616 while (true)
29618 tree property, ivar;
29619 property = cp_parser_identifier (parser);
29620 if (property == error_mark_node)
29622 cp_parser_consume_semicolon_at_end_of_statement (parser);
29623 return;
29625 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
29627 cp_lexer_consume_token (parser->lexer);
29628 ivar = cp_parser_identifier (parser);
29629 if (ivar == error_mark_node)
29631 cp_parser_consume_semicolon_at_end_of_statement (parser);
29632 return;
29635 else
29636 ivar = NULL_TREE;
29637 list = chainon (list, build_tree_list (ivar, property));
29638 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29639 cp_lexer_consume_token (parser->lexer);
29640 else
29641 break;
29643 cp_parser_consume_semicolon_at_end_of_statement (parser);
29644 objc_add_synthesize_declaration (loc, list);
29647 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
29649 objc-dynamic-declaration:
29650 @dynamic identifier-list ;
29652 For example:
29653 @dynamic MyProperty;
29654 @dynamic MyProperty, AnotherProperty;
29656 PS: This function is identical to c_parser_objc_at_dynamic_declaration
29657 for C. Keep them in sync.
29659 static void
29660 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
29662 tree list = NULL_TREE;
29663 location_t loc;
29664 loc = cp_lexer_peek_token (parser->lexer)->location;
29666 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
29667 while (true)
29669 tree property;
29670 property = cp_parser_identifier (parser);
29671 if (property == error_mark_node)
29673 cp_parser_consume_semicolon_at_end_of_statement (parser);
29674 return;
29676 list = chainon (list, build_tree_list (NULL, property));
29677 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29678 cp_lexer_consume_token (parser->lexer);
29679 else
29680 break;
29682 cp_parser_consume_semicolon_at_end_of_statement (parser);
29683 objc_add_dynamic_declaration (loc, list);
29687 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
29689 /* Returns name of the next clause.
29690 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
29691 the token is not consumed. Otherwise appropriate pragma_omp_clause is
29692 returned and the token is consumed. */
29694 static pragma_omp_clause
29695 cp_parser_omp_clause_name (cp_parser *parser)
29697 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
29699 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
29700 result = PRAGMA_OACC_CLAUSE_AUTO;
29701 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
29702 result = PRAGMA_OMP_CLAUSE_IF;
29703 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
29704 result = PRAGMA_OMP_CLAUSE_DEFAULT;
29705 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
29706 result = PRAGMA_OACC_CLAUSE_DELETE;
29707 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
29708 result = PRAGMA_OMP_CLAUSE_PRIVATE;
29709 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29710 result = PRAGMA_OMP_CLAUSE_FOR;
29711 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29713 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29714 const char *p = IDENTIFIER_POINTER (id);
29716 switch (p[0])
29718 case 'a':
29719 if (!strcmp ("aligned", p))
29720 result = PRAGMA_OMP_CLAUSE_ALIGNED;
29721 else if (!strcmp ("async", p))
29722 result = PRAGMA_OACC_CLAUSE_ASYNC;
29723 break;
29724 case 'c':
29725 if (!strcmp ("collapse", p))
29726 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
29727 else if (!strcmp ("copy", p))
29728 result = PRAGMA_OACC_CLAUSE_COPY;
29729 else if (!strcmp ("copyin", p))
29730 result = PRAGMA_OMP_CLAUSE_COPYIN;
29731 else if (!strcmp ("copyout", p))
29732 result = PRAGMA_OACC_CLAUSE_COPYOUT;
29733 else if (!strcmp ("copyprivate", p))
29734 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
29735 else if (!strcmp ("create", p))
29736 result = PRAGMA_OACC_CLAUSE_CREATE;
29737 break;
29738 case 'd':
29739 if (!strcmp ("defaultmap", p))
29740 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
29741 else if (!strcmp ("depend", p))
29742 result = PRAGMA_OMP_CLAUSE_DEPEND;
29743 else if (!strcmp ("device", p))
29744 result = PRAGMA_OMP_CLAUSE_DEVICE;
29745 else if (!strcmp ("deviceptr", p))
29746 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
29747 else if (!strcmp ("device_resident", p))
29748 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
29749 else if (!strcmp ("dist_schedule", p))
29750 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
29751 break;
29752 case 'f':
29753 if (!strcmp ("final", p))
29754 result = PRAGMA_OMP_CLAUSE_FINAL;
29755 else if (!strcmp ("firstprivate", p))
29756 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
29757 else if (!strcmp ("from", p))
29758 result = PRAGMA_OMP_CLAUSE_FROM;
29759 break;
29760 case 'g':
29761 if (!strcmp ("gang", p))
29762 result = PRAGMA_OACC_CLAUSE_GANG;
29763 else if (!strcmp ("grainsize", p))
29764 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
29765 break;
29766 case 'h':
29767 if (!strcmp ("hint", p))
29768 result = PRAGMA_OMP_CLAUSE_HINT;
29769 else if (!strcmp ("host", p))
29770 result = PRAGMA_OACC_CLAUSE_HOST;
29771 break;
29772 case 'i':
29773 if (!strcmp ("inbranch", p))
29774 result = PRAGMA_OMP_CLAUSE_INBRANCH;
29775 else if (!strcmp ("independent", p))
29776 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
29777 else if (!strcmp ("is_device_ptr", p))
29778 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
29779 break;
29780 case 'l':
29781 if (!strcmp ("lastprivate", p))
29782 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
29783 else if (!strcmp ("linear", p))
29784 result = PRAGMA_OMP_CLAUSE_LINEAR;
29785 else if (!strcmp ("link", p))
29786 result = PRAGMA_OMP_CLAUSE_LINK;
29787 break;
29788 case 'm':
29789 if (!strcmp ("map", p))
29790 result = PRAGMA_OMP_CLAUSE_MAP;
29791 else if (!strcmp ("mergeable", p))
29792 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
29793 else if (flag_cilkplus && !strcmp ("mask", p))
29794 result = PRAGMA_CILK_CLAUSE_MASK;
29795 break;
29796 case 'n':
29797 if (!strcmp ("nogroup", p))
29798 result = PRAGMA_OMP_CLAUSE_NOGROUP;
29799 else if (!strcmp ("notinbranch", p))
29800 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
29801 else if (!strcmp ("nowait", p))
29802 result = PRAGMA_OMP_CLAUSE_NOWAIT;
29803 else if (flag_cilkplus && !strcmp ("nomask", p))
29804 result = PRAGMA_CILK_CLAUSE_NOMASK;
29805 else if (!strcmp ("num_gangs", p))
29806 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
29807 else if (!strcmp ("num_tasks", p))
29808 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
29809 else if (!strcmp ("num_teams", p))
29810 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
29811 else if (!strcmp ("num_threads", p))
29812 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
29813 else if (!strcmp ("num_workers", p))
29814 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
29815 break;
29816 case 'o':
29817 if (!strcmp ("ordered", p))
29818 result = PRAGMA_OMP_CLAUSE_ORDERED;
29819 break;
29820 case 'p':
29821 if (!strcmp ("parallel", p))
29822 result = PRAGMA_OMP_CLAUSE_PARALLEL;
29823 else if (!strcmp ("present", p))
29824 result = PRAGMA_OACC_CLAUSE_PRESENT;
29825 else if (!strcmp ("present_or_copy", p)
29826 || !strcmp ("pcopy", p))
29827 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
29828 else if (!strcmp ("present_or_copyin", p)
29829 || !strcmp ("pcopyin", p))
29830 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
29831 else if (!strcmp ("present_or_copyout", p)
29832 || !strcmp ("pcopyout", p))
29833 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
29834 else if (!strcmp ("present_or_create", p)
29835 || !strcmp ("pcreate", p))
29836 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
29837 else if (!strcmp ("priority", p))
29838 result = PRAGMA_OMP_CLAUSE_PRIORITY;
29839 else if (!strcmp ("proc_bind", p))
29840 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
29841 break;
29842 case 'r':
29843 if (!strcmp ("reduction", p))
29844 result = PRAGMA_OMP_CLAUSE_REDUCTION;
29845 break;
29846 case 's':
29847 if (!strcmp ("safelen", p))
29848 result = PRAGMA_OMP_CLAUSE_SAFELEN;
29849 else if (!strcmp ("schedule", p))
29850 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
29851 else if (!strcmp ("sections", p))
29852 result = PRAGMA_OMP_CLAUSE_SECTIONS;
29853 else if (!strcmp ("self", p))
29854 result = PRAGMA_OACC_CLAUSE_SELF;
29855 else if (!strcmp ("seq", p))
29856 result = PRAGMA_OACC_CLAUSE_SEQ;
29857 else if (!strcmp ("shared", p))
29858 result = PRAGMA_OMP_CLAUSE_SHARED;
29859 else if (!strcmp ("simd", p))
29860 result = PRAGMA_OMP_CLAUSE_SIMD;
29861 else if (!strcmp ("simdlen", p))
29862 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
29863 break;
29864 case 't':
29865 if (!strcmp ("taskgroup", p))
29866 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
29867 else if (!strcmp ("thread_limit", p))
29868 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
29869 else if (!strcmp ("threads", p))
29870 result = PRAGMA_OMP_CLAUSE_THREADS;
29871 else if (!strcmp ("tile", p))
29872 result = PRAGMA_OACC_CLAUSE_TILE;
29873 else if (!strcmp ("to", p))
29874 result = PRAGMA_OMP_CLAUSE_TO;
29875 break;
29876 case 'u':
29877 if (!strcmp ("uniform", p))
29878 result = PRAGMA_OMP_CLAUSE_UNIFORM;
29879 else if (!strcmp ("untied", p))
29880 result = PRAGMA_OMP_CLAUSE_UNTIED;
29881 else if (!strcmp ("use_device", p))
29882 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
29883 else if (!strcmp ("use_device_ptr", p))
29884 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
29885 break;
29886 case 'v':
29887 if (!strcmp ("vector", p))
29888 result = PRAGMA_OACC_CLAUSE_VECTOR;
29889 else if (!strcmp ("vector_length", p))
29890 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
29891 else if (flag_cilkplus && !strcmp ("vectorlength", p))
29892 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
29893 break;
29894 case 'w':
29895 if (!strcmp ("wait", p))
29896 result = PRAGMA_OACC_CLAUSE_WAIT;
29897 else if (!strcmp ("worker", p))
29898 result = PRAGMA_OACC_CLAUSE_WORKER;
29899 break;
29903 if (result != PRAGMA_OMP_CLAUSE_NONE)
29904 cp_lexer_consume_token (parser->lexer);
29906 return result;
29909 /* Validate that a clause of the given type does not already exist. */
29911 static void
29912 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
29913 const char *name, location_t location)
29915 tree c;
29917 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29918 if (OMP_CLAUSE_CODE (c) == code)
29920 error_at (location, "too many %qs clauses", name);
29921 break;
29925 /* OpenMP 2.5:
29926 variable-list:
29927 identifier
29928 variable-list , identifier
29930 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
29931 colon). An opening parenthesis will have been consumed by the caller.
29933 If KIND is nonzero, create the appropriate node and install the decl
29934 in OMP_CLAUSE_DECL and add the node to the head of the list.
29936 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
29937 return the list created.
29939 COLON can be NULL if only closing parenthesis should end the list,
29940 or pointer to bool which will receive false if the list is terminated
29941 by closing parenthesis or true if the list is terminated by colon. */
29943 static tree
29944 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
29945 tree list, bool *colon)
29947 cp_token *token;
29948 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
29949 if (colon)
29951 parser->colon_corrects_to_scope_p = false;
29952 *colon = false;
29954 while (1)
29956 tree name, decl;
29958 token = cp_lexer_peek_token (parser->lexer);
29959 if (kind != 0
29960 && current_class_ptr
29961 && cp_parser_is_keyword (token, RID_THIS))
29963 decl = finish_this_expr ();
29964 if (TREE_CODE (decl) == NON_LVALUE_EXPR
29965 || CONVERT_EXPR_P (decl))
29966 decl = TREE_OPERAND (decl, 0);
29967 cp_lexer_consume_token (parser->lexer);
29969 else
29971 name = cp_parser_id_expression (parser, /*template_p=*/false,
29972 /*check_dependency_p=*/true,
29973 /*template_p=*/NULL,
29974 /*declarator_p=*/false,
29975 /*optional_p=*/false);
29976 if (name == error_mark_node)
29977 goto skip_comma;
29979 decl = cp_parser_lookup_name_simple (parser, name, token->location);
29980 if (decl == error_mark_node)
29981 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
29982 token->location);
29984 if (decl == error_mark_node)
29986 else if (kind != 0)
29988 switch (kind)
29990 case OMP_CLAUSE__CACHE_:
29991 /* The OpenACC cache directive explicitly only allows "array
29992 elements or subarrays". */
29993 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
29995 error_at (token->location, "expected %<[%>");
29996 decl = error_mark_node;
29997 break;
29999 /* FALLTHROUGH. */
30000 case OMP_CLAUSE_MAP:
30001 case OMP_CLAUSE_FROM:
30002 case OMP_CLAUSE_TO:
30003 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
30005 location_t loc
30006 = cp_lexer_peek_token (parser->lexer)->location;
30007 cp_id_kind idk = CP_ID_KIND_NONE;
30008 cp_lexer_consume_token (parser->lexer);
30009 decl = convert_from_reference (decl);
30010 decl
30011 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
30012 decl, false,
30013 &idk, loc);
30015 /* FALLTHROUGH. */
30016 case OMP_CLAUSE_DEPEND:
30017 case OMP_CLAUSE_REDUCTION:
30018 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
30020 tree low_bound = NULL_TREE, length = NULL_TREE;
30022 parser->colon_corrects_to_scope_p = false;
30023 cp_lexer_consume_token (parser->lexer);
30024 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30025 low_bound = cp_parser_expression (parser);
30026 if (!colon)
30027 parser->colon_corrects_to_scope_p
30028 = saved_colon_corrects_to_scope_p;
30029 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
30030 length = integer_one_node;
30031 else
30033 /* Look for `:'. */
30034 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30035 goto skip_comma;
30036 if (!cp_lexer_next_token_is (parser->lexer,
30037 CPP_CLOSE_SQUARE))
30038 length = cp_parser_expression (parser);
30040 /* Look for the closing `]'. */
30041 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
30042 RT_CLOSE_SQUARE))
30043 goto skip_comma;
30045 decl = tree_cons (low_bound, length, decl);
30047 break;
30048 default:
30049 break;
30052 tree u = build_omp_clause (token->location, kind);
30053 OMP_CLAUSE_DECL (u) = decl;
30054 OMP_CLAUSE_CHAIN (u) = list;
30055 list = u;
30057 else
30058 list = tree_cons (decl, NULL_TREE, list);
30060 get_comma:
30061 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
30062 break;
30063 cp_lexer_consume_token (parser->lexer);
30066 if (colon)
30067 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30069 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30071 *colon = true;
30072 cp_parser_require (parser, CPP_COLON, RT_COLON);
30073 return list;
30076 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30078 int ending;
30080 /* Try to resync to an unnested comma. Copied from
30081 cp_parser_parenthesized_expression_list. */
30082 skip_comma:
30083 if (colon)
30084 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30085 ending = cp_parser_skip_to_closing_parenthesis (parser,
30086 /*recovering=*/true,
30087 /*or_comma=*/true,
30088 /*consume_paren=*/true);
30089 if (ending < 0)
30090 goto get_comma;
30093 return list;
30096 /* Similarly, but expect leading and trailing parenthesis. This is a very
30097 common case for omp clauses. */
30099 static tree
30100 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
30102 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30103 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
30104 return list;
30107 /* OpenACC 2.0:
30108 copy ( variable-list )
30109 copyin ( variable-list )
30110 copyout ( variable-list )
30111 create ( variable-list )
30112 delete ( variable-list )
30113 present ( variable-list )
30114 present_or_copy ( variable-list )
30115 pcopy ( variable-list )
30116 present_or_copyin ( variable-list )
30117 pcopyin ( variable-list )
30118 present_or_copyout ( variable-list )
30119 pcopyout ( variable-list )
30120 present_or_create ( variable-list )
30121 pcreate ( variable-list ) */
30123 static tree
30124 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
30125 tree list)
30127 enum gomp_map_kind kind;
30128 switch (c_kind)
30130 case PRAGMA_OACC_CLAUSE_COPY:
30131 kind = GOMP_MAP_FORCE_TOFROM;
30132 break;
30133 case PRAGMA_OACC_CLAUSE_COPYIN:
30134 kind = GOMP_MAP_FORCE_TO;
30135 break;
30136 case PRAGMA_OACC_CLAUSE_COPYOUT:
30137 kind = GOMP_MAP_FORCE_FROM;
30138 break;
30139 case PRAGMA_OACC_CLAUSE_CREATE:
30140 kind = GOMP_MAP_FORCE_ALLOC;
30141 break;
30142 case PRAGMA_OACC_CLAUSE_DELETE:
30143 kind = GOMP_MAP_DELETE;
30144 break;
30145 case PRAGMA_OACC_CLAUSE_DEVICE:
30146 kind = GOMP_MAP_FORCE_TO;
30147 break;
30148 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
30149 kind = GOMP_MAP_DEVICE_RESIDENT;
30150 break;
30151 case PRAGMA_OACC_CLAUSE_HOST:
30152 case PRAGMA_OACC_CLAUSE_SELF:
30153 kind = GOMP_MAP_FORCE_FROM;
30154 break;
30155 case PRAGMA_OACC_CLAUSE_LINK:
30156 kind = GOMP_MAP_LINK;
30157 break;
30158 case PRAGMA_OACC_CLAUSE_PRESENT:
30159 kind = GOMP_MAP_FORCE_PRESENT;
30160 break;
30161 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
30162 kind = GOMP_MAP_TOFROM;
30163 break;
30164 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
30165 kind = GOMP_MAP_TO;
30166 break;
30167 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
30168 kind = GOMP_MAP_FROM;
30169 break;
30170 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
30171 kind = GOMP_MAP_ALLOC;
30172 break;
30173 default:
30174 gcc_unreachable ();
30176 tree nl, c;
30177 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
30179 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
30180 OMP_CLAUSE_SET_MAP_KIND (c, kind);
30182 return nl;
30185 /* OpenACC 2.0:
30186 deviceptr ( variable-list ) */
30188 static tree
30189 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
30191 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30192 tree vars, t;
30194 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
30195 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
30196 variable-list must only allow for pointer variables. */
30197 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30198 for (t = vars; t; t = TREE_CHAIN (t))
30200 tree v = TREE_PURPOSE (t);
30201 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
30202 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
30203 OMP_CLAUSE_DECL (u) = v;
30204 OMP_CLAUSE_CHAIN (u) = list;
30205 list = u;
30208 return list;
30211 /* OpenACC 2.0:
30212 auto
30213 independent
30214 nohost
30215 seq */
30217 static tree
30218 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
30219 enum omp_clause_code code,
30220 tree list, location_t location)
30222 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
30223 tree c = build_omp_clause (location, code);
30224 OMP_CLAUSE_CHAIN (c) = list;
30225 return c;
30228 /* OpenACC:
30229 num_gangs ( expression )
30230 num_workers ( expression )
30231 vector_length ( expression ) */
30233 static tree
30234 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
30235 const char *str, tree list)
30237 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30239 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30240 return list;
30242 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
30244 if (t == error_mark_node
30245 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30247 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30248 /*or_comma=*/false,
30249 /*consume_paren=*/true);
30250 return list;
30253 check_no_duplicate_clause (list, code, str, loc);
30255 tree c = build_omp_clause (loc, code);
30256 OMP_CLAUSE_OPERAND (c, 0) = t;
30257 OMP_CLAUSE_CHAIN (c) = list;
30258 return c;
30261 /* OpenACC:
30263 gang [( gang-arg-list )]
30264 worker [( [num:] int-expr )]
30265 vector [( [length:] int-expr )]
30267 where gang-arg is one of:
30269 [num:] int-expr
30270 static: size-expr
30272 and size-expr may be:
30275 int-expr
30278 static tree
30279 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
30280 const char *str, tree list)
30282 const char *id = "num";
30283 cp_lexer *lexer = parser->lexer;
30284 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
30285 location_t loc = cp_lexer_peek_token (lexer)->location;
30287 if (kind == OMP_CLAUSE_VECTOR)
30288 id = "length";
30290 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
30292 cp_lexer_consume_token (lexer);
30296 cp_token *next = cp_lexer_peek_token (lexer);
30297 int idx = 0;
30299 /* Gang static argument. */
30300 if (kind == OMP_CLAUSE_GANG
30301 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
30303 cp_lexer_consume_token (lexer);
30305 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30306 goto cleanup_error;
30308 idx = 1;
30309 if (ops[idx] != NULL)
30311 cp_parser_error (parser, "too many %<static%> arguments");
30312 goto cleanup_error;
30315 /* Check for the '*' argument. */
30316 if (cp_lexer_next_token_is (lexer, CPP_MULT)
30317 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30318 || cp_lexer_nth_token_is (parser->lexer, 2,
30319 CPP_CLOSE_PAREN)))
30321 cp_lexer_consume_token (lexer);
30322 ops[idx] = integer_minus_one_node;
30324 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
30326 cp_lexer_consume_token (lexer);
30327 continue;
30329 else break;
30332 /* Worker num: argument and vector length: arguments. */
30333 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
30334 && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
30335 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
30337 cp_lexer_consume_token (lexer); /* id */
30338 cp_lexer_consume_token (lexer); /* ':' */
30341 /* Now collect the actual argument. */
30342 if (ops[idx] != NULL_TREE)
30344 cp_parser_error (parser, "unexpected argument");
30345 goto cleanup_error;
30348 tree expr = cp_parser_assignment_expression (parser, NULL, false,
30349 false);
30350 if (expr == error_mark_node)
30351 goto cleanup_error;
30353 mark_exp_read (expr);
30354 ops[idx] = expr;
30356 if (kind == OMP_CLAUSE_GANG
30357 && cp_lexer_next_token_is (lexer, CPP_COMMA))
30359 cp_lexer_consume_token (lexer);
30360 continue;
30362 break;
30364 while (1);
30366 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30367 goto cleanup_error;
30370 check_no_duplicate_clause (list, kind, str, loc);
30372 c = build_omp_clause (loc, kind);
30374 if (ops[1])
30375 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
30377 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
30378 OMP_CLAUSE_CHAIN (c) = list;
30380 return c;
30382 cleanup_error:
30383 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
30384 return list;
30387 /* OpenACC 2.0:
30388 tile ( size-expr-list ) */
30390 static tree
30391 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
30393 tree c, expr = error_mark_node;
30394 tree tile = NULL_TREE;
30396 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
30398 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30399 return list;
30403 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
30404 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30405 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
30407 cp_lexer_consume_token (parser->lexer);
30408 expr = integer_minus_one_node;
30410 else
30411 expr = cp_parser_assignment_expression (parser, NULL, false, false);
30413 if (expr == error_mark_node)
30414 return list;
30416 tile = tree_cons (NULL_TREE, expr, tile);
30418 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30419 cp_lexer_consume_token (parser->lexer);
30421 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
30423 /* Consume the trailing ')'. */
30424 cp_lexer_consume_token (parser->lexer);
30426 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
30427 tile = nreverse (tile);
30428 OMP_CLAUSE_TILE_LIST (c) = tile;
30429 OMP_CLAUSE_CHAIN (c) = list;
30430 return c;
30433 /* OpenACC 2.0
30434 Parse wait clause or directive parameters. */
30436 static tree
30437 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
30439 vec<tree, va_gc> *args;
30440 tree t, args_tree;
30442 args = cp_parser_parenthesized_expression_list (parser, non_attr,
30443 /*cast_p=*/false,
30444 /*allow_expansion_p=*/true,
30445 /*non_constant_p=*/NULL);
30447 if (args == NULL || args->length () == 0)
30449 cp_parser_error (parser, "expected integer expression before ')'");
30450 if (args != NULL)
30451 release_tree_vector (args);
30452 return list;
30455 args_tree = build_tree_list_vec (args);
30457 release_tree_vector (args);
30459 for (t = args_tree; t; t = TREE_CHAIN (t))
30461 tree targ = TREE_VALUE (t);
30463 if (targ != error_mark_node)
30465 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
30466 error ("%<wait%> expression must be integral");
30467 else
30469 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
30471 mark_rvalue_use (targ);
30472 OMP_CLAUSE_DECL (c) = targ;
30473 OMP_CLAUSE_CHAIN (c) = list;
30474 list = c;
30479 return list;
30482 /* OpenACC:
30483 wait ( int-expr-list ) */
30485 static tree
30486 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
30488 location_t location = cp_lexer_peek_token (parser->lexer)->location;
30490 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
30491 return list;
30493 list = cp_parser_oacc_wait_list (parser, location, list);
30495 return list;
30498 /* OpenMP 3.0:
30499 collapse ( constant-expression ) */
30501 static tree
30502 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
30504 tree c, num;
30505 location_t loc;
30506 HOST_WIDE_INT n;
30508 loc = cp_lexer_peek_token (parser->lexer)->location;
30509 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30510 return list;
30512 num = cp_parser_constant_expression (parser);
30514 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30515 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30516 /*or_comma=*/false,
30517 /*consume_paren=*/true);
30519 if (num == error_mark_node)
30520 return list;
30521 num = fold_non_dependent_expr (num);
30522 if (!tree_fits_shwi_p (num)
30523 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
30524 || (n = tree_to_shwi (num)) <= 0
30525 || (int) n != n)
30527 error_at (loc, "collapse argument needs positive constant integer expression");
30528 return list;
30531 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
30532 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
30533 OMP_CLAUSE_CHAIN (c) = list;
30534 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
30536 return c;
30539 /* OpenMP 2.5:
30540 default ( shared | none )
30542 OpenACC 2.0
30543 default (none) */
30545 static tree
30546 cp_parser_omp_clause_default (cp_parser *parser, tree list,
30547 location_t location, bool is_oacc)
30549 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
30550 tree c;
30552 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30553 return list;
30554 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30556 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30557 const char *p = IDENTIFIER_POINTER (id);
30559 switch (p[0])
30561 case 'n':
30562 if (strcmp ("none", p) != 0)
30563 goto invalid_kind;
30564 kind = OMP_CLAUSE_DEFAULT_NONE;
30565 break;
30567 case 's':
30568 if (strcmp ("shared", p) != 0 || is_oacc)
30569 goto invalid_kind;
30570 kind = OMP_CLAUSE_DEFAULT_SHARED;
30571 break;
30573 default:
30574 goto invalid_kind;
30577 cp_lexer_consume_token (parser->lexer);
30579 else
30581 invalid_kind:
30582 if (is_oacc)
30583 cp_parser_error (parser, "expected %<none%>");
30584 else
30585 cp_parser_error (parser, "expected %<none%> or %<shared%>");
30588 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30589 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30590 /*or_comma=*/false,
30591 /*consume_paren=*/true);
30593 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
30594 return list;
30596 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
30597 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
30598 OMP_CLAUSE_CHAIN (c) = list;
30599 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
30601 return c;
30604 /* OpenMP 3.1:
30605 final ( expression ) */
30607 static tree
30608 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
30610 tree t, c;
30612 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30613 return list;
30615 t = cp_parser_condition (parser);
30617 if (t == error_mark_node
30618 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30619 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30620 /*or_comma=*/false,
30621 /*consume_paren=*/true);
30623 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
30625 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
30626 OMP_CLAUSE_FINAL_EXPR (c) = t;
30627 OMP_CLAUSE_CHAIN (c) = list;
30629 return c;
30632 /* OpenMP 2.5:
30633 if ( expression )
30635 OpenMP 4.5:
30636 if ( directive-name-modifier : expression )
30638 directive-name-modifier:
30639 parallel | task | taskloop | target data | target | target update
30640 | target enter data | target exit data */
30642 static tree
30643 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
30644 bool is_omp)
30646 tree t, c;
30647 enum tree_code if_modifier = ERROR_MARK;
30649 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30650 return list;
30652 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30654 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30655 const char *p = IDENTIFIER_POINTER (id);
30656 int n = 2;
30658 if (strcmp ("parallel", p) == 0)
30659 if_modifier = OMP_PARALLEL;
30660 else if (strcmp ("task", p) == 0)
30661 if_modifier = OMP_TASK;
30662 else if (strcmp ("taskloop", p) == 0)
30663 if_modifier = OMP_TASKLOOP;
30664 else if (strcmp ("target", p) == 0)
30666 if_modifier = OMP_TARGET;
30667 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
30669 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
30670 p = IDENTIFIER_POINTER (id);
30671 if (strcmp ("data", p) == 0)
30672 if_modifier = OMP_TARGET_DATA;
30673 else if (strcmp ("update", p) == 0)
30674 if_modifier = OMP_TARGET_UPDATE;
30675 else if (strcmp ("enter", p) == 0)
30676 if_modifier = OMP_TARGET_ENTER_DATA;
30677 else if (strcmp ("exit", p) == 0)
30678 if_modifier = OMP_TARGET_EXIT_DATA;
30679 if (if_modifier != OMP_TARGET)
30680 n = 3;
30681 else
30683 location_t loc
30684 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
30685 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
30686 "or %<exit%>");
30687 if_modifier = ERROR_MARK;
30689 if (if_modifier == OMP_TARGET_ENTER_DATA
30690 || if_modifier == OMP_TARGET_EXIT_DATA)
30692 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
30694 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
30695 p = IDENTIFIER_POINTER (id);
30696 if (strcmp ("data", p) == 0)
30697 n = 4;
30699 if (n != 4)
30701 location_t loc
30702 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
30703 error_at (loc, "expected %<data%>");
30704 if_modifier = ERROR_MARK;
30709 if (if_modifier != ERROR_MARK)
30711 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
30713 while (n-- > 0)
30714 cp_lexer_consume_token (parser->lexer);
30716 else
30718 if (n > 2)
30720 location_t loc
30721 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
30722 error_at (loc, "expected %<:%>");
30724 if_modifier = ERROR_MARK;
30729 t = cp_parser_condition (parser);
30731 if (t == error_mark_node
30732 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30733 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30734 /*or_comma=*/false,
30735 /*consume_paren=*/true);
30737 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
30738 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
30740 if (if_modifier != ERROR_MARK
30741 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30743 const char *p = NULL;
30744 switch (if_modifier)
30746 case OMP_PARALLEL: p = "parallel"; break;
30747 case OMP_TASK: p = "task"; break;
30748 case OMP_TASKLOOP: p = "taskloop"; break;
30749 case OMP_TARGET_DATA: p = "target data"; break;
30750 case OMP_TARGET: p = "target"; break;
30751 case OMP_TARGET_UPDATE: p = "target update"; break;
30752 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
30753 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
30754 default: gcc_unreachable ();
30756 error_at (location, "too many %<if%> clauses with %qs modifier",
30758 return list;
30760 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30762 if (!is_omp)
30763 error_at (location, "too many %<if%> clauses");
30764 else
30765 error_at (location, "too many %<if%> clauses without modifier");
30766 return list;
30768 else if (if_modifier == ERROR_MARK
30769 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
30771 error_at (location, "if any %<if%> clause has modifier, then all "
30772 "%<if%> clauses have to use modifier");
30773 return list;
30777 c = build_omp_clause (location, OMP_CLAUSE_IF);
30778 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
30779 OMP_CLAUSE_IF_EXPR (c) = t;
30780 OMP_CLAUSE_CHAIN (c) = list;
30782 return c;
30785 /* OpenMP 3.1:
30786 mergeable */
30788 static tree
30789 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
30790 tree list, location_t location)
30792 tree c;
30794 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
30795 location);
30797 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
30798 OMP_CLAUSE_CHAIN (c) = list;
30799 return c;
30802 /* OpenMP 2.5:
30803 nowait */
30805 static tree
30806 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
30807 tree list, location_t location)
30809 tree c;
30811 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
30813 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
30814 OMP_CLAUSE_CHAIN (c) = list;
30815 return c;
30818 /* OpenMP 2.5:
30819 num_threads ( expression ) */
30821 static tree
30822 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
30823 location_t location)
30825 tree t, c;
30827 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30828 return list;
30830 t = cp_parser_expression (parser);
30832 if (t == error_mark_node
30833 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30834 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30835 /*or_comma=*/false,
30836 /*consume_paren=*/true);
30838 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
30839 "num_threads", location);
30841 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
30842 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
30843 OMP_CLAUSE_CHAIN (c) = list;
30845 return c;
30848 /* OpenMP 4.5:
30849 num_tasks ( expression ) */
30851 static tree
30852 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
30853 location_t location)
30855 tree t, c;
30857 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30858 return list;
30860 t = cp_parser_expression (parser);
30862 if (t == error_mark_node
30863 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30864 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30865 /*or_comma=*/false,
30866 /*consume_paren=*/true);
30868 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
30869 "num_tasks", location);
30871 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
30872 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
30873 OMP_CLAUSE_CHAIN (c) = list;
30875 return c;
30878 /* OpenMP 4.5:
30879 grainsize ( expression ) */
30881 static tree
30882 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
30883 location_t location)
30885 tree t, c;
30887 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30888 return list;
30890 t = cp_parser_expression (parser);
30892 if (t == error_mark_node
30893 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30894 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30895 /*or_comma=*/false,
30896 /*consume_paren=*/true);
30898 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
30899 "grainsize", location);
30901 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
30902 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
30903 OMP_CLAUSE_CHAIN (c) = list;
30905 return c;
30908 /* OpenMP 4.5:
30909 priority ( expression ) */
30911 static tree
30912 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
30913 location_t location)
30915 tree t, c;
30917 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30918 return list;
30920 t = cp_parser_expression (parser);
30922 if (t == error_mark_node
30923 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30924 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30925 /*or_comma=*/false,
30926 /*consume_paren=*/true);
30928 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
30929 "priority", location);
30931 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
30932 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
30933 OMP_CLAUSE_CHAIN (c) = list;
30935 return c;
30938 /* OpenMP 4.5:
30939 hint ( expression ) */
30941 static tree
30942 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
30943 location_t location)
30945 tree t, c;
30947 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30948 return list;
30950 t = cp_parser_expression (parser);
30952 if (t == error_mark_node
30953 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30954 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30955 /*or_comma=*/false,
30956 /*consume_paren=*/true);
30958 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
30960 c = build_omp_clause (location, OMP_CLAUSE_HINT);
30961 OMP_CLAUSE_HINT_EXPR (c) = t;
30962 OMP_CLAUSE_CHAIN (c) = list;
30964 return c;
30967 /* OpenMP 4.5:
30968 defaultmap ( tofrom : scalar ) */
30970 static tree
30971 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
30972 location_t location)
30974 tree c, id;
30975 const char *p;
30977 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30978 return list;
30980 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30982 cp_parser_error (parser, "expected %<tofrom%>");
30983 goto out_err;
30985 id = cp_lexer_peek_token (parser->lexer)->u.value;
30986 p = IDENTIFIER_POINTER (id);
30987 if (strcmp (p, "tofrom") != 0)
30989 cp_parser_error (parser, "expected %<tofrom%>");
30990 goto out_err;
30992 cp_lexer_consume_token (parser->lexer);
30993 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30994 goto out_err;
30996 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30998 cp_parser_error (parser, "expected %<scalar%>");
30999 goto out_err;
31001 id = cp_lexer_peek_token (parser->lexer)->u.value;
31002 p = IDENTIFIER_POINTER (id);
31003 if (strcmp (p, "scalar") != 0)
31005 cp_parser_error (parser, "expected %<scalar%>");
31006 goto out_err;
31008 cp_lexer_consume_token (parser->lexer);
31009 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31010 goto out_err;
31012 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
31013 location);
31015 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
31016 OMP_CLAUSE_CHAIN (c) = list;
31017 return c;
31019 out_err:
31020 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31021 /*or_comma=*/false,
31022 /*consume_paren=*/true);
31023 return list;
31026 /* OpenMP 2.5:
31027 ordered
31029 OpenMP 4.5:
31030 ordered ( constant-expression ) */
31032 static tree
31033 cp_parser_omp_clause_ordered (cp_parser *parser,
31034 tree list, location_t location)
31036 tree c, num = NULL_TREE;
31037 HOST_WIDE_INT n;
31039 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
31040 "ordered", location);
31042 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31044 cp_lexer_consume_token (parser->lexer);
31046 num = cp_parser_constant_expression (parser);
31048 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31049 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31050 /*or_comma=*/false,
31051 /*consume_paren=*/true);
31053 if (num == error_mark_node)
31054 return list;
31055 num = fold_non_dependent_expr (num);
31056 if (!tree_fits_shwi_p (num)
31057 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31058 || (n = tree_to_shwi (num)) <= 0
31059 || (int) n != n)
31061 error_at (location,
31062 "ordered argument needs positive constant integer "
31063 "expression");
31064 return list;
31068 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
31069 OMP_CLAUSE_ORDERED_EXPR (c) = num;
31070 OMP_CLAUSE_CHAIN (c) = list;
31071 return c;
31074 /* OpenMP 2.5:
31075 reduction ( reduction-operator : variable-list )
31077 reduction-operator:
31078 One of: + * - & ^ | && ||
31080 OpenMP 3.1:
31082 reduction-operator:
31083 One of: + * - & ^ | && || min max
31085 OpenMP 4.0:
31087 reduction-operator:
31088 One of: + * - & ^ | && ||
31089 id-expression */
31091 static tree
31092 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
31094 enum tree_code code = ERROR_MARK;
31095 tree nlist, c, id = NULL_TREE;
31097 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31098 return list;
31100 switch (cp_lexer_peek_token (parser->lexer)->type)
31102 case CPP_PLUS: code = PLUS_EXPR; break;
31103 case CPP_MULT: code = MULT_EXPR; break;
31104 case CPP_MINUS: code = MINUS_EXPR; break;
31105 case CPP_AND: code = BIT_AND_EXPR; break;
31106 case CPP_XOR: code = BIT_XOR_EXPR; break;
31107 case CPP_OR: code = BIT_IOR_EXPR; break;
31108 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
31109 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
31110 default: break;
31113 if (code != ERROR_MARK)
31114 cp_lexer_consume_token (parser->lexer);
31115 else
31117 bool saved_colon_corrects_to_scope_p;
31118 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31119 parser->colon_corrects_to_scope_p = false;
31120 id = cp_parser_id_expression (parser, /*template_p=*/false,
31121 /*check_dependency_p=*/true,
31122 /*template_p=*/NULL,
31123 /*declarator_p=*/false,
31124 /*optional_p=*/false);
31125 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31126 if (identifier_p (id))
31128 const char *p = IDENTIFIER_POINTER (id);
31130 if (strcmp (p, "min") == 0)
31131 code = MIN_EXPR;
31132 else if (strcmp (p, "max") == 0)
31133 code = MAX_EXPR;
31134 else if (id == ansi_opname (PLUS_EXPR))
31135 code = PLUS_EXPR;
31136 else if (id == ansi_opname (MULT_EXPR))
31137 code = MULT_EXPR;
31138 else if (id == ansi_opname (MINUS_EXPR))
31139 code = MINUS_EXPR;
31140 else if (id == ansi_opname (BIT_AND_EXPR))
31141 code = BIT_AND_EXPR;
31142 else if (id == ansi_opname (BIT_IOR_EXPR))
31143 code = BIT_IOR_EXPR;
31144 else if (id == ansi_opname (BIT_XOR_EXPR))
31145 code = BIT_XOR_EXPR;
31146 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
31147 code = TRUTH_ANDIF_EXPR;
31148 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
31149 code = TRUTH_ORIF_EXPR;
31150 id = omp_reduction_id (code, id, NULL_TREE);
31151 tree scope = parser->scope;
31152 if (scope)
31153 id = build_qualified_name (NULL_TREE, scope, id, false);
31154 parser->scope = NULL_TREE;
31155 parser->qualifying_scope = NULL_TREE;
31156 parser->object_scope = NULL_TREE;
31158 else
31160 error ("invalid reduction-identifier");
31161 resync_fail:
31162 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31163 /*or_comma=*/false,
31164 /*consume_paren=*/true);
31165 return list;
31169 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31170 goto resync_fail;
31172 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
31173 NULL);
31174 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31176 OMP_CLAUSE_REDUCTION_CODE (c) = code;
31177 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
31180 return nlist;
31183 /* OpenMP 2.5:
31184 schedule ( schedule-kind )
31185 schedule ( schedule-kind , expression )
31187 schedule-kind:
31188 static | dynamic | guided | runtime | auto
31190 OpenMP 4.5:
31191 schedule ( schedule-modifier : schedule-kind )
31192 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
31194 schedule-modifier:
31195 simd
31196 monotonic
31197 nonmonotonic */
31199 static tree
31200 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
31202 tree c, t;
31203 int modifiers = 0, nmodifiers = 0;
31205 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31206 return list;
31208 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
31210 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31212 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31213 const char *p = IDENTIFIER_POINTER (id);
31214 if (strcmp ("simd", p) == 0)
31215 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
31216 else if (strcmp ("monotonic", p) == 0)
31217 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
31218 else if (strcmp ("nonmonotonic", p) == 0)
31219 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
31220 else
31221 break;
31222 cp_lexer_consume_token (parser->lexer);
31223 if (nmodifiers++ == 0
31224 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31225 cp_lexer_consume_token (parser->lexer);
31226 else
31228 cp_parser_require (parser, CPP_COLON, RT_COLON);
31229 break;
31233 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31235 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31236 const char *p = IDENTIFIER_POINTER (id);
31238 switch (p[0])
31240 case 'd':
31241 if (strcmp ("dynamic", p) != 0)
31242 goto invalid_kind;
31243 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
31244 break;
31246 case 'g':
31247 if (strcmp ("guided", p) != 0)
31248 goto invalid_kind;
31249 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
31250 break;
31252 case 'r':
31253 if (strcmp ("runtime", p) != 0)
31254 goto invalid_kind;
31255 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
31256 break;
31258 default:
31259 goto invalid_kind;
31262 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
31263 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
31264 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31265 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
31266 else
31267 goto invalid_kind;
31268 cp_lexer_consume_token (parser->lexer);
31270 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
31271 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31272 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
31273 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31275 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
31276 "specified");
31277 modifiers = 0;
31280 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31282 cp_token *token;
31283 cp_lexer_consume_token (parser->lexer);
31285 token = cp_lexer_peek_token (parser->lexer);
31286 t = cp_parser_assignment_expression (parser);
31288 if (t == error_mark_node)
31289 goto resync_fail;
31290 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
31291 error_at (token->location, "schedule %<runtime%> does not take "
31292 "a %<chunk_size%> parameter");
31293 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
31294 error_at (token->location, "schedule %<auto%> does not take "
31295 "a %<chunk_size%> parameter");
31296 else
31297 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
31299 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31300 goto resync_fail;
31302 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31303 goto resync_fail;
31305 OMP_CLAUSE_SCHEDULE_KIND (c)
31306 = (enum omp_clause_schedule_kind)
31307 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
31309 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
31310 OMP_CLAUSE_CHAIN (c) = list;
31311 return c;
31313 invalid_kind:
31314 cp_parser_error (parser, "invalid schedule kind");
31315 resync_fail:
31316 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31317 /*or_comma=*/false,
31318 /*consume_paren=*/true);
31319 return list;
31322 /* OpenMP 3.0:
31323 untied */
31325 static tree
31326 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
31327 tree list, location_t location)
31329 tree c;
31331 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
31333 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
31334 OMP_CLAUSE_CHAIN (c) = list;
31335 return c;
31338 /* OpenMP 4.0:
31339 inbranch
31340 notinbranch */
31342 static tree
31343 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
31344 tree list, location_t location)
31346 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31347 tree c = build_omp_clause (location, code);
31348 OMP_CLAUSE_CHAIN (c) = list;
31349 return c;
31352 /* OpenMP 4.0:
31353 parallel
31355 sections
31356 taskgroup */
31358 static tree
31359 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
31360 enum omp_clause_code code,
31361 tree list, location_t location)
31363 tree c = build_omp_clause (location, code);
31364 OMP_CLAUSE_CHAIN (c) = list;
31365 return c;
31368 /* OpenMP 4.5:
31369 nogroup */
31371 static tree
31372 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
31373 tree list, location_t location)
31375 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
31376 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
31377 OMP_CLAUSE_CHAIN (c) = list;
31378 return c;
31381 /* OpenMP 4.5:
31382 simd
31383 threads */
31385 static tree
31386 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
31387 enum omp_clause_code code,
31388 tree list, location_t location)
31390 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31391 tree c = build_omp_clause (location, code);
31392 OMP_CLAUSE_CHAIN (c) = list;
31393 return c;
31396 /* OpenMP 4.0:
31397 num_teams ( expression ) */
31399 static tree
31400 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
31401 location_t location)
31403 tree t, c;
31405 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31406 return list;
31408 t = cp_parser_expression (parser);
31410 if (t == error_mark_node
31411 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31412 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31413 /*or_comma=*/false,
31414 /*consume_paren=*/true);
31416 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
31417 "num_teams", location);
31419 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
31420 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
31421 OMP_CLAUSE_CHAIN (c) = list;
31423 return c;
31426 /* OpenMP 4.0:
31427 thread_limit ( expression ) */
31429 static tree
31430 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
31431 location_t location)
31433 tree t, c;
31435 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31436 return list;
31438 t = cp_parser_expression (parser);
31440 if (t == error_mark_node
31441 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31442 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31443 /*or_comma=*/false,
31444 /*consume_paren=*/true);
31446 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
31447 "thread_limit", location);
31449 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
31450 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
31451 OMP_CLAUSE_CHAIN (c) = list;
31453 return c;
31456 /* OpenMP 4.0:
31457 aligned ( variable-list )
31458 aligned ( variable-list : constant-expression ) */
31460 static tree
31461 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
31463 tree nlist, c, alignment = NULL_TREE;
31464 bool colon;
31466 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31467 return list;
31469 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
31470 &colon);
31472 if (colon)
31474 alignment = cp_parser_constant_expression (parser);
31476 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31477 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31478 /*or_comma=*/false,
31479 /*consume_paren=*/true);
31481 if (alignment == error_mark_node)
31482 alignment = NULL_TREE;
31485 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31486 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
31488 return nlist;
31491 /* OpenMP 4.0:
31492 linear ( variable-list )
31493 linear ( variable-list : expression )
31495 OpenMP 4.5:
31496 linear ( modifier ( variable-list ) )
31497 linear ( modifier ( variable-list ) : expression ) */
31499 static tree
31500 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
31501 bool is_cilk_simd_fn, bool declare_simd)
31503 tree nlist, c, step = integer_one_node;
31504 bool colon;
31505 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
31507 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31508 return list;
31510 if (!is_cilk_simd_fn
31511 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31513 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31514 const char *p = IDENTIFIER_POINTER (id);
31516 if (strcmp ("ref", p) == 0)
31517 kind = OMP_CLAUSE_LINEAR_REF;
31518 else if (strcmp ("val", p) == 0)
31519 kind = OMP_CLAUSE_LINEAR_VAL;
31520 else if (strcmp ("uval", p) == 0)
31521 kind = OMP_CLAUSE_LINEAR_UVAL;
31522 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
31523 cp_lexer_consume_token (parser->lexer);
31524 else
31525 kind = OMP_CLAUSE_LINEAR_DEFAULT;
31528 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
31529 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
31530 &colon);
31531 else
31533 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
31534 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
31535 if (colon)
31536 cp_parser_require (parser, CPP_COLON, RT_COLON);
31537 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31538 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31539 /*or_comma=*/false,
31540 /*consume_paren=*/true);
31543 if (colon)
31545 step = NULL_TREE;
31546 if (declare_simd
31547 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31548 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
31550 cp_token *token = cp_lexer_peek_token (parser->lexer);
31551 cp_parser_parse_tentatively (parser);
31552 step = cp_parser_id_expression (parser, /*template_p=*/false,
31553 /*check_dependency_p=*/true,
31554 /*template_p=*/NULL,
31555 /*declarator_p=*/false,
31556 /*optional_p=*/false);
31557 if (step != error_mark_node)
31558 step = cp_parser_lookup_name_simple (parser, step, token->location);
31559 if (step == error_mark_node)
31561 step = NULL_TREE;
31562 cp_parser_abort_tentative_parse (parser);
31564 else if (!cp_parser_parse_definitely (parser))
31565 step = NULL_TREE;
31567 if (!step)
31568 step = cp_parser_expression (parser);
31570 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
31572 sorry ("using parameters for %<linear%> step is not supported yet");
31573 step = integer_one_node;
31575 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31576 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31577 /*or_comma=*/false,
31578 /*consume_paren=*/true);
31580 if (step == error_mark_node)
31581 return list;
31584 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31586 OMP_CLAUSE_LINEAR_STEP (c) = step;
31587 OMP_CLAUSE_LINEAR_KIND (c) = kind;
31590 return nlist;
31593 /* OpenMP 4.0:
31594 safelen ( constant-expression ) */
31596 static tree
31597 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
31598 location_t location)
31600 tree t, c;
31602 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31603 return list;
31605 t = cp_parser_constant_expression (parser);
31607 if (t == error_mark_node
31608 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31609 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31610 /*or_comma=*/false,
31611 /*consume_paren=*/true);
31613 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
31615 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
31616 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
31617 OMP_CLAUSE_CHAIN (c) = list;
31619 return c;
31622 /* OpenMP 4.0:
31623 simdlen ( constant-expression ) */
31625 static tree
31626 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
31627 location_t location)
31629 tree t, c;
31631 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31632 return list;
31634 t = cp_parser_constant_expression (parser);
31636 if (t == error_mark_node
31637 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31638 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31639 /*or_comma=*/false,
31640 /*consume_paren=*/true);
31642 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
31644 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
31645 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
31646 OMP_CLAUSE_CHAIN (c) = list;
31648 return c;
31651 /* OpenMP 4.5:
31652 vec:
31653 identifier [+/- integer]
31654 vec , identifier [+/- integer]
31657 static tree
31658 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
31659 tree list)
31661 tree vec = NULL;
31663 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31665 cp_parser_error (parser, "expected identifier");
31666 return list;
31669 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31671 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
31672 tree t, identifier = cp_parser_identifier (parser);
31673 tree addend = NULL;
31675 if (identifier == error_mark_node)
31676 t = error_mark_node;
31677 else
31679 t = cp_parser_lookup_name_simple
31680 (parser, identifier,
31681 cp_lexer_peek_token (parser->lexer)->location);
31682 if (t == error_mark_node)
31683 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
31684 id_loc);
31687 bool neg = false;
31688 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
31689 neg = true;
31690 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
31692 addend = integer_zero_node;
31693 goto add_to_vector;
31695 cp_lexer_consume_token (parser->lexer);
31697 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
31699 cp_parser_error (parser, "expected integer");
31700 return list;
31703 addend = cp_lexer_peek_token (parser->lexer)->u.value;
31704 if (TREE_CODE (addend) != INTEGER_CST)
31706 cp_parser_error (parser, "expected integer");
31707 return list;
31709 cp_lexer_consume_token (parser->lexer);
31711 add_to_vector:
31712 if (t != error_mark_node)
31714 vec = tree_cons (addend, t, vec);
31715 if (neg)
31716 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
31719 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31720 break;
31722 cp_lexer_consume_token (parser->lexer);
31725 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
31727 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
31728 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
31729 OMP_CLAUSE_DECL (u) = nreverse (vec);
31730 OMP_CLAUSE_CHAIN (u) = list;
31731 return u;
31733 return list;
31736 /* OpenMP 4.0:
31737 depend ( depend-kind : variable-list )
31739 depend-kind:
31740 in | out | inout
31742 OpenMP 4.5:
31743 depend ( source )
31745 depend ( sink : vec ) */
31747 static tree
31748 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
31750 tree nlist, c;
31751 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
31753 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31754 return list;
31756 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31758 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31759 const char *p = IDENTIFIER_POINTER (id);
31761 if (strcmp ("in", p) == 0)
31762 kind = OMP_CLAUSE_DEPEND_IN;
31763 else if (strcmp ("inout", p) == 0)
31764 kind = OMP_CLAUSE_DEPEND_INOUT;
31765 else if (strcmp ("out", p) == 0)
31766 kind = OMP_CLAUSE_DEPEND_OUT;
31767 else if (strcmp ("source", p) == 0)
31768 kind = OMP_CLAUSE_DEPEND_SOURCE;
31769 else if (strcmp ("sink", p) == 0)
31770 kind = OMP_CLAUSE_DEPEND_SINK;
31771 else
31772 goto invalid_kind;
31774 else
31775 goto invalid_kind;
31777 cp_lexer_consume_token (parser->lexer);
31779 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
31781 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
31782 OMP_CLAUSE_DEPEND_KIND (c) = kind;
31783 OMP_CLAUSE_DECL (c) = NULL_TREE;
31784 OMP_CLAUSE_CHAIN (c) = list;
31785 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31786 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31787 /*or_comma=*/false,
31788 /*consume_paren=*/true);
31789 return c;
31792 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31793 goto resync_fail;
31795 if (kind == OMP_CLAUSE_DEPEND_SINK)
31796 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
31797 else
31799 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
31800 list, NULL);
31802 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31803 OMP_CLAUSE_DEPEND_KIND (c) = kind;
31805 return nlist;
31807 invalid_kind:
31808 cp_parser_error (parser, "invalid depend kind");
31809 resync_fail:
31810 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31811 /*or_comma=*/false,
31812 /*consume_paren=*/true);
31813 return list;
31816 /* OpenMP 4.0:
31817 map ( map-kind : variable-list )
31818 map ( variable-list )
31820 map-kind:
31821 alloc | to | from | tofrom
31823 OpenMP 4.5:
31824 map-kind:
31825 alloc | to | from | tofrom | release | delete
31827 map ( always [,] map-kind: variable-list ) */
31829 static tree
31830 cp_parser_omp_clause_map (cp_parser *parser, tree list)
31832 tree nlist, c;
31833 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
31834 bool always = false;
31836 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31837 return list;
31839 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31841 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31842 const char *p = IDENTIFIER_POINTER (id);
31844 if (strcmp ("always", p) == 0)
31846 int nth = 2;
31847 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
31848 nth++;
31849 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
31850 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
31851 == RID_DELETE))
31852 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
31853 == CPP_COLON))
31855 always = true;
31856 cp_lexer_consume_token (parser->lexer);
31857 if (nth == 3)
31858 cp_lexer_consume_token (parser->lexer);
31863 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31864 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31866 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31867 const char *p = IDENTIFIER_POINTER (id);
31869 if (strcmp ("alloc", p) == 0)
31870 kind = GOMP_MAP_ALLOC;
31871 else if (strcmp ("to", p) == 0)
31872 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
31873 else if (strcmp ("from", p) == 0)
31874 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
31875 else if (strcmp ("tofrom", p) == 0)
31876 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
31877 else if (strcmp ("release", p) == 0)
31878 kind = GOMP_MAP_RELEASE;
31879 else
31881 cp_parser_error (parser, "invalid map kind");
31882 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31883 /*or_comma=*/false,
31884 /*consume_paren=*/true);
31885 return list;
31887 cp_lexer_consume_token (parser->lexer);
31888 cp_lexer_consume_token (parser->lexer);
31890 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
31891 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31893 kind = GOMP_MAP_DELETE;
31894 cp_lexer_consume_token (parser->lexer);
31895 cp_lexer_consume_token (parser->lexer);
31898 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
31899 NULL);
31901 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31902 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31904 return nlist;
31907 /* OpenMP 4.0:
31908 device ( expression ) */
31910 static tree
31911 cp_parser_omp_clause_device (cp_parser *parser, tree list,
31912 location_t location)
31914 tree t, c;
31916 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31917 return list;
31919 t = cp_parser_expression (parser);
31921 if (t == error_mark_node
31922 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31923 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31924 /*or_comma=*/false,
31925 /*consume_paren=*/true);
31927 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
31928 "device", location);
31930 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
31931 OMP_CLAUSE_DEVICE_ID (c) = t;
31932 OMP_CLAUSE_CHAIN (c) = list;
31934 return c;
31937 /* OpenMP 4.0:
31938 dist_schedule ( static )
31939 dist_schedule ( static , expression ) */
31941 static tree
31942 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
31943 location_t location)
31945 tree c, t;
31947 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31948 return list;
31950 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
31952 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
31953 goto invalid_kind;
31954 cp_lexer_consume_token (parser->lexer);
31956 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31958 cp_lexer_consume_token (parser->lexer);
31960 t = cp_parser_assignment_expression (parser);
31962 if (t == error_mark_node)
31963 goto resync_fail;
31964 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
31966 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31967 goto resync_fail;
31969 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31970 goto resync_fail;
31972 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
31973 location);
31974 OMP_CLAUSE_CHAIN (c) = list;
31975 return c;
31977 invalid_kind:
31978 cp_parser_error (parser, "invalid dist_schedule kind");
31979 resync_fail:
31980 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31981 /*or_comma=*/false,
31982 /*consume_paren=*/true);
31983 return list;
31986 /* OpenMP 4.0:
31987 proc_bind ( proc-bind-kind )
31989 proc-bind-kind:
31990 master | close | spread */
31992 static tree
31993 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
31994 location_t location)
31996 tree c;
31997 enum omp_clause_proc_bind_kind kind;
31999 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32000 return list;
32002 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32004 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32005 const char *p = IDENTIFIER_POINTER (id);
32007 if (strcmp ("master", p) == 0)
32008 kind = OMP_CLAUSE_PROC_BIND_MASTER;
32009 else if (strcmp ("close", p) == 0)
32010 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
32011 else if (strcmp ("spread", p) == 0)
32012 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
32013 else
32014 goto invalid_kind;
32016 else
32017 goto invalid_kind;
32019 cp_lexer_consume_token (parser->lexer);
32020 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32021 goto resync_fail;
32023 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
32024 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
32025 location);
32026 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
32027 OMP_CLAUSE_CHAIN (c) = list;
32028 return c;
32030 invalid_kind:
32031 cp_parser_error (parser, "invalid depend kind");
32032 resync_fail:
32033 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32034 /*or_comma=*/false,
32035 /*consume_paren=*/true);
32036 return list;
32039 /* OpenACC:
32040 async [( int-expr )] */
32042 static tree
32043 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
32045 tree c, t;
32046 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32048 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
32050 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32052 cp_lexer_consume_token (parser->lexer);
32054 t = cp_parser_expression (parser);
32055 if (t == error_mark_node
32056 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32057 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32058 /*or_comma=*/false,
32059 /*consume_paren=*/true);
32062 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
32064 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
32065 OMP_CLAUSE_ASYNC_EXPR (c) = t;
32066 OMP_CLAUSE_CHAIN (c) = list;
32067 list = c;
32069 return list;
32072 /* Parse all OpenACC clauses. The set clauses allowed by the directive
32073 is a bitmask in MASK. Return the list of clauses found. */
32075 static tree
32076 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
32077 const char *where, cp_token *pragma_tok,
32078 bool finish_p = true)
32080 tree clauses = NULL;
32081 bool first = true;
32083 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32085 location_t here;
32086 pragma_omp_clause c_kind;
32087 omp_clause_code code;
32088 const char *c_name;
32089 tree prev = clauses;
32091 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32092 cp_lexer_consume_token (parser->lexer);
32094 here = cp_lexer_peek_token (parser->lexer)->location;
32095 c_kind = cp_parser_omp_clause_name (parser);
32097 switch (c_kind)
32099 case PRAGMA_OACC_CLAUSE_ASYNC:
32100 clauses = cp_parser_oacc_clause_async (parser, clauses);
32101 c_name = "async";
32102 break;
32103 case PRAGMA_OACC_CLAUSE_AUTO:
32104 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
32105 clauses, here);
32106 c_name = "auto";
32107 break;
32108 case PRAGMA_OACC_CLAUSE_COLLAPSE:
32109 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
32110 c_name = "collapse";
32111 break;
32112 case PRAGMA_OACC_CLAUSE_COPY:
32113 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32114 c_name = "copy";
32115 break;
32116 case PRAGMA_OACC_CLAUSE_COPYIN:
32117 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32118 c_name = "copyin";
32119 break;
32120 case PRAGMA_OACC_CLAUSE_COPYOUT:
32121 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32122 c_name = "copyout";
32123 break;
32124 case PRAGMA_OACC_CLAUSE_CREATE:
32125 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32126 c_name = "create";
32127 break;
32128 case PRAGMA_OACC_CLAUSE_DELETE:
32129 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32130 c_name = "delete";
32131 break;
32132 case PRAGMA_OMP_CLAUSE_DEFAULT:
32133 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
32134 c_name = "default";
32135 break;
32136 case PRAGMA_OACC_CLAUSE_DEVICE:
32137 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32138 c_name = "device";
32139 break;
32140 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
32141 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
32142 c_name = "deviceptr";
32143 break;
32144 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32145 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32146 c_name = "device_resident";
32147 break;
32148 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
32149 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32150 clauses);
32151 c_name = "firstprivate";
32152 break;
32153 case PRAGMA_OACC_CLAUSE_GANG:
32154 c_name = "gang";
32155 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
32156 c_name, clauses);
32157 break;
32158 case PRAGMA_OACC_CLAUSE_HOST:
32159 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32160 c_name = "host";
32161 break;
32162 case PRAGMA_OACC_CLAUSE_IF:
32163 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
32164 c_name = "if";
32165 break;
32166 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
32167 clauses = cp_parser_oacc_simple_clause (parser,
32168 OMP_CLAUSE_INDEPENDENT,
32169 clauses, here);
32170 c_name = "independent";
32171 break;
32172 case PRAGMA_OACC_CLAUSE_LINK:
32173 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32174 c_name = "link";
32175 break;
32176 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
32177 code = OMP_CLAUSE_NUM_GANGS;
32178 c_name = "num_gangs";
32179 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32180 clauses);
32181 break;
32182 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
32183 c_name = "num_workers";
32184 code = OMP_CLAUSE_NUM_WORKERS;
32185 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32186 clauses);
32187 break;
32188 case PRAGMA_OACC_CLAUSE_PRESENT:
32189 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32190 c_name = "present";
32191 break;
32192 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
32193 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32194 c_name = "present_or_copy";
32195 break;
32196 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
32197 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32198 c_name = "present_or_copyin";
32199 break;
32200 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
32201 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32202 c_name = "present_or_copyout";
32203 break;
32204 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
32205 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32206 c_name = "present_or_create";
32207 break;
32208 case PRAGMA_OACC_CLAUSE_PRIVATE:
32209 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32210 clauses);
32211 c_name = "private";
32212 break;
32213 case PRAGMA_OACC_CLAUSE_REDUCTION:
32214 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32215 c_name = "reduction";
32216 break;
32217 case PRAGMA_OACC_CLAUSE_SELF:
32218 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32219 c_name = "self";
32220 break;
32221 case PRAGMA_OACC_CLAUSE_SEQ:
32222 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
32223 clauses, here);
32224 c_name = "seq";
32225 break;
32226 case PRAGMA_OACC_CLAUSE_TILE:
32227 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
32228 c_name = "tile";
32229 break;
32230 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
32231 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32232 clauses);
32233 c_name = "use_device";
32234 break;
32235 case PRAGMA_OACC_CLAUSE_VECTOR:
32236 c_name = "vector";
32237 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
32238 c_name, clauses);
32239 break;
32240 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
32241 c_name = "vector_length";
32242 code = OMP_CLAUSE_VECTOR_LENGTH;
32243 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32244 clauses);
32245 break;
32246 case PRAGMA_OACC_CLAUSE_WAIT:
32247 clauses = cp_parser_oacc_clause_wait (parser, clauses);
32248 c_name = "wait";
32249 break;
32250 case PRAGMA_OACC_CLAUSE_WORKER:
32251 c_name = "worker";
32252 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
32253 c_name, clauses);
32254 break;
32255 default:
32256 cp_parser_error (parser, "expected %<#pragma acc%> clause");
32257 goto saw_error;
32260 first = false;
32262 if (((mask >> c_kind) & 1) == 0)
32264 /* Remove the invalid clause(s) from the list to avoid
32265 confusing the rest of the compiler. */
32266 clauses = prev;
32267 error_at (here, "%qs is not valid for %qs", c_name, where);
32271 saw_error:
32272 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32274 if (finish_p)
32275 return finish_omp_clauses (clauses, C_ORT_ACC);
32277 return clauses;
32280 /* Parse all OpenMP clauses. The set clauses allowed by the directive
32281 is a bitmask in MASK. Return the list of clauses found; the result
32282 of clause default goes in *pdefault. */
32284 static tree
32285 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
32286 const char *where, cp_token *pragma_tok,
32287 bool finish_p = true)
32289 tree clauses = NULL;
32290 bool first = true;
32291 cp_token *token = NULL;
32293 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32295 pragma_omp_clause c_kind;
32296 const char *c_name;
32297 tree prev = clauses;
32299 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32300 cp_lexer_consume_token (parser->lexer);
32302 token = cp_lexer_peek_token (parser->lexer);
32303 c_kind = cp_parser_omp_clause_name (parser);
32305 switch (c_kind)
32307 case PRAGMA_OMP_CLAUSE_COLLAPSE:
32308 clauses = cp_parser_omp_clause_collapse (parser, clauses,
32309 token->location);
32310 c_name = "collapse";
32311 break;
32312 case PRAGMA_OMP_CLAUSE_COPYIN:
32313 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
32314 c_name = "copyin";
32315 break;
32316 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
32317 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
32318 clauses);
32319 c_name = "copyprivate";
32320 break;
32321 case PRAGMA_OMP_CLAUSE_DEFAULT:
32322 clauses = cp_parser_omp_clause_default (parser, clauses,
32323 token->location, false);
32324 c_name = "default";
32325 break;
32326 case PRAGMA_OMP_CLAUSE_FINAL:
32327 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
32328 c_name = "final";
32329 break;
32330 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
32331 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32332 clauses);
32333 c_name = "firstprivate";
32334 break;
32335 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
32336 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
32337 token->location);
32338 c_name = "grainsize";
32339 break;
32340 case PRAGMA_OMP_CLAUSE_HINT:
32341 clauses = cp_parser_omp_clause_hint (parser, clauses,
32342 token->location);
32343 c_name = "hint";
32344 break;
32345 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
32346 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
32347 token->location);
32348 c_name = "defaultmap";
32349 break;
32350 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
32351 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32352 clauses);
32353 c_name = "use_device_ptr";
32354 break;
32355 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
32356 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
32357 clauses);
32358 c_name = "is_device_ptr";
32359 break;
32360 case PRAGMA_OMP_CLAUSE_IF:
32361 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
32362 true);
32363 c_name = "if";
32364 break;
32365 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
32366 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32367 clauses);
32368 c_name = "lastprivate";
32369 break;
32370 case PRAGMA_OMP_CLAUSE_MERGEABLE:
32371 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
32372 token->location);
32373 c_name = "mergeable";
32374 break;
32375 case PRAGMA_OMP_CLAUSE_NOWAIT:
32376 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
32377 c_name = "nowait";
32378 break;
32379 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
32380 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
32381 token->location);
32382 c_name = "num_tasks";
32383 break;
32384 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
32385 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
32386 token->location);
32387 c_name = "num_threads";
32388 break;
32389 case PRAGMA_OMP_CLAUSE_ORDERED:
32390 clauses = cp_parser_omp_clause_ordered (parser, clauses,
32391 token->location);
32392 c_name = "ordered";
32393 break;
32394 case PRAGMA_OMP_CLAUSE_PRIORITY:
32395 clauses = cp_parser_omp_clause_priority (parser, clauses,
32396 token->location);
32397 c_name = "priority";
32398 break;
32399 case PRAGMA_OMP_CLAUSE_PRIVATE:
32400 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32401 clauses);
32402 c_name = "private";
32403 break;
32404 case PRAGMA_OMP_CLAUSE_REDUCTION:
32405 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32406 c_name = "reduction";
32407 break;
32408 case PRAGMA_OMP_CLAUSE_SCHEDULE:
32409 clauses = cp_parser_omp_clause_schedule (parser, clauses,
32410 token->location);
32411 c_name = "schedule";
32412 break;
32413 case PRAGMA_OMP_CLAUSE_SHARED:
32414 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
32415 clauses);
32416 c_name = "shared";
32417 break;
32418 case PRAGMA_OMP_CLAUSE_UNTIED:
32419 clauses = cp_parser_omp_clause_untied (parser, clauses,
32420 token->location);
32421 c_name = "untied";
32422 break;
32423 case PRAGMA_OMP_CLAUSE_INBRANCH:
32424 case PRAGMA_CILK_CLAUSE_MASK:
32425 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
32426 clauses, token->location);
32427 c_name = "inbranch";
32428 break;
32429 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
32430 case PRAGMA_CILK_CLAUSE_NOMASK:
32431 clauses = cp_parser_omp_clause_branch (parser,
32432 OMP_CLAUSE_NOTINBRANCH,
32433 clauses, token->location);
32434 c_name = "notinbranch";
32435 break;
32436 case PRAGMA_OMP_CLAUSE_PARALLEL:
32437 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
32438 clauses, token->location);
32439 c_name = "parallel";
32440 if (!first)
32442 clause_not_first:
32443 error_at (token->location, "%qs must be the first clause of %qs",
32444 c_name, where);
32445 clauses = prev;
32447 break;
32448 case PRAGMA_OMP_CLAUSE_FOR:
32449 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
32450 clauses, token->location);
32451 c_name = "for";
32452 if (!first)
32453 goto clause_not_first;
32454 break;
32455 case PRAGMA_OMP_CLAUSE_SECTIONS:
32456 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
32457 clauses, token->location);
32458 c_name = "sections";
32459 if (!first)
32460 goto clause_not_first;
32461 break;
32462 case PRAGMA_OMP_CLAUSE_TASKGROUP:
32463 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
32464 clauses, token->location);
32465 c_name = "taskgroup";
32466 if (!first)
32467 goto clause_not_first;
32468 break;
32469 case PRAGMA_OMP_CLAUSE_LINK:
32470 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
32471 c_name = "to";
32472 break;
32473 case PRAGMA_OMP_CLAUSE_TO:
32474 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
32475 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
32476 clauses);
32477 else
32478 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
32479 c_name = "to";
32480 break;
32481 case PRAGMA_OMP_CLAUSE_FROM:
32482 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
32483 c_name = "from";
32484 break;
32485 case PRAGMA_OMP_CLAUSE_UNIFORM:
32486 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
32487 clauses);
32488 c_name = "uniform";
32489 break;
32490 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
32491 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
32492 token->location);
32493 c_name = "num_teams";
32494 break;
32495 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
32496 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
32497 token->location);
32498 c_name = "thread_limit";
32499 break;
32500 case PRAGMA_OMP_CLAUSE_ALIGNED:
32501 clauses = cp_parser_omp_clause_aligned (parser, clauses);
32502 c_name = "aligned";
32503 break;
32504 case PRAGMA_OMP_CLAUSE_LINEAR:
32506 bool cilk_simd_fn = false, declare_simd = false;
32507 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
32508 cilk_simd_fn = true;
32509 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
32510 declare_simd = true;
32511 clauses = cp_parser_omp_clause_linear (parser, clauses,
32512 cilk_simd_fn, declare_simd);
32514 c_name = "linear";
32515 break;
32516 case PRAGMA_OMP_CLAUSE_DEPEND:
32517 clauses = cp_parser_omp_clause_depend (parser, clauses,
32518 token->location);
32519 c_name = "depend";
32520 break;
32521 case PRAGMA_OMP_CLAUSE_MAP:
32522 clauses = cp_parser_omp_clause_map (parser, clauses);
32523 c_name = "map";
32524 break;
32525 case PRAGMA_OMP_CLAUSE_DEVICE:
32526 clauses = cp_parser_omp_clause_device (parser, clauses,
32527 token->location);
32528 c_name = "device";
32529 break;
32530 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
32531 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
32532 token->location);
32533 c_name = "dist_schedule";
32534 break;
32535 case PRAGMA_OMP_CLAUSE_PROC_BIND:
32536 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
32537 token->location);
32538 c_name = "proc_bind";
32539 break;
32540 case PRAGMA_OMP_CLAUSE_SAFELEN:
32541 clauses = cp_parser_omp_clause_safelen (parser, clauses,
32542 token->location);
32543 c_name = "safelen";
32544 break;
32545 case PRAGMA_OMP_CLAUSE_SIMDLEN:
32546 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
32547 token->location);
32548 c_name = "simdlen";
32549 break;
32550 case PRAGMA_OMP_CLAUSE_NOGROUP:
32551 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
32552 token->location);
32553 c_name = "nogroup";
32554 break;
32555 case PRAGMA_OMP_CLAUSE_THREADS:
32556 clauses
32557 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
32558 clauses, token->location);
32559 c_name = "threads";
32560 break;
32561 case PRAGMA_OMP_CLAUSE_SIMD:
32562 clauses
32563 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
32564 clauses, token->location);
32565 c_name = "simd";
32566 break;
32567 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
32568 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
32569 c_name = "simdlen";
32570 break;
32571 default:
32572 cp_parser_error (parser, "expected %<#pragma omp%> clause");
32573 goto saw_error;
32576 first = false;
32578 if (((mask >> c_kind) & 1) == 0)
32580 /* Remove the invalid clause(s) from the list to avoid
32581 confusing the rest of the compiler. */
32582 clauses = prev;
32583 error_at (token->location, "%qs is not valid for %qs", c_name, where);
32586 saw_error:
32587 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
32588 no reason to skip to the end. */
32589 if (!(flag_cilkplus && pragma_tok == NULL))
32590 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32591 if (finish_p)
32593 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
32594 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
32595 else
32596 return finish_omp_clauses (clauses, C_ORT_OMP);
32598 return clauses;
32601 /* OpenMP 2.5:
32602 structured-block:
32603 statement
32605 In practice, we're also interested in adding the statement to an
32606 outer node. So it is convenient if we work around the fact that
32607 cp_parser_statement calls add_stmt. */
32609 static unsigned
32610 cp_parser_begin_omp_structured_block (cp_parser *parser)
32612 unsigned save = parser->in_statement;
32614 /* Only move the values to IN_OMP_BLOCK if they weren't false.
32615 This preserves the "not within loop or switch" style error messages
32616 for nonsense cases like
32617 void foo() {
32618 #pragma omp single
32619 break;
32622 if (parser->in_statement)
32623 parser->in_statement = IN_OMP_BLOCK;
32625 return save;
32628 static void
32629 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
32631 parser->in_statement = save;
32634 static tree
32635 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
32637 tree stmt = begin_omp_structured_block ();
32638 unsigned int save = cp_parser_begin_omp_structured_block (parser);
32640 cp_parser_statement (parser, NULL_TREE, false, if_p);
32642 cp_parser_end_omp_structured_block (parser, save);
32643 return finish_omp_structured_block (stmt);
32646 /* OpenMP 2.5:
32647 # pragma omp atomic new-line
32648 expression-stmt
32650 expression-stmt:
32651 x binop= expr | x++ | ++x | x-- | --x
32652 binop:
32653 +, *, -, /, &, ^, |, <<, >>
32655 where x is an lvalue expression with scalar type.
32657 OpenMP 3.1:
32658 # pragma omp atomic new-line
32659 update-stmt
32661 # pragma omp atomic read new-line
32662 read-stmt
32664 # pragma omp atomic write new-line
32665 write-stmt
32667 # pragma omp atomic update new-line
32668 update-stmt
32670 # pragma omp atomic capture new-line
32671 capture-stmt
32673 # pragma omp atomic capture new-line
32674 capture-block
32676 read-stmt:
32677 v = x
32678 write-stmt:
32679 x = expr
32680 update-stmt:
32681 expression-stmt | x = x binop expr
32682 capture-stmt:
32683 v = expression-stmt
32684 capture-block:
32685 { v = x; update-stmt; } | { update-stmt; v = x; }
32687 OpenMP 4.0:
32688 update-stmt:
32689 expression-stmt | x = x binop expr | x = expr binop x
32690 capture-stmt:
32691 v = update-stmt
32692 capture-block:
32693 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
32695 where x and v are lvalue expressions with scalar type. */
32697 static void
32698 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
32700 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
32701 tree rhs1 = NULL_TREE, orig_lhs;
32702 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
32703 bool structured_block = false;
32704 bool seq_cst = false;
32706 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32708 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32709 const char *p = IDENTIFIER_POINTER (id);
32711 if (!strcmp (p, "seq_cst"))
32713 seq_cst = true;
32714 cp_lexer_consume_token (parser->lexer);
32715 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32716 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32717 cp_lexer_consume_token (parser->lexer);
32720 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32722 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32723 const char *p = IDENTIFIER_POINTER (id);
32725 if (!strcmp (p, "read"))
32726 code = OMP_ATOMIC_READ;
32727 else if (!strcmp (p, "write"))
32728 code = NOP_EXPR;
32729 else if (!strcmp (p, "update"))
32730 code = OMP_ATOMIC;
32731 else if (!strcmp (p, "capture"))
32732 code = OMP_ATOMIC_CAPTURE_NEW;
32733 else
32734 p = NULL;
32735 if (p)
32736 cp_lexer_consume_token (parser->lexer);
32738 if (!seq_cst)
32740 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32741 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32742 cp_lexer_consume_token (parser->lexer);
32744 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32746 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32747 const char *p = IDENTIFIER_POINTER (id);
32749 if (!strcmp (p, "seq_cst"))
32751 seq_cst = true;
32752 cp_lexer_consume_token (parser->lexer);
32756 cp_parser_require_pragma_eol (parser, pragma_tok);
32758 switch (code)
32760 case OMP_ATOMIC_READ:
32761 case NOP_EXPR: /* atomic write */
32762 v = cp_parser_unary_expression (parser);
32763 if (v == error_mark_node)
32764 goto saw_error;
32765 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32766 goto saw_error;
32767 if (code == NOP_EXPR)
32768 lhs = cp_parser_expression (parser);
32769 else
32770 lhs = cp_parser_unary_expression (parser);
32771 if (lhs == error_mark_node)
32772 goto saw_error;
32773 if (code == NOP_EXPR)
32775 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
32776 opcode. */
32777 code = OMP_ATOMIC;
32778 rhs = lhs;
32779 lhs = v;
32780 v = NULL_TREE;
32782 goto done;
32783 case OMP_ATOMIC_CAPTURE_NEW:
32784 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
32786 cp_lexer_consume_token (parser->lexer);
32787 structured_block = true;
32789 else
32791 v = cp_parser_unary_expression (parser);
32792 if (v == error_mark_node)
32793 goto saw_error;
32794 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32795 goto saw_error;
32797 default:
32798 break;
32801 restart:
32802 lhs = cp_parser_unary_expression (parser);
32803 orig_lhs = lhs;
32804 switch (TREE_CODE (lhs))
32806 case ERROR_MARK:
32807 goto saw_error;
32809 case POSTINCREMENT_EXPR:
32810 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32811 code = OMP_ATOMIC_CAPTURE_OLD;
32812 /* FALLTHROUGH */
32813 case PREINCREMENT_EXPR:
32814 lhs = TREE_OPERAND (lhs, 0);
32815 opcode = PLUS_EXPR;
32816 rhs = integer_one_node;
32817 break;
32819 case POSTDECREMENT_EXPR:
32820 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32821 code = OMP_ATOMIC_CAPTURE_OLD;
32822 /* FALLTHROUGH */
32823 case PREDECREMENT_EXPR:
32824 lhs = TREE_OPERAND (lhs, 0);
32825 opcode = MINUS_EXPR;
32826 rhs = integer_one_node;
32827 break;
32829 case COMPOUND_EXPR:
32830 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
32831 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
32832 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
32833 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
32834 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
32835 (TREE_OPERAND (lhs, 1), 0), 0)))
32836 == BOOLEAN_TYPE)
32837 /* Undo effects of boolean_increment for post {in,de}crement. */
32838 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
32839 /* FALLTHRU */
32840 case MODIFY_EXPR:
32841 if (TREE_CODE (lhs) == MODIFY_EXPR
32842 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
32844 /* Undo effects of boolean_increment. */
32845 if (integer_onep (TREE_OPERAND (lhs, 1)))
32847 /* This is pre or post increment. */
32848 rhs = TREE_OPERAND (lhs, 1);
32849 lhs = TREE_OPERAND (lhs, 0);
32850 opcode = NOP_EXPR;
32851 if (code == OMP_ATOMIC_CAPTURE_NEW
32852 && !structured_block
32853 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
32854 code = OMP_ATOMIC_CAPTURE_OLD;
32855 break;
32858 /* FALLTHRU */
32859 default:
32860 switch (cp_lexer_peek_token (parser->lexer)->type)
32862 case CPP_MULT_EQ:
32863 opcode = MULT_EXPR;
32864 break;
32865 case CPP_DIV_EQ:
32866 opcode = TRUNC_DIV_EXPR;
32867 break;
32868 case CPP_PLUS_EQ:
32869 opcode = PLUS_EXPR;
32870 break;
32871 case CPP_MINUS_EQ:
32872 opcode = MINUS_EXPR;
32873 break;
32874 case CPP_LSHIFT_EQ:
32875 opcode = LSHIFT_EXPR;
32876 break;
32877 case CPP_RSHIFT_EQ:
32878 opcode = RSHIFT_EXPR;
32879 break;
32880 case CPP_AND_EQ:
32881 opcode = BIT_AND_EXPR;
32882 break;
32883 case CPP_OR_EQ:
32884 opcode = BIT_IOR_EXPR;
32885 break;
32886 case CPP_XOR_EQ:
32887 opcode = BIT_XOR_EXPR;
32888 break;
32889 case CPP_EQ:
32890 enum cp_parser_prec oprec;
32891 cp_token *token;
32892 cp_lexer_consume_token (parser->lexer);
32893 cp_parser_parse_tentatively (parser);
32894 rhs1 = cp_parser_simple_cast_expression (parser);
32895 if (rhs1 == error_mark_node)
32897 cp_parser_abort_tentative_parse (parser);
32898 cp_parser_simple_cast_expression (parser);
32899 goto saw_error;
32901 token = cp_lexer_peek_token (parser->lexer);
32902 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
32904 cp_parser_abort_tentative_parse (parser);
32905 cp_parser_parse_tentatively (parser);
32906 rhs = cp_parser_binary_expression (parser, false, true,
32907 PREC_NOT_OPERATOR, NULL);
32908 if (rhs == error_mark_node)
32910 cp_parser_abort_tentative_parse (parser);
32911 cp_parser_binary_expression (parser, false, true,
32912 PREC_NOT_OPERATOR, NULL);
32913 goto saw_error;
32915 switch (TREE_CODE (rhs))
32917 case MULT_EXPR:
32918 case TRUNC_DIV_EXPR:
32919 case RDIV_EXPR:
32920 case PLUS_EXPR:
32921 case MINUS_EXPR:
32922 case LSHIFT_EXPR:
32923 case RSHIFT_EXPR:
32924 case BIT_AND_EXPR:
32925 case BIT_IOR_EXPR:
32926 case BIT_XOR_EXPR:
32927 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
32929 if (cp_parser_parse_definitely (parser))
32931 opcode = TREE_CODE (rhs);
32932 rhs1 = TREE_OPERAND (rhs, 0);
32933 rhs = TREE_OPERAND (rhs, 1);
32934 goto stmt_done;
32936 else
32937 goto saw_error;
32939 break;
32940 default:
32941 break;
32943 cp_parser_abort_tentative_parse (parser);
32944 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
32946 rhs = cp_parser_expression (parser);
32947 if (rhs == error_mark_node)
32948 goto saw_error;
32949 opcode = NOP_EXPR;
32950 rhs1 = NULL_TREE;
32951 goto stmt_done;
32953 cp_parser_error (parser,
32954 "invalid form of %<#pragma omp atomic%>");
32955 goto saw_error;
32957 if (!cp_parser_parse_definitely (parser))
32958 goto saw_error;
32959 switch (token->type)
32961 case CPP_SEMICOLON:
32962 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
32964 code = OMP_ATOMIC_CAPTURE_OLD;
32965 v = lhs;
32966 lhs = NULL_TREE;
32967 lhs1 = rhs1;
32968 rhs1 = NULL_TREE;
32969 cp_lexer_consume_token (parser->lexer);
32970 goto restart;
32972 else if (structured_block)
32974 opcode = NOP_EXPR;
32975 rhs = rhs1;
32976 rhs1 = NULL_TREE;
32977 goto stmt_done;
32979 cp_parser_error (parser,
32980 "invalid form of %<#pragma omp atomic%>");
32981 goto saw_error;
32982 case CPP_MULT:
32983 opcode = MULT_EXPR;
32984 break;
32985 case CPP_DIV:
32986 opcode = TRUNC_DIV_EXPR;
32987 break;
32988 case CPP_PLUS:
32989 opcode = PLUS_EXPR;
32990 break;
32991 case CPP_MINUS:
32992 opcode = MINUS_EXPR;
32993 break;
32994 case CPP_LSHIFT:
32995 opcode = LSHIFT_EXPR;
32996 break;
32997 case CPP_RSHIFT:
32998 opcode = RSHIFT_EXPR;
32999 break;
33000 case CPP_AND:
33001 opcode = BIT_AND_EXPR;
33002 break;
33003 case CPP_OR:
33004 opcode = BIT_IOR_EXPR;
33005 break;
33006 case CPP_XOR:
33007 opcode = BIT_XOR_EXPR;
33008 break;
33009 default:
33010 cp_parser_error (parser,
33011 "invalid operator for %<#pragma omp atomic%>");
33012 goto saw_error;
33014 oprec = TOKEN_PRECEDENCE (token);
33015 gcc_assert (oprec != PREC_NOT_OPERATOR);
33016 if (commutative_tree_code (opcode))
33017 oprec = (enum cp_parser_prec) (oprec - 1);
33018 cp_lexer_consume_token (parser->lexer);
33019 rhs = cp_parser_binary_expression (parser, false, false,
33020 oprec, NULL);
33021 if (rhs == error_mark_node)
33022 goto saw_error;
33023 goto stmt_done;
33024 /* FALLTHROUGH */
33025 default:
33026 cp_parser_error (parser,
33027 "invalid operator for %<#pragma omp atomic%>");
33028 goto saw_error;
33030 cp_lexer_consume_token (parser->lexer);
33032 rhs = cp_parser_expression (parser);
33033 if (rhs == error_mark_node)
33034 goto saw_error;
33035 break;
33037 stmt_done:
33038 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33040 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
33041 goto saw_error;
33042 v = cp_parser_unary_expression (parser);
33043 if (v == error_mark_node)
33044 goto saw_error;
33045 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33046 goto saw_error;
33047 lhs1 = cp_parser_unary_expression (parser);
33048 if (lhs1 == error_mark_node)
33049 goto saw_error;
33051 if (structured_block)
33053 cp_parser_consume_semicolon_at_end_of_statement (parser);
33054 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
33056 done:
33057 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
33058 if (!structured_block)
33059 cp_parser_consume_semicolon_at_end_of_statement (parser);
33060 return;
33062 saw_error:
33063 cp_parser_skip_to_end_of_block_or_statement (parser);
33064 if (structured_block)
33066 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33067 cp_lexer_consume_token (parser->lexer);
33068 else if (code == OMP_ATOMIC_CAPTURE_NEW)
33070 cp_parser_skip_to_end_of_block_or_statement (parser);
33071 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33072 cp_lexer_consume_token (parser->lexer);
33078 /* OpenMP 2.5:
33079 # pragma omp barrier new-line */
33081 static void
33082 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
33084 cp_parser_require_pragma_eol (parser, pragma_tok);
33085 finish_omp_barrier ();
33088 /* OpenMP 2.5:
33089 # pragma omp critical [(name)] new-line
33090 structured-block
33092 OpenMP 4.5:
33093 # pragma omp critical [(name) [hint(expression)]] new-line
33094 structured-block */
33096 #define OMP_CRITICAL_CLAUSE_MASK \
33097 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
33099 static tree
33100 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
33102 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
33104 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33106 cp_lexer_consume_token (parser->lexer);
33108 name = cp_parser_identifier (parser);
33110 if (name == error_mark_node
33111 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33112 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33113 /*or_comma=*/false,
33114 /*consume_paren=*/true);
33115 if (name == error_mark_node)
33116 name = NULL;
33118 clauses = cp_parser_omp_all_clauses (parser,
33119 OMP_CRITICAL_CLAUSE_MASK,
33120 "#pragma omp critical", pragma_tok);
33122 else
33123 cp_parser_require_pragma_eol (parser, pragma_tok);
33125 stmt = cp_parser_omp_structured_block (parser, if_p);
33126 return c_finish_omp_critical (input_location, stmt, name, clauses);
33129 /* OpenMP 2.5:
33130 # pragma omp flush flush-vars[opt] new-line
33132 flush-vars:
33133 ( variable-list ) */
33135 static void
33136 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
33138 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33139 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
33140 cp_parser_require_pragma_eol (parser, pragma_tok);
33142 finish_omp_flush ();
33145 /* Helper function, to parse omp for increment expression. */
33147 static tree
33148 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
33150 tree cond = cp_parser_binary_expression (parser, false, true,
33151 PREC_NOT_OPERATOR, NULL);
33152 if (cond == error_mark_node
33153 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33155 cp_parser_skip_to_end_of_statement (parser);
33156 return error_mark_node;
33159 switch (TREE_CODE (cond))
33161 case GT_EXPR:
33162 case GE_EXPR:
33163 case LT_EXPR:
33164 case LE_EXPR:
33165 break;
33166 case NE_EXPR:
33167 if (code == CILK_SIMD || code == CILK_FOR)
33168 break;
33169 /* Fall through: OpenMP disallows NE_EXPR. */
33170 default:
33171 return error_mark_node;
33174 /* If decl is an iterator, preserve LHS and RHS of the relational
33175 expr until finish_omp_for. */
33176 if (decl
33177 && (type_dependent_expression_p (decl)
33178 || CLASS_TYPE_P (TREE_TYPE (decl))))
33179 return cond;
33181 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
33182 TREE_CODE (cond),
33183 TREE_OPERAND (cond, 0), ERROR_MARK,
33184 TREE_OPERAND (cond, 1), ERROR_MARK,
33185 /*overload=*/NULL, tf_warning_or_error);
33188 /* Helper function, to parse omp for increment expression. */
33190 static tree
33191 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
33193 cp_token *token = cp_lexer_peek_token (parser->lexer);
33194 enum tree_code op;
33195 tree lhs, rhs;
33196 cp_id_kind idk;
33197 bool decl_first;
33199 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33201 op = (token->type == CPP_PLUS_PLUS
33202 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
33203 cp_lexer_consume_token (parser->lexer);
33204 lhs = cp_parser_simple_cast_expression (parser);
33205 if (lhs != decl
33206 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33207 return error_mark_node;
33208 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33211 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
33212 if (lhs != decl
33213 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33214 return error_mark_node;
33216 token = cp_lexer_peek_token (parser->lexer);
33217 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33219 op = (token->type == CPP_PLUS_PLUS
33220 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
33221 cp_lexer_consume_token (parser->lexer);
33222 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33225 op = cp_parser_assignment_operator_opt (parser);
33226 if (op == ERROR_MARK)
33227 return error_mark_node;
33229 if (op != NOP_EXPR)
33231 rhs = cp_parser_assignment_expression (parser);
33232 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
33233 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33236 lhs = cp_parser_binary_expression (parser, false, false,
33237 PREC_ADDITIVE_EXPRESSION, NULL);
33238 token = cp_lexer_peek_token (parser->lexer);
33239 decl_first = (lhs == decl
33240 || (processing_template_decl && cp_tree_equal (lhs, decl)));
33241 if (decl_first)
33242 lhs = NULL_TREE;
33243 if (token->type != CPP_PLUS
33244 && token->type != CPP_MINUS)
33245 return error_mark_node;
33249 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
33250 cp_lexer_consume_token (parser->lexer);
33251 rhs = cp_parser_binary_expression (parser, false, false,
33252 PREC_ADDITIVE_EXPRESSION, NULL);
33253 token = cp_lexer_peek_token (parser->lexer);
33254 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
33256 if (lhs == NULL_TREE)
33258 if (op == PLUS_EXPR)
33259 lhs = rhs;
33260 else
33261 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
33262 tf_warning_or_error);
33264 else
33265 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
33266 ERROR_MARK, NULL, tf_warning_or_error);
33269 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
33271 if (!decl_first)
33273 if ((rhs != decl
33274 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
33275 || op == MINUS_EXPR)
33276 return error_mark_node;
33277 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
33279 else
33280 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
33282 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33285 /* Parse the initialization statement of either an OpenMP for loop or
33286 a Cilk Plus for loop.
33288 Return true if the resulting construct should have an
33289 OMP_CLAUSE_PRIVATE added to it. */
33291 static tree
33292 cp_parser_omp_for_loop_init (cp_parser *parser,
33293 enum tree_code code,
33294 tree &this_pre_body,
33295 vec<tree, va_gc> *for_block,
33296 tree &init,
33297 tree &orig_init,
33298 tree &decl,
33299 tree &real_decl)
33301 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33302 return NULL_TREE;
33304 tree add_private_clause = NULL_TREE;
33306 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
33308 init-expr:
33309 var = lb
33310 integer-type var = lb
33311 random-access-iterator-type var = lb
33312 pointer-type var = lb
33314 cp_decl_specifier_seq type_specifiers;
33316 /* First, try to parse as an initialized declaration. See
33317 cp_parser_condition, from whence the bulk of this is copied. */
33319 cp_parser_parse_tentatively (parser);
33320 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
33321 /*is_trailing_return=*/false,
33322 &type_specifiers);
33323 if (cp_parser_parse_definitely (parser))
33325 /* If parsing a type specifier seq succeeded, then this
33326 MUST be a initialized declaration. */
33327 tree asm_specification, attributes;
33328 cp_declarator *declarator;
33330 declarator = cp_parser_declarator (parser,
33331 CP_PARSER_DECLARATOR_NAMED,
33332 /*ctor_dtor_or_conv_p=*/NULL,
33333 /*parenthesized_p=*/NULL,
33334 /*member_p=*/false,
33335 /*friend_p=*/false);
33336 attributes = cp_parser_attributes_opt (parser);
33337 asm_specification = cp_parser_asm_specification_opt (parser);
33339 if (declarator == cp_error_declarator)
33340 cp_parser_skip_to_end_of_statement (parser);
33342 else
33344 tree pushed_scope, auto_node;
33346 decl = start_decl (declarator, &type_specifiers,
33347 SD_INITIALIZED, attributes,
33348 /*prefix_attributes=*/NULL_TREE,
33349 &pushed_scope);
33351 auto_node = type_uses_auto (TREE_TYPE (decl));
33352 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
33354 if (cp_lexer_next_token_is (parser->lexer,
33355 CPP_OPEN_PAREN))
33357 if (code != CILK_SIMD && code != CILK_FOR)
33358 error ("parenthesized initialization is not allowed in "
33359 "OpenMP %<for%> loop");
33360 else
33361 error ("parenthesized initialization is "
33362 "not allowed in for-loop");
33364 else
33365 /* Trigger an error. */
33366 cp_parser_require (parser, CPP_EQ, RT_EQ);
33368 init = error_mark_node;
33369 cp_parser_skip_to_end_of_statement (parser);
33371 else if (CLASS_TYPE_P (TREE_TYPE (decl))
33372 || type_dependent_expression_p (decl)
33373 || auto_node)
33375 bool is_direct_init, is_non_constant_init;
33377 init = cp_parser_initializer (parser,
33378 &is_direct_init,
33379 &is_non_constant_init);
33381 if (auto_node)
33383 TREE_TYPE (decl)
33384 = do_auto_deduction (TREE_TYPE (decl), init,
33385 auto_node);
33387 if (!CLASS_TYPE_P (TREE_TYPE (decl))
33388 && !type_dependent_expression_p (decl))
33389 goto non_class;
33392 cp_finish_decl (decl, init, !is_non_constant_init,
33393 asm_specification,
33394 LOOKUP_ONLYCONVERTING);
33395 orig_init = init;
33396 if (CLASS_TYPE_P (TREE_TYPE (decl)))
33398 vec_safe_push (for_block, this_pre_body);
33399 init = NULL_TREE;
33401 else
33402 init = pop_stmt_list (this_pre_body);
33403 this_pre_body = NULL_TREE;
33405 else
33407 /* Consume '='. */
33408 cp_lexer_consume_token (parser->lexer);
33409 init = cp_parser_assignment_expression (parser);
33411 non_class:
33412 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
33413 init = error_mark_node;
33414 else
33415 cp_finish_decl (decl, NULL_TREE,
33416 /*init_const_expr_p=*/false,
33417 asm_specification,
33418 LOOKUP_ONLYCONVERTING);
33421 if (pushed_scope)
33422 pop_scope (pushed_scope);
33425 else
33427 cp_id_kind idk;
33428 /* If parsing a type specifier sequence failed, then
33429 this MUST be a simple expression. */
33430 if (code == CILK_FOR)
33431 error ("%<_Cilk_for%> allows expression instead of declaration only "
33432 "in C, not in C++");
33433 cp_parser_parse_tentatively (parser);
33434 decl = cp_parser_primary_expression (parser, false, false,
33435 false, &idk);
33436 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
33437 if (!cp_parser_error_occurred (parser)
33438 && decl
33439 && (TREE_CODE (decl) == COMPONENT_REF
33440 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
33442 cp_parser_abort_tentative_parse (parser);
33443 cp_parser_parse_tentatively (parser);
33444 cp_token *token = cp_lexer_peek_token (parser->lexer);
33445 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
33446 /*check_dependency_p=*/true,
33447 /*template_p=*/NULL,
33448 /*declarator_p=*/false,
33449 /*optional_p=*/false);
33450 if (name != error_mark_node
33451 && last_tok == cp_lexer_peek_token (parser->lexer))
33453 decl = cp_parser_lookup_name_simple (parser, name,
33454 token->location);
33455 if (TREE_CODE (decl) == FIELD_DECL)
33456 add_private_clause = omp_privatize_field (decl, false);
33458 cp_parser_abort_tentative_parse (parser);
33459 cp_parser_parse_tentatively (parser);
33460 decl = cp_parser_primary_expression (parser, false, false,
33461 false, &idk);
33463 if (!cp_parser_error_occurred (parser)
33464 && decl
33465 && DECL_P (decl)
33466 && CLASS_TYPE_P (TREE_TYPE (decl)))
33468 tree rhs;
33470 cp_parser_parse_definitely (parser);
33471 cp_parser_require (parser, CPP_EQ, RT_EQ);
33472 rhs = cp_parser_assignment_expression (parser);
33473 orig_init = rhs;
33474 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
33475 decl, NOP_EXPR,
33476 rhs,
33477 tf_warning_or_error));
33478 if (!add_private_clause)
33479 add_private_clause = decl;
33481 else
33483 decl = NULL;
33484 cp_parser_abort_tentative_parse (parser);
33485 init = cp_parser_expression (parser);
33486 if (init)
33488 if (TREE_CODE (init) == MODIFY_EXPR
33489 || TREE_CODE (init) == MODOP_EXPR)
33490 real_decl = TREE_OPERAND (init, 0);
33494 return add_private_clause;
33497 /* Parse the restricted form of the for statement allowed by OpenMP. */
33499 static tree
33500 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
33501 tree *cclauses, bool *if_p)
33503 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
33504 tree real_decl, initv, condv, incrv, declv;
33505 tree this_pre_body, cl, ordered_cl = NULL_TREE;
33506 location_t loc_first;
33507 bool collapse_err = false;
33508 int i, collapse = 1, ordered = 0, count, nbraces = 0;
33509 vec<tree, va_gc> *for_block = make_tree_vector ();
33510 auto_vec<tree, 4> orig_inits;
33512 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
33513 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
33514 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
33515 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
33516 && OMP_CLAUSE_ORDERED_EXPR (cl))
33518 ordered_cl = cl;
33519 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
33522 if (ordered && ordered < collapse)
33524 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
33525 "%<ordered%> clause parameter is less than %<collapse%>");
33526 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
33527 = build_int_cst (NULL_TREE, collapse);
33528 ordered = collapse;
33530 if (ordered)
33532 for (tree *pc = &clauses; *pc; )
33533 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
33535 error_at (OMP_CLAUSE_LOCATION (*pc),
33536 "%<linear%> clause may not be specified together "
33537 "with %<ordered%> clause with a parameter");
33538 *pc = OMP_CLAUSE_CHAIN (*pc);
33540 else
33541 pc = &OMP_CLAUSE_CHAIN (*pc);
33544 gcc_assert (collapse >= 1 && ordered >= 0);
33545 count = ordered ? ordered : collapse;
33547 declv = make_tree_vec (count);
33548 initv = make_tree_vec (count);
33549 condv = make_tree_vec (count);
33550 incrv = make_tree_vec (count);
33552 loc_first = cp_lexer_peek_token (parser->lexer)->location;
33554 for (i = 0; i < count; i++)
33556 int bracecount = 0;
33557 tree add_private_clause = NULL_TREE;
33558 location_t loc;
33560 if (code != CILK_FOR
33561 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33563 cp_parser_error (parser, "for statement expected");
33564 return NULL;
33566 if (code == CILK_FOR
33567 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
33569 cp_parser_error (parser, "_Cilk_for statement expected");
33570 return NULL;
33572 loc = cp_lexer_consume_token (parser->lexer)->location;
33574 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33575 return NULL;
33577 init = orig_init = decl = real_decl = NULL;
33578 this_pre_body = push_stmt_list ();
33580 add_private_clause
33581 = cp_parser_omp_for_loop_init (parser, code,
33582 this_pre_body, for_block,
33583 init, orig_init, decl, real_decl);
33585 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33586 if (this_pre_body)
33588 this_pre_body = pop_stmt_list (this_pre_body);
33589 if (pre_body)
33591 tree t = pre_body;
33592 pre_body = push_stmt_list ();
33593 add_stmt (t);
33594 add_stmt (this_pre_body);
33595 pre_body = pop_stmt_list (pre_body);
33597 else
33598 pre_body = this_pre_body;
33601 if (decl)
33602 real_decl = decl;
33603 if (cclauses != NULL
33604 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
33605 && real_decl != NULL_TREE)
33607 tree *c;
33608 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
33609 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
33610 && OMP_CLAUSE_DECL (*c) == real_decl)
33612 error_at (loc, "iteration variable %qD"
33613 " should not be firstprivate", real_decl);
33614 *c = OMP_CLAUSE_CHAIN (*c);
33616 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
33617 && OMP_CLAUSE_DECL (*c) == real_decl)
33619 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
33620 tree l = *c;
33621 *c = OMP_CLAUSE_CHAIN (*c);
33622 if (code == OMP_SIMD)
33624 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33625 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
33627 else
33629 OMP_CLAUSE_CHAIN (l) = clauses;
33630 clauses = l;
33632 add_private_clause = NULL_TREE;
33634 else
33636 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
33637 && OMP_CLAUSE_DECL (*c) == real_decl)
33638 add_private_clause = NULL_TREE;
33639 c = &OMP_CLAUSE_CHAIN (*c);
33643 if (add_private_clause)
33645 tree c;
33646 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
33648 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
33649 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
33650 && OMP_CLAUSE_DECL (c) == decl)
33651 break;
33652 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
33653 && OMP_CLAUSE_DECL (c) == decl)
33654 error_at (loc, "iteration variable %qD "
33655 "should not be firstprivate",
33656 decl);
33657 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
33658 && OMP_CLAUSE_DECL (c) == decl)
33659 error_at (loc, "iteration variable %qD should not be reduction",
33660 decl);
33662 if (c == NULL)
33664 if (code != OMP_SIMD)
33665 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
33666 else if (collapse == 1)
33667 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33668 else
33669 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
33670 OMP_CLAUSE_DECL (c) = add_private_clause;
33671 c = finish_omp_clauses (c, C_ORT_OMP);
33672 if (c)
33674 OMP_CLAUSE_CHAIN (c) = clauses;
33675 clauses = c;
33676 /* For linear, signal that we need to fill up
33677 the so far unknown linear step. */
33678 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
33679 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
33684 cond = NULL;
33685 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33686 cond = cp_parser_omp_for_cond (parser, decl, code);
33687 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33689 incr = NULL;
33690 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
33692 /* If decl is an iterator, preserve the operator on decl
33693 until finish_omp_for. */
33694 if (real_decl
33695 && ((processing_template_decl
33696 && (TREE_TYPE (real_decl) == NULL_TREE
33697 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
33698 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
33699 incr = cp_parser_omp_for_incr (parser, real_decl);
33700 else
33701 incr = cp_parser_expression (parser);
33702 if (!EXPR_HAS_LOCATION (incr))
33703 protected_set_expr_location (incr, input_location);
33706 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33707 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33708 /*or_comma=*/false,
33709 /*consume_paren=*/true);
33711 TREE_VEC_ELT (declv, i) = decl;
33712 TREE_VEC_ELT (initv, i) = init;
33713 TREE_VEC_ELT (condv, i) = cond;
33714 TREE_VEC_ELT (incrv, i) = incr;
33715 if (orig_init)
33717 orig_inits.safe_grow_cleared (i + 1);
33718 orig_inits[i] = orig_init;
33721 if (i == count - 1)
33722 break;
33724 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
33725 in between the collapsed for loops to be still considered perfectly
33726 nested. Hopefully the final version clarifies this.
33727 For now handle (multiple) {'s and empty statements. */
33728 cp_parser_parse_tentatively (parser);
33731 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33732 break;
33733 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33735 cp_lexer_consume_token (parser->lexer);
33736 bracecount++;
33738 else if (bracecount
33739 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33740 cp_lexer_consume_token (parser->lexer);
33741 else
33743 loc = cp_lexer_peek_token (parser->lexer)->location;
33744 error_at (loc, "not enough collapsed for loops");
33745 collapse_err = true;
33746 cp_parser_abort_tentative_parse (parser);
33747 declv = NULL_TREE;
33748 break;
33751 while (1);
33753 if (declv)
33755 cp_parser_parse_definitely (parser);
33756 nbraces += bracecount;
33760 if (nbraces)
33761 if_p = NULL;
33763 /* Note that we saved the original contents of this flag when we entered
33764 the structured block, and so we don't need to re-save it here. */
33765 if (code == CILK_SIMD || code == CILK_FOR)
33766 parser->in_statement = IN_CILK_SIMD_FOR;
33767 else
33768 parser->in_statement = IN_OMP_FOR;
33770 /* Note that the grammar doesn't call for a structured block here,
33771 though the loop as a whole is a structured block. */
33772 body = push_stmt_list ();
33773 cp_parser_statement (parser, NULL_TREE, false, if_p);
33774 body = pop_stmt_list (body);
33776 if (declv == NULL_TREE)
33777 ret = NULL_TREE;
33778 else
33779 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
33780 body, pre_body, &orig_inits, clauses);
33782 while (nbraces)
33784 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33786 cp_lexer_consume_token (parser->lexer);
33787 nbraces--;
33789 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33790 cp_lexer_consume_token (parser->lexer);
33791 else
33793 if (!collapse_err)
33795 error_at (cp_lexer_peek_token (parser->lexer)->location,
33796 "collapsed loops not perfectly nested");
33798 collapse_err = true;
33799 cp_parser_statement_seq_opt (parser, NULL);
33800 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
33801 break;
33805 while (!for_block->is_empty ())
33806 add_stmt (pop_stmt_list (for_block->pop ()));
33807 release_tree_vector (for_block);
33809 return ret;
33812 /* Helper function for OpenMP parsing, split clauses and call
33813 finish_omp_clauses on each of the set of clauses afterwards. */
33815 static void
33816 cp_omp_split_clauses (location_t loc, enum tree_code code,
33817 omp_clause_mask mask, tree clauses, tree *cclauses)
33819 int i;
33820 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
33821 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
33822 if (cclauses[i])
33823 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
33826 /* OpenMP 4.0:
33827 #pragma omp simd simd-clause[optseq] new-line
33828 for-loop */
33830 #define OMP_SIMD_CLAUSE_MASK \
33831 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
33832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
33833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
33835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33840 static tree
33841 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
33842 char *p_name, omp_clause_mask mask, tree *cclauses,
33843 bool *if_p)
33845 tree clauses, sb, ret;
33846 unsigned int save;
33847 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33849 strcat (p_name, " simd");
33850 mask |= OMP_SIMD_CLAUSE_MASK;
33852 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33853 cclauses == NULL);
33854 if (cclauses)
33856 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
33857 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
33858 tree c = find_omp_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
33859 OMP_CLAUSE_ORDERED);
33860 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
33862 error_at (OMP_CLAUSE_LOCATION (c),
33863 "%<ordered%> clause with parameter may not be specified "
33864 "on %qs construct", p_name);
33865 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
33869 sb = begin_omp_structured_block ();
33870 save = cp_parser_begin_omp_structured_block (parser);
33872 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
33874 cp_parser_end_omp_structured_block (parser, save);
33875 add_stmt (finish_omp_structured_block (sb));
33877 return ret;
33880 /* OpenMP 2.5:
33881 #pragma omp for for-clause[optseq] new-line
33882 for-loop
33884 OpenMP 4.0:
33885 #pragma omp for simd for-simd-clause[optseq] new-line
33886 for-loop */
33888 #define OMP_FOR_CLAUSE_MASK \
33889 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
33895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
33896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
33897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33899 static tree
33900 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
33901 char *p_name, omp_clause_mask mask, tree *cclauses,
33902 bool *if_p)
33904 tree clauses, sb, ret;
33905 unsigned int save;
33906 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33908 strcat (p_name, " for");
33909 mask |= OMP_FOR_CLAUSE_MASK;
33910 /* parallel for{, simd} disallows nowait clause, but for
33911 target {teams distribute ,}parallel for{, simd} it should be accepted. */
33912 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
33913 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
33914 /* Composite distribute parallel for{, simd} disallows ordered clause. */
33915 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33916 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
33918 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33920 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33921 const char *p = IDENTIFIER_POINTER (id);
33923 if (strcmp (p, "simd") == 0)
33925 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
33926 if (cclauses == NULL)
33927 cclauses = cclauses_buf;
33929 cp_lexer_consume_token (parser->lexer);
33930 if (!flag_openmp) /* flag_openmp_simd */
33931 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33932 cclauses, if_p);
33933 sb = begin_omp_structured_block ();
33934 save = cp_parser_begin_omp_structured_block (parser);
33935 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33936 cclauses, if_p);
33937 cp_parser_end_omp_structured_block (parser, save);
33938 tree body = finish_omp_structured_block (sb);
33939 if (ret == NULL)
33940 return ret;
33941 ret = make_node (OMP_FOR);
33942 TREE_TYPE (ret) = void_type_node;
33943 OMP_FOR_BODY (ret) = body;
33944 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33945 SET_EXPR_LOCATION (ret, loc);
33946 add_stmt (ret);
33947 return ret;
33950 if (!flag_openmp) /* flag_openmp_simd */
33952 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33953 return NULL_TREE;
33956 /* Composite distribute parallel for disallows linear clause. */
33957 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33958 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
33960 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33961 cclauses == NULL);
33962 if (cclauses)
33964 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
33965 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33968 sb = begin_omp_structured_block ();
33969 save = cp_parser_begin_omp_structured_block (parser);
33971 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
33973 cp_parser_end_omp_structured_block (parser, save);
33974 add_stmt (finish_omp_structured_block (sb));
33976 return ret;
33979 /* OpenMP 2.5:
33980 # pragma omp master new-line
33981 structured-block */
33983 static tree
33984 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
33986 cp_parser_require_pragma_eol (parser, pragma_tok);
33987 return c_finish_omp_master (input_location,
33988 cp_parser_omp_structured_block (parser, if_p));
33991 /* OpenMP 2.5:
33992 # pragma omp ordered new-line
33993 structured-block
33995 OpenMP 4.5:
33996 # pragma omp ordered ordered-clauses new-line
33997 structured-block */
33999 #define OMP_ORDERED_CLAUSE_MASK \
34000 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
34001 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
34003 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
34004 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
34006 static bool
34007 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
34008 enum pragma_context context, bool *if_p)
34010 location_t loc = pragma_tok->location;
34012 if (context != pragma_stmt && context != pragma_compound)
34014 cp_parser_error (parser, "expected declaration specifiers");
34015 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34016 return false;
34019 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34021 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34022 const char *p = IDENTIFIER_POINTER (id);
34024 if (strcmp (p, "depend") == 0)
34026 if (context == pragma_stmt)
34028 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
34029 "%<depend%> clause may only be used in compound "
34030 "statements");
34031 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34032 return false;
34034 tree clauses
34035 = cp_parser_omp_all_clauses (parser,
34036 OMP_ORDERED_DEPEND_CLAUSE_MASK,
34037 "#pragma omp ordered", pragma_tok);
34038 c_finish_omp_ordered (loc, clauses, NULL_TREE);
34039 return false;
34043 tree clauses
34044 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
34045 "#pragma omp ordered", pragma_tok);
34046 c_finish_omp_ordered (loc, clauses,
34047 cp_parser_omp_structured_block (parser, if_p));
34048 return true;
34051 /* OpenMP 2.5:
34053 section-scope:
34054 { section-sequence }
34056 section-sequence:
34057 section-directive[opt] structured-block
34058 section-sequence section-directive structured-block */
34060 static tree
34061 cp_parser_omp_sections_scope (cp_parser *parser)
34063 tree stmt, substmt;
34064 bool error_suppress = false;
34065 cp_token *tok;
34067 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
34068 return NULL_TREE;
34070 stmt = push_stmt_list ();
34072 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
34073 != PRAGMA_OMP_SECTION)
34075 substmt = cp_parser_omp_structured_block (parser, NULL);
34076 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34077 add_stmt (substmt);
34080 while (1)
34082 tok = cp_lexer_peek_token (parser->lexer);
34083 if (tok->type == CPP_CLOSE_BRACE)
34084 break;
34085 if (tok->type == CPP_EOF)
34086 break;
34088 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
34090 cp_lexer_consume_token (parser->lexer);
34091 cp_parser_require_pragma_eol (parser, tok);
34092 error_suppress = false;
34094 else if (!error_suppress)
34096 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
34097 error_suppress = true;
34100 substmt = cp_parser_omp_structured_block (parser, NULL);
34101 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34102 add_stmt (substmt);
34104 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34106 substmt = pop_stmt_list (stmt);
34108 stmt = make_node (OMP_SECTIONS);
34109 TREE_TYPE (stmt) = void_type_node;
34110 OMP_SECTIONS_BODY (stmt) = substmt;
34112 add_stmt (stmt);
34113 return stmt;
34116 /* OpenMP 2.5:
34117 # pragma omp sections sections-clause[optseq] newline
34118 sections-scope */
34120 #define OMP_SECTIONS_CLAUSE_MASK \
34121 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34127 static tree
34128 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
34129 char *p_name, omp_clause_mask mask, tree *cclauses)
34131 tree clauses, ret;
34132 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34134 strcat (p_name, " sections");
34135 mask |= OMP_SECTIONS_CLAUSE_MASK;
34136 if (cclauses)
34137 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34139 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34140 cclauses == NULL);
34141 if (cclauses)
34143 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
34144 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
34147 ret = cp_parser_omp_sections_scope (parser);
34148 if (ret)
34149 OMP_SECTIONS_CLAUSES (ret) = clauses;
34151 return ret;
34154 /* OpenMP 2.5:
34155 # pragma omp parallel parallel-clause[optseq] new-line
34156 structured-block
34157 # pragma omp parallel for parallel-for-clause[optseq] new-line
34158 structured-block
34159 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
34160 structured-block
34162 OpenMP 4.0:
34163 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
34164 structured-block */
34166 #define OMP_PARALLEL_CLAUSE_MASK \
34167 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34168 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
34171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
34173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
34175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
34177 static tree
34178 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
34179 char *p_name, omp_clause_mask mask, tree *cclauses,
34180 bool *if_p)
34182 tree stmt, clauses, block;
34183 unsigned int save;
34184 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34186 strcat (p_name, " parallel");
34187 mask |= OMP_PARALLEL_CLAUSE_MASK;
34188 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
34189 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
34190 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
34191 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
34193 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34195 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34196 if (cclauses == NULL)
34197 cclauses = cclauses_buf;
34199 cp_lexer_consume_token (parser->lexer);
34200 if (!flag_openmp) /* flag_openmp_simd */
34201 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
34202 if_p);
34203 block = begin_omp_parallel ();
34204 save = cp_parser_begin_omp_structured_block (parser);
34205 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
34206 if_p);
34207 cp_parser_end_omp_structured_block (parser, save);
34208 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34209 block);
34210 if (ret == NULL_TREE)
34211 return ret;
34212 OMP_PARALLEL_COMBINED (stmt) = 1;
34213 return stmt;
34215 /* When combined with distribute, parallel has to be followed by for.
34216 #pragma omp target parallel is allowed though. */
34217 else if (cclauses
34218 && (mask & (OMP_CLAUSE_MASK_1
34219 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34221 error_at (loc, "expected %<for%> after %qs", p_name);
34222 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34223 return NULL_TREE;
34225 else if (!flag_openmp) /* flag_openmp_simd */
34227 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34228 return NULL_TREE;
34230 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34232 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34233 const char *p = IDENTIFIER_POINTER (id);
34234 if (strcmp (p, "sections") == 0)
34236 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34237 cclauses = cclauses_buf;
34239 cp_lexer_consume_token (parser->lexer);
34240 block = begin_omp_parallel ();
34241 save = cp_parser_begin_omp_structured_block (parser);
34242 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
34243 cp_parser_end_omp_structured_block (parser, save);
34244 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34245 block);
34246 OMP_PARALLEL_COMBINED (stmt) = 1;
34247 return stmt;
34251 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34252 cclauses == NULL);
34253 if (cclauses)
34255 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
34256 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
34259 block = begin_omp_parallel ();
34260 save = cp_parser_begin_omp_structured_block (parser);
34261 cp_parser_statement (parser, NULL_TREE, false, if_p);
34262 cp_parser_end_omp_structured_block (parser, save);
34263 stmt = finish_omp_parallel (clauses, block);
34264 return stmt;
34267 /* OpenMP 2.5:
34268 # pragma omp single single-clause[optseq] new-line
34269 structured-block */
34271 #define OMP_SINGLE_CLAUSE_MASK \
34272 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
34275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34277 static tree
34278 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34280 tree stmt = make_node (OMP_SINGLE);
34281 TREE_TYPE (stmt) = void_type_node;
34283 OMP_SINGLE_CLAUSES (stmt)
34284 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
34285 "#pragma omp single", pragma_tok);
34286 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34288 return add_stmt (stmt);
34291 /* OpenMP 3.0:
34292 # pragma omp task task-clause[optseq] new-line
34293 structured-block */
34295 #define OMP_TASK_CLAUSE_MASK \
34296 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
34298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
34299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
34303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
34304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
34307 static tree
34308 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34310 tree clauses, block;
34311 unsigned int save;
34313 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
34314 "#pragma omp task", pragma_tok);
34315 block = begin_omp_task ();
34316 save = cp_parser_begin_omp_structured_block (parser);
34317 cp_parser_statement (parser, NULL_TREE, false, if_p);
34318 cp_parser_end_omp_structured_block (parser, save);
34319 return finish_omp_task (clauses, block);
34322 /* OpenMP 3.0:
34323 # pragma omp taskwait new-line */
34325 static void
34326 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
34328 cp_parser_require_pragma_eol (parser, pragma_tok);
34329 finish_omp_taskwait ();
34332 /* OpenMP 3.1:
34333 # pragma omp taskyield new-line */
34335 static void
34336 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
34338 cp_parser_require_pragma_eol (parser, pragma_tok);
34339 finish_omp_taskyield ();
34342 /* OpenMP 4.0:
34343 # pragma omp taskgroup new-line
34344 structured-block */
34346 static tree
34347 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34349 cp_parser_require_pragma_eol (parser, pragma_tok);
34350 return c_finish_omp_taskgroup (input_location,
34351 cp_parser_omp_structured_block (parser,
34352 if_p));
34356 /* OpenMP 2.5:
34357 # pragma omp threadprivate (variable-list) */
34359 static void
34360 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
34362 tree vars;
34364 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34365 cp_parser_require_pragma_eol (parser, pragma_tok);
34367 finish_omp_threadprivate (vars);
34370 /* OpenMP 4.0:
34371 # pragma omp cancel cancel-clause[optseq] new-line */
34373 #define OMP_CANCEL_CLAUSE_MASK \
34374 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
34375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
34376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
34377 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
34378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
34380 static void
34381 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
34383 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
34384 "#pragma omp cancel", pragma_tok);
34385 finish_omp_cancel (clauses);
34388 /* OpenMP 4.0:
34389 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
34391 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
34392 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
34393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
34394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
34395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
34397 static void
34398 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
34399 enum pragma_context context)
34401 tree clauses;
34402 bool point_seen = false;
34404 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34406 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34407 const char *p = IDENTIFIER_POINTER (id);
34409 if (strcmp (p, "point") == 0)
34411 cp_lexer_consume_token (parser->lexer);
34412 point_seen = true;
34415 if (!point_seen)
34417 cp_parser_error (parser, "expected %<point%>");
34418 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34419 return;
34422 if (context != pragma_compound)
34424 if (context == pragma_stmt)
34425 error_at (pragma_tok->location,
34426 "%<#pragma omp cancellation point%> may only be used in"
34427 " compound statements");
34428 else
34429 cp_parser_error (parser, "expected declaration specifiers");
34430 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34431 return;
34434 clauses = cp_parser_omp_all_clauses (parser,
34435 OMP_CANCELLATION_POINT_CLAUSE_MASK,
34436 "#pragma omp cancellation point",
34437 pragma_tok);
34438 finish_omp_cancellation_point (clauses);
34441 /* OpenMP 4.0:
34442 #pragma omp distribute distribute-clause[optseq] new-line
34443 for-loop */
34445 #define OMP_DISTRIBUTE_CLAUSE_MASK \
34446 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34447 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
34450 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34452 static tree
34453 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
34454 char *p_name, omp_clause_mask mask, tree *cclauses,
34455 bool *if_p)
34457 tree clauses, sb, ret;
34458 unsigned int save;
34459 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34461 strcat (p_name, " distribute");
34462 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
34464 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34466 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34467 const char *p = IDENTIFIER_POINTER (id);
34468 bool simd = false;
34469 bool parallel = false;
34471 if (strcmp (p, "simd") == 0)
34472 simd = true;
34473 else
34474 parallel = strcmp (p, "parallel") == 0;
34475 if (parallel || simd)
34477 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34478 if (cclauses == NULL)
34479 cclauses = cclauses_buf;
34480 cp_lexer_consume_token (parser->lexer);
34481 if (!flag_openmp) /* flag_openmp_simd */
34483 if (simd)
34484 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34485 cclauses, if_p);
34486 else
34487 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34488 cclauses, if_p);
34490 sb = begin_omp_structured_block ();
34491 save = cp_parser_begin_omp_structured_block (parser);
34492 if (simd)
34493 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34494 cclauses, if_p);
34495 else
34496 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34497 cclauses, if_p);
34498 cp_parser_end_omp_structured_block (parser, save);
34499 tree body = finish_omp_structured_block (sb);
34500 if (ret == NULL)
34501 return ret;
34502 ret = make_node (OMP_DISTRIBUTE);
34503 TREE_TYPE (ret) = void_type_node;
34504 OMP_FOR_BODY (ret) = body;
34505 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34506 SET_EXPR_LOCATION (ret, loc);
34507 add_stmt (ret);
34508 return ret;
34511 if (!flag_openmp) /* flag_openmp_simd */
34513 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34514 return NULL_TREE;
34517 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34518 cclauses == NULL);
34519 if (cclauses)
34521 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
34522 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34525 sb = begin_omp_structured_block ();
34526 save = cp_parser_begin_omp_structured_block (parser);
34528 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
34530 cp_parser_end_omp_structured_block (parser, save);
34531 add_stmt (finish_omp_structured_block (sb));
34533 return ret;
34536 /* OpenMP 4.0:
34537 # pragma omp teams teams-clause[optseq] new-line
34538 structured-block */
34540 #define OMP_TEAMS_CLAUSE_MASK \
34541 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
34546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
34547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
34549 static tree
34550 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
34551 char *p_name, omp_clause_mask mask, tree *cclauses,
34552 bool *if_p)
34554 tree clauses, sb, ret;
34555 unsigned int save;
34556 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34558 strcat (p_name, " teams");
34559 mask |= OMP_TEAMS_CLAUSE_MASK;
34561 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34563 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34564 const char *p = IDENTIFIER_POINTER (id);
34565 if (strcmp (p, "distribute") == 0)
34567 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34568 if (cclauses == NULL)
34569 cclauses = cclauses_buf;
34571 cp_lexer_consume_token (parser->lexer);
34572 if (!flag_openmp) /* flag_openmp_simd */
34573 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34574 cclauses, if_p);
34575 sb = begin_omp_structured_block ();
34576 save = cp_parser_begin_omp_structured_block (parser);
34577 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34578 cclauses, if_p);
34579 cp_parser_end_omp_structured_block (parser, save);
34580 tree body = finish_omp_structured_block (sb);
34581 if (ret == NULL)
34582 return ret;
34583 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34584 ret = make_node (OMP_TEAMS);
34585 TREE_TYPE (ret) = void_type_node;
34586 OMP_TEAMS_CLAUSES (ret) = clauses;
34587 OMP_TEAMS_BODY (ret) = body;
34588 OMP_TEAMS_COMBINED (ret) = 1;
34589 return add_stmt (ret);
34592 if (!flag_openmp) /* flag_openmp_simd */
34594 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34595 return NULL_TREE;
34598 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34599 cclauses == NULL);
34600 if (cclauses)
34602 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
34603 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34606 tree stmt = make_node (OMP_TEAMS);
34607 TREE_TYPE (stmt) = void_type_node;
34608 OMP_TEAMS_CLAUSES (stmt) = clauses;
34609 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34611 return add_stmt (stmt);
34614 /* OpenMP 4.0:
34615 # pragma omp target data target-data-clause[optseq] new-line
34616 structured-block */
34618 #define OMP_TARGET_DATA_CLAUSE_MASK \
34619 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
34624 static tree
34625 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34627 tree clauses
34628 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
34629 "#pragma omp target data", pragma_tok);
34630 int map_seen = 0;
34631 for (tree *pc = &clauses; *pc;)
34633 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34634 switch (OMP_CLAUSE_MAP_KIND (*pc))
34636 case GOMP_MAP_TO:
34637 case GOMP_MAP_ALWAYS_TO:
34638 case GOMP_MAP_FROM:
34639 case GOMP_MAP_ALWAYS_FROM:
34640 case GOMP_MAP_TOFROM:
34641 case GOMP_MAP_ALWAYS_TOFROM:
34642 case GOMP_MAP_ALLOC:
34643 map_seen = 3;
34644 break;
34645 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34646 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34647 case GOMP_MAP_ALWAYS_POINTER:
34648 break;
34649 default:
34650 map_seen |= 1;
34651 error_at (OMP_CLAUSE_LOCATION (*pc),
34652 "%<#pragma omp target data%> with map-type other "
34653 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
34654 "on %<map%> clause");
34655 *pc = OMP_CLAUSE_CHAIN (*pc);
34656 continue;
34658 pc = &OMP_CLAUSE_CHAIN (*pc);
34661 if (map_seen != 3)
34663 if (map_seen == 0)
34664 error_at (pragma_tok->location,
34665 "%<#pragma omp target data%> must contain at least "
34666 "one %<map%> clause");
34667 return NULL_TREE;
34670 tree stmt = make_node (OMP_TARGET_DATA);
34671 TREE_TYPE (stmt) = void_type_node;
34672 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
34674 keep_next_level (true);
34675 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34677 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34678 return add_stmt (stmt);
34681 /* OpenMP 4.5:
34682 # pragma omp target enter data target-enter-data-clause[optseq] new-line
34683 structured-block */
34685 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
34686 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34690 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34692 static tree
34693 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
34694 enum pragma_context context)
34696 bool data_seen = false;
34697 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34699 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34700 const char *p = IDENTIFIER_POINTER (id);
34702 if (strcmp (p, "data") == 0)
34704 cp_lexer_consume_token (parser->lexer);
34705 data_seen = true;
34708 if (!data_seen)
34710 cp_parser_error (parser, "expected %<data%>");
34711 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34712 return NULL_TREE;
34715 if (context == pragma_stmt)
34717 error_at (pragma_tok->location,
34718 "%<#pragma omp target enter data%> may only be "
34719 "used in compound statements");
34720 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34721 return NULL_TREE;
34724 tree clauses
34725 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
34726 "#pragma omp target enter data", pragma_tok);
34727 int map_seen = 0;
34728 for (tree *pc = &clauses; *pc;)
34730 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34731 switch (OMP_CLAUSE_MAP_KIND (*pc))
34733 case GOMP_MAP_TO:
34734 case GOMP_MAP_ALWAYS_TO:
34735 case GOMP_MAP_ALLOC:
34736 map_seen = 3;
34737 break;
34738 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34739 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34740 case GOMP_MAP_ALWAYS_POINTER:
34741 break;
34742 default:
34743 map_seen |= 1;
34744 error_at (OMP_CLAUSE_LOCATION (*pc),
34745 "%<#pragma omp target enter data%> with map-type other "
34746 "than %<to%> or %<alloc%> on %<map%> clause");
34747 *pc = OMP_CLAUSE_CHAIN (*pc);
34748 continue;
34750 pc = &OMP_CLAUSE_CHAIN (*pc);
34753 if (map_seen != 3)
34755 if (map_seen == 0)
34756 error_at (pragma_tok->location,
34757 "%<#pragma omp target enter data%> must contain at least "
34758 "one %<map%> clause");
34759 return NULL_TREE;
34762 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
34763 TREE_TYPE (stmt) = void_type_node;
34764 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
34765 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34766 return add_stmt (stmt);
34769 /* OpenMP 4.5:
34770 # pragma omp target exit data target-enter-data-clause[optseq] new-line
34771 structured-block */
34773 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
34774 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34780 static tree
34781 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
34782 enum pragma_context context)
34784 bool data_seen = false;
34785 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34787 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34788 const char *p = IDENTIFIER_POINTER (id);
34790 if (strcmp (p, "data") == 0)
34792 cp_lexer_consume_token (parser->lexer);
34793 data_seen = true;
34796 if (!data_seen)
34798 cp_parser_error (parser, "expected %<data%>");
34799 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34800 return NULL_TREE;
34803 if (context == pragma_stmt)
34805 error_at (pragma_tok->location,
34806 "%<#pragma omp target exit data%> may only be "
34807 "used in compound statements");
34808 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34809 return NULL_TREE;
34812 tree clauses
34813 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
34814 "#pragma omp target exit data", pragma_tok);
34815 int map_seen = 0;
34816 for (tree *pc = &clauses; *pc;)
34818 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34819 switch (OMP_CLAUSE_MAP_KIND (*pc))
34821 case GOMP_MAP_FROM:
34822 case GOMP_MAP_ALWAYS_FROM:
34823 case GOMP_MAP_RELEASE:
34824 case GOMP_MAP_DELETE:
34825 map_seen = 3;
34826 break;
34827 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34828 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34829 case GOMP_MAP_ALWAYS_POINTER:
34830 break;
34831 default:
34832 map_seen |= 1;
34833 error_at (OMP_CLAUSE_LOCATION (*pc),
34834 "%<#pragma omp target exit data%> with map-type other "
34835 "than %<from%>, %<release%> or %<delete%> on %<map%>"
34836 " clause");
34837 *pc = OMP_CLAUSE_CHAIN (*pc);
34838 continue;
34840 pc = &OMP_CLAUSE_CHAIN (*pc);
34843 if (map_seen != 3)
34845 if (map_seen == 0)
34846 error_at (pragma_tok->location,
34847 "%<#pragma omp target exit data%> must contain at least "
34848 "one %<map%> clause");
34849 return NULL_TREE;
34852 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
34853 TREE_TYPE (stmt) = void_type_node;
34854 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
34855 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34856 return add_stmt (stmt);
34859 /* OpenMP 4.0:
34860 # pragma omp target update target-update-clause[optseq] new-line */
34862 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
34863 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
34864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
34865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34870 static bool
34871 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
34872 enum pragma_context context)
34874 if (context == pragma_stmt)
34876 error_at (pragma_tok->location,
34877 "%<#pragma omp target update%> may only be "
34878 "used in compound statements");
34879 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34880 return false;
34883 tree clauses
34884 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
34885 "#pragma omp target update", pragma_tok);
34886 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
34887 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
34889 error_at (pragma_tok->location,
34890 "%<#pragma omp target update%> must contain at least one "
34891 "%<from%> or %<to%> clauses");
34892 return false;
34895 tree stmt = make_node (OMP_TARGET_UPDATE);
34896 TREE_TYPE (stmt) = void_type_node;
34897 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
34898 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34899 add_stmt (stmt);
34900 return false;
34903 /* OpenMP 4.0:
34904 # pragma omp target target-clause[optseq] new-line
34905 structured-block */
34907 #define OMP_TARGET_CLAUSE_MASK \
34908 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34914 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34915 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
34916 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
34918 static bool
34919 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
34920 enum pragma_context context, bool *if_p)
34922 tree *pc = NULL, stmt;
34924 if (context != pragma_stmt && context != pragma_compound)
34926 cp_parser_error (parser, "expected declaration specifiers");
34927 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34928 return false;
34931 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34933 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34934 const char *p = IDENTIFIER_POINTER (id);
34935 enum tree_code ccode = ERROR_MARK;
34937 if (strcmp (p, "teams") == 0)
34938 ccode = OMP_TEAMS;
34939 else if (strcmp (p, "parallel") == 0)
34940 ccode = OMP_PARALLEL;
34941 else if (strcmp (p, "simd") == 0)
34942 ccode = OMP_SIMD;
34943 if (ccode != ERROR_MARK)
34945 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
34946 char p_name[sizeof ("#pragma omp target teams distribute "
34947 "parallel for simd")];
34949 cp_lexer_consume_token (parser->lexer);
34950 strcpy (p_name, "#pragma omp target");
34951 if (!flag_openmp) /* flag_openmp_simd */
34953 tree stmt;
34954 switch (ccode)
34956 case OMP_TEAMS:
34957 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
34958 OMP_TARGET_CLAUSE_MASK,
34959 cclauses, if_p);
34960 break;
34961 case OMP_PARALLEL:
34962 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
34963 OMP_TARGET_CLAUSE_MASK,
34964 cclauses, if_p);
34965 break;
34966 case OMP_SIMD:
34967 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
34968 OMP_TARGET_CLAUSE_MASK,
34969 cclauses, if_p);
34970 break;
34971 default:
34972 gcc_unreachable ();
34974 return stmt != NULL_TREE;
34976 keep_next_level (true);
34977 tree sb = begin_omp_structured_block (), ret;
34978 unsigned save = cp_parser_begin_omp_structured_block (parser);
34979 switch (ccode)
34981 case OMP_TEAMS:
34982 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
34983 OMP_TARGET_CLAUSE_MASK, cclauses,
34984 if_p);
34985 break;
34986 case OMP_PARALLEL:
34987 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
34988 OMP_TARGET_CLAUSE_MASK, cclauses,
34989 if_p);
34990 break;
34991 case OMP_SIMD:
34992 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
34993 OMP_TARGET_CLAUSE_MASK, cclauses,
34994 if_p);
34995 break;
34996 default:
34997 gcc_unreachable ();
34999 cp_parser_end_omp_structured_block (parser, save);
35000 tree body = finish_omp_structured_block (sb);
35001 if (ret == NULL_TREE)
35002 return false;
35003 if (ccode == OMP_TEAMS && !processing_template_decl)
35005 /* For combined target teams, ensure the num_teams and
35006 thread_limit clause expressions are evaluated on the host,
35007 before entering the target construct. */
35008 tree c;
35009 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35010 c; c = OMP_CLAUSE_CHAIN (c))
35011 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
35012 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
35013 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
35015 tree expr = OMP_CLAUSE_OPERAND (c, 0);
35016 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
35017 if (expr == error_mark_node)
35018 continue;
35019 tree tmp = TARGET_EXPR_SLOT (expr);
35020 add_stmt (expr);
35021 OMP_CLAUSE_OPERAND (c, 0) = expr;
35022 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
35023 OMP_CLAUSE_FIRSTPRIVATE);
35024 OMP_CLAUSE_DECL (tc) = tmp;
35025 OMP_CLAUSE_CHAIN (tc)
35026 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35027 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
35030 tree stmt = make_node (OMP_TARGET);
35031 TREE_TYPE (stmt) = void_type_node;
35032 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35033 OMP_TARGET_BODY (stmt) = body;
35034 OMP_TARGET_COMBINED (stmt) = 1;
35035 add_stmt (stmt);
35036 pc = &OMP_TARGET_CLAUSES (stmt);
35037 goto check_clauses;
35039 else if (!flag_openmp) /* flag_openmp_simd */
35041 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35042 return false;
35044 else if (strcmp (p, "data") == 0)
35046 cp_lexer_consume_token (parser->lexer);
35047 cp_parser_omp_target_data (parser, pragma_tok, if_p);
35048 return true;
35050 else if (strcmp (p, "enter") == 0)
35052 cp_lexer_consume_token (parser->lexer);
35053 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
35054 return false;
35056 else if (strcmp (p, "exit") == 0)
35058 cp_lexer_consume_token (parser->lexer);
35059 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
35060 return false;
35062 else if (strcmp (p, "update") == 0)
35064 cp_lexer_consume_token (parser->lexer);
35065 return cp_parser_omp_target_update (parser, pragma_tok, context);
35069 stmt = make_node (OMP_TARGET);
35070 TREE_TYPE (stmt) = void_type_node;
35072 OMP_TARGET_CLAUSES (stmt)
35073 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
35074 "#pragma omp target", pragma_tok);
35075 pc = &OMP_TARGET_CLAUSES (stmt);
35076 keep_next_level (true);
35077 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35079 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35080 add_stmt (stmt);
35082 check_clauses:
35083 while (*pc)
35085 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35086 switch (OMP_CLAUSE_MAP_KIND (*pc))
35088 case GOMP_MAP_TO:
35089 case GOMP_MAP_ALWAYS_TO:
35090 case GOMP_MAP_FROM:
35091 case GOMP_MAP_ALWAYS_FROM:
35092 case GOMP_MAP_TOFROM:
35093 case GOMP_MAP_ALWAYS_TOFROM:
35094 case GOMP_MAP_ALLOC:
35095 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35096 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35097 case GOMP_MAP_ALWAYS_POINTER:
35098 break;
35099 default:
35100 error_at (OMP_CLAUSE_LOCATION (*pc),
35101 "%<#pragma omp target%> with map-type other "
35102 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35103 "on %<map%> clause");
35104 *pc = OMP_CLAUSE_CHAIN (*pc);
35105 continue;
35107 pc = &OMP_CLAUSE_CHAIN (*pc);
35109 return true;
35112 /* OpenACC 2.0:
35113 # pragma acc cache (variable-list) new-line
35116 static tree
35117 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
35119 tree stmt, clauses;
35121 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
35122 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
35124 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
35126 stmt = make_node (OACC_CACHE);
35127 TREE_TYPE (stmt) = void_type_node;
35128 OACC_CACHE_CLAUSES (stmt) = clauses;
35129 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35130 add_stmt (stmt);
35132 return stmt;
35135 /* OpenACC 2.0:
35136 # pragma acc data oacc-data-clause[optseq] new-line
35137 structured-block */
35139 #define OACC_DATA_CLAUSE_MASK \
35140 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35152 static tree
35153 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35155 tree stmt, clauses, block;
35156 unsigned int save;
35158 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
35159 "#pragma acc data", pragma_tok);
35161 block = begin_omp_parallel ();
35162 save = cp_parser_begin_omp_structured_block (parser);
35163 cp_parser_statement (parser, NULL_TREE, false, if_p);
35164 cp_parser_end_omp_structured_block (parser, save);
35165 stmt = finish_oacc_data (clauses, block);
35166 return stmt;
35169 /* OpenACC 2.0:
35170 # pragma acc host_data <clauses> new-line
35171 structured-block */
35173 #define OACC_HOST_DATA_CLAUSE_MASK \
35174 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
35176 static tree
35177 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35179 tree stmt, clauses, block;
35180 unsigned int save;
35182 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
35183 "#pragma acc host_data", pragma_tok);
35185 block = begin_omp_parallel ();
35186 save = cp_parser_begin_omp_structured_block (parser);
35187 cp_parser_statement (parser, NULL_TREE, false, if_p);
35188 cp_parser_end_omp_structured_block (parser, save);
35189 stmt = finish_oacc_host_data (clauses, block);
35190 return stmt;
35193 /* OpenACC 2.0:
35194 # pragma acc declare oacc-data-clause[optseq] new-line
35197 #define OACC_DECLARE_CLAUSE_MASK \
35198 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35199 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35200 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35201 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35202 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35203 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
35204 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
35205 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35206 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35211 static tree
35212 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
35214 tree clauses, stmt;
35215 bool error = false;
35217 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
35218 "#pragma acc declare", pragma_tok, true);
35221 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35223 error_at (pragma_tok->location,
35224 "no valid clauses specified in %<#pragma acc declare%>");
35225 return NULL_TREE;
35228 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
35230 location_t loc = OMP_CLAUSE_LOCATION (t);
35231 tree decl = OMP_CLAUSE_DECL (t);
35232 if (!DECL_P (decl))
35234 error_at (loc, "array section in %<#pragma acc declare%>");
35235 error = true;
35236 continue;
35238 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
35239 switch (OMP_CLAUSE_MAP_KIND (t))
35241 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35242 case GOMP_MAP_FORCE_ALLOC:
35243 case GOMP_MAP_FORCE_TO:
35244 case GOMP_MAP_FORCE_DEVICEPTR:
35245 case GOMP_MAP_DEVICE_RESIDENT:
35246 break;
35248 case GOMP_MAP_POINTER:
35249 /* Generated by c_finish_omp_clauses from array sections;
35250 avoid spurious diagnostics. */
35251 break;
35253 case GOMP_MAP_LINK:
35254 if (!global_bindings_p ()
35255 && (TREE_STATIC (decl)
35256 || !DECL_EXTERNAL (decl)))
35258 error_at (loc,
35259 "%qD must be a global variable in"
35260 "%<#pragma acc declare link%>",
35261 decl);
35262 error = true;
35263 continue;
35265 break;
35267 default:
35268 if (global_bindings_p ())
35270 error_at (loc, "invalid OpenACC clause at file scope");
35271 error = true;
35272 continue;
35274 if (DECL_EXTERNAL (decl))
35276 error_at (loc,
35277 "invalid use of %<extern%> variable %qD "
35278 "in %<#pragma acc declare%>", decl);
35279 error = true;
35280 continue;
35282 else if (TREE_PUBLIC (decl))
35284 error_at (loc,
35285 "invalid use of %<global%> variable %qD "
35286 "in %<#pragma acc declare%>", decl);
35287 error = true;
35288 continue;
35290 break;
35293 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
35294 || lookup_attribute ("omp declare target link",
35295 DECL_ATTRIBUTES (decl)))
35297 error_at (loc, "variable %qD used more than once with "
35298 "%<#pragma acc declare%>", decl);
35299 error = true;
35300 continue;
35303 if (!error)
35305 tree id;
35307 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
35308 id = get_identifier ("omp declare target link");
35309 else
35310 id = get_identifier ("omp declare target");
35312 DECL_ATTRIBUTES (decl)
35313 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
35314 if (global_bindings_p ())
35316 symtab_node *node = symtab_node::get (decl);
35317 if (node != NULL)
35319 node->offloadable = 1;
35320 if (ENABLE_OFFLOADING)
35322 g->have_offload = true;
35323 if (is_a <varpool_node *> (node))
35324 vec_safe_push (offload_vars, decl);
35331 if (error || global_bindings_p ())
35332 return NULL_TREE;
35334 stmt = make_node (OACC_DECLARE);
35335 TREE_TYPE (stmt) = void_type_node;
35336 OACC_DECLARE_CLAUSES (stmt) = clauses;
35337 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35339 add_stmt (stmt);
35341 return NULL_TREE;
35344 /* OpenACC 2.0:
35345 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
35349 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
35351 LOC is the location of the #pragma token.
35354 #define OACC_ENTER_DATA_CLAUSE_MASK \
35355 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35363 #define OACC_EXIT_DATA_CLAUSE_MASK \
35364 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
35368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35370 static tree
35371 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
35372 bool enter)
35374 tree stmt, clauses;
35376 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
35377 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
35379 cp_parser_error (parser, enter
35380 ? "expected %<data%> in %<#pragma acc enter data%>"
35381 : "expected %<data%> in %<#pragma acc exit data%>");
35382 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35383 return NULL_TREE;
35386 const char *p =
35387 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35388 if (strcmp (p, "data") != 0)
35390 cp_parser_error (parser, "invalid pragma");
35391 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35392 return NULL_TREE;
35395 cp_lexer_consume_token (parser->lexer);
35397 if (enter)
35398 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
35399 "#pragma acc enter data", pragma_tok);
35400 else
35401 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
35402 "#pragma acc exit data", pragma_tok);
35404 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35406 error_at (pragma_tok->location,
35407 "%<#pragma acc enter data%> has no data movement clause");
35408 return NULL_TREE;
35411 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
35412 TREE_TYPE (stmt) = void_type_node;
35413 OMP_STANDALONE_CLAUSES (stmt) = clauses;
35414 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35415 add_stmt (stmt);
35416 return stmt;
35419 /* OpenACC 2.0:
35420 # pragma acc loop oacc-loop-clause[optseq] new-line
35421 structured-block */
35423 #define OACC_LOOP_CLAUSE_MASK \
35424 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
35425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
35426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
35427 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
35428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
35429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
35430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
35431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
35432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
35433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
35435 static tree
35436 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
35437 omp_clause_mask mask, tree *cclauses, bool *if_p)
35439 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
35441 strcat (p_name, " loop");
35442 mask |= OACC_LOOP_CLAUSE_MASK;
35444 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
35445 cclauses == NULL);
35446 if (cclauses)
35448 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
35449 if (*cclauses)
35450 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
35451 if (clauses)
35452 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
35455 tree block = begin_omp_structured_block ();
35456 int save = cp_parser_begin_omp_structured_block (parser);
35457 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
35458 cp_parser_end_omp_structured_block (parser, save);
35459 add_stmt (finish_omp_structured_block (block));
35461 return stmt;
35464 /* OpenACC 2.0:
35465 # pragma acc kernels oacc-kernels-clause[optseq] new-line
35466 structured-block
35470 # pragma acc parallel oacc-parallel-clause[optseq] new-line
35471 structured-block
35474 #define OACC_KERNELS_CLAUSE_MASK \
35475 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
35481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35490 #define OACC_PARALLEL_CLAUSE_MASK \
35491 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
35497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
35499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
35501 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
35502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
35508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
35509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
35510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35512 static tree
35513 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
35514 char *p_name, bool *if_p)
35516 omp_clause_mask mask;
35517 enum tree_code code;
35518 switch (cp_parser_pragma_kind (pragma_tok))
35520 case PRAGMA_OACC_KERNELS:
35521 strcat (p_name, " kernels");
35522 mask = OACC_KERNELS_CLAUSE_MASK;
35523 code = OACC_KERNELS;
35524 break;
35525 case PRAGMA_OACC_PARALLEL:
35526 strcat (p_name, " parallel");
35527 mask = OACC_PARALLEL_CLAUSE_MASK;
35528 code = OACC_PARALLEL;
35529 break;
35530 default:
35531 gcc_unreachable ();
35534 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35536 const char *p
35537 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35538 if (strcmp (p, "loop") == 0)
35540 cp_lexer_consume_token (parser->lexer);
35541 tree block = begin_omp_parallel ();
35542 tree clauses;
35543 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
35544 if_p);
35545 return finish_omp_construct (code, block, clauses);
35549 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
35551 tree block = begin_omp_parallel ();
35552 unsigned int save = cp_parser_begin_omp_structured_block (parser);
35553 cp_parser_statement (parser, NULL_TREE, false, if_p);
35554 cp_parser_end_omp_structured_block (parser, save);
35555 return finish_omp_construct (code, block, clauses);
35558 /* OpenACC 2.0:
35559 # pragma acc update oacc-update-clause[optseq] new-line
35562 #define OACC_UPDATE_CLAUSE_MASK \
35563 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
35565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
35566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
35568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
35570 static tree
35571 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
35573 tree stmt, clauses;
35575 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
35576 "#pragma acc update", pragma_tok);
35578 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35580 error_at (pragma_tok->location,
35581 "%<#pragma acc update%> must contain at least one "
35582 "%<device%> or %<host%> or %<self%> clause");
35583 return NULL_TREE;
35586 stmt = make_node (OACC_UPDATE);
35587 TREE_TYPE (stmt) = void_type_node;
35588 OACC_UPDATE_CLAUSES (stmt) = clauses;
35589 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35590 add_stmt (stmt);
35591 return stmt;
35594 /* OpenACC 2.0:
35595 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
35597 LOC is the location of the #pragma token.
35600 #define OACC_WAIT_CLAUSE_MASK \
35601 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
35603 static tree
35604 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
35606 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
35607 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35609 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
35610 list = cp_parser_oacc_wait_list (parser, loc, list);
35612 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
35613 "#pragma acc wait", pragma_tok);
35615 stmt = c_finish_oacc_wait (loc, list, clauses);
35616 stmt = finish_expr_stmt (stmt);
35618 return stmt;
35621 /* OpenMP 4.0:
35622 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
35624 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
35625 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35627 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35628 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
35629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
35630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
35632 static void
35633 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
35634 enum pragma_context context)
35636 bool first_p = parser->omp_declare_simd == NULL;
35637 cp_omp_declare_simd_data data;
35638 if (first_p)
35640 data.error_seen = false;
35641 data.fndecl_seen = false;
35642 data.tokens = vNULL;
35643 parser->omp_declare_simd = &data;
35645 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
35646 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
35647 cp_lexer_consume_token (parser->lexer);
35648 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35649 parser->omp_declare_simd->error_seen = true;
35650 cp_parser_require_pragma_eol (parser, pragma_tok);
35651 struct cp_token_cache *cp
35652 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
35653 parser->omp_declare_simd->tokens.safe_push (cp);
35654 if (first_p)
35656 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
35657 cp_parser_pragma (parser, context, NULL);
35658 switch (context)
35660 case pragma_external:
35661 cp_parser_declaration (parser);
35662 break;
35663 case pragma_member:
35664 cp_parser_member_declaration (parser);
35665 break;
35666 case pragma_objc_icode:
35667 cp_parser_block_declaration (parser, /*statement_p=*/false);
35668 break;
35669 default:
35670 cp_parser_declaration_statement (parser);
35671 break;
35673 if (parser->omp_declare_simd
35674 && !parser->omp_declare_simd->error_seen
35675 && !parser->omp_declare_simd->fndecl_seen)
35676 error_at (pragma_tok->location,
35677 "%<#pragma omp declare simd%> not immediately followed by "
35678 "function declaration or definition");
35679 data.tokens.release ();
35680 parser->omp_declare_simd = NULL;
35684 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
35685 This function is modelled similar to the late parsing of omp declare
35686 simd. */
35688 static tree
35689 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
35691 struct cp_token_cache *ce;
35692 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
35693 int ii = 0;
35695 if (parser->omp_declare_simd != NULL
35696 || lookup_attribute ("simd", attrs))
35698 error ("%<#pragma omp declare simd%> of %<simd%> attribute cannot be "
35699 "used in the same function marked as a Cilk Plus SIMD-enabled "
35700 " function");
35701 parser->cilk_simd_fn_info->tokens.release ();
35702 XDELETE (parser->cilk_simd_fn_info);
35703 parser->cilk_simd_fn_info = NULL;
35704 return attrs;
35706 if (!info->error_seen && info->fndecl_seen)
35708 error ("vector attribute not immediately followed by a single function"
35709 " declaration or definition");
35710 info->error_seen = true;
35712 if (info->error_seen)
35713 return attrs;
35715 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
35717 tree c, cl;
35719 cp_parser_push_lexer_for_tokens (parser, ce);
35720 parser->lexer->in_pragma = true;
35721 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
35722 "SIMD-enabled functions attribute",
35723 NULL);
35724 cp_parser_pop_lexer (parser);
35725 if (cl)
35726 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35728 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
35729 TREE_CHAIN (c) = attrs;
35730 attrs = c;
35732 c = build_tree_list (get_identifier ("omp declare simd"), cl);
35733 TREE_CHAIN (c) = attrs;
35734 if (processing_template_decl)
35735 ATTR_IS_DEPENDENT (c) = 1;
35736 attrs = c;
35738 info->fndecl_seen = true;
35739 parser->cilk_simd_fn_info->tokens.release ();
35740 XDELETE (parser->cilk_simd_fn_info);
35741 parser->cilk_simd_fn_info = NULL;
35742 return attrs;
35745 /* Finalize #pragma omp declare simd clauses after direct declarator has
35746 been parsed, and put that into "omp declare simd" attribute. */
35748 static tree
35749 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
35751 struct cp_token_cache *ce;
35752 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
35753 int i;
35755 if (!data->error_seen && data->fndecl_seen)
35757 error ("%<#pragma omp declare simd%> not immediately followed by "
35758 "a single function declaration or definition");
35759 data->error_seen = true;
35760 return attrs;
35762 if (data->error_seen)
35763 return attrs;
35765 FOR_EACH_VEC_ELT (data->tokens, i, ce)
35767 tree c, cl;
35769 cp_parser_push_lexer_for_tokens (parser, ce);
35770 parser->lexer->in_pragma = true;
35771 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
35772 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
35773 cp_lexer_consume_token (parser->lexer);
35774 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
35775 "#pragma omp declare simd", pragma_tok);
35776 cp_parser_pop_lexer (parser);
35777 if (cl)
35778 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35779 c = build_tree_list (get_identifier ("omp declare simd"), cl);
35780 TREE_CHAIN (c) = attrs;
35781 if (processing_template_decl)
35782 ATTR_IS_DEPENDENT (c) = 1;
35783 attrs = c;
35786 data->fndecl_seen = true;
35787 return attrs;
35791 /* OpenMP 4.0:
35792 # pragma omp declare target new-line
35793 declarations and definitions
35794 # pragma omp end declare target new-line
35796 OpenMP 4.5:
35797 # pragma omp declare target ( extended-list ) new-line
35799 # pragma omp declare target declare-target-clauses[seq] new-line */
35801 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
35802 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
35805 static void
35806 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
35808 tree clauses = NULL_TREE;
35809 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35810 clauses
35811 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
35812 "#pragma omp declare target", pragma_tok);
35813 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35815 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
35816 clauses);
35817 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
35818 cp_parser_require_pragma_eol (parser, pragma_tok);
35820 else
35822 cp_parser_require_pragma_eol (parser, pragma_tok);
35823 scope_chain->omp_declare_target_attribute++;
35824 return;
35826 if (scope_chain->omp_declare_target_attribute)
35827 error_at (pragma_tok->location,
35828 "%<#pragma omp declare target%> with clauses in between "
35829 "%<#pragma omp declare target%> without clauses and "
35830 "%<#pragma omp end declare target%>");
35831 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
35833 tree t = OMP_CLAUSE_DECL (c), id;
35834 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
35835 tree at2 = lookup_attribute ("omp declare target link",
35836 DECL_ATTRIBUTES (t));
35837 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
35839 id = get_identifier ("omp declare target link");
35840 std::swap (at1, at2);
35842 else
35843 id = get_identifier ("omp declare target");
35844 if (at2)
35846 error_at (OMP_CLAUSE_LOCATION (c),
35847 "%qD specified both in declare target %<link%> and %<to%>"
35848 " clauses", t);
35849 continue;
35851 if (!at1)
35853 symtab_node *node = symtab_node::get (t);
35854 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
35855 if (node != NULL)
35857 node->offloadable = 1;
35858 if (ENABLE_OFFLOADING)
35860 g->have_offload = true;
35861 if (is_a <varpool_node *> (node))
35862 vec_safe_push (offload_vars, t);
35869 static void
35870 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
35872 const char *p = "";
35873 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35875 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35876 p = IDENTIFIER_POINTER (id);
35878 if (strcmp (p, "declare") == 0)
35880 cp_lexer_consume_token (parser->lexer);
35881 p = "";
35882 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35884 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35885 p = IDENTIFIER_POINTER (id);
35887 if (strcmp (p, "target") == 0)
35888 cp_lexer_consume_token (parser->lexer);
35889 else
35891 cp_parser_error (parser, "expected %<target%>");
35892 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35893 return;
35896 else
35898 cp_parser_error (parser, "expected %<declare%>");
35899 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35900 return;
35902 cp_parser_require_pragma_eol (parser, pragma_tok);
35903 if (!scope_chain->omp_declare_target_attribute)
35904 error_at (pragma_tok->location,
35905 "%<#pragma omp end declare target%> without corresponding "
35906 "%<#pragma omp declare target%>");
35907 else
35908 scope_chain->omp_declare_target_attribute--;
35911 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
35912 expression and optional initializer clause of
35913 #pragma omp declare reduction. We store the expression(s) as
35914 either 3, 6 or 7 special statements inside of the artificial function's
35915 body. The first two statements are DECL_EXPRs for the artificial
35916 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
35917 expression that uses those variables.
35918 If there was any INITIALIZER clause, this is followed by further statements,
35919 the fourth and fifth statements are DECL_EXPRs for the artificial
35920 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
35921 constructor variant (first token after open paren is not omp_priv),
35922 then the sixth statement is a statement with the function call expression
35923 that uses the OMP_PRIV and optionally OMP_ORIG variable.
35924 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
35925 to initialize the OMP_PRIV artificial variable and there is seventh
35926 statement, a DECL_EXPR of the OMP_PRIV statement again. */
35928 static bool
35929 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
35931 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
35932 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
35933 type = TREE_TYPE (type);
35934 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
35935 DECL_ARTIFICIAL (omp_out) = 1;
35936 pushdecl (omp_out);
35937 add_decl_expr (omp_out);
35938 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
35939 DECL_ARTIFICIAL (omp_in) = 1;
35940 pushdecl (omp_in);
35941 add_decl_expr (omp_in);
35942 tree combiner;
35943 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
35945 keep_next_level (true);
35946 tree block = begin_omp_structured_block ();
35947 combiner = cp_parser_expression (parser);
35948 finish_expr_stmt (combiner);
35949 block = finish_omp_structured_block (block);
35950 add_stmt (block);
35952 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
35953 return false;
35955 const char *p = "";
35956 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35958 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35959 p = IDENTIFIER_POINTER (id);
35962 if (strcmp (p, "initializer") == 0)
35964 cp_lexer_consume_token (parser->lexer);
35965 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35966 return false;
35968 p = "";
35969 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35971 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35972 p = IDENTIFIER_POINTER (id);
35975 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
35976 DECL_ARTIFICIAL (omp_priv) = 1;
35977 pushdecl (omp_priv);
35978 add_decl_expr (omp_priv);
35979 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
35980 DECL_ARTIFICIAL (omp_orig) = 1;
35981 pushdecl (omp_orig);
35982 add_decl_expr (omp_orig);
35984 keep_next_level (true);
35985 block = begin_omp_structured_block ();
35987 bool ctor = false;
35988 if (strcmp (p, "omp_priv") == 0)
35990 bool is_direct_init, is_non_constant_init;
35991 ctor = true;
35992 cp_lexer_consume_token (parser->lexer);
35993 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
35994 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
35995 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
35996 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
35997 == CPP_CLOSE_PAREN
35998 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
35999 == CPP_CLOSE_PAREN))
36001 finish_omp_structured_block (block);
36002 error ("invalid initializer clause");
36003 return false;
36005 initializer = cp_parser_initializer (parser, &is_direct_init,
36006 &is_non_constant_init);
36007 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
36008 NULL_TREE, LOOKUP_ONLYCONVERTING);
36010 else
36012 cp_parser_parse_tentatively (parser);
36013 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
36014 /*check_dependency_p=*/true,
36015 /*template_p=*/NULL,
36016 /*declarator_p=*/false,
36017 /*optional_p=*/false);
36018 vec<tree, va_gc> *args;
36019 if (fn_name == error_mark_node
36020 || cp_parser_error_occurred (parser)
36021 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36022 || ((args = cp_parser_parenthesized_expression_list
36023 (parser, non_attr, /*cast_p=*/false,
36024 /*allow_expansion_p=*/true,
36025 /*non_constant_p=*/NULL)),
36026 cp_parser_error_occurred (parser)))
36028 finish_omp_structured_block (block);
36029 cp_parser_abort_tentative_parse (parser);
36030 cp_parser_error (parser, "expected id-expression (arguments)");
36031 return false;
36033 unsigned int i;
36034 tree arg;
36035 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
36036 if (arg == omp_priv
36037 || (TREE_CODE (arg) == ADDR_EXPR
36038 && TREE_OPERAND (arg, 0) == omp_priv))
36039 break;
36040 cp_parser_abort_tentative_parse (parser);
36041 if (arg == NULL_TREE)
36042 error ("one of the initializer call arguments should be %<omp_priv%>"
36043 " or %<&omp_priv%>");
36044 initializer = cp_parser_postfix_expression (parser, false, false, false,
36045 false, NULL);
36046 finish_expr_stmt (initializer);
36049 block = finish_omp_structured_block (block);
36050 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
36051 add_stmt (block);
36053 if (ctor)
36054 add_decl_expr (omp_orig);
36056 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36057 return false;
36060 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
36061 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
36063 return true;
36066 /* OpenMP 4.0
36067 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36068 initializer-clause[opt] new-line
36070 initializer-clause:
36071 initializer (omp_priv initializer)
36072 initializer (function-name (argument-list)) */
36074 static void
36075 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
36076 enum pragma_context)
36078 auto_vec<tree> types;
36079 enum tree_code reduc_code = ERROR_MARK;
36080 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
36081 unsigned int i;
36082 cp_token *first_token;
36083 cp_token_cache *cp;
36084 int errs;
36085 void *p;
36087 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
36088 p = obstack_alloc (&declarator_obstack, 0);
36090 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36091 goto fail;
36093 switch (cp_lexer_peek_token (parser->lexer)->type)
36095 case CPP_PLUS:
36096 reduc_code = PLUS_EXPR;
36097 break;
36098 case CPP_MULT:
36099 reduc_code = MULT_EXPR;
36100 break;
36101 case CPP_MINUS:
36102 reduc_code = MINUS_EXPR;
36103 break;
36104 case CPP_AND:
36105 reduc_code = BIT_AND_EXPR;
36106 break;
36107 case CPP_XOR:
36108 reduc_code = BIT_XOR_EXPR;
36109 break;
36110 case CPP_OR:
36111 reduc_code = BIT_IOR_EXPR;
36112 break;
36113 case CPP_AND_AND:
36114 reduc_code = TRUTH_ANDIF_EXPR;
36115 break;
36116 case CPP_OR_OR:
36117 reduc_code = TRUTH_ORIF_EXPR;
36118 break;
36119 case CPP_NAME:
36120 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
36121 break;
36122 default:
36123 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
36124 "%<|%>, %<&&%>, %<||%> or identifier");
36125 goto fail;
36128 if (reduc_code != ERROR_MARK)
36129 cp_lexer_consume_token (parser->lexer);
36131 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
36132 if (reduc_id == error_mark_node)
36133 goto fail;
36135 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36136 goto fail;
36138 /* Types may not be defined in declare reduction type list. */
36139 const char *saved_message;
36140 saved_message = parser->type_definition_forbidden_message;
36141 parser->type_definition_forbidden_message
36142 = G_("types may not be defined in declare reduction type list");
36143 bool saved_colon_corrects_to_scope_p;
36144 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36145 parser->colon_corrects_to_scope_p = false;
36146 bool saved_colon_doesnt_start_class_def_p;
36147 saved_colon_doesnt_start_class_def_p
36148 = parser->colon_doesnt_start_class_def_p;
36149 parser->colon_doesnt_start_class_def_p = true;
36151 while (true)
36153 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36154 type = cp_parser_type_id (parser);
36155 if (type == error_mark_node)
36157 else if (ARITHMETIC_TYPE_P (type)
36158 && (orig_reduc_id == NULL_TREE
36159 || (TREE_CODE (type) != COMPLEX_TYPE
36160 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36161 "min") == 0
36162 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36163 "max") == 0))))
36164 error_at (loc, "predeclared arithmetic type %qT in "
36165 "%<#pragma omp declare reduction%>", type);
36166 else if (TREE_CODE (type) == FUNCTION_TYPE
36167 || TREE_CODE (type) == METHOD_TYPE
36168 || TREE_CODE (type) == ARRAY_TYPE)
36169 error_at (loc, "function or array type %qT in "
36170 "%<#pragma omp declare reduction%>", type);
36171 else if (TREE_CODE (type) == REFERENCE_TYPE)
36172 error_at (loc, "reference type %qT in "
36173 "%<#pragma omp declare reduction%>", type);
36174 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
36175 error_at (loc, "const, volatile or __restrict qualified type %qT in "
36176 "%<#pragma omp declare reduction%>", type);
36177 else
36178 types.safe_push (type);
36180 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36181 cp_lexer_consume_token (parser->lexer);
36182 else
36183 break;
36186 /* Restore the saved message. */
36187 parser->type_definition_forbidden_message = saved_message;
36188 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36189 parser->colon_doesnt_start_class_def_p
36190 = saved_colon_doesnt_start_class_def_p;
36192 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
36193 || types.is_empty ())
36195 fail:
36196 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36197 goto done;
36200 first_token = cp_lexer_peek_token (parser->lexer);
36201 cp = NULL;
36202 errs = errorcount;
36203 FOR_EACH_VEC_ELT (types, i, type)
36205 tree fntype
36206 = build_function_type_list (void_type_node,
36207 cp_build_reference_type (type, false),
36208 NULL_TREE);
36209 tree this_reduc_id = reduc_id;
36210 if (!dependent_type_p (type))
36211 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
36212 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
36213 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
36214 DECL_ARTIFICIAL (fndecl) = 1;
36215 DECL_EXTERNAL (fndecl) = 1;
36216 DECL_DECLARED_INLINE_P (fndecl) = 1;
36217 DECL_IGNORED_P (fndecl) = 1;
36218 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
36219 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
36220 DECL_ATTRIBUTES (fndecl)
36221 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
36222 DECL_ATTRIBUTES (fndecl));
36223 if (processing_template_decl)
36224 fndecl = push_template_decl (fndecl);
36225 bool block_scope = false;
36226 tree block = NULL_TREE;
36227 if (current_function_decl)
36229 block_scope = true;
36230 DECL_CONTEXT (fndecl) = global_namespace;
36231 if (!processing_template_decl)
36232 pushdecl (fndecl);
36234 else if (current_class_type)
36236 if (cp == NULL)
36238 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36239 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36240 cp_lexer_consume_token (parser->lexer);
36241 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36242 goto fail;
36243 cp = cp_token_cache_new (first_token,
36244 cp_lexer_peek_nth_token (parser->lexer,
36245 2));
36247 DECL_STATIC_FUNCTION_P (fndecl) = 1;
36248 finish_member_declaration (fndecl);
36249 DECL_PENDING_INLINE_INFO (fndecl) = cp;
36250 DECL_PENDING_INLINE_P (fndecl) = 1;
36251 vec_safe_push (unparsed_funs_with_definitions, fndecl);
36252 continue;
36254 else
36256 DECL_CONTEXT (fndecl) = current_namespace;
36257 pushdecl (fndecl);
36259 if (!block_scope)
36260 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
36261 else
36262 block = begin_omp_structured_block ();
36263 if (cp)
36265 cp_parser_push_lexer_for_tokens (parser, cp);
36266 parser->lexer->in_pragma = true;
36268 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
36270 if (!block_scope)
36271 finish_function (0);
36272 else
36273 DECL_CONTEXT (fndecl) = current_function_decl;
36274 if (cp)
36275 cp_parser_pop_lexer (parser);
36276 goto fail;
36278 if (cp)
36279 cp_parser_pop_lexer (parser);
36280 if (!block_scope)
36281 finish_function (0);
36282 else
36284 DECL_CONTEXT (fndecl) = current_function_decl;
36285 block = finish_omp_structured_block (block);
36286 if (TREE_CODE (block) == BIND_EXPR)
36287 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
36288 else if (TREE_CODE (block) == STATEMENT_LIST)
36289 DECL_SAVED_TREE (fndecl) = block;
36290 if (processing_template_decl)
36291 add_decl_expr (fndecl);
36293 cp_check_omp_declare_reduction (fndecl);
36294 if (cp == NULL && types.length () > 1)
36295 cp = cp_token_cache_new (first_token,
36296 cp_lexer_peek_nth_token (parser->lexer, 2));
36297 if (errs != errorcount)
36298 break;
36301 cp_parser_require_pragma_eol (parser, pragma_tok);
36303 done:
36304 /* Free any declarators allocated. */
36305 obstack_free (&declarator_obstack, p);
36308 /* OpenMP 4.0
36309 #pragma omp declare simd declare-simd-clauses[optseq] new-line
36310 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36311 initializer-clause[opt] new-line
36312 #pragma omp declare target new-line */
36314 static void
36315 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
36316 enum pragma_context context)
36318 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36320 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36321 const char *p = IDENTIFIER_POINTER (id);
36323 if (strcmp (p, "simd") == 0)
36325 cp_lexer_consume_token (parser->lexer);
36326 cp_parser_omp_declare_simd (parser, pragma_tok,
36327 context);
36328 return;
36330 cp_ensure_no_omp_declare_simd (parser);
36331 if (strcmp (p, "reduction") == 0)
36333 cp_lexer_consume_token (parser->lexer);
36334 cp_parser_omp_declare_reduction (parser, pragma_tok,
36335 context);
36336 return;
36338 if (!flag_openmp) /* flag_openmp_simd */
36340 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36341 return;
36343 if (strcmp (p, "target") == 0)
36345 cp_lexer_consume_token (parser->lexer);
36346 cp_parser_omp_declare_target (parser, pragma_tok);
36347 return;
36350 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
36351 "or %<target%>");
36352 cp_parser_require_pragma_eol (parser, pragma_tok);
36355 /* OpenMP 4.5:
36356 #pragma omp taskloop taskloop-clause[optseq] new-line
36357 for-loop
36359 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
36360 for-loop */
36362 #define OMP_TASKLOOP_CLAUSE_MASK \
36363 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
36368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
36369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
36370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
36371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
36372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36373 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
36374 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
36375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
36376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
36378 static tree
36379 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
36380 char *p_name, omp_clause_mask mask, tree *cclauses,
36381 bool *if_p)
36383 tree clauses, sb, ret;
36384 unsigned int save;
36385 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36387 strcat (p_name, " taskloop");
36388 mask |= OMP_TASKLOOP_CLAUSE_MASK;
36390 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36392 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36393 const char *p = IDENTIFIER_POINTER (id);
36395 if (strcmp (p, "simd") == 0)
36397 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36398 if (cclauses == NULL)
36399 cclauses = cclauses_buf;
36401 cp_lexer_consume_token (parser->lexer);
36402 if (!flag_openmp) /* flag_openmp_simd */
36403 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36404 cclauses, if_p);
36405 sb = begin_omp_structured_block ();
36406 save = cp_parser_begin_omp_structured_block (parser);
36407 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36408 cclauses, if_p);
36409 cp_parser_end_omp_structured_block (parser, save);
36410 tree body = finish_omp_structured_block (sb);
36411 if (ret == NULL)
36412 return ret;
36413 ret = make_node (OMP_TASKLOOP);
36414 TREE_TYPE (ret) = void_type_node;
36415 OMP_FOR_BODY (ret) = body;
36416 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36417 SET_EXPR_LOCATION (ret, loc);
36418 add_stmt (ret);
36419 return ret;
36422 if (!flag_openmp) /* flag_openmp_simd */
36424 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36425 return NULL_TREE;
36428 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36429 cclauses == NULL);
36430 if (cclauses)
36432 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
36433 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36436 sb = begin_omp_structured_block ();
36437 save = cp_parser_begin_omp_structured_block (parser);
36439 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
36440 if_p);
36442 cp_parser_end_omp_structured_block (parser, save);
36443 add_stmt (finish_omp_structured_block (sb));
36445 return ret;
36449 /* OpenACC 2.0:
36450 # pragma acc routine oacc-routine-clause[optseq] new-line
36451 function-definition
36453 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
36456 #define OACC_ROUTINE_CLAUSE_MASK \
36457 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
36463 /* Parse the OpenACC routine pragma. This has an optional '( name )'
36464 component, which must resolve to a declared namespace-scope
36465 function. The clauses are either processed directly (for a named
36466 function), or defered until the immediatley following declaration
36467 is parsed. */
36469 static void
36470 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
36471 enum pragma_context context)
36473 bool first_p = parser->oacc_routine == NULL;
36474 location_t loc = pragma_tok->location;
36475 cp_omp_declare_simd_data data;
36476 if (first_p)
36478 data.error_seen = false;
36479 data.fndecl_seen = false;
36480 data.tokens = vNULL;
36481 data.clauses = NULL_TREE;
36482 parser->oacc_routine = &data;
36485 tree decl = NULL_TREE;
36486 /* Create a dummy claue, to record location. */
36487 tree c_head = build_omp_clause (pragma_tok->location, OMP_CLAUSE_SEQ);
36489 if (context != pragma_external)
36491 cp_parser_error (parser, "%<#pragma acc routine%> not at file scope");
36492 parser->oacc_routine->error_seen = true;
36493 parser->oacc_routine = NULL;
36494 return;
36497 /* Look for optional '( name )'. */
36498 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36500 if (!first_p)
36502 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36503 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36504 cp_lexer_consume_token (parser->lexer);
36505 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36506 parser->oacc_routine->error_seen = true;
36507 cp_parser_require_pragma_eol (parser, pragma_tok);
36509 error_at (OMP_CLAUSE_LOCATION (parser->oacc_routine->clauses),
36510 "%<#pragma acc routine%> not followed by a "
36511 "function declaration or definition");
36513 parser->oacc_routine->error_seen = true;
36514 return;
36517 cp_lexer_consume_token (parser->lexer);
36518 cp_token *token = cp_lexer_peek_token (parser->lexer);
36520 /* We parse the name as an id-expression. If it resolves to
36521 anything other than a non-overloaded function at namespace
36522 scope, it's an error. */
36523 tree id = cp_parser_id_expression (parser,
36524 /*template_keyword_p=*/false,
36525 /*check_dependency_p=*/false,
36526 /*template_p=*/NULL,
36527 /*declarator_p=*/false,
36528 /*optional_p=*/false);
36529 decl = cp_parser_lookup_name_simple (parser, id, token->location);
36530 if (id != error_mark_node && decl == error_mark_node)
36531 cp_parser_name_lookup_error (parser, id, decl, NLE_NULL,
36532 token->location);
36534 if (decl == error_mark_node
36535 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36537 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36538 parser->oacc_routine = NULL;
36539 return;
36542 /* Build a chain of clauses. */
36543 parser->lexer->in_pragma = true;
36544 tree clauses = NULL_TREE;
36545 clauses = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36546 "#pragma acc routine",
36547 cp_lexer_peek_token
36548 (parser->lexer));
36550 /* Force clauses to be non-null, by attaching context to it. */
36551 clauses = tree_cons (c_head, clauses, NULL_TREE);
36553 if (decl && is_overloaded_fn (decl)
36554 && (TREE_CODE (decl) != FUNCTION_DECL
36555 || DECL_FUNCTION_TEMPLATE_P (decl)))
36557 error_at (loc, "%<#pragma acc routine%> names a set of overloads");
36558 parser->oacc_routine = NULL;
36559 return;
36562 /* Perhaps we should use the same rule as declarations in different
36563 namespaces? */
36564 if (!DECL_NAMESPACE_SCOPE_P (decl))
36566 error_at (loc, "%<#pragma acc routine%> does not refer to a "
36567 "namespace scope function");
36568 parser->oacc_routine = NULL;
36569 return;
36572 if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
36574 error_at (loc,
36575 "%<#pragma acc routine%> does not refer to a function");
36576 parser->oacc_routine = NULL;
36577 return;
36580 data.clauses = clauses;
36582 cp_finalize_oacc_routine (parser, decl, false);
36583 data.tokens.release ();
36584 parser->oacc_routine = NULL;
36586 else
36588 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36589 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36590 cp_lexer_consume_token (parser->lexer);
36591 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36592 parser->oacc_routine->error_seen = true;
36593 cp_parser_require_pragma_eol (parser, pragma_tok);
36595 struct cp_token_cache *cp
36596 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36597 parser->oacc_routine->tokens.safe_push (cp);
36599 if (first_p)
36600 parser->oacc_routine->clauses = c_head;
36602 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36603 cp_parser_pragma (parser, context, NULL);
36605 if (first_p)
36607 /* Create an empty list of clauses. */
36608 parser->oacc_routine->clauses = tree_cons (c_head, NULL_TREE,
36609 NULL_TREE);
36610 cp_parser_declaration (parser);
36612 if (parser->oacc_routine
36613 && !parser->oacc_routine->error_seen
36614 && !parser->oacc_routine->fndecl_seen)
36615 error_at (loc, "%<#pragma acc routine%> not followed by a "
36616 "function declaration or definition");
36618 data.tokens.release ();
36619 parser->oacc_routine = NULL;
36624 /* Finalize #pragma acc routine clauses after direct declarator has
36625 been parsed, and put that into "oacc function" attribute. */
36627 static tree
36628 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
36630 struct cp_token_cache *ce;
36631 cp_omp_declare_simd_data *data = parser->oacc_routine;
36632 tree cl, clauses = parser->oacc_routine->clauses;
36633 location_t loc;
36635 loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
36637 if ((!data->error_seen && data->fndecl_seen)
36638 || data->tokens.length () != 1)
36640 error_at (loc, "%<#pragma acc routine%> not followed by a "
36641 "function declaration or definition");
36642 data->error_seen = true;
36643 return attrs;
36645 if (data->error_seen)
36646 return attrs;
36648 ce = data->tokens[0];
36650 cp_parser_push_lexer_for_tokens (parser, ce);
36651 parser->lexer->in_pragma = true;
36652 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36654 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36655 cl = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36656 "#pragma acc routine", pragma_tok);
36657 cp_parser_pop_lexer (parser);
36659 tree c_head = build_omp_clause (loc, OMP_CLAUSE_SEQ);
36661 /* Force clauses to be non-null, by attaching context to it. */
36662 parser->oacc_routine->clauses = tree_cons (c_head, cl, NULL_TREE);
36664 data->fndecl_seen = true;
36665 return attrs;
36668 /* Apply any saved OpenACC routine clauses to a just-parsed
36669 declaration. */
36671 static void
36672 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
36674 if (__builtin_expect (parser->oacc_routine != NULL, 0))
36676 tree clauses = parser->oacc_routine->clauses;
36677 location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
36679 if (parser->oacc_routine->error_seen)
36680 return;
36682 if (fndecl == error_mark_node)
36684 parser->oacc_routine = NULL;
36685 return;
36688 if (TREE_CODE (fndecl) != FUNCTION_DECL)
36690 cp_ensure_no_oacc_routine (parser);
36691 return;
36694 if (!fndecl || TREE_CODE (fndecl) != FUNCTION_DECL)
36696 error_at (loc,
36697 "%<#pragma acc routine%> not followed by a function "
36698 "declaration or definition");
36699 parser->oacc_routine = NULL;
36702 if (get_oacc_fn_attrib (fndecl))
36704 error_at (loc, "%<#pragma acc routine%> already applied to %D",
36705 fndecl);
36706 parser->oacc_routine = NULL;
36709 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
36711 error_at (loc, "%<#pragma acc routine%> must be applied before %s",
36712 TREE_USED (fndecl) ? "use" : "definition");
36713 parser->oacc_routine = NULL;
36716 /* Process for function attrib */
36717 tree dims = build_oacc_routine_dims (TREE_VALUE (clauses));
36718 replace_oacc_fn_attrib (fndecl, dims);
36720 /* Add an "omp target" attribute. */
36721 DECL_ATTRIBUTES (fndecl)
36722 = tree_cons (get_identifier ("omp declare target"),
36723 NULL_TREE, DECL_ATTRIBUTES (fndecl));
36727 /* Main entry point to OpenMP statement pragmas. */
36729 static void
36730 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36732 tree stmt;
36733 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
36734 omp_clause_mask mask (0);
36736 switch (cp_parser_pragma_kind (pragma_tok))
36738 case PRAGMA_OACC_ATOMIC:
36739 cp_parser_omp_atomic (parser, pragma_tok);
36740 return;
36741 case PRAGMA_OACC_CACHE:
36742 stmt = cp_parser_oacc_cache (parser, pragma_tok);
36743 break;
36744 case PRAGMA_OACC_DATA:
36745 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
36746 break;
36747 case PRAGMA_OACC_ENTER_DATA:
36748 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
36749 break;
36750 case PRAGMA_OACC_EXIT_DATA:
36751 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
36752 break;
36753 case PRAGMA_OACC_HOST_DATA:
36754 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
36755 break;
36756 case PRAGMA_OACC_KERNELS:
36757 case PRAGMA_OACC_PARALLEL:
36758 strcpy (p_name, "#pragma acc");
36759 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
36760 if_p);
36761 break;
36762 case PRAGMA_OACC_LOOP:
36763 strcpy (p_name, "#pragma acc");
36764 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
36765 if_p);
36766 break;
36767 case PRAGMA_OACC_UPDATE:
36768 stmt = cp_parser_oacc_update (parser, pragma_tok);
36769 break;
36770 case PRAGMA_OACC_WAIT:
36771 stmt = cp_parser_oacc_wait (parser, pragma_tok);
36772 break;
36773 case PRAGMA_OMP_ATOMIC:
36774 cp_parser_omp_atomic (parser, pragma_tok);
36775 return;
36776 case PRAGMA_OMP_CRITICAL:
36777 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
36778 break;
36779 case PRAGMA_OMP_DISTRIBUTE:
36780 strcpy (p_name, "#pragma omp");
36781 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
36782 if_p);
36783 break;
36784 case PRAGMA_OMP_FOR:
36785 strcpy (p_name, "#pragma omp");
36786 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
36787 if_p);
36788 break;
36789 case PRAGMA_OMP_MASTER:
36790 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
36791 break;
36792 case PRAGMA_OMP_PARALLEL:
36793 strcpy (p_name, "#pragma omp");
36794 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
36795 if_p);
36796 break;
36797 case PRAGMA_OMP_SECTIONS:
36798 strcpy (p_name, "#pragma omp");
36799 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
36800 break;
36801 case PRAGMA_OMP_SIMD:
36802 strcpy (p_name, "#pragma omp");
36803 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
36804 if_p);
36805 break;
36806 case PRAGMA_OMP_SINGLE:
36807 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
36808 break;
36809 case PRAGMA_OMP_TASK:
36810 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
36811 break;
36812 case PRAGMA_OMP_TASKGROUP:
36813 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
36814 break;
36815 case PRAGMA_OMP_TASKLOOP:
36816 strcpy (p_name, "#pragma omp");
36817 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
36818 if_p);
36819 break;
36820 case PRAGMA_OMP_TEAMS:
36821 strcpy (p_name, "#pragma omp");
36822 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
36823 if_p);
36824 break;
36825 default:
36826 gcc_unreachable ();
36829 protected_set_expr_location (stmt, pragma_tok->location);
36832 /* Transactional Memory parsing routines. */
36834 /* Parse a transaction attribute.
36836 txn-attribute:
36837 attribute
36838 [ [ identifier ] ]
36840 We use this instead of cp_parser_attributes_opt for transactions to avoid
36841 the pedwarn in C++98 mode. */
36843 static tree
36844 cp_parser_txn_attribute_opt (cp_parser *parser)
36846 cp_token *token;
36847 tree attr_name, attr = NULL;
36849 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
36850 return cp_parser_attributes_opt (parser);
36852 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
36853 return NULL_TREE;
36854 cp_lexer_consume_token (parser->lexer);
36855 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
36856 goto error1;
36858 token = cp_lexer_peek_token (parser->lexer);
36859 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
36861 token = cp_lexer_consume_token (parser->lexer);
36863 attr_name = (token->type == CPP_KEYWORD
36864 /* For keywords, use the canonical spelling,
36865 not the parsed identifier. */
36866 ? ridpointers[(int) token->keyword]
36867 : token->u.value);
36868 attr = build_tree_list (attr_name, NULL_TREE);
36870 else
36871 cp_parser_error (parser, "expected identifier");
36873 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36874 error1:
36875 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36876 return attr;
36879 /* Parse a __transaction_atomic or __transaction_relaxed statement.
36881 transaction-statement:
36882 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
36883 compound-statement
36884 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
36887 static tree
36888 cp_parser_transaction (cp_parser *parser, cp_token *token)
36890 unsigned char old_in = parser->in_transaction;
36891 unsigned char this_in = 1, new_in;
36892 enum rid keyword = token->keyword;
36893 tree stmt, attrs, noex;
36895 cp_lexer_consume_token (parser->lexer);
36897 if (keyword == RID_TRANSACTION_RELAXED
36898 || keyword == RID_SYNCHRONIZED)
36899 this_in |= TM_STMT_ATTR_RELAXED;
36900 else
36902 attrs = cp_parser_txn_attribute_opt (parser);
36903 if (attrs)
36904 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
36907 /* Parse a noexcept specification. */
36908 if (keyword == RID_ATOMIC_NOEXCEPT)
36909 noex = boolean_true_node;
36910 else if (keyword == RID_ATOMIC_CANCEL)
36912 /* cancel-and-throw is unimplemented. */
36913 sorry ("atomic_cancel");
36914 noex = NULL_TREE;
36916 else
36917 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
36919 /* Keep track if we're in the lexical scope of an outer transaction. */
36920 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
36922 stmt = begin_transaction_stmt (token->location, NULL, this_in);
36924 parser->in_transaction = new_in;
36925 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
36926 parser->in_transaction = old_in;
36928 finish_transaction_stmt (stmt, NULL, this_in, noex);
36930 return stmt;
36933 /* Parse a __transaction_atomic or __transaction_relaxed expression.
36935 transaction-expression:
36936 __transaction_atomic txn-noexcept-spec[opt] ( expression )
36937 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
36940 static tree
36941 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
36943 unsigned char old_in = parser->in_transaction;
36944 unsigned char this_in = 1;
36945 cp_token *token;
36946 tree expr, noex;
36947 bool noex_expr;
36948 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36950 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
36951 || keyword == RID_TRANSACTION_RELAXED);
36953 if (!flag_tm)
36954 error_at (loc,
36955 keyword == RID_TRANSACTION_RELAXED
36956 ? G_("%<__transaction_relaxed%> without transactional memory "
36957 "support enabled")
36958 : G_("%<__transaction_atomic%> without transactional memory "
36959 "support enabled"));
36961 token = cp_parser_require_keyword (parser, keyword,
36962 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
36963 : RT_TRANSACTION_RELAXED));
36964 gcc_assert (token != NULL);
36966 if (keyword == RID_TRANSACTION_RELAXED)
36967 this_in |= TM_STMT_ATTR_RELAXED;
36969 /* Set this early. This might mean that we allow transaction_cancel in
36970 an expression that we find out later actually has to be a constexpr.
36971 However, we expect that cxx_constant_value will be able to deal with
36972 this; also, if the noexcept has no constexpr, then what we parse next
36973 really is a transaction's body. */
36974 parser->in_transaction = this_in;
36976 /* Parse a noexcept specification. */
36977 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
36978 true);
36980 if (!noex || !noex_expr
36981 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36983 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
36985 expr = cp_parser_expression (parser);
36986 expr = finish_parenthesized_expr (expr);
36988 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
36990 else
36992 /* The only expression that is available got parsed for the noexcept
36993 already. noexcept is true then. */
36994 expr = noex;
36995 noex = boolean_true_node;
36998 expr = build_transaction_expr (token->location, expr, this_in, noex);
36999 parser->in_transaction = old_in;
37001 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
37002 return error_mark_node;
37004 return (flag_tm ? expr : error_mark_node);
37007 /* Parse a function-transaction-block.
37009 function-transaction-block:
37010 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
37011 function-body
37012 __transaction_atomic txn-attribute[opt] function-try-block
37013 __transaction_relaxed ctor-initializer[opt] function-body
37014 __transaction_relaxed function-try-block
37017 static bool
37018 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
37020 unsigned char old_in = parser->in_transaction;
37021 unsigned char new_in = 1;
37022 tree compound_stmt, stmt, attrs;
37023 bool ctor_initializer_p;
37024 cp_token *token;
37026 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37027 || keyword == RID_TRANSACTION_RELAXED);
37028 token = cp_parser_require_keyword (parser, keyword,
37029 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37030 : RT_TRANSACTION_RELAXED));
37031 gcc_assert (token != NULL);
37033 if (keyword == RID_TRANSACTION_RELAXED)
37034 new_in |= TM_STMT_ATTR_RELAXED;
37035 else
37037 attrs = cp_parser_txn_attribute_opt (parser);
37038 if (attrs)
37039 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37042 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
37044 parser->in_transaction = new_in;
37046 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
37047 ctor_initializer_p = cp_parser_function_try_block (parser);
37048 else
37049 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
37050 (parser, /*in_function_try_block=*/false);
37052 parser->in_transaction = old_in;
37054 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
37056 return ctor_initializer_p;
37059 /* Parse a __transaction_cancel statement.
37061 cancel-statement:
37062 __transaction_cancel txn-attribute[opt] ;
37063 __transaction_cancel txn-attribute[opt] throw-expression ;
37065 ??? Cancel and throw is not yet implemented. */
37067 static tree
37068 cp_parser_transaction_cancel (cp_parser *parser)
37070 cp_token *token;
37071 bool is_outer = false;
37072 tree stmt, attrs;
37074 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
37075 RT_TRANSACTION_CANCEL);
37076 gcc_assert (token != NULL);
37078 attrs = cp_parser_txn_attribute_opt (parser);
37079 if (attrs)
37080 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
37082 /* ??? Parse cancel-and-throw here. */
37084 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37086 if (!flag_tm)
37088 error_at (token->location, "%<__transaction_cancel%> without "
37089 "transactional memory support enabled");
37090 return error_mark_node;
37092 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
37094 error_at (token->location, "%<__transaction_cancel%> within a "
37095 "%<__transaction_relaxed%>");
37096 return error_mark_node;
37098 else if (is_outer)
37100 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
37101 && !is_tm_may_cancel_outer (current_function_decl))
37103 error_at (token->location, "outer %<__transaction_cancel%> not "
37104 "within outer %<__transaction_atomic%>");
37105 error_at (token->location,
37106 " or a %<transaction_may_cancel_outer%> function");
37107 return error_mark_node;
37110 else if (parser->in_transaction == 0)
37112 error_at (token->location, "%<__transaction_cancel%> not within "
37113 "%<__transaction_atomic%>");
37114 return error_mark_node;
37117 stmt = build_tm_abort_call (token->location, is_outer);
37118 add_stmt (stmt);
37120 return stmt;
37123 /* The parser. */
37125 static GTY (()) cp_parser *the_parser;
37128 /* Special handling for the first token or line in the file. The first
37129 thing in the file might be #pragma GCC pch_preprocess, which loads a
37130 PCH file, which is a GC collection point. So we need to handle this
37131 first pragma without benefit of an existing lexer structure.
37133 Always returns one token to the caller in *FIRST_TOKEN. This is
37134 either the true first token of the file, or the first token after
37135 the initial pragma. */
37137 static void
37138 cp_parser_initial_pragma (cp_token *first_token)
37140 tree name = NULL;
37142 cp_lexer_get_preprocessor_token (NULL, first_token);
37143 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
37144 return;
37146 cp_lexer_get_preprocessor_token (NULL, first_token);
37147 if (first_token->type == CPP_STRING)
37149 name = first_token->u.value;
37151 cp_lexer_get_preprocessor_token (NULL, first_token);
37152 if (first_token->type != CPP_PRAGMA_EOL)
37153 error_at (first_token->location,
37154 "junk at end of %<#pragma GCC pch_preprocess%>");
37156 else
37157 error_at (first_token->location, "expected string literal");
37159 /* Skip to the end of the pragma. */
37160 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
37161 cp_lexer_get_preprocessor_token (NULL, first_token);
37163 /* Now actually load the PCH file. */
37164 if (name)
37165 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
37167 /* Read one more token to return to our caller. We have to do this
37168 after reading the PCH file in, since its pointers have to be
37169 live. */
37170 cp_lexer_get_preprocessor_token (NULL, first_token);
37173 /* Parses the grainsize pragma for the _Cilk_for statement.
37174 Syntax:
37175 #pragma cilk grainsize = <VALUE>. */
37177 static void
37178 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37180 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
37182 tree exp = cp_parser_binary_expression (parser, false, false,
37183 PREC_NOT_OPERATOR, NULL);
37184 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37185 if (!exp || exp == error_mark_node)
37187 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
37188 return;
37191 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
37192 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
37193 cp_parser_cilk_for (parser, exp, if_p);
37194 else
37195 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
37196 "%<#pragma cilk grainsize%> is not followed by "
37197 "%<_Cilk_for%>");
37198 return;
37200 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37203 /* Normal parsing of a pragma token. Here we can (and must) use the
37204 regular lexer. */
37206 static bool
37207 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
37209 cp_token *pragma_tok;
37210 unsigned int id;
37211 tree stmt;
37212 bool ret;
37214 pragma_tok = cp_lexer_consume_token (parser->lexer);
37215 gcc_assert (pragma_tok->type == CPP_PRAGMA);
37216 parser->lexer->in_pragma = true;
37218 id = cp_parser_pragma_kind (pragma_tok);
37219 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
37220 cp_ensure_no_omp_declare_simd (parser);
37221 switch (id)
37223 case PRAGMA_GCC_PCH_PREPROCESS:
37224 error_at (pragma_tok->location,
37225 "%<#pragma GCC pch_preprocess%> must be first");
37226 break;
37228 case PRAGMA_OMP_BARRIER:
37229 switch (context)
37231 case pragma_compound:
37232 cp_parser_omp_barrier (parser, pragma_tok);
37233 return false;
37234 case pragma_stmt:
37235 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
37236 "used in compound statements");
37237 break;
37238 default:
37239 goto bad_stmt;
37241 break;
37243 case PRAGMA_OMP_FLUSH:
37244 switch (context)
37246 case pragma_compound:
37247 cp_parser_omp_flush (parser, pragma_tok);
37248 return false;
37249 case pragma_stmt:
37250 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
37251 "used in compound statements");
37252 break;
37253 default:
37254 goto bad_stmt;
37256 break;
37258 case PRAGMA_OMP_TASKWAIT:
37259 switch (context)
37261 case pragma_compound:
37262 cp_parser_omp_taskwait (parser, pragma_tok);
37263 return false;
37264 case pragma_stmt:
37265 error_at (pragma_tok->location,
37266 "%<#pragma omp taskwait%> may only be "
37267 "used in compound statements");
37268 break;
37269 default:
37270 goto bad_stmt;
37272 break;
37274 case PRAGMA_OMP_TASKYIELD:
37275 switch (context)
37277 case pragma_compound:
37278 cp_parser_omp_taskyield (parser, pragma_tok);
37279 return false;
37280 case pragma_stmt:
37281 error_at (pragma_tok->location,
37282 "%<#pragma omp taskyield%> may only be "
37283 "used in compound statements");
37284 break;
37285 default:
37286 goto bad_stmt;
37288 break;
37290 case PRAGMA_OMP_CANCEL:
37291 switch (context)
37293 case pragma_compound:
37294 cp_parser_omp_cancel (parser, pragma_tok);
37295 return false;
37296 case pragma_stmt:
37297 error_at (pragma_tok->location,
37298 "%<#pragma omp cancel%> may only be "
37299 "used in compound statements");
37300 break;
37301 default:
37302 goto bad_stmt;
37304 break;
37306 case PRAGMA_OMP_CANCELLATION_POINT:
37307 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
37308 return false;
37310 case PRAGMA_OMP_THREADPRIVATE:
37311 cp_parser_omp_threadprivate (parser, pragma_tok);
37312 return false;
37314 case PRAGMA_OMP_DECLARE:
37315 cp_parser_omp_declare (parser, pragma_tok, context);
37316 return false;
37318 case PRAGMA_OACC_DECLARE:
37319 cp_parser_oacc_declare (parser, pragma_tok);
37320 return false;
37322 case PRAGMA_OACC_ROUTINE:
37323 cp_parser_oacc_routine (parser, pragma_tok, context);
37324 return false;
37326 case PRAGMA_OACC_ATOMIC:
37327 case PRAGMA_OACC_CACHE:
37328 case PRAGMA_OACC_DATA:
37329 case PRAGMA_OACC_ENTER_DATA:
37330 case PRAGMA_OACC_EXIT_DATA:
37331 case PRAGMA_OACC_HOST_DATA:
37332 case PRAGMA_OACC_KERNELS:
37333 case PRAGMA_OACC_PARALLEL:
37334 case PRAGMA_OACC_LOOP:
37335 case PRAGMA_OACC_UPDATE:
37336 case PRAGMA_OACC_WAIT:
37337 case PRAGMA_OMP_ATOMIC:
37338 case PRAGMA_OMP_CRITICAL:
37339 case PRAGMA_OMP_DISTRIBUTE:
37340 case PRAGMA_OMP_FOR:
37341 case PRAGMA_OMP_MASTER:
37342 case PRAGMA_OMP_PARALLEL:
37343 case PRAGMA_OMP_SECTIONS:
37344 case PRAGMA_OMP_SIMD:
37345 case PRAGMA_OMP_SINGLE:
37346 case PRAGMA_OMP_TASK:
37347 case PRAGMA_OMP_TASKGROUP:
37348 case PRAGMA_OMP_TASKLOOP:
37349 case PRAGMA_OMP_TEAMS:
37350 if (context != pragma_stmt && context != pragma_compound)
37351 goto bad_stmt;
37352 stmt = push_omp_privatization_clauses (false);
37353 cp_parser_omp_construct (parser, pragma_tok, if_p);
37354 pop_omp_privatization_clauses (stmt);
37355 return true;
37357 case PRAGMA_OMP_ORDERED:
37358 stmt = push_omp_privatization_clauses (false);
37359 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
37360 pop_omp_privatization_clauses (stmt);
37361 return ret;
37363 case PRAGMA_OMP_TARGET:
37364 stmt = push_omp_privatization_clauses (false);
37365 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
37366 pop_omp_privatization_clauses (stmt);
37367 return ret;
37369 case PRAGMA_OMP_END_DECLARE_TARGET:
37370 cp_parser_omp_end_declare_target (parser, pragma_tok);
37371 return false;
37373 case PRAGMA_OMP_SECTION:
37374 error_at (pragma_tok->location,
37375 "%<#pragma omp section%> may only be used in "
37376 "%<#pragma omp sections%> construct");
37377 break;
37379 case PRAGMA_IVDEP:
37381 if (context == pragma_external)
37383 error_at (pragma_tok->location,
37384 "%<#pragma GCC ivdep%> must be inside a function");
37385 break;
37387 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37388 cp_token *tok;
37389 tok = cp_lexer_peek_token (the_parser->lexer);
37390 if (tok->type != CPP_KEYWORD
37391 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
37392 && tok->keyword != RID_DO))
37394 cp_parser_error (parser, "for, while or do statement expected");
37395 return false;
37397 cp_parser_iteration_statement (parser, if_p, true);
37398 return true;
37401 case PRAGMA_CILK_SIMD:
37402 if (context == pragma_external)
37404 error_at (pragma_tok->location,
37405 "%<#pragma simd%> must be inside a function");
37406 break;
37408 stmt = push_omp_privatization_clauses (false);
37409 cp_parser_cilk_simd (parser, pragma_tok, if_p);
37410 pop_omp_privatization_clauses (stmt);
37411 return true;
37413 case PRAGMA_CILK_GRAINSIZE:
37414 if (context == pragma_external)
37416 error_at (pragma_tok->location,
37417 "%<#pragma cilk grainsize%> must be inside a function");
37418 break;
37421 /* Ignore the pragma if Cilk Plus is not enabled. */
37422 if (flag_cilkplus)
37424 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
37425 return true;
37427 else
37429 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
37430 "%<#pragma cilk grainsize%>");
37431 break;
37434 default:
37435 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
37436 c_invoke_pragma_handler (id);
37437 break;
37439 bad_stmt:
37440 cp_parser_error (parser, "expected declaration specifiers");
37441 break;
37444 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37445 return false;
37448 /* The interface the pragma parsers have to the lexer. */
37450 enum cpp_ttype
37451 pragma_lex (tree *value, location_t *loc)
37453 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
37454 enum cpp_ttype ret = tok->type;
37456 *value = tok->u.value;
37457 if (loc)
37458 *loc = tok->location;
37460 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
37461 ret = CPP_EOF;
37462 else if (ret == CPP_STRING)
37463 *value = cp_parser_string_literal (the_parser, false, false);
37464 else
37466 if (ret == CPP_KEYWORD)
37467 ret = CPP_NAME;
37468 cp_lexer_consume_token (the_parser->lexer);
37471 return ret;
37475 /* External interface. */
37477 /* Parse one entire translation unit. */
37479 void
37480 c_parse_file (void)
37482 static bool already_called = false;
37484 if (already_called)
37485 fatal_error (input_location,
37486 "inter-module optimizations not implemented for C++");
37487 already_called = true;
37489 the_parser = cp_parser_new ();
37490 push_deferring_access_checks (flag_access_control
37491 ? dk_no_deferred : dk_no_check);
37492 cp_parser_translation_unit (the_parser);
37493 the_parser = NULL;
37496 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
37497 vectorlength clause:
37498 Syntax:
37499 vectorlength ( constant-expression ) */
37501 static tree
37502 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
37503 bool is_simd_fn)
37505 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37506 tree expr;
37507 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
37508 safelen clause. Thus, vectorlength is represented as OMP 4.0
37509 safelen. For SIMD-enabled function it is represented by OMP 4.0
37510 simdlen. */
37511 if (!is_simd_fn)
37512 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
37513 loc);
37514 else
37515 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
37516 loc);
37518 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37519 return error_mark_node;
37521 expr = cp_parser_constant_expression (parser);
37522 expr = maybe_constant_value (expr);
37524 /* If expr == error_mark_node, then don't emit any errors nor
37525 create a clause. if any of the above functions returns
37526 error mark node then they would have emitted an error message. */
37527 if (expr == error_mark_node)
37529 else if (!TREE_TYPE (expr)
37530 || !TREE_CONSTANT (expr)
37531 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
37532 error_at (loc, "vectorlength must be an integer constant");
37533 else if (TREE_CONSTANT (expr)
37534 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
37535 error_at (loc, "vectorlength must be a power of 2");
37536 else
37538 tree c;
37539 if (!is_simd_fn)
37541 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
37542 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
37543 OMP_CLAUSE_CHAIN (c) = clauses;
37544 clauses = c;
37546 else
37548 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
37549 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
37550 OMP_CLAUSE_CHAIN (c) = clauses;
37551 clauses = c;
37555 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37556 return error_mark_node;
37557 return clauses;
37560 /* Handles the Cilk Plus #pragma simd linear clause.
37561 Syntax:
37562 linear ( simd-linear-variable-list )
37564 simd-linear-variable-list:
37565 simd-linear-variable
37566 simd-linear-variable-list , simd-linear-variable
37568 simd-linear-variable:
37569 id-expression
37570 id-expression : simd-linear-step
37572 simd-linear-step:
37573 conditional-expression */
37575 static tree
37576 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
37578 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37580 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37581 return clauses;
37582 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37584 cp_parser_error (parser, "expected identifier");
37585 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37586 return error_mark_node;
37589 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37590 parser->colon_corrects_to_scope_p = false;
37591 while (1)
37593 cp_token *token = cp_lexer_peek_token (parser->lexer);
37594 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37596 cp_parser_error (parser, "expected variable-name");
37597 clauses = error_mark_node;
37598 break;
37601 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
37602 false, false);
37603 tree decl = cp_parser_lookup_name_simple (parser, var_name,
37604 token->location);
37605 if (decl == error_mark_node)
37607 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
37608 token->location);
37609 clauses = error_mark_node;
37611 else
37613 tree e = NULL_TREE;
37614 tree step_size = integer_one_node;
37616 /* If present, parse the linear step. Otherwise, assume the default
37617 value of 1. */
37618 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
37620 cp_lexer_consume_token (parser->lexer);
37622 e = cp_parser_assignment_expression (parser);
37623 e = maybe_constant_value (e);
37625 if (e == error_mark_node)
37627 /* If an error has occurred, then the whole pragma is
37628 considered ill-formed. Thus, no reason to keep
37629 parsing. */
37630 clauses = error_mark_node;
37631 break;
37633 else if (type_dependent_expression_p (e)
37634 || value_dependent_expression_p (e)
37635 || (TREE_TYPE (e)
37636 && INTEGRAL_TYPE_P (TREE_TYPE (e))
37637 && (TREE_CONSTANT (e)
37638 || DECL_P (e))))
37639 step_size = e;
37640 else
37641 cp_parser_error (parser,
37642 "step size must be an integer constant "
37643 "expression or an integer variable");
37646 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
37647 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
37648 OMP_CLAUSE_DECL (l) = decl;
37649 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
37650 OMP_CLAUSE_CHAIN (l) = clauses;
37651 clauses = l;
37653 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37654 cp_lexer_consume_token (parser->lexer);
37655 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37656 break;
37657 else
37659 error_at (cp_lexer_peek_token (parser->lexer)->location,
37660 "expected %<,%> or %<)%> after %qE", decl);
37661 clauses = error_mark_node;
37662 break;
37665 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37666 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37667 return clauses;
37670 /* Returns the name of the next clause. If the clause is not
37671 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
37672 token is not consumed. Otherwise, the appropriate enum from the
37673 pragma_simd_clause is returned and the token is consumed. */
37675 static pragma_omp_clause
37676 cp_parser_cilk_simd_clause_name (cp_parser *parser)
37678 pragma_omp_clause clause_type;
37679 cp_token *token = cp_lexer_peek_token (parser->lexer);
37681 if (token->keyword == RID_PRIVATE)
37682 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
37683 else if (!token->u.value || token->type != CPP_NAME)
37684 return PRAGMA_CILK_CLAUSE_NONE;
37685 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
37686 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
37687 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
37688 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
37689 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
37690 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
37691 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
37692 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
37693 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
37694 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
37695 else
37696 return PRAGMA_CILK_CLAUSE_NONE;
37698 cp_lexer_consume_token (parser->lexer);
37699 return clause_type;
37702 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
37704 static tree
37705 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
37707 tree clauses = NULL_TREE;
37709 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37710 && clauses != error_mark_node)
37712 pragma_omp_clause c_kind;
37713 c_kind = cp_parser_cilk_simd_clause_name (parser);
37714 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
37715 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
37716 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
37717 clauses = cp_parser_cilk_simd_linear (parser, clauses);
37718 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
37719 /* Use the OpenMP 4.0 equivalent function. */
37720 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
37721 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
37722 /* Use the OpenMP 4.0 equivalent function. */
37723 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
37724 clauses);
37725 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
37726 /* Use the OMP 4.0 equivalent function. */
37727 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
37728 clauses);
37729 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
37730 /* Use the OMP 4.0 equivalent function. */
37731 clauses = cp_parser_omp_clause_reduction (parser, clauses);
37732 else
37734 clauses = error_mark_node;
37735 cp_parser_error (parser, "expected %<#pragma simd%> clause");
37736 break;
37740 cp_parser_skip_to_pragma_eol (parser, pragma_token);
37742 if (clauses == error_mark_node)
37743 return error_mark_node;
37744 else
37745 return finish_omp_clauses (clauses, C_ORT_CILK);
37748 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
37750 static void
37751 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
37753 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
37755 if (clauses == error_mark_node)
37756 return;
37758 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
37760 error_at (cp_lexer_peek_token (parser->lexer)->location,
37761 "for statement expected");
37762 return;
37765 tree sb = begin_omp_structured_block ();
37766 int save = cp_parser_begin_omp_structured_block (parser);
37767 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
37768 if (ret)
37769 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
37770 cp_parser_end_omp_structured_block (parser, save);
37771 add_stmt (finish_omp_structured_block (sb));
37774 /* Main entry-point for parsing Cilk Plus _Cilk_for
37775 loops. The return value is error_mark_node
37776 when errors happen and CILK_FOR tree on success. */
37778 static tree
37779 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
37781 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
37782 gcc_unreachable ();
37784 tree sb = begin_omp_structured_block ();
37785 int save = cp_parser_begin_omp_structured_block (parser);
37787 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
37788 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
37789 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
37790 clauses = finish_omp_clauses (clauses, C_ORT_CILK);
37792 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
37793 if (ret)
37794 cpp_validate_cilk_plus_loop (ret);
37795 else
37796 ret = error_mark_node;
37798 cp_parser_end_omp_structured_block (parser, save);
37799 add_stmt (finish_omp_structured_block (sb));
37800 return ret;
37803 /* Create an identifier for a generic parameter type (a synthesized
37804 template parameter implied by `auto' or a concept identifier). */
37806 static GTY(()) int generic_parm_count;
37807 static tree
37808 make_generic_type_name ()
37810 char buf[32];
37811 sprintf (buf, "auto:%d", ++generic_parm_count);
37812 return get_identifier (buf);
37815 /* Predicate that behaves as is_auto_or_concept but matches the parent
37816 node of the generic type rather than the generic type itself. This
37817 allows for type transformation in add_implicit_template_parms. */
37819 static inline bool
37820 tree_type_is_auto_or_concept (const_tree t)
37822 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
37825 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
37826 (creating a new template parameter list if necessary). Returns the newly
37827 created template type parm. */
37829 static tree
37830 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
37832 gcc_assert (current_binding_level->kind == sk_function_parms);
37834 /* Before committing to modifying any scope, if we're in an
37835 implicit template scope, and we're trying to synthesize a
37836 constrained parameter, try to find a previous parameter with
37837 the same name. This is the same-type rule for abbreviated
37838 function templates. */
37839 if (parser->implicit_template_scope && constr)
37841 tree t = parser->implicit_template_parms;
37842 while (t)
37844 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
37846 tree d = TREE_VALUE (t);
37847 if (TREE_CODE (d) == PARM_DECL)
37848 /* Return the TEMPLATE_PARM_INDEX. */
37849 d = DECL_INITIAL (d);
37850 return d;
37852 t = TREE_CHAIN (t);
37856 /* We are either continuing a function template that already contains implicit
37857 template parameters, creating a new fully-implicit function template, or
37858 extending an existing explicit function template with implicit template
37859 parameters. */
37861 cp_binding_level *const entry_scope = current_binding_level;
37863 bool become_template = false;
37864 cp_binding_level *parent_scope = 0;
37866 if (parser->implicit_template_scope)
37868 gcc_assert (parser->implicit_template_parms);
37870 current_binding_level = parser->implicit_template_scope;
37872 else
37874 /* Roll back to the existing template parameter scope (in the case of
37875 extending an explicit function template) or introduce a new template
37876 parameter scope ahead of the function parameter scope (or class scope
37877 in the case of out-of-line member definitions). The function scope is
37878 added back after template parameter synthesis below. */
37880 cp_binding_level *scope = entry_scope;
37882 while (scope->kind == sk_function_parms)
37884 parent_scope = scope;
37885 scope = scope->level_chain;
37887 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
37889 /* If not defining a class, then any class scope is a scope level in
37890 an out-of-line member definition. In this case simply wind back
37891 beyond the first such scope to inject the template parameter list.
37892 Otherwise wind back to the class being defined. The latter can
37893 occur in class member friend declarations such as:
37895 class A {
37896 void foo (auto);
37898 class B {
37899 friend void A::foo (auto);
37902 The template parameter list synthesized for the friend declaration
37903 must be injected in the scope of 'B'. This can also occur in
37904 erroneous cases such as:
37906 struct A {
37907 struct B {
37908 void foo (auto);
37910 void B::foo (auto) {}
37913 Here the attempted definition of 'B::foo' within 'A' is ill-formed
37914 but, nevertheless, the template parameter list synthesized for the
37915 declarator should be injected into the scope of 'A' as if the
37916 ill-formed template was specified explicitly. */
37918 while (scope->kind == sk_class && !scope->defining_class_p)
37920 parent_scope = scope;
37921 scope = scope->level_chain;
37925 current_binding_level = scope;
37927 if (scope->kind != sk_template_parms
37928 || !function_being_declared_is_template_p (parser))
37930 /* Introduce a new template parameter list for implicit template
37931 parameters. */
37933 become_template = true;
37935 parser->implicit_template_scope
37936 = begin_scope (sk_template_parms, NULL);
37938 ++processing_template_decl;
37940 parser->fully_implicit_function_template_p = true;
37941 ++parser->num_template_parameter_lists;
37943 else
37945 /* Synthesize implicit template parameters at the end of the explicit
37946 template parameter list. */
37948 gcc_assert (current_template_parms);
37950 parser->implicit_template_scope = scope;
37952 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
37953 parser->implicit_template_parms
37954 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
37958 /* Synthesize a new template parameter and track the current template
37959 parameter chain with implicit_template_parms. */
37961 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
37962 tree synth_id = make_generic_type_name ();
37963 tree synth_tmpl_parm;
37964 bool non_type = false;
37966 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
37967 synth_tmpl_parm
37968 = finish_template_type_parm (class_type_node, synth_id);
37969 else if (TREE_CODE (proto) == TEMPLATE_DECL)
37970 synth_tmpl_parm
37971 = finish_constrained_template_template_parm (proto, synth_id);
37972 else
37974 synth_tmpl_parm = copy_decl (proto);
37975 DECL_NAME (synth_tmpl_parm) = synth_id;
37976 non_type = true;
37979 // Attach the constraint to the parm before processing.
37980 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
37981 TREE_TYPE (node) = constr;
37982 tree new_parm
37983 = process_template_parm (parser->implicit_template_parms,
37984 input_location,
37985 node,
37986 /*non_type=*/non_type,
37987 /*param_pack=*/false);
37989 // Chain the new parameter to the list of implicit parameters.
37990 if (parser->implicit_template_parms)
37991 parser->implicit_template_parms
37992 = TREE_CHAIN (parser->implicit_template_parms);
37993 else
37994 parser->implicit_template_parms = new_parm;
37996 tree new_decl = getdecls ();
37997 if (non_type)
37998 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
37999 new_decl = DECL_INITIAL (new_decl);
38001 /* If creating a fully implicit function template, start the new implicit
38002 template parameter list with this synthesized type, otherwise grow the
38003 current template parameter list. */
38005 if (become_template)
38007 parent_scope->level_chain = current_binding_level;
38009 tree new_parms = make_tree_vec (1);
38010 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
38011 current_template_parms = tree_cons (size_int (processing_template_decl),
38012 new_parms, current_template_parms);
38014 else
38016 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38017 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
38018 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
38019 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
38022 // If the new parameter was constrained, we need to add that to the
38023 // constraints in the template parameter list.
38024 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
38026 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
38027 reqs = conjoin_constraints (reqs, req);
38028 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
38031 current_binding_level = entry_scope;
38033 return new_decl;
38036 /* Finish the declaration of a fully implicit function template. Such a
38037 template has no explicit template parameter list so has not been through the
38038 normal template head and tail processing. synthesize_implicit_template_parm
38039 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
38040 provided if the declaration is a class member such that its template
38041 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
38042 form is returned. Otherwise NULL_TREE is returned. */
38044 static tree
38045 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
38047 gcc_assert (parser->fully_implicit_function_template_p);
38049 if (member_decl_opt && member_decl_opt != error_mark_node
38050 && DECL_VIRTUAL_P (member_decl_opt))
38052 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
38053 "implicit templates may not be %<virtual%>");
38054 DECL_VIRTUAL_P (member_decl_opt) = false;
38057 if (member_decl_opt)
38058 member_decl_opt = finish_member_template_decl (member_decl_opt);
38059 end_template_decl ();
38061 parser->fully_implicit_function_template_p = false;
38062 --parser->num_template_parameter_lists;
38064 return member_decl_opt;
38067 #include "gt-cp-parser.h"