selftest: split out named_temp_file from temp_source_file
[official-gcc.git] / gcc / cp / parser.c
blobca9f8b9761a5aaf829f81f21e8166078466bd270
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"
43 #include "gcc-rich-location.h"
44 #include "tree-iterator.h"
47 /* The lexer. */
49 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
50 and c-lex.c) and the C++ parser. */
52 static cp_token eof_token =
54 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
57 /* The various kinds of non integral constant we encounter. */
58 enum non_integral_constant {
59 NIC_NONE,
60 /* floating-point literal */
61 NIC_FLOAT,
62 /* %<this%> */
63 NIC_THIS,
64 /* %<__FUNCTION__%> */
65 NIC_FUNC_NAME,
66 /* %<__PRETTY_FUNCTION__%> */
67 NIC_PRETTY_FUNC,
68 /* %<__func__%> */
69 NIC_C99_FUNC,
70 /* "%<va_arg%> */
71 NIC_VA_ARG,
72 /* a cast */
73 NIC_CAST,
74 /* %<typeid%> operator */
75 NIC_TYPEID,
76 /* non-constant compound literals */
77 NIC_NCC,
78 /* a function call */
79 NIC_FUNC_CALL,
80 /* an increment */
81 NIC_INC,
82 /* an decrement */
83 NIC_DEC,
84 /* an array reference */
85 NIC_ARRAY_REF,
86 /* %<->%> */
87 NIC_ARROW,
88 /* %<.%> */
89 NIC_POINT,
90 /* the address of a label */
91 NIC_ADDR_LABEL,
92 /* %<*%> */
93 NIC_STAR,
94 /* %<&%> */
95 NIC_ADDR,
96 /* %<++%> */
97 NIC_PREINCREMENT,
98 /* %<--%> */
99 NIC_PREDECREMENT,
100 /* %<new%> */
101 NIC_NEW,
102 /* %<delete%> */
103 NIC_DEL,
104 /* calls to overloaded operators */
105 NIC_OVERLOADED,
106 /* an assignment */
107 NIC_ASSIGNMENT,
108 /* a comma operator */
109 NIC_COMMA,
110 /* a call to a constructor */
111 NIC_CONSTRUCTOR,
112 /* a transaction expression */
113 NIC_TRANSACTION
116 /* The various kinds of errors about name-lookup failing. */
117 enum name_lookup_error {
118 /* NULL */
119 NLE_NULL,
120 /* is not a type */
121 NLE_TYPE,
122 /* is not a class or namespace */
123 NLE_CXX98,
124 /* is not a class, namespace, or enumeration */
125 NLE_NOT_CXX98
128 /* The various kinds of required token */
129 enum required_token {
130 RT_NONE,
131 RT_SEMICOLON, /* ';' */
132 RT_OPEN_PAREN, /* '(' */
133 RT_CLOSE_BRACE, /* '}' */
134 RT_OPEN_BRACE, /* '{' */
135 RT_CLOSE_SQUARE, /* ']' */
136 RT_OPEN_SQUARE, /* '[' */
137 RT_COMMA, /* ',' */
138 RT_SCOPE, /* '::' */
139 RT_LESS, /* '<' */
140 RT_GREATER, /* '>' */
141 RT_EQ, /* '=' */
142 RT_ELLIPSIS, /* '...' */
143 RT_MULT, /* '*' */
144 RT_COMPL, /* '~' */
145 RT_COLON, /* ':' */
146 RT_COLON_SCOPE, /* ':' or '::' */
147 RT_CLOSE_PAREN, /* ')' */
148 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
149 RT_PRAGMA_EOL, /* end of line */
150 RT_NAME, /* identifier */
152 /* The type is CPP_KEYWORD */
153 RT_NEW, /* new */
154 RT_DELETE, /* delete */
155 RT_RETURN, /* return */
156 RT_WHILE, /* while */
157 RT_EXTERN, /* extern */
158 RT_STATIC_ASSERT, /* static_assert */
159 RT_DECLTYPE, /* decltype */
160 RT_OPERATOR, /* operator */
161 RT_CLASS, /* class */
162 RT_TEMPLATE, /* template */
163 RT_NAMESPACE, /* namespace */
164 RT_USING, /* using */
165 RT_ASM, /* asm */
166 RT_TRY, /* try */
167 RT_CATCH, /* catch */
168 RT_THROW, /* throw */
169 RT_LABEL, /* __label__ */
170 RT_AT_TRY, /* @try */
171 RT_AT_SYNCHRONIZED, /* @synchronized */
172 RT_AT_THROW, /* @throw */
174 RT_SELECT, /* selection-statement */
175 RT_INTERATION, /* iteration-statement */
176 RT_JUMP, /* jump-statement */
177 RT_CLASS_KEY, /* class-key */
178 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
179 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
180 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
181 RT_TRANSACTION_CANCEL /* __transaction_cancel */
184 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
185 reverting it on destruction. */
187 class type_id_in_expr_sentinel
189 cp_parser *parser;
190 bool saved;
191 public:
192 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
193 : parser (parser),
194 saved (parser->in_type_id_in_expr_p)
195 { parser->in_type_id_in_expr_p = set; }
196 ~type_id_in_expr_sentinel ()
197 { parser->in_type_id_in_expr_p = saved; }
200 /* Prototypes. */
202 static cp_lexer *cp_lexer_new_main
203 (void);
204 static cp_lexer *cp_lexer_new_from_tokens
205 (cp_token_cache *tokens);
206 static void cp_lexer_destroy
207 (cp_lexer *);
208 static int cp_lexer_saving_tokens
209 (const cp_lexer *);
210 static cp_token *cp_lexer_token_at
211 (cp_lexer *, cp_token_position);
212 static void cp_lexer_get_preprocessor_token
213 (cp_lexer *, cp_token *);
214 static inline cp_token *cp_lexer_peek_token
215 (cp_lexer *);
216 static cp_token *cp_lexer_peek_nth_token
217 (cp_lexer *, size_t);
218 static inline bool cp_lexer_next_token_is
219 (cp_lexer *, enum cpp_ttype);
220 static bool cp_lexer_next_token_is_not
221 (cp_lexer *, enum cpp_ttype);
222 static bool cp_lexer_next_token_is_keyword
223 (cp_lexer *, enum rid);
224 static cp_token *cp_lexer_consume_token
225 (cp_lexer *);
226 static void cp_lexer_purge_token
227 (cp_lexer *);
228 static void cp_lexer_purge_tokens_after
229 (cp_lexer *, cp_token_position);
230 static void cp_lexer_save_tokens
231 (cp_lexer *);
232 static void cp_lexer_commit_tokens
233 (cp_lexer *);
234 static void cp_lexer_rollback_tokens
235 (cp_lexer *);
236 static void cp_lexer_print_token
237 (FILE *, cp_token *);
238 static inline bool cp_lexer_debugging_p
239 (cp_lexer *);
240 static void cp_lexer_start_debugging
241 (cp_lexer *) ATTRIBUTE_UNUSED;
242 static void cp_lexer_stop_debugging
243 (cp_lexer *) ATTRIBUTE_UNUSED;
245 static cp_token_cache *cp_token_cache_new
246 (cp_token *, cp_token *);
248 static void cp_parser_initial_pragma
249 (cp_token *);
251 static tree cp_literal_operator_id
252 (const char *);
254 static void cp_parser_cilk_simd
255 (cp_parser *, cp_token *, bool *);
256 static tree cp_parser_cilk_for
257 (cp_parser *, tree, bool *);
258 static bool cp_parser_omp_declare_reduction_exprs
259 (tree, cp_parser *);
260 static tree cp_parser_cilk_simd_vectorlength
261 (cp_parser *, tree, bool);
262 static void cp_finalize_oacc_routine
263 (cp_parser *, tree, bool);
265 /* Manifest constants. */
266 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
267 #define CP_SAVED_TOKEN_STACK 5
269 /* Variables. */
271 /* The stream to which debugging output should be written. */
272 static FILE *cp_lexer_debug_stream;
274 /* Nonzero if we are parsing an unevaluated operand: an operand to
275 sizeof, typeof, or alignof. */
276 int cp_unevaluated_operand;
278 /* Dump up to NUM tokens in BUFFER to FILE starting with token
279 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
280 first token in BUFFER. If NUM is 0, dump all the tokens. If
281 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
282 highlighted by surrounding it in [[ ]]. */
284 static void
285 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
286 cp_token *start_token, unsigned num,
287 cp_token *curr_token)
289 unsigned i, nprinted;
290 cp_token *token;
291 bool do_print;
293 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
295 if (buffer == NULL)
296 return;
298 if (num == 0)
299 num = buffer->length ();
301 if (start_token == NULL)
302 start_token = buffer->address ();
304 if (start_token > buffer->address ())
306 cp_lexer_print_token (file, &(*buffer)[0]);
307 fprintf (file, " ... ");
310 do_print = false;
311 nprinted = 0;
312 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
314 if (token == start_token)
315 do_print = true;
317 if (!do_print)
318 continue;
320 nprinted++;
321 if (token == curr_token)
322 fprintf (file, "[[");
324 cp_lexer_print_token (file, token);
326 if (token == curr_token)
327 fprintf (file, "]]");
329 switch (token->type)
331 case CPP_SEMICOLON:
332 case CPP_OPEN_BRACE:
333 case CPP_CLOSE_BRACE:
334 case CPP_EOF:
335 fputc ('\n', file);
336 break;
338 default:
339 fputc (' ', file);
343 if (i == num && i < buffer->length ())
345 fprintf (file, " ... ");
346 cp_lexer_print_token (file, &buffer->last ());
349 fprintf (file, "\n");
353 /* Dump all tokens in BUFFER to stderr. */
355 void
356 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
358 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
361 DEBUG_FUNCTION void
362 debug (vec<cp_token, va_gc> &ref)
364 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
367 DEBUG_FUNCTION void
368 debug (vec<cp_token, va_gc> *ptr)
370 if (ptr)
371 debug (*ptr);
372 else
373 fprintf (stderr, "<nil>\n");
377 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
378 description for T. */
380 static void
381 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
383 if (t)
385 fprintf (file, "%s: ", desc);
386 print_node_brief (file, "", t, 0);
391 /* Dump parser context C to FILE. */
393 static void
394 cp_debug_print_context (FILE *file, cp_parser_context *c)
396 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
397 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
398 print_node_brief (file, "", c->object_type, 0);
399 fprintf (file, "}\n");
403 /* Print the stack of parsing contexts to FILE starting with FIRST. */
405 static void
406 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
408 unsigned i;
409 cp_parser_context *c;
411 fprintf (file, "Parsing context stack:\n");
412 for (i = 0, c = first; c; c = c->next, i++)
414 fprintf (file, "\t#%u: ", i);
415 cp_debug_print_context (file, c);
420 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
422 static void
423 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
425 if (flag)
426 fprintf (file, "%s: true\n", desc);
430 /* Print an unparsed function entry UF to FILE. */
432 static void
433 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
435 unsigned i;
436 cp_default_arg_entry *default_arg_fn;
437 tree fn;
439 fprintf (file, "\tFunctions with default args:\n");
440 for (i = 0;
441 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
442 i++)
444 fprintf (file, "\t\tClass type: ");
445 print_node_brief (file, "", default_arg_fn->class_type, 0);
446 fprintf (file, "\t\tDeclaration: ");
447 print_node_brief (file, "", default_arg_fn->decl, 0);
448 fprintf (file, "\n");
451 fprintf (file, "\n\tFunctions with definitions that require "
452 "post-processing\n\t\t");
453 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
455 print_node_brief (file, "", fn, 0);
456 fprintf (file, " ");
458 fprintf (file, "\n");
460 fprintf (file, "\n\tNon-static data members with initializers that require "
461 "post-processing\n\t\t");
462 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
464 print_node_brief (file, "", fn, 0);
465 fprintf (file, " ");
467 fprintf (file, "\n");
471 /* Print the stack of unparsed member functions S to FILE. */
473 static void
474 cp_debug_print_unparsed_queues (FILE *file,
475 vec<cp_unparsed_functions_entry, va_gc> *s)
477 unsigned i;
478 cp_unparsed_functions_entry *uf;
480 fprintf (file, "Unparsed functions\n");
481 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
483 fprintf (file, "#%u:\n", i);
484 cp_debug_print_unparsed_function (file, uf);
489 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
490 the given PARSER. If FILE is NULL, the output is printed on stderr. */
492 static void
493 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
495 cp_token *next_token, *first_token, *start_token;
497 if (file == NULL)
498 file = stderr;
500 next_token = parser->lexer->next_token;
501 first_token = parser->lexer->buffer->address ();
502 start_token = (next_token > first_token + window_size / 2)
503 ? next_token - window_size / 2
504 : first_token;
505 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
506 next_token);
510 /* Dump debugging information for the given PARSER. If FILE is NULL,
511 the output is printed on stderr. */
513 void
514 cp_debug_parser (FILE *file, cp_parser *parser)
516 const size_t window_size = 20;
517 cp_token *token;
518 expanded_location eloc;
520 if (file == NULL)
521 file = stderr;
523 fprintf (file, "Parser state\n\n");
524 fprintf (file, "Number of tokens: %u\n",
525 vec_safe_length (parser->lexer->buffer));
526 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
527 cp_debug_print_tree_if_set (file, "Object scope",
528 parser->object_scope);
529 cp_debug_print_tree_if_set (file, "Qualifying scope",
530 parser->qualifying_scope);
531 cp_debug_print_context_stack (file, parser->context);
532 cp_debug_print_flag (file, "Allow GNU extensions",
533 parser->allow_gnu_extensions_p);
534 cp_debug_print_flag (file, "'>' token is greater-than",
535 parser->greater_than_is_operator_p);
536 cp_debug_print_flag (file, "Default args allowed in current "
537 "parameter list", parser->default_arg_ok_p);
538 cp_debug_print_flag (file, "Parsing integral constant-expression",
539 parser->integral_constant_expression_p);
540 cp_debug_print_flag (file, "Allow non-constant expression in current "
541 "constant-expression",
542 parser->allow_non_integral_constant_expression_p);
543 cp_debug_print_flag (file, "Seen non-constant expression",
544 parser->non_integral_constant_expression_p);
545 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
546 "current context",
547 parser->local_variables_forbidden_p);
548 cp_debug_print_flag (file, "In unbraced linkage specification",
549 parser->in_unbraced_linkage_specification_p);
550 cp_debug_print_flag (file, "Parsing a declarator",
551 parser->in_declarator_p);
552 cp_debug_print_flag (file, "In template argument list",
553 parser->in_template_argument_list_p);
554 cp_debug_print_flag (file, "Parsing an iteration statement",
555 parser->in_statement & IN_ITERATION_STMT);
556 cp_debug_print_flag (file, "Parsing a switch statement",
557 parser->in_statement & IN_SWITCH_STMT);
558 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
559 parser->in_statement & IN_OMP_BLOCK);
560 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
561 parser->in_statement & IN_CILK_SIMD_FOR);
562 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
563 parser->in_statement & IN_OMP_FOR);
564 cp_debug_print_flag (file, "Parsing an if statement",
565 parser->in_statement & IN_IF_STMT);
566 cp_debug_print_flag (file, "Parsing a type-id in an expression "
567 "context", parser->in_type_id_in_expr_p);
568 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
569 parser->implicit_extern_c);
570 cp_debug_print_flag (file, "String expressions should be translated "
571 "to execution character set",
572 parser->translate_strings_p);
573 cp_debug_print_flag (file, "Parsing function body outside of a "
574 "local class", parser->in_function_body);
575 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
576 parser->colon_corrects_to_scope_p);
577 cp_debug_print_flag (file, "Colon doesn't start a class definition",
578 parser->colon_doesnt_start_class_def_p);
579 if (parser->type_definition_forbidden_message)
580 fprintf (file, "Error message for forbidden type definitions: %s\n",
581 parser->type_definition_forbidden_message);
582 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
583 fprintf (file, "Number of class definitions in progress: %u\n",
584 parser->num_classes_being_defined);
585 fprintf (file, "Number of template parameter lists for the current "
586 "declaration: %u\n", parser->num_template_parameter_lists);
587 cp_debug_parser_tokens (file, parser, window_size);
588 token = parser->lexer->next_token;
589 fprintf (file, "Next token to parse:\n");
590 fprintf (file, "\tToken: ");
591 cp_lexer_print_token (file, token);
592 eloc = expand_location (token->location);
593 fprintf (file, "\n\tFile: %s\n", eloc.file);
594 fprintf (file, "\tLine: %d\n", eloc.line);
595 fprintf (file, "\tColumn: %d\n", eloc.column);
598 DEBUG_FUNCTION void
599 debug (cp_parser &ref)
601 cp_debug_parser (stderr, &ref);
604 DEBUG_FUNCTION void
605 debug (cp_parser *ptr)
607 if (ptr)
608 debug (*ptr);
609 else
610 fprintf (stderr, "<nil>\n");
613 /* Allocate memory for a new lexer object and return it. */
615 static cp_lexer *
616 cp_lexer_alloc (void)
618 cp_lexer *lexer;
620 c_common_no_more_pch ();
622 /* Allocate the memory. */
623 lexer = ggc_cleared_alloc<cp_lexer> ();
625 /* Initially we are not debugging. */
626 lexer->debugging_p = false;
628 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
630 /* Create the buffer. */
631 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
633 return lexer;
637 /* Create a new main C++ lexer, the lexer that gets tokens from the
638 preprocessor. */
640 static cp_lexer *
641 cp_lexer_new_main (void)
643 cp_lexer *lexer;
644 cp_token token;
646 /* It's possible that parsing the first pragma will load a PCH file,
647 which is a GC collection point. So we have to do that before
648 allocating any memory. */
649 cp_parser_initial_pragma (&token);
651 lexer = cp_lexer_alloc ();
653 /* Put the first token in the buffer. */
654 lexer->buffer->quick_push (token);
656 /* Get the remaining tokens from the preprocessor. */
657 while (token.type != CPP_EOF)
659 cp_lexer_get_preprocessor_token (lexer, &token);
660 vec_safe_push (lexer->buffer, token);
663 lexer->last_token = lexer->buffer->address ()
664 + lexer->buffer->length ()
665 - 1;
666 lexer->next_token = lexer->buffer->length ()
667 ? lexer->buffer->address ()
668 : &eof_token;
670 /* Subsequent preprocessor diagnostics should use compiler
671 diagnostic functions to get the compiler source location. */
672 done_lexing = true;
674 gcc_assert (!lexer->next_token->purged_p);
675 return lexer;
678 /* Create a new lexer whose token stream is primed with the tokens in
679 CACHE. When these tokens are exhausted, no new tokens will be read. */
681 static cp_lexer *
682 cp_lexer_new_from_tokens (cp_token_cache *cache)
684 cp_token *first = cache->first;
685 cp_token *last = cache->last;
686 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
688 /* We do not own the buffer. */
689 lexer->buffer = NULL;
690 lexer->next_token = first == last ? &eof_token : first;
691 lexer->last_token = last;
693 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
695 /* Initially we are not debugging. */
696 lexer->debugging_p = false;
698 gcc_assert (!lexer->next_token->purged_p);
699 return lexer;
702 /* Frees all resources associated with LEXER. */
704 static void
705 cp_lexer_destroy (cp_lexer *lexer)
707 vec_free (lexer->buffer);
708 lexer->saved_tokens.release ();
709 ggc_free (lexer);
712 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
713 be used. The point of this flag is to help the compiler to fold away calls
714 to cp_lexer_debugging_p within this source file at compile time, when the
715 lexer is not being debugged. */
717 #define LEXER_DEBUGGING_ENABLED_P false
719 /* Returns nonzero if debugging information should be output. */
721 static inline bool
722 cp_lexer_debugging_p (cp_lexer *lexer)
724 if (!LEXER_DEBUGGING_ENABLED_P)
725 return false;
727 return lexer->debugging_p;
731 static inline cp_token_position
732 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
734 gcc_assert (!previous_p || lexer->next_token != &eof_token);
736 return lexer->next_token - previous_p;
739 static inline cp_token *
740 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
742 return pos;
745 static inline void
746 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
748 lexer->next_token = cp_lexer_token_at (lexer, pos);
751 static inline cp_token_position
752 cp_lexer_previous_token_position (cp_lexer *lexer)
754 if (lexer->next_token == &eof_token)
755 return lexer->last_token - 1;
756 else
757 return cp_lexer_token_position (lexer, true);
760 static inline cp_token *
761 cp_lexer_previous_token (cp_lexer *lexer)
763 cp_token_position tp = cp_lexer_previous_token_position (lexer);
765 /* Skip past purged tokens. */
766 while (tp->purged_p)
768 gcc_assert (tp != lexer->buffer->address ());
769 tp--;
772 return cp_lexer_token_at (lexer, tp);
775 /* nonzero if we are presently saving tokens. */
777 static inline int
778 cp_lexer_saving_tokens (const cp_lexer* lexer)
780 return lexer->saved_tokens.length () != 0;
783 /* Store the next token from the preprocessor in *TOKEN. Return true
784 if we reach EOF. If LEXER is NULL, assume we are handling an
785 initial #pragma pch_preprocess, and thus want the lexer to return
786 processed strings. */
788 static void
789 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
791 static int is_extern_c = 0;
793 /* Get a new token from the preprocessor. */
794 token->type
795 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
796 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
797 token->keyword = RID_MAX;
798 token->purged_p = false;
799 token->error_reported = false;
801 /* On some systems, some header files are surrounded by an
802 implicit extern "C" block. Set a flag in the token if it
803 comes from such a header. */
804 is_extern_c += pending_lang_change;
805 pending_lang_change = 0;
806 token->implicit_extern_c = is_extern_c > 0;
808 /* Check to see if this token is a keyword. */
809 if (token->type == CPP_NAME)
811 if (C_IS_RESERVED_WORD (token->u.value))
813 /* Mark this token as a keyword. */
814 token->type = CPP_KEYWORD;
815 /* Record which keyword. */
816 token->keyword = C_RID_CODE (token->u.value);
818 else
820 if (warn_cxx11_compat
821 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
822 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
824 /* Warn about the C++0x keyword (but still treat it as
825 an identifier). */
826 warning (OPT_Wc__11_compat,
827 "identifier %qE is a keyword in C++11",
828 token->u.value);
830 /* Clear out the C_RID_CODE so we don't warn about this
831 particular identifier-turned-keyword again. */
832 C_SET_RID_CODE (token->u.value, RID_MAX);
835 token->keyword = RID_MAX;
838 else if (token->type == CPP_AT_NAME)
840 /* This only happens in Objective-C++; it must be a keyword. */
841 token->type = CPP_KEYWORD;
842 switch (C_RID_CODE (token->u.value))
844 /* Replace 'class' with '@class', 'private' with '@private',
845 etc. This prevents confusion with the C++ keyword
846 'class', and makes the tokens consistent with other
847 Objective-C 'AT' keywords. For example '@class' is
848 reported as RID_AT_CLASS which is consistent with
849 '@synchronized', which is reported as
850 RID_AT_SYNCHRONIZED.
852 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
853 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
854 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
855 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
856 case RID_THROW: token->keyword = RID_AT_THROW; break;
857 case RID_TRY: token->keyword = RID_AT_TRY; break;
858 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
859 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
860 default: token->keyword = C_RID_CODE (token->u.value);
865 /* Update the globals input_location and the input file stack from TOKEN. */
866 static inline void
867 cp_lexer_set_source_position_from_token (cp_token *token)
869 if (token->type != CPP_EOF)
871 input_location = token->location;
875 /* Update the globals input_location and the input file stack from LEXER. */
876 static inline void
877 cp_lexer_set_source_position (cp_lexer *lexer)
879 cp_token *token = cp_lexer_peek_token (lexer);
880 cp_lexer_set_source_position_from_token (token);
883 /* Return a pointer to the next token in the token stream, but do not
884 consume it. */
886 static inline cp_token *
887 cp_lexer_peek_token (cp_lexer *lexer)
889 if (cp_lexer_debugging_p (lexer))
891 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
892 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
893 putc ('\n', cp_lexer_debug_stream);
895 return lexer->next_token;
898 /* Return true if the next token has the indicated TYPE. */
900 static inline bool
901 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
903 return cp_lexer_peek_token (lexer)->type == type;
906 /* Return true if the next token does not have the indicated TYPE. */
908 static inline bool
909 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
911 return !cp_lexer_next_token_is (lexer, type);
914 /* Return true if the next token is the indicated KEYWORD. */
916 static inline bool
917 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
919 return cp_lexer_peek_token (lexer)->keyword == keyword;
922 static inline bool
923 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
925 return cp_lexer_peek_nth_token (lexer, n)->type == type;
928 static inline bool
929 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
931 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
934 /* Return true if the next token is not the indicated KEYWORD. */
936 static inline bool
937 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
939 return cp_lexer_peek_token (lexer)->keyword != keyword;
942 /* Return true if KEYWORD can start a decl-specifier. */
944 bool
945 cp_keyword_starts_decl_specifier_p (enum rid keyword)
947 switch (keyword)
949 /* auto specifier: storage-class-specifier in C++,
950 simple-type-specifier in C++0x. */
951 case RID_AUTO:
952 /* Storage classes. */
953 case RID_REGISTER:
954 case RID_STATIC:
955 case RID_EXTERN:
956 case RID_MUTABLE:
957 case RID_THREAD:
958 /* Elaborated type specifiers. */
959 case RID_ENUM:
960 case RID_CLASS:
961 case RID_STRUCT:
962 case RID_UNION:
963 case RID_TYPENAME:
964 /* Simple type specifiers. */
965 case RID_CHAR:
966 case RID_CHAR16:
967 case RID_CHAR32:
968 case RID_WCHAR:
969 case RID_BOOL:
970 case RID_SHORT:
971 case RID_INT:
972 case RID_LONG:
973 case RID_SIGNED:
974 case RID_UNSIGNED:
975 case RID_FLOAT:
976 case RID_DOUBLE:
977 case RID_VOID:
978 /* GNU extensions. */
979 case RID_ATTRIBUTE:
980 case RID_TYPEOF:
981 /* C++0x extensions. */
982 case RID_DECLTYPE:
983 case RID_UNDERLYING_TYPE:
984 case RID_CONSTEXPR:
985 return true;
987 default:
988 if (keyword >= RID_FIRST_INT_N
989 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
990 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
991 return true;
992 return false;
996 /* Return true if the next token is a keyword for a decl-specifier. */
998 static bool
999 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
1001 cp_token *token;
1003 token = cp_lexer_peek_token (lexer);
1004 return cp_keyword_starts_decl_specifier_p (token->keyword);
1007 /* Returns TRUE iff the token T begins a decltype type. */
1009 static bool
1010 token_is_decltype (cp_token *t)
1012 return (t->keyword == RID_DECLTYPE
1013 || t->type == CPP_DECLTYPE);
1016 /* Returns TRUE iff the next token begins a decltype type. */
1018 static bool
1019 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1021 cp_token *t = cp_lexer_peek_token (lexer);
1022 return token_is_decltype (t);
1025 /* Called when processing a token with tree_check_value; perform or defer the
1026 associated checks and return the value. */
1028 static tree
1029 saved_checks_value (struct tree_check *check_value)
1031 /* Perform any access checks that were deferred. */
1032 vec<deferred_access_check, va_gc> *checks;
1033 deferred_access_check *chk;
1034 checks = check_value->checks;
1035 if (checks)
1037 int i;
1038 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1039 perform_or_defer_access_check (chk->binfo,
1040 chk->decl,
1041 chk->diag_decl, tf_warning_or_error);
1043 /* Return the stored value. */
1044 return check_value->value;
1047 /* Return a pointer to the Nth token in the token stream. If N is 1,
1048 then this is precisely equivalent to cp_lexer_peek_token (except
1049 that it is not inline). One would like to disallow that case, but
1050 there is one case (cp_parser_nth_token_starts_template_id) where
1051 the caller passes a variable for N and it might be 1. */
1053 static cp_token *
1054 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1056 cp_token *token;
1058 /* N is 1-based, not zero-based. */
1059 gcc_assert (n > 0);
1061 if (cp_lexer_debugging_p (lexer))
1062 fprintf (cp_lexer_debug_stream,
1063 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1065 --n;
1066 token = lexer->next_token;
1067 gcc_assert (!n || token != &eof_token);
1068 while (n != 0)
1070 ++token;
1071 if (token == lexer->last_token)
1073 token = &eof_token;
1074 break;
1077 if (!token->purged_p)
1078 --n;
1081 if (cp_lexer_debugging_p (lexer))
1083 cp_lexer_print_token (cp_lexer_debug_stream, token);
1084 putc ('\n', cp_lexer_debug_stream);
1087 return token;
1090 /* Return the next token, and advance the lexer's next_token pointer
1091 to point to the next non-purged token. */
1093 static cp_token *
1094 cp_lexer_consume_token (cp_lexer* lexer)
1096 cp_token *token = lexer->next_token;
1098 gcc_assert (token != &eof_token);
1099 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1103 lexer->next_token++;
1104 if (lexer->next_token == lexer->last_token)
1106 lexer->next_token = &eof_token;
1107 break;
1111 while (lexer->next_token->purged_p);
1113 cp_lexer_set_source_position_from_token (token);
1115 /* Provide debugging output. */
1116 if (cp_lexer_debugging_p (lexer))
1118 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1119 cp_lexer_print_token (cp_lexer_debug_stream, token);
1120 putc ('\n', cp_lexer_debug_stream);
1123 return token;
1126 /* Permanently remove the next token from the token stream, and
1127 advance the next_token pointer to refer to the next non-purged
1128 token. */
1130 static void
1131 cp_lexer_purge_token (cp_lexer *lexer)
1133 cp_token *tok = lexer->next_token;
1135 gcc_assert (tok != &eof_token);
1136 tok->purged_p = true;
1137 tok->location = UNKNOWN_LOCATION;
1138 tok->u.value = NULL_TREE;
1139 tok->keyword = RID_MAX;
1143 tok++;
1144 if (tok == lexer->last_token)
1146 tok = &eof_token;
1147 break;
1150 while (tok->purged_p);
1151 lexer->next_token = tok;
1154 /* Permanently remove all tokens after TOK, up to, but not
1155 including, the token that will be returned next by
1156 cp_lexer_peek_token. */
1158 static void
1159 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1161 cp_token *peek = lexer->next_token;
1163 if (peek == &eof_token)
1164 peek = lexer->last_token;
1166 gcc_assert (tok < peek);
1168 for ( tok += 1; tok != peek; tok += 1)
1170 tok->purged_p = true;
1171 tok->location = UNKNOWN_LOCATION;
1172 tok->u.value = NULL_TREE;
1173 tok->keyword = RID_MAX;
1177 /* Begin saving tokens. All tokens consumed after this point will be
1178 preserved. */
1180 static void
1181 cp_lexer_save_tokens (cp_lexer* lexer)
1183 /* Provide debugging output. */
1184 if (cp_lexer_debugging_p (lexer))
1185 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1187 lexer->saved_tokens.safe_push (lexer->next_token);
1190 /* Commit to the portion of the token stream most recently saved. */
1192 static void
1193 cp_lexer_commit_tokens (cp_lexer* lexer)
1195 /* Provide debugging output. */
1196 if (cp_lexer_debugging_p (lexer))
1197 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1199 lexer->saved_tokens.pop ();
1202 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1203 to the token stream. Stop saving tokens. */
1205 static void
1206 cp_lexer_rollback_tokens (cp_lexer* lexer)
1208 /* Provide debugging output. */
1209 if (cp_lexer_debugging_p (lexer))
1210 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1212 lexer->next_token = lexer->saved_tokens.pop ();
1215 /* RAII wrapper around the above functions, with sanity checking. Creating
1216 a variable saves tokens, which are committed when the variable is
1217 destroyed unless they are explicitly rolled back by calling the rollback
1218 member function. */
1220 struct saved_token_sentinel
1222 cp_lexer *lexer;
1223 unsigned len;
1224 bool commit;
1225 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1227 len = lexer->saved_tokens.length ();
1228 cp_lexer_save_tokens (lexer);
1230 void rollback ()
1232 cp_lexer_rollback_tokens (lexer);
1233 commit = false;
1235 ~saved_token_sentinel()
1237 if (commit)
1238 cp_lexer_commit_tokens (lexer);
1239 gcc_assert (lexer->saved_tokens.length () == len);
1243 /* Print a representation of the TOKEN on the STREAM. */
1245 static void
1246 cp_lexer_print_token (FILE * stream, cp_token *token)
1248 /* We don't use cpp_type2name here because the parser defines
1249 a few tokens of its own. */
1250 static const char *const token_names[] = {
1251 /* cpplib-defined token types */
1252 #define OP(e, s) #e,
1253 #define TK(e, s) #e,
1254 TTYPE_TABLE
1255 #undef OP
1256 #undef TK
1257 /* C++ parser token types - see "Manifest constants", above. */
1258 "KEYWORD",
1259 "TEMPLATE_ID",
1260 "NESTED_NAME_SPECIFIER",
1263 /* For some tokens, print the associated data. */
1264 switch (token->type)
1266 case CPP_KEYWORD:
1267 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1268 For example, `struct' is mapped to an INTEGER_CST. */
1269 if (!identifier_p (token->u.value))
1270 break;
1271 /* fall through */
1272 case CPP_NAME:
1273 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1274 break;
1276 case CPP_STRING:
1277 case CPP_STRING16:
1278 case CPP_STRING32:
1279 case CPP_WSTRING:
1280 case CPP_UTF8STRING:
1281 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1282 break;
1284 case CPP_NUMBER:
1285 print_generic_expr (stream, token->u.value, 0);
1286 break;
1288 default:
1289 /* If we have a name for the token, print it out. Otherwise, we
1290 simply give the numeric code. */
1291 if (token->type < ARRAY_SIZE(token_names))
1292 fputs (token_names[token->type], stream);
1293 else
1294 fprintf (stream, "[%d]", token->type);
1295 break;
1299 DEBUG_FUNCTION void
1300 debug (cp_token &ref)
1302 cp_lexer_print_token (stderr, &ref);
1303 fprintf (stderr, "\n");
1306 DEBUG_FUNCTION void
1307 debug (cp_token *ptr)
1309 if (ptr)
1310 debug (*ptr);
1311 else
1312 fprintf (stderr, "<nil>\n");
1316 /* Start emitting debugging information. */
1318 static void
1319 cp_lexer_start_debugging (cp_lexer* lexer)
1321 if (!LEXER_DEBUGGING_ENABLED_P)
1322 fatal_error (input_location,
1323 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1325 lexer->debugging_p = true;
1326 cp_lexer_debug_stream = stderr;
1329 /* Stop emitting debugging information. */
1331 static void
1332 cp_lexer_stop_debugging (cp_lexer* lexer)
1334 if (!LEXER_DEBUGGING_ENABLED_P)
1335 fatal_error (input_location,
1336 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1338 lexer->debugging_p = false;
1339 cp_lexer_debug_stream = NULL;
1342 /* Create a new cp_token_cache, representing a range of tokens. */
1344 static cp_token_cache *
1345 cp_token_cache_new (cp_token *first, cp_token *last)
1347 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1348 cache->first = first;
1349 cache->last = last;
1350 return cache;
1353 /* Diagnose if #pragma omp declare simd isn't followed immediately
1354 by function declaration or definition. */
1356 static inline void
1357 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1359 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1361 error ("%<#pragma omp declare simd%> not immediately followed by "
1362 "function declaration or definition");
1363 parser->omp_declare_simd = NULL;
1367 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1368 and put that into "omp declare simd" attribute. */
1370 static inline void
1371 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1373 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1375 if (fndecl == error_mark_node)
1377 parser->omp_declare_simd = NULL;
1378 return;
1380 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1382 cp_ensure_no_omp_declare_simd (parser);
1383 return;
1388 /* Diagnose if #pragma acc routine isn't followed immediately by function
1389 declaration or definition. */
1391 static inline void
1392 cp_ensure_no_oacc_routine (cp_parser *parser)
1394 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1396 error_at (parser->oacc_routine->loc,
1397 "%<#pragma acc routine%> not immediately followed by "
1398 "function declaration or definition");
1399 parser->oacc_routine = NULL;
1403 /* Decl-specifiers. */
1405 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1407 static void
1408 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1410 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1413 /* Declarators. */
1415 /* Nothing other than the parser should be creating declarators;
1416 declarators are a semi-syntactic representation of C++ entities.
1417 Other parts of the front end that need to create entities (like
1418 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1420 static cp_declarator *make_call_declarator
1421 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1422 static cp_declarator *make_array_declarator
1423 (cp_declarator *, tree);
1424 static cp_declarator *make_pointer_declarator
1425 (cp_cv_quals, cp_declarator *, tree);
1426 static cp_declarator *make_reference_declarator
1427 (cp_cv_quals, cp_declarator *, bool, tree);
1428 static cp_declarator *make_ptrmem_declarator
1429 (cp_cv_quals, tree, cp_declarator *, tree);
1431 /* An erroneous declarator. */
1432 static cp_declarator *cp_error_declarator;
1434 /* The obstack on which declarators and related data structures are
1435 allocated. */
1436 static struct obstack declarator_obstack;
1438 /* Alloc BYTES from the declarator memory pool. */
1440 static inline void *
1441 alloc_declarator (size_t bytes)
1443 return obstack_alloc (&declarator_obstack, bytes);
1446 /* Allocate a declarator of the indicated KIND. Clear fields that are
1447 common to all declarators. */
1449 static cp_declarator *
1450 make_declarator (cp_declarator_kind kind)
1452 cp_declarator *declarator;
1454 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1455 declarator->kind = kind;
1456 declarator->attributes = NULL_TREE;
1457 declarator->std_attributes = NULL_TREE;
1458 declarator->declarator = NULL;
1459 declarator->parameter_pack_p = false;
1460 declarator->id_loc = UNKNOWN_LOCATION;
1462 return declarator;
1465 /* Make a declarator for a generalized identifier. If
1466 QUALIFYING_SCOPE is non-NULL, the identifier is
1467 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1468 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1469 is, if any. */
1471 static cp_declarator *
1472 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1473 special_function_kind sfk)
1475 cp_declarator *declarator;
1477 /* It is valid to write:
1479 class C { void f(); };
1480 typedef C D;
1481 void D::f();
1483 The standard is not clear about whether `typedef const C D' is
1484 legal; as of 2002-09-15 the committee is considering that
1485 question. EDG 3.0 allows that syntax. Therefore, we do as
1486 well. */
1487 if (qualifying_scope && TYPE_P (qualifying_scope))
1488 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1490 gcc_assert (identifier_p (unqualified_name)
1491 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1492 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1494 declarator = make_declarator (cdk_id);
1495 declarator->u.id.qualifying_scope = qualifying_scope;
1496 declarator->u.id.unqualified_name = unqualified_name;
1497 declarator->u.id.sfk = sfk;
1499 return declarator;
1502 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1503 of modifiers such as const or volatile to apply to the pointer
1504 type, represented as identifiers. ATTRIBUTES represent the attributes that
1505 appertain to the pointer or reference. */
1507 cp_declarator *
1508 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1509 tree attributes)
1511 cp_declarator *declarator;
1513 declarator = make_declarator (cdk_pointer);
1514 declarator->declarator = target;
1515 declarator->u.pointer.qualifiers = cv_qualifiers;
1516 declarator->u.pointer.class_type = NULL_TREE;
1517 if (target)
1519 declarator->id_loc = target->id_loc;
1520 declarator->parameter_pack_p = target->parameter_pack_p;
1521 target->parameter_pack_p = false;
1523 else
1524 declarator->parameter_pack_p = false;
1526 declarator->std_attributes = attributes;
1528 return declarator;
1531 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1532 represent the attributes that appertain to the pointer or
1533 reference. */
1535 cp_declarator *
1536 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1537 bool rvalue_ref, tree attributes)
1539 cp_declarator *declarator;
1541 declarator = make_declarator (cdk_reference);
1542 declarator->declarator = target;
1543 declarator->u.reference.qualifiers = cv_qualifiers;
1544 declarator->u.reference.rvalue_ref = rvalue_ref;
1545 if (target)
1547 declarator->id_loc = target->id_loc;
1548 declarator->parameter_pack_p = target->parameter_pack_p;
1549 target->parameter_pack_p = false;
1551 else
1552 declarator->parameter_pack_p = false;
1554 declarator->std_attributes = attributes;
1556 return declarator;
1559 /* Like make_pointer_declarator -- but for a pointer to a non-static
1560 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1561 appertain to the pointer or reference. */
1563 cp_declarator *
1564 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1565 cp_declarator *pointee,
1566 tree attributes)
1568 cp_declarator *declarator;
1570 declarator = make_declarator (cdk_ptrmem);
1571 declarator->declarator = pointee;
1572 declarator->u.pointer.qualifiers = cv_qualifiers;
1573 declarator->u.pointer.class_type = class_type;
1575 if (pointee)
1577 declarator->parameter_pack_p = pointee->parameter_pack_p;
1578 pointee->parameter_pack_p = false;
1580 else
1581 declarator->parameter_pack_p = false;
1583 declarator->std_attributes = attributes;
1585 return declarator;
1588 /* Make a declarator for the function given by TARGET, with the
1589 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1590 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1591 indicates what exceptions can be thrown. */
1593 cp_declarator *
1594 make_call_declarator (cp_declarator *target,
1595 tree parms,
1596 cp_cv_quals cv_qualifiers,
1597 cp_virt_specifiers virt_specifiers,
1598 cp_ref_qualifier ref_qualifier,
1599 tree tx_qualifier,
1600 tree exception_specification,
1601 tree late_return_type,
1602 tree requires_clause)
1604 cp_declarator *declarator;
1606 declarator = make_declarator (cdk_function);
1607 declarator->declarator = target;
1608 declarator->u.function.parameters = parms;
1609 declarator->u.function.qualifiers = cv_qualifiers;
1610 declarator->u.function.virt_specifiers = virt_specifiers;
1611 declarator->u.function.ref_qualifier = ref_qualifier;
1612 declarator->u.function.tx_qualifier = tx_qualifier;
1613 declarator->u.function.exception_specification = exception_specification;
1614 declarator->u.function.late_return_type = late_return_type;
1615 declarator->u.function.requires_clause = requires_clause;
1616 if (target)
1618 declarator->id_loc = target->id_loc;
1619 declarator->parameter_pack_p = target->parameter_pack_p;
1620 target->parameter_pack_p = false;
1622 else
1623 declarator->parameter_pack_p = false;
1625 return declarator;
1628 /* Make a declarator for an array of BOUNDS elements, each of which is
1629 defined by ELEMENT. */
1631 cp_declarator *
1632 make_array_declarator (cp_declarator *element, tree bounds)
1634 cp_declarator *declarator;
1636 declarator = make_declarator (cdk_array);
1637 declarator->declarator = element;
1638 declarator->u.array.bounds = bounds;
1639 if (element)
1641 declarator->id_loc = element->id_loc;
1642 declarator->parameter_pack_p = element->parameter_pack_p;
1643 element->parameter_pack_p = false;
1645 else
1646 declarator->parameter_pack_p = false;
1648 return declarator;
1651 /* Determine whether the declarator we've seen so far can be a
1652 parameter pack, when followed by an ellipsis. */
1653 static bool
1654 declarator_can_be_parameter_pack (cp_declarator *declarator)
1656 if (declarator && declarator->parameter_pack_p)
1657 /* We already saw an ellipsis. */
1658 return false;
1660 /* Search for a declarator name, or any other declarator that goes
1661 after the point where the ellipsis could appear in a parameter
1662 pack. If we find any of these, then this declarator can not be
1663 made into a parameter pack. */
1664 bool found = false;
1665 while (declarator && !found)
1667 switch ((int)declarator->kind)
1669 case cdk_id:
1670 case cdk_array:
1671 found = true;
1672 break;
1674 case cdk_error:
1675 return true;
1677 default:
1678 declarator = declarator->declarator;
1679 break;
1683 return !found;
1686 cp_parameter_declarator *no_parameters;
1688 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1689 DECLARATOR and DEFAULT_ARGUMENT. */
1691 cp_parameter_declarator *
1692 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1693 cp_declarator *declarator,
1694 tree default_argument,
1695 bool template_parameter_pack_p = false)
1697 cp_parameter_declarator *parameter;
1699 parameter = ((cp_parameter_declarator *)
1700 alloc_declarator (sizeof (cp_parameter_declarator)));
1701 parameter->next = NULL;
1702 if (decl_specifiers)
1703 parameter->decl_specifiers = *decl_specifiers;
1704 else
1705 clear_decl_specs (&parameter->decl_specifiers);
1706 parameter->declarator = declarator;
1707 parameter->default_argument = default_argument;
1708 parameter->template_parameter_pack_p = template_parameter_pack_p;
1710 return parameter;
1713 /* Returns true iff DECLARATOR is a declaration for a function. */
1715 static bool
1716 function_declarator_p (const cp_declarator *declarator)
1718 while (declarator)
1720 if (declarator->kind == cdk_function
1721 && declarator->declarator->kind == cdk_id)
1722 return true;
1723 if (declarator->kind == cdk_id
1724 || declarator->kind == cdk_error)
1725 return false;
1726 declarator = declarator->declarator;
1728 return false;
1731 /* The parser. */
1733 /* Overview
1734 --------
1736 A cp_parser parses the token stream as specified by the C++
1737 grammar. Its job is purely parsing, not semantic analysis. For
1738 example, the parser breaks the token stream into declarators,
1739 expressions, statements, and other similar syntactic constructs.
1740 It does not check that the types of the expressions on either side
1741 of an assignment-statement are compatible, or that a function is
1742 not declared with a parameter of type `void'.
1744 The parser invokes routines elsewhere in the compiler to perform
1745 semantic analysis and to build up the abstract syntax tree for the
1746 code processed.
1748 The parser (and the template instantiation code, which is, in a
1749 way, a close relative of parsing) are the only parts of the
1750 compiler that should be calling push_scope and pop_scope, or
1751 related functions. The parser (and template instantiation code)
1752 keeps track of what scope is presently active; everything else
1753 should simply honor that. (The code that generates static
1754 initializers may also need to set the scope, in order to check
1755 access control correctly when emitting the initializers.)
1757 Methodology
1758 -----------
1760 The parser is of the standard recursive-descent variety. Upcoming
1761 tokens in the token stream are examined in order to determine which
1762 production to use when parsing a non-terminal. Some C++ constructs
1763 require arbitrary look ahead to disambiguate. For example, it is
1764 impossible, in the general case, to tell whether a statement is an
1765 expression or declaration without scanning the entire statement.
1766 Therefore, the parser is capable of "parsing tentatively." When the
1767 parser is not sure what construct comes next, it enters this mode.
1768 Then, while we attempt to parse the construct, the parser queues up
1769 error messages, rather than issuing them immediately, and saves the
1770 tokens it consumes. If the construct is parsed successfully, the
1771 parser "commits", i.e., it issues any queued error messages and
1772 the tokens that were being preserved are permanently discarded.
1773 If, however, the construct is not parsed successfully, the parser
1774 rolls back its state completely so that it can resume parsing using
1775 a different alternative.
1777 Future Improvements
1778 -------------------
1780 The performance of the parser could probably be improved substantially.
1781 We could often eliminate the need to parse tentatively by looking ahead
1782 a little bit. In some places, this approach might not entirely eliminate
1783 the need to parse tentatively, but it might still speed up the average
1784 case. */
1786 /* Flags that are passed to some parsing functions. These values can
1787 be bitwise-ored together. */
1789 enum
1791 /* No flags. */
1792 CP_PARSER_FLAGS_NONE = 0x0,
1793 /* The construct is optional. If it is not present, then no error
1794 should be issued. */
1795 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1796 /* When parsing a type-specifier, treat user-defined type-names
1797 as non-type identifiers. */
1798 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1799 /* When parsing a type-specifier, do not try to parse a class-specifier
1800 or enum-specifier. */
1801 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1802 /* When parsing a decl-specifier-seq, only allow type-specifier or
1803 constexpr. */
1804 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1805 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1806 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1809 /* This type is used for parameters and variables which hold
1810 combinations of the above flags. */
1811 typedef int cp_parser_flags;
1813 /* The different kinds of declarators we want to parse. */
1815 enum cp_parser_declarator_kind
1817 /* We want an abstract declarator. */
1818 CP_PARSER_DECLARATOR_ABSTRACT,
1819 /* We want a named declarator. */
1820 CP_PARSER_DECLARATOR_NAMED,
1821 /* We don't mind, but the name must be an unqualified-id. */
1822 CP_PARSER_DECLARATOR_EITHER
1825 /* The precedence values used to parse binary expressions. The minimum value
1826 of PREC must be 1, because zero is reserved to quickly discriminate
1827 binary operators from other tokens. */
1829 enum cp_parser_prec
1831 PREC_NOT_OPERATOR,
1832 PREC_LOGICAL_OR_EXPRESSION,
1833 PREC_LOGICAL_AND_EXPRESSION,
1834 PREC_INCLUSIVE_OR_EXPRESSION,
1835 PREC_EXCLUSIVE_OR_EXPRESSION,
1836 PREC_AND_EXPRESSION,
1837 PREC_EQUALITY_EXPRESSION,
1838 PREC_RELATIONAL_EXPRESSION,
1839 PREC_SHIFT_EXPRESSION,
1840 PREC_ADDITIVE_EXPRESSION,
1841 PREC_MULTIPLICATIVE_EXPRESSION,
1842 PREC_PM_EXPRESSION,
1843 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1846 /* A mapping from a token type to a corresponding tree node type, with a
1847 precedence value. */
1849 struct cp_parser_binary_operations_map_node
1851 /* The token type. */
1852 enum cpp_ttype token_type;
1853 /* The corresponding tree code. */
1854 enum tree_code tree_type;
1855 /* The precedence of this operator. */
1856 enum cp_parser_prec prec;
1859 struct cp_parser_expression_stack_entry
1861 /* Left hand side of the binary operation we are currently
1862 parsing. */
1863 cp_expr lhs;
1864 /* Original tree code for left hand side, if it was a binary
1865 expression itself (used for -Wparentheses). */
1866 enum tree_code lhs_type;
1867 /* Tree code for the binary operation we are parsing. */
1868 enum tree_code tree_type;
1869 /* Precedence of the binary operation we are parsing. */
1870 enum cp_parser_prec prec;
1871 /* Location of the binary operation we are parsing. */
1872 location_t loc;
1875 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1876 entries because precedence levels on the stack are monotonically
1877 increasing. */
1878 typedef struct cp_parser_expression_stack_entry
1879 cp_parser_expression_stack[NUM_PREC_VALUES];
1881 /* Prototypes. */
1883 /* Constructors and destructors. */
1885 static cp_parser_context *cp_parser_context_new
1886 (cp_parser_context *);
1888 /* Class variables. */
1890 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1892 /* The operator-precedence table used by cp_parser_binary_expression.
1893 Transformed into an associative array (binops_by_token) by
1894 cp_parser_new. */
1896 static const cp_parser_binary_operations_map_node binops[] = {
1897 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1898 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1900 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1901 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1902 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1904 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1905 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1907 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1908 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1910 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1911 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1912 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1913 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1915 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1916 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1918 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1920 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1922 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1924 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1926 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1929 /* The same as binops, but initialized by cp_parser_new so that
1930 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1931 for speed. */
1932 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1934 /* Constructors and destructors. */
1936 /* Construct a new context. The context below this one on the stack
1937 is given by NEXT. */
1939 static cp_parser_context *
1940 cp_parser_context_new (cp_parser_context* next)
1942 cp_parser_context *context;
1944 /* Allocate the storage. */
1945 if (cp_parser_context_free_list != NULL)
1947 /* Pull the first entry from the free list. */
1948 context = cp_parser_context_free_list;
1949 cp_parser_context_free_list = context->next;
1950 memset (context, 0, sizeof (*context));
1952 else
1953 context = ggc_cleared_alloc<cp_parser_context> ();
1955 /* No errors have occurred yet in this context. */
1956 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1957 /* If this is not the bottommost context, copy information that we
1958 need from the previous context. */
1959 if (next)
1961 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1962 expression, then we are parsing one in this context, too. */
1963 context->object_type = next->object_type;
1964 /* Thread the stack. */
1965 context->next = next;
1968 return context;
1971 /* Managing the unparsed function queues. */
1973 #define unparsed_funs_with_default_args \
1974 parser->unparsed_queues->last ().funs_with_default_args
1975 #define unparsed_funs_with_definitions \
1976 parser->unparsed_queues->last ().funs_with_definitions
1977 #define unparsed_nsdmis \
1978 parser->unparsed_queues->last ().nsdmis
1979 #define unparsed_classes \
1980 parser->unparsed_queues->last ().classes
1982 static void
1983 push_unparsed_function_queues (cp_parser *parser)
1985 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1986 vec_safe_push (parser->unparsed_queues, e);
1989 static void
1990 pop_unparsed_function_queues (cp_parser *parser)
1992 release_tree_vector (unparsed_funs_with_definitions);
1993 parser->unparsed_queues->pop ();
1996 /* Prototypes. */
1998 /* Constructors and destructors. */
2000 static cp_parser *cp_parser_new
2001 (void);
2003 /* Routines to parse various constructs.
2005 Those that return `tree' will return the error_mark_node (rather
2006 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2007 Sometimes, they will return an ordinary node if error-recovery was
2008 attempted, even though a parse error occurred. So, to check
2009 whether or not a parse error occurred, you should always use
2010 cp_parser_error_occurred. If the construct is optional (indicated
2011 either by an `_opt' in the name of the function that does the
2012 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2013 the construct is not present. */
2015 /* Lexical conventions [gram.lex] */
2017 static cp_expr cp_parser_identifier
2018 (cp_parser *);
2019 static cp_expr cp_parser_string_literal
2020 (cp_parser *, bool, bool, bool);
2021 static cp_expr cp_parser_userdef_char_literal
2022 (cp_parser *);
2023 static tree cp_parser_userdef_string_literal
2024 (tree);
2025 static cp_expr cp_parser_userdef_numeric_literal
2026 (cp_parser *);
2028 /* Basic concepts [gram.basic] */
2030 static bool cp_parser_translation_unit
2031 (cp_parser *);
2033 /* Expressions [gram.expr] */
2035 static cp_expr cp_parser_primary_expression
2036 (cp_parser *, bool, bool, bool, cp_id_kind *);
2037 static cp_expr cp_parser_id_expression
2038 (cp_parser *, bool, bool, bool *, bool, bool);
2039 static cp_expr cp_parser_unqualified_id
2040 (cp_parser *, bool, bool, bool, bool);
2041 static tree cp_parser_nested_name_specifier_opt
2042 (cp_parser *, bool, bool, bool, bool);
2043 static tree cp_parser_nested_name_specifier
2044 (cp_parser *, bool, bool, bool, bool);
2045 static tree cp_parser_qualifying_entity
2046 (cp_parser *, bool, bool, bool, bool, bool);
2047 static cp_expr cp_parser_postfix_expression
2048 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2049 static tree cp_parser_postfix_open_square_expression
2050 (cp_parser *, tree, bool, bool);
2051 static tree cp_parser_postfix_dot_deref_expression
2052 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2053 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2054 (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2055 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2056 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2057 static void cp_parser_pseudo_destructor_name
2058 (cp_parser *, tree, tree *, tree *);
2059 static cp_expr cp_parser_unary_expression
2060 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2061 static enum tree_code cp_parser_unary_operator
2062 (cp_token *);
2063 static tree cp_parser_new_expression
2064 (cp_parser *);
2065 static vec<tree, va_gc> *cp_parser_new_placement
2066 (cp_parser *);
2067 static tree cp_parser_new_type_id
2068 (cp_parser *, tree *);
2069 static cp_declarator *cp_parser_new_declarator_opt
2070 (cp_parser *);
2071 static cp_declarator *cp_parser_direct_new_declarator
2072 (cp_parser *);
2073 static vec<tree, va_gc> *cp_parser_new_initializer
2074 (cp_parser *);
2075 static tree cp_parser_delete_expression
2076 (cp_parser *);
2077 static cp_expr cp_parser_cast_expression
2078 (cp_parser *, bool, bool, bool, cp_id_kind *);
2079 static cp_expr cp_parser_binary_expression
2080 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2081 static tree cp_parser_question_colon_clause
2082 (cp_parser *, cp_expr);
2083 static cp_expr cp_parser_assignment_expression
2084 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2085 static enum tree_code cp_parser_assignment_operator_opt
2086 (cp_parser *);
2087 static cp_expr cp_parser_expression
2088 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2089 static cp_expr cp_parser_constant_expression
2090 (cp_parser *, bool = false, bool * = NULL);
2091 static cp_expr cp_parser_builtin_offsetof
2092 (cp_parser *);
2093 static cp_expr cp_parser_lambda_expression
2094 (cp_parser *);
2095 static void cp_parser_lambda_introducer
2096 (cp_parser *, tree);
2097 static bool cp_parser_lambda_declarator_opt
2098 (cp_parser *, tree);
2099 static void cp_parser_lambda_body
2100 (cp_parser *, tree);
2102 /* Statements [gram.stmt.stmt] */
2104 static void cp_parser_statement
2105 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL);
2106 static void cp_parser_label_for_labeled_statement
2107 (cp_parser *, tree);
2108 static tree cp_parser_expression_statement
2109 (cp_parser *, tree);
2110 static tree cp_parser_compound_statement
2111 (cp_parser *, tree, int, bool);
2112 static void cp_parser_statement_seq_opt
2113 (cp_parser *, tree);
2114 static tree cp_parser_selection_statement
2115 (cp_parser *, bool *, vec<tree> *);
2116 static tree cp_parser_condition
2117 (cp_parser *);
2118 static tree cp_parser_iteration_statement
2119 (cp_parser *, bool *, bool);
2120 static bool cp_parser_for_init_statement
2121 (cp_parser *, tree *decl);
2122 static tree cp_parser_for
2123 (cp_parser *, bool);
2124 static tree cp_parser_c_for
2125 (cp_parser *, tree, tree, bool);
2126 static tree cp_parser_range_for
2127 (cp_parser *, tree, tree, tree, bool);
2128 static void do_range_for_auto_deduction
2129 (tree, tree);
2130 static tree cp_parser_perform_range_for_lookup
2131 (tree, tree *, tree *);
2132 static tree cp_parser_range_for_member_function
2133 (tree, tree);
2134 static tree cp_parser_jump_statement
2135 (cp_parser *);
2136 static void cp_parser_declaration_statement
2137 (cp_parser *);
2139 static tree cp_parser_implicitly_scoped_statement
2140 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2141 static void cp_parser_already_scoped_statement
2142 (cp_parser *, bool *, const token_indent_info &);
2144 /* Declarations [gram.dcl.dcl] */
2146 static void cp_parser_declaration_seq_opt
2147 (cp_parser *);
2148 static void cp_parser_declaration
2149 (cp_parser *);
2150 static void cp_parser_block_declaration
2151 (cp_parser *, bool);
2152 static void cp_parser_simple_declaration
2153 (cp_parser *, bool, tree *);
2154 static void cp_parser_decl_specifier_seq
2155 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2156 static tree cp_parser_storage_class_specifier_opt
2157 (cp_parser *);
2158 static tree cp_parser_function_specifier_opt
2159 (cp_parser *, cp_decl_specifier_seq *);
2160 static tree cp_parser_type_specifier
2161 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2162 int *, bool *);
2163 static tree cp_parser_simple_type_specifier
2164 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2165 static tree cp_parser_type_name
2166 (cp_parser *, bool);
2167 static tree cp_parser_type_name
2168 (cp_parser *);
2169 static tree cp_parser_nonclass_name
2170 (cp_parser* parser);
2171 static tree cp_parser_elaborated_type_specifier
2172 (cp_parser *, bool, bool);
2173 static tree cp_parser_enum_specifier
2174 (cp_parser *);
2175 static void cp_parser_enumerator_list
2176 (cp_parser *, tree);
2177 static void cp_parser_enumerator_definition
2178 (cp_parser *, tree);
2179 static tree cp_parser_namespace_name
2180 (cp_parser *);
2181 static void cp_parser_namespace_definition
2182 (cp_parser *);
2183 static void cp_parser_namespace_body
2184 (cp_parser *);
2185 static tree cp_parser_qualified_namespace_specifier
2186 (cp_parser *);
2187 static void cp_parser_namespace_alias_definition
2188 (cp_parser *);
2189 static bool cp_parser_using_declaration
2190 (cp_parser *, bool);
2191 static void cp_parser_using_directive
2192 (cp_parser *);
2193 static tree cp_parser_alias_declaration
2194 (cp_parser *);
2195 static void cp_parser_asm_definition
2196 (cp_parser *);
2197 static void cp_parser_linkage_specification
2198 (cp_parser *);
2199 static void cp_parser_static_assert
2200 (cp_parser *, bool);
2201 static tree cp_parser_decltype
2202 (cp_parser *);
2204 /* Declarators [gram.dcl.decl] */
2206 static tree cp_parser_init_declarator
2207 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2208 bool, bool, int, bool *, tree *, location_t *, tree *);
2209 static cp_declarator *cp_parser_declarator
2210 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2211 static cp_declarator *cp_parser_direct_declarator
2212 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2213 static enum tree_code cp_parser_ptr_operator
2214 (cp_parser *, tree *, cp_cv_quals *, tree *);
2215 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2216 (cp_parser *);
2217 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2218 (cp_parser *);
2219 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2220 (cp_parser *);
2221 static tree cp_parser_tx_qualifier_opt
2222 (cp_parser *);
2223 static tree cp_parser_late_return_type_opt
2224 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2225 static tree cp_parser_declarator_id
2226 (cp_parser *, bool);
2227 static tree cp_parser_type_id
2228 (cp_parser *);
2229 static tree cp_parser_template_type_arg
2230 (cp_parser *);
2231 static tree cp_parser_trailing_type_id (cp_parser *);
2232 static tree cp_parser_type_id_1
2233 (cp_parser *, bool, bool);
2234 static void cp_parser_type_specifier_seq
2235 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2236 static tree cp_parser_parameter_declaration_clause
2237 (cp_parser *);
2238 static tree cp_parser_parameter_declaration_list
2239 (cp_parser *, bool *);
2240 static cp_parameter_declarator *cp_parser_parameter_declaration
2241 (cp_parser *, bool, bool *);
2242 static tree cp_parser_default_argument
2243 (cp_parser *, bool);
2244 static void cp_parser_function_body
2245 (cp_parser *, bool);
2246 static tree cp_parser_initializer
2247 (cp_parser *, bool *, bool *);
2248 static cp_expr cp_parser_initializer_clause
2249 (cp_parser *, bool *);
2250 static cp_expr cp_parser_braced_list
2251 (cp_parser*, bool*);
2252 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2253 (cp_parser *, bool *);
2255 static bool cp_parser_ctor_initializer_opt_and_function_body
2256 (cp_parser *, bool);
2258 static tree cp_parser_late_parsing_omp_declare_simd
2259 (cp_parser *, tree);
2261 static tree cp_parser_late_parsing_cilk_simd_fn_info
2262 (cp_parser *, tree);
2264 static tree cp_parser_late_parsing_oacc_routine
2265 (cp_parser *, tree);
2267 static tree synthesize_implicit_template_parm
2268 (cp_parser *, tree);
2269 static tree finish_fully_implicit_template
2270 (cp_parser *, tree);
2272 /* Classes [gram.class] */
2274 static tree cp_parser_class_name
2275 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2276 static tree cp_parser_class_specifier
2277 (cp_parser *);
2278 static tree cp_parser_class_head
2279 (cp_parser *, bool *);
2280 static enum tag_types cp_parser_class_key
2281 (cp_parser *);
2282 static void cp_parser_type_parameter_key
2283 (cp_parser* parser);
2284 static void cp_parser_member_specification_opt
2285 (cp_parser *);
2286 static void cp_parser_member_declaration
2287 (cp_parser *);
2288 static tree cp_parser_pure_specifier
2289 (cp_parser *);
2290 static tree cp_parser_constant_initializer
2291 (cp_parser *);
2293 /* Derived classes [gram.class.derived] */
2295 static tree cp_parser_base_clause
2296 (cp_parser *);
2297 static tree cp_parser_base_specifier
2298 (cp_parser *);
2300 /* Special member functions [gram.special] */
2302 static tree cp_parser_conversion_function_id
2303 (cp_parser *);
2304 static tree cp_parser_conversion_type_id
2305 (cp_parser *);
2306 static cp_declarator *cp_parser_conversion_declarator_opt
2307 (cp_parser *);
2308 static bool cp_parser_ctor_initializer_opt
2309 (cp_parser *);
2310 static void cp_parser_mem_initializer_list
2311 (cp_parser *);
2312 static tree cp_parser_mem_initializer
2313 (cp_parser *);
2314 static tree cp_parser_mem_initializer_id
2315 (cp_parser *);
2317 /* Overloading [gram.over] */
2319 static cp_expr cp_parser_operator_function_id
2320 (cp_parser *);
2321 static cp_expr cp_parser_operator
2322 (cp_parser *);
2324 /* Templates [gram.temp] */
2326 static void cp_parser_template_declaration
2327 (cp_parser *, bool);
2328 static tree cp_parser_template_parameter_list
2329 (cp_parser *);
2330 static tree cp_parser_template_parameter
2331 (cp_parser *, bool *, bool *);
2332 static tree cp_parser_type_parameter
2333 (cp_parser *, bool *);
2334 static tree cp_parser_template_id
2335 (cp_parser *, bool, bool, enum tag_types, bool);
2336 static tree cp_parser_template_name
2337 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2338 static tree cp_parser_template_argument_list
2339 (cp_parser *);
2340 static tree cp_parser_template_argument
2341 (cp_parser *);
2342 static void cp_parser_explicit_instantiation
2343 (cp_parser *);
2344 static void cp_parser_explicit_specialization
2345 (cp_parser *);
2347 /* Exception handling [gram.exception] */
2349 static tree cp_parser_try_block
2350 (cp_parser *);
2351 static bool cp_parser_function_try_block
2352 (cp_parser *);
2353 static void cp_parser_handler_seq
2354 (cp_parser *);
2355 static void cp_parser_handler
2356 (cp_parser *);
2357 static tree cp_parser_exception_declaration
2358 (cp_parser *);
2359 static tree cp_parser_throw_expression
2360 (cp_parser *);
2361 static tree cp_parser_exception_specification_opt
2362 (cp_parser *);
2363 static tree cp_parser_type_id_list
2364 (cp_parser *);
2366 /* GNU Extensions */
2368 static tree cp_parser_asm_specification_opt
2369 (cp_parser *);
2370 static tree cp_parser_asm_operand_list
2371 (cp_parser *);
2372 static tree cp_parser_asm_clobber_list
2373 (cp_parser *);
2374 static tree cp_parser_asm_label_list
2375 (cp_parser *);
2376 static bool cp_next_tokens_can_be_attribute_p
2377 (cp_parser *);
2378 static bool cp_next_tokens_can_be_gnu_attribute_p
2379 (cp_parser *);
2380 static bool cp_next_tokens_can_be_std_attribute_p
2381 (cp_parser *);
2382 static bool cp_nth_tokens_can_be_std_attribute_p
2383 (cp_parser *, size_t);
2384 static bool cp_nth_tokens_can_be_gnu_attribute_p
2385 (cp_parser *, size_t);
2386 static bool cp_nth_tokens_can_be_attribute_p
2387 (cp_parser *, size_t);
2388 static tree cp_parser_attributes_opt
2389 (cp_parser *);
2390 static tree cp_parser_gnu_attributes_opt
2391 (cp_parser *);
2392 static tree cp_parser_gnu_attribute_list
2393 (cp_parser *);
2394 static tree cp_parser_std_attribute
2395 (cp_parser *);
2396 static tree cp_parser_std_attribute_spec
2397 (cp_parser *);
2398 static tree cp_parser_std_attribute_spec_seq
2399 (cp_parser *);
2400 static bool cp_parser_extension_opt
2401 (cp_parser *, int *);
2402 static void cp_parser_label_declaration
2403 (cp_parser *);
2405 /* Concept Extensions */
2407 static tree cp_parser_requires_clause
2408 (cp_parser *);
2409 static tree cp_parser_requires_clause_opt
2410 (cp_parser *);
2411 static tree cp_parser_requires_expression
2412 (cp_parser *);
2413 static tree cp_parser_requirement_parameter_list
2414 (cp_parser *);
2415 static tree cp_parser_requirement_body
2416 (cp_parser *);
2417 static tree cp_parser_requirement_list
2418 (cp_parser *);
2419 static tree cp_parser_requirement
2420 (cp_parser *);
2421 static tree cp_parser_simple_requirement
2422 (cp_parser *);
2423 static tree cp_parser_compound_requirement
2424 (cp_parser *);
2425 static tree cp_parser_type_requirement
2426 (cp_parser *);
2427 static tree cp_parser_nested_requirement
2428 (cp_parser *);
2430 /* Transactional Memory Extensions */
2432 static tree cp_parser_transaction
2433 (cp_parser *, cp_token *);
2434 static tree cp_parser_transaction_expression
2435 (cp_parser *, enum rid);
2436 static bool cp_parser_function_transaction
2437 (cp_parser *, enum rid);
2438 static tree cp_parser_transaction_cancel
2439 (cp_parser *);
2441 enum pragma_context {
2442 pragma_external,
2443 pragma_member,
2444 pragma_objc_icode,
2445 pragma_stmt,
2446 pragma_compound
2448 static bool cp_parser_pragma
2449 (cp_parser *, enum pragma_context, bool *);
2451 /* Objective-C++ Productions */
2453 static tree cp_parser_objc_message_receiver
2454 (cp_parser *);
2455 static tree cp_parser_objc_message_args
2456 (cp_parser *);
2457 static tree cp_parser_objc_message_expression
2458 (cp_parser *);
2459 static cp_expr cp_parser_objc_encode_expression
2460 (cp_parser *);
2461 static tree cp_parser_objc_defs_expression
2462 (cp_parser *);
2463 static tree cp_parser_objc_protocol_expression
2464 (cp_parser *);
2465 static tree cp_parser_objc_selector_expression
2466 (cp_parser *);
2467 static cp_expr cp_parser_objc_expression
2468 (cp_parser *);
2469 static bool cp_parser_objc_selector_p
2470 (enum cpp_ttype);
2471 static tree cp_parser_objc_selector
2472 (cp_parser *);
2473 static tree cp_parser_objc_protocol_refs_opt
2474 (cp_parser *);
2475 static void cp_parser_objc_declaration
2476 (cp_parser *, tree);
2477 static tree cp_parser_objc_statement
2478 (cp_parser *);
2479 static bool cp_parser_objc_valid_prefix_attributes
2480 (cp_parser *, tree *);
2481 static void cp_parser_objc_at_property_declaration
2482 (cp_parser *) ;
2483 static void cp_parser_objc_at_synthesize_declaration
2484 (cp_parser *) ;
2485 static void cp_parser_objc_at_dynamic_declaration
2486 (cp_parser *) ;
2487 static tree cp_parser_objc_struct_declaration
2488 (cp_parser *) ;
2490 /* Utility Routines */
2492 static cp_expr cp_parser_lookup_name
2493 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2494 static tree cp_parser_lookup_name_simple
2495 (cp_parser *, tree, location_t);
2496 static tree cp_parser_maybe_treat_template_as_class
2497 (tree, bool);
2498 static bool cp_parser_check_declarator_template_parameters
2499 (cp_parser *, cp_declarator *, location_t);
2500 static bool cp_parser_check_template_parameters
2501 (cp_parser *, unsigned, location_t, cp_declarator *);
2502 static cp_expr cp_parser_simple_cast_expression
2503 (cp_parser *);
2504 static tree cp_parser_global_scope_opt
2505 (cp_parser *, bool);
2506 static bool cp_parser_constructor_declarator_p
2507 (cp_parser *, bool);
2508 static tree cp_parser_function_definition_from_specifiers_and_declarator
2509 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2510 static tree cp_parser_function_definition_after_declarator
2511 (cp_parser *, bool);
2512 static bool cp_parser_template_declaration_after_export
2513 (cp_parser *, bool);
2514 static void cp_parser_perform_template_parameter_access_checks
2515 (vec<deferred_access_check, va_gc> *);
2516 static tree cp_parser_single_declaration
2517 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2518 static cp_expr cp_parser_functional_cast
2519 (cp_parser *, tree);
2520 static tree cp_parser_save_member_function_body
2521 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2522 static tree cp_parser_save_nsdmi
2523 (cp_parser *);
2524 static tree cp_parser_enclosed_template_argument_list
2525 (cp_parser *);
2526 static void cp_parser_save_default_args
2527 (cp_parser *, tree);
2528 static void cp_parser_late_parsing_for_member
2529 (cp_parser *, tree);
2530 static tree cp_parser_late_parse_one_default_arg
2531 (cp_parser *, tree, tree, tree);
2532 static void cp_parser_late_parsing_nsdmi
2533 (cp_parser *, tree);
2534 static void cp_parser_late_parsing_default_args
2535 (cp_parser *, tree);
2536 static tree cp_parser_sizeof_operand
2537 (cp_parser *, enum rid);
2538 static tree cp_parser_trait_expr
2539 (cp_parser *, enum rid);
2540 static bool cp_parser_declares_only_class_p
2541 (cp_parser *);
2542 static void cp_parser_set_storage_class
2543 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2544 static void cp_parser_set_decl_spec_type
2545 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2546 static void set_and_check_decl_spec_loc
2547 (cp_decl_specifier_seq *decl_specs,
2548 cp_decl_spec ds, cp_token *);
2549 static bool cp_parser_friend_p
2550 (const cp_decl_specifier_seq *);
2551 static void cp_parser_required_error
2552 (cp_parser *, required_token, bool);
2553 static cp_token *cp_parser_require
2554 (cp_parser *, enum cpp_ttype, required_token);
2555 static cp_token *cp_parser_require_keyword
2556 (cp_parser *, enum rid, required_token);
2557 static bool cp_parser_token_starts_function_definition_p
2558 (cp_token *);
2559 static bool cp_parser_next_token_starts_class_definition_p
2560 (cp_parser *);
2561 static bool cp_parser_next_token_ends_template_argument_p
2562 (cp_parser *);
2563 static bool cp_parser_nth_token_starts_template_argument_list_p
2564 (cp_parser *, size_t);
2565 static enum tag_types cp_parser_token_is_class_key
2566 (cp_token *);
2567 static enum tag_types cp_parser_token_is_type_parameter_key
2568 (cp_token *);
2569 static void cp_parser_check_class_key
2570 (enum tag_types, tree type);
2571 static void cp_parser_check_access_in_redeclaration
2572 (tree type, location_t location);
2573 static bool cp_parser_optional_template_keyword
2574 (cp_parser *);
2575 static void cp_parser_pre_parsed_nested_name_specifier
2576 (cp_parser *);
2577 static bool cp_parser_cache_group
2578 (cp_parser *, enum cpp_ttype, unsigned);
2579 static tree cp_parser_cache_defarg
2580 (cp_parser *parser, bool nsdmi);
2581 static void cp_parser_parse_tentatively
2582 (cp_parser *);
2583 static void cp_parser_commit_to_tentative_parse
2584 (cp_parser *);
2585 static void cp_parser_commit_to_topmost_tentative_parse
2586 (cp_parser *);
2587 static void cp_parser_abort_tentative_parse
2588 (cp_parser *);
2589 static bool cp_parser_parse_definitely
2590 (cp_parser *);
2591 static inline bool cp_parser_parsing_tentatively
2592 (cp_parser *);
2593 static bool cp_parser_uncommitted_to_tentative_parse_p
2594 (cp_parser *);
2595 static void cp_parser_error
2596 (cp_parser *, const char *);
2597 static void cp_parser_name_lookup_error
2598 (cp_parser *, tree, tree, name_lookup_error, location_t);
2599 static bool cp_parser_simulate_error
2600 (cp_parser *);
2601 static bool cp_parser_check_type_definition
2602 (cp_parser *);
2603 static void cp_parser_check_for_definition_in_return_type
2604 (cp_declarator *, tree, location_t type_location);
2605 static void cp_parser_check_for_invalid_template_id
2606 (cp_parser *, tree, enum tag_types, location_t location);
2607 static bool cp_parser_non_integral_constant_expression
2608 (cp_parser *, non_integral_constant);
2609 static void cp_parser_diagnose_invalid_type_name
2610 (cp_parser *, tree, location_t);
2611 static bool cp_parser_parse_and_diagnose_invalid_type_name
2612 (cp_parser *);
2613 static int cp_parser_skip_to_closing_parenthesis
2614 (cp_parser *, bool, bool, bool);
2615 static void cp_parser_skip_to_end_of_statement
2616 (cp_parser *);
2617 static void cp_parser_consume_semicolon_at_end_of_statement
2618 (cp_parser *);
2619 static void cp_parser_skip_to_end_of_block_or_statement
2620 (cp_parser *);
2621 static bool cp_parser_skip_to_closing_brace
2622 (cp_parser *);
2623 static void cp_parser_skip_to_end_of_template_parameter_list
2624 (cp_parser *);
2625 static void cp_parser_skip_to_pragma_eol
2626 (cp_parser*, cp_token *);
2627 static bool cp_parser_error_occurred
2628 (cp_parser *);
2629 static bool cp_parser_allow_gnu_extensions_p
2630 (cp_parser *);
2631 static bool cp_parser_is_pure_string_literal
2632 (cp_token *);
2633 static bool cp_parser_is_string_literal
2634 (cp_token *);
2635 static bool cp_parser_is_keyword
2636 (cp_token *, enum rid);
2637 static tree cp_parser_make_typename_type
2638 (cp_parser *, tree, location_t location);
2639 static cp_declarator * cp_parser_make_indirect_declarator
2640 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2641 static bool cp_parser_compound_literal_p
2642 (cp_parser *);
2643 static bool cp_parser_array_designator_p
2644 (cp_parser *);
2645 static bool cp_parser_skip_to_closing_square_bracket
2646 (cp_parser *);
2648 /* Concept-related syntactic transformations */
2650 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2651 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2653 // -------------------------------------------------------------------------- //
2654 // Unevaluated Operand Guard
2656 // Implementation of an RAII helper for unevaluated operand parsing.
2657 cp_unevaluated::cp_unevaluated ()
2659 ++cp_unevaluated_operand;
2660 ++c_inhibit_evaluation_warnings;
2663 cp_unevaluated::~cp_unevaluated ()
2665 --c_inhibit_evaluation_warnings;
2666 --cp_unevaluated_operand;
2669 // -------------------------------------------------------------------------- //
2670 // Tentative Parsing
2672 /* Returns nonzero if we are parsing tentatively. */
2674 static inline bool
2675 cp_parser_parsing_tentatively (cp_parser* parser)
2677 return parser->context->next != NULL;
2680 /* Returns nonzero if TOKEN is a string literal. */
2682 static bool
2683 cp_parser_is_pure_string_literal (cp_token* token)
2685 return (token->type == CPP_STRING ||
2686 token->type == CPP_STRING16 ||
2687 token->type == CPP_STRING32 ||
2688 token->type == CPP_WSTRING ||
2689 token->type == CPP_UTF8STRING);
2692 /* Returns nonzero if TOKEN is a string literal
2693 of a user-defined string literal. */
2695 static bool
2696 cp_parser_is_string_literal (cp_token* token)
2698 return (cp_parser_is_pure_string_literal (token) ||
2699 token->type == CPP_STRING_USERDEF ||
2700 token->type == CPP_STRING16_USERDEF ||
2701 token->type == CPP_STRING32_USERDEF ||
2702 token->type == CPP_WSTRING_USERDEF ||
2703 token->type == CPP_UTF8STRING_USERDEF);
2706 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2708 static bool
2709 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2711 return token->keyword == keyword;
2714 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2715 PRAGMA_NONE. */
2717 static enum pragma_kind
2718 cp_parser_pragma_kind (cp_token *token)
2720 if (token->type != CPP_PRAGMA)
2721 return PRAGMA_NONE;
2722 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2723 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2726 /* Helper function for cp_parser_error.
2727 Having peeked a token of kind TOK1_KIND that might signify
2728 a conflict marker, peek successor tokens to determine
2729 if we actually do have a conflict marker.
2730 Specifically, we consider a run of 7 '<', '=' or '>' characters
2731 at the start of a line as a conflict marker.
2732 These come through the lexer as three pairs and a single,
2733 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2734 If it returns true, *OUT_LOC is written to with the location/range
2735 of the marker. */
2737 static bool
2738 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2739 location_t *out_loc)
2741 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2742 if (token2->type != tok1_kind)
2743 return false;
2744 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2745 if (token3->type != tok1_kind)
2746 return false;
2747 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2748 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2749 return false;
2751 /* It must be at the start of the line. */
2752 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2753 if (LOCATION_COLUMN (start_loc) != 1)
2754 return false;
2756 /* We have a conflict marker. Construct a location of the form:
2757 <<<<<<<
2758 ^~~~~~~
2759 with start == caret, finishing at the end of the marker. */
2760 location_t finish_loc = get_finish (token4->location);
2761 *out_loc = make_location (start_loc, start_loc, finish_loc);
2763 return true;
2766 /* If not parsing tentatively, issue a diagnostic of the form
2767 FILE:LINE: MESSAGE before TOKEN
2768 where TOKEN is the next token in the input stream. MESSAGE
2769 (specified by the caller) is usually of the form "expected
2770 OTHER-TOKEN". */
2772 static void
2773 cp_parser_error (cp_parser* parser, const char* gmsgid)
2775 if (!cp_parser_simulate_error (parser))
2777 cp_token *token = cp_lexer_peek_token (parser->lexer);
2778 /* This diagnostic makes more sense if it is tagged to the line
2779 of the token we just peeked at. */
2780 cp_lexer_set_source_position_from_token (token);
2782 if (token->type == CPP_PRAGMA)
2784 error_at (token->location,
2785 "%<#pragma%> is not allowed here");
2786 cp_parser_skip_to_pragma_eol (parser, token);
2787 return;
2790 /* If this is actually a conflict marker, report it as such. */
2791 if (token->type == CPP_LSHIFT
2792 || token->type == CPP_RSHIFT
2793 || token->type == CPP_EQ_EQ)
2795 location_t loc;
2796 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2798 error_at (loc, "version control conflict marker in file");
2799 return;
2803 c_parse_error (gmsgid,
2804 /* Because c_parser_error does not understand
2805 CPP_KEYWORD, keywords are treated like
2806 identifiers. */
2807 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2808 token->u.value, token->flags);
2812 /* Issue an error about name-lookup failing. NAME is the
2813 IDENTIFIER_NODE DECL is the result of
2814 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2815 the thing that we hoped to find. */
2817 static void
2818 cp_parser_name_lookup_error (cp_parser* parser,
2819 tree name,
2820 tree decl,
2821 name_lookup_error desired,
2822 location_t location)
2824 /* If name lookup completely failed, tell the user that NAME was not
2825 declared. */
2826 if (decl == error_mark_node)
2828 if (parser->scope && parser->scope != global_namespace)
2829 error_at (location, "%<%E::%E%> has not been declared",
2830 parser->scope, name);
2831 else if (parser->scope == global_namespace)
2832 error_at (location, "%<::%E%> has not been declared", name);
2833 else if (parser->object_scope
2834 && !CLASS_TYPE_P (parser->object_scope))
2835 error_at (location, "request for member %qE in non-class type %qT",
2836 name, parser->object_scope);
2837 else if (parser->object_scope)
2838 error_at (location, "%<%T::%E%> has not been declared",
2839 parser->object_scope, name);
2840 else
2841 error_at (location, "%qE has not been declared", name);
2843 else if (parser->scope && parser->scope != global_namespace)
2845 switch (desired)
2847 case NLE_TYPE:
2848 error_at (location, "%<%E::%E%> is not a type",
2849 parser->scope, name);
2850 break;
2851 case NLE_CXX98:
2852 error_at (location, "%<%E::%E%> is not a class or namespace",
2853 parser->scope, name);
2854 break;
2855 case NLE_NOT_CXX98:
2856 error_at (location,
2857 "%<%E::%E%> is not a class, namespace, or enumeration",
2858 parser->scope, name);
2859 break;
2860 default:
2861 gcc_unreachable ();
2865 else if (parser->scope == global_namespace)
2867 switch (desired)
2869 case NLE_TYPE:
2870 error_at (location, "%<::%E%> is not a type", name);
2871 break;
2872 case NLE_CXX98:
2873 error_at (location, "%<::%E%> is not a class or namespace", name);
2874 break;
2875 case NLE_NOT_CXX98:
2876 error_at (location,
2877 "%<::%E%> is not a class, namespace, or enumeration",
2878 name);
2879 break;
2880 default:
2881 gcc_unreachable ();
2884 else
2886 switch (desired)
2888 case NLE_TYPE:
2889 error_at (location, "%qE is not a type", name);
2890 break;
2891 case NLE_CXX98:
2892 error_at (location, "%qE is not a class or namespace", name);
2893 break;
2894 case NLE_NOT_CXX98:
2895 error_at (location,
2896 "%qE is not a class, namespace, or enumeration", name);
2897 break;
2898 default:
2899 gcc_unreachable ();
2904 /* If we are parsing tentatively, remember that an error has occurred
2905 during this tentative parse. Returns true if the error was
2906 simulated; false if a message should be issued by the caller. */
2908 static bool
2909 cp_parser_simulate_error (cp_parser* parser)
2911 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2913 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2914 return true;
2916 return false;
2919 /* This function is called when a type is defined. If type
2920 definitions are forbidden at this point, an error message is
2921 issued. */
2923 static bool
2924 cp_parser_check_type_definition (cp_parser* parser)
2926 /* If types are forbidden here, issue a message. */
2927 if (parser->type_definition_forbidden_message)
2929 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2930 in the message need to be interpreted. */
2931 error (parser->type_definition_forbidden_message);
2932 return false;
2934 return true;
2937 /* This function is called when the DECLARATOR is processed. The TYPE
2938 was a type defined in the decl-specifiers. If it is invalid to
2939 define a type in the decl-specifiers for DECLARATOR, an error is
2940 issued. TYPE_LOCATION is the location of TYPE and is used
2941 for error reporting. */
2943 static void
2944 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2945 tree type, location_t type_location)
2947 /* [dcl.fct] forbids type definitions in return types.
2948 Unfortunately, it's not easy to know whether or not we are
2949 processing a return type until after the fact. */
2950 while (declarator
2951 && (declarator->kind == cdk_pointer
2952 || declarator->kind == cdk_reference
2953 || declarator->kind == cdk_ptrmem))
2954 declarator = declarator->declarator;
2955 if (declarator
2956 && declarator->kind == cdk_function)
2958 error_at (type_location,
2959 "new types may not be defined in a return type");
2960 inform (type_location,
2961 "(perhaps a semicolon is missing after the definition of %qT)",
2962 type);
2966 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2967 "<" in any valid C++ program. If the next token is indeed "<",
2968 issue a message warning the user about what appears to be an
2969 invalid attempt to form a template-id. LOCATION is the location
2970 of the type-specifier (TYPE) */
2972 static void
2973 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2974 tree type,
2975 enum tag_types tag_type,
2976 location_t location)
2978 cp_token_position start = 0;
2980 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2982 if (TYPE_P (type))
2983 error_at (location, "%qT is not a template", type);
2984 else if (identifier_p (type))
2986 if (tag_type != none_type)
2987 error_at (location, "%qE is not a class template", type);
2988 else
2989 error_at (location, "%qE is not a template", type);
2991 else
2992 error_at (location, "invalid template-id");
2993 /* Remember the location of the invalid "<". */
2994 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2995 start = cp_lexer_token_position (parser->lexer, true);
2996 /* Consume the "<". */
2997 cp_lexer_consume_token (parser->lexer);
2998 /* Parse the template arguments. */
2999 cp_parser_enclosed_template_argument_list (parser);
3000 /* Permanently remove the invalid template arguments so that
3001 this error message is not issued again. */
3002 if (start)
3003 cp_lexer_purge_tokens_after (parser->lexer, start);
3007 /* If parsing an integral constant-expression, issue an error message
3008 about the fact that THING appeared and return true. Otherwise,
3009 return false. In either case, set
3010 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3012 static bool
3013 cp_parser_non_integral_constant_expression (cp_parser *parser,
3014 non_integral_constant thing)
3016 parser->non_integral_constant_expression_p = true;
3017 if (parser->integral_constant_expression_p)
3019 if (!parser->allow_non_integral_constant_expression_p)
3021 const char *msg = NULL;
3022 switch (thing)
3024 case NIC_FLOAT:
3025 error ("floating-point literal "
3026 "cannot appear in a constant-expression");
3027 return true;
3028 case NIC_CAST:
3029 error ("a cast to a type other than an integral or "
3030 "enumeration type cannot appear in a "
3031 "constant-expression");
3032 return true;
3033 case NIC_TYPEID:
3034 error ("%<typeid%> operator "
3035 "cannot appear in a constant-expression");
3036 return true;
3037 case NIC_NCC:
3038 error ("non-constant compound literals "
3039 "cannot appear in a constant-expression");
3040 return true;
3041 case NIC_FUNC_CALL:
3042 error ("a function call "
3043 "cannot appear in a constant-expression");
3044 return true;
3045 case NIC_INC:
3046 error ("an increment "
3047 "cannot appear in a constant-expression");
3048 return true;
3049 case NIC_DEC:
3050 error ("an decrement "
3051 "cannot appear in a constant-expression");
3052 return true;
3053 case NIC_ARRAY_REF:
3054 error ("an array reference "
3055 "cannot appear in a constant-expression");
3056 return true;
3057 case NIC_ADDR_LABEL:
3058 error ("the address of a label "
3059 "cannot appear in a constant-expression");
3060 return true;
3061 case NIC_OVERLOADED:
3062 error ("calls to overloaded operators "
3063 "cannot appear in a constant-expression");
3064 return true;
3065 case NIC_ASSIGNMENT:
3066 error ("an assignment cannot appear in a constant-expression");
3067 return true;
3068 case NIC_COMMA:
3069 error ("a comma operator "
3070 "cannot appear in a constant-expression");
3071 return true;
3072 case NIC_CONSTRUCTOR:
3073 error ("a call to a constructor "
3074 "cannot appear in a constant-expression");
3075 return true;
3076 case NIC_TRANSACTION:
3077 error ("a transaction expression "
3078 "cannot appear in a constant-expression");
3079 return true;
3080 case NIC_THIS:
3081 msg = "this";
3082 break;
3083 case NIC_FUNC_NAME:
3084 msg = "__FUNCTION__";
3085 break;
3086 case NIC_PRETTY_FUNC:
3087 msg = "__PRETTY_FUNCTION__";
3088 break;
3089 case NIC_C99_FUNC:
3090 msg = "__func__";
3091 break;
3092 case NIC_VA_ARG:
3093 msg = "va_arg";
3094 break;
3095 case NIC_ARROW:
3096 msg = "->";
3097 break;
3098 case NIC_POINT:
3099 msg = ".";
3100 break;
3101 case NIC_STAR:
3102 msg = "*";
3103 break;
3104 case NIC_ADDR:
3105 msg = "&";
3106 break;
3107 case NIC_PREINCREMENT:
3108 msg = "++";
3109 break;
3110 case NIC_PREDECREMENT:
3111 msg = "--";
3112 break;
3113 case NIC_NEW:
3114 msg = "new";
3115 break;
3116 case NIC_DEL:
3117 msg = "delete";
3118 break;
3119 default:
3120 gcc_unreachable ();
3122 if (msg)
3123 error ("%qs cannot appear in a constant-expression", msg);
3124 return true;
3127 return false;
3130 /* Emit a diagnostic for an invalid type name. This function commits
3131 to the current active tentative parse, if any. (Otherwise, the
3132 problematic construct might be encountered again later, resulting
3133 in duplicate error messages.) LOCATION is the location of ID. */
3135 static void
3136 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3137 location_t location)
3139 tree decl, ambiguous_decls;
3140 cp_parser_commit_to_tentative_parse (parser);
3141 /* Try to lookup the identifier. */
3142 decl = cp_parser_lookup_name (parser, id, none_type,
3143 /*is_template=*/false,
3144 /*is_namespace=*/false,
3145 /*check_dependency=*/true,
3146 &ambiguous_decls, location);
3147 if (ambiguous_decls)
3148 /* If the lookup was ambiguous, an error will already have
3149 been issued. */
3150 return;
3151 /* If the lookup found a template-name, it means that the user forgot
3152 to specify an argument list. Emit a useful error message. */
3153 if (DECL_TYPE_TEMPLATE_P (decl))
3155 error_at (location,
3156 "invalid use of template-name %qE without an argument list",
3157 decl);
3158 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3160 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3161 error_at (location, "invalid use of destructor %qD as a type", id);
3162 else if (TREE_CODE (decl) == TYPE_DECL)
3163 /* Something like 'unsigned A a;' */
3164 error_at (location, "invalid combination of multiple type-specifiers");
3165 else if (!parser->scope)
3167 /* Issue an error message. */
3168 const char *suggestion = NULL;
3169 if (TREE_CODE (id) == IDENTIFIER_NODE)
3170 suggestion = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME);
3171 if (suggestion)
3173 gcc_rich_location richloc (location);
3174 richloc.add_fixit_replace (suggestion);
3175 error_at_rich_loc (&richloc,
3176 "%qE does not name a type; did you mean %qs?",
3177 id, suggestion);
3179 else
3180 error_at (location, "%qE does not name a type", id);
3181 /* If we're in a template class, it's possible that the user was
3182 referring to a type from a base class. For example:
3184 template <typename T> struct A { typedef T X; };
3185 template <typename T> struct B : public A<T> { X x; };
3187 The user should have said "typename A<T>::X". */
3188 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3189 inform (location, "C++11 %<constexpr%> only available with "
3190 "-std=c++11 or -std=gnu++11");
3191 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3192 inform (location, "C++11 %<noexcept%> only available with "
3193 "-std=c++11 or -std=gnu++11");
3194 else if (cxx_dialect < cxx11
3195 && TREE_CODE (id) == IDENTIFIER_NODE
3196 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
3197 inform (location, "C++11 %<thread_local%> only available with "
3198 "-std=c++11 or -std=gnu++11");
3199 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3200 inform (location, "%<concept%> only available with -fconcepts");
3201 else if (processing_template_decl && current_class_type
3202 && TYPE_BINFO (current_class_type))
3204 tree b;
3206 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3208 b = TREE_CHAIN (b))
3210 tree base_type = BINFO_TYPE (b);
3211 if (CLASS_TYPE_P (base_type)
3212 && dependent_type_p (base_type))
3214 tree field;
3215 /* Go from a particular instantiation of the
3216 template (which will have an empty TYPE_FIELDs),
3217 to the main version. */
3218 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3219 for (field = TYPE_FIELDS (base_type);
3220 field;
3221 field = DECL_CHAIN (field))
3222 if (TREE_CODE (field) == TYPE_DECL
3223 && DECL_NAME (field) == id)
3225 inform (location,
3226 "(perhaps %<typename %T::%E%> was intended)",
3227 BINFO_TYPE (b), id);
3228 break;
3230 if (field)
3231 break;
3236 /* Here we diagnose qualified-ids where the scope is actually correct,
3237 but the identifier does not resolve to a valid type name. */
3238 else if (parser->scope != error_mark_node)
3240 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3242 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3243 error_at (location_of (id),
3244 "%qE in namespace %qE does not name a template type",
3245 id, parser->scope);
3246 else
3247 error_at (location_of (id),
3248 "%qE in namespace %qE does not name a type",
3249 id, parser->scope);
3250 if (DECL_P (decl))
3251 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3253 else if (CLASS_TYPE_P (parser->scope)
3254 && constructor_name_p (id, parser->scope))
3256 /* A<T>::A<T>() */
3257 error_at (location, "%<%T::%E%> names the constructor, not"
3258 " the type", parser->scope, id);
3259 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3260 error_at (location, "and %qT has no template constructors",
3261 parser->scope);
3263 else if (TYPE_P (parser->scope)
3264 && dependent_scope_p (parser->scope))
3265 error_at (location, "need %<typename%> before %<%T::%E%> because "
3266 "%qT is a dependent scope",
3267 parser->scope, id, parser->scope);
3268 else if (TYPE_P (parser->scope))
3270 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3271 error_at (location_of (id),
3272 "%qE in %q#T does not name a template type",
3273 id, parser->scope);
3274 else
3275 error_at (location_of (id),
3276 "%qE in %q#T does not name a type",
3277 id, parser->scope);
3278 if (DECL_P (decl))
3279 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3281 else
3282 gcc_unreachable ();
3286 /* Check for a common situation where a type-name should be present,
3287 but is not, and issue a sensible error message. Returns true if an
3288 invalid type-name was detected.
3290 The situation handled by this function are variable declarations of the
3291 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3292 Usually, `ID' should name a type, but if we got here it means that it
3293 does not. We try to emit the best possible error message depending on
3294 how exactly the id-expression looks like. */
3296 static bool
3297 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3299 tree id;
3300 cp_token *token = cp_lexer_peek_token (parser->lexer);
3302 /* Avoid duplicate error about ambiguous lookup. */
3303 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3305 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3306 if (next->type == CPP_NAME && next->error_reported)
3307 goto out;
3310 cp_parser_parse_tentatively (parser);
3311 id = cp_parser_id_expression (parser,
3312 /*template_keyword_p=*/false,
3313 /*check_dependency_p=*/true,
3314 /*template_p=*/NULL,
3315 /*declarator_p=*/true,
3316 /*optional_p=*/false);
3317 /* If the next token is a (, this is a function with no explicit return
3318 type, i.e. constructor, destructor or conversion op. */
3319 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3320 || TREE_CODE (id) == TYPE_DECL)
3322 cp_parser_abort_tentative_parse (parser);
3323 return false;
3325 if (!cp_parser_parse_definitely (parser))
3326 return false;
3328 /* Emit a diagnostic for the invalid type. */
3329 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3330 out:
3331 /* If we aren't in the middle of a declarator (i.e. in a
3332 parameter-declaration-clause), skip to the end of the declaration;
3333 there's no point in trying to process it. */
3334 if (!parser->in_declarator_p)
3335 cp_parser_skip_to_end_of_block_or_statement (parser);
3336 return true;
3339 /* Consume tokens up to, and including, the next non-nested closing `)'.
3340 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3341 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3342 found an unnested token of that type. */
3344 static int
3345 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3346 bool recovering,
3347 cpp_ttype or_ttype,
3348 bool consume_paren)
3350 unsigned paren_depth = 0;
3351 unsigned brace_depth = 0;
3352 unsigned square_depth = 0;
3354 if (recovering && or_ttype == CPP_EOF
3355 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3356 return 0;
3358 while (true)
3360 cp_token * token = cp_lexer_peek_token (parser->lexer);
3362 /* Have we found what we're looking for before the closing paren? */
3363 if (token->type == or_ttype && or_ttype != CPP_EOF
3364 && !brace_depth && !paren_depth && !square_depth)
3365 return -1;
3367 switch (token->type)
3369 case CPP_EOF:
3370 case CPP_PRAGMA_EOL:
3371 /* If we've run out of tokens, then there is no closing `)'. */
3372 return 0;
3374 /* This is good for lambda expression capture-lists. */
3375 case CPP_OPEN_SQUARE:
3376 ++square_depth;
3377 break;
3378 case CPP_CLOSE_SQUARE:
3379 if (!square_depth--)
3380 return 0;
3381 break;
3383 case CPP_SEMICOLON:
3384 /* This matches the processing in skip_to_end_of_statement. */
3385 if (!brace_depth)
3386 return 0;
3387 break;
3389 case CPP_OPEN_BRACE:
3390 ++brace_depth;
3391 break;
3392 case CPP_CLOSE_BRACE:
3393 if (!brace_depth--)
3394 return 0;
3395 break;
3397 case CPP_OPEN_PAREN:
3398 if (!brace_depth)
3399 ++paren_depth;
3400 break;
3402 case CPP_CLOSE_PAREN:
3403 if (!brace_depth && !paren_depth--)
3405 if (consume_paren)
3406 cp_lexer_consume_token (parser->lexer);
3407 return 1;
3409 break;
3411 default:
3412 break;
3415 /* Consume the token. */
3416 cp_lexer_consume_token (parser->lexer);
3420 /* Consume tokens up to, and including, the next non-nested closing `)'.
3421 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3422 are doing error recovery. Returns -1 if OR_COMMA is true and we
3423 found an unnested token of that type. */
3425 static int
3426 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3427 bool recovering,
3428 bool or_comma,
3429 bool consume_paren)
3431 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3432 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3433 ttype, consume_paren);
3436 /* Consume tokens until we reach the end of the current statement.
3437 Normally, that will be just before consuming a `;'. However, if a
3438 non-nested `}' comes first, then we stop before consuming that. */
3440 static void
3441 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3443 unsigned nesting_depth = 0;
3445 /* Unwind generic function template scope if necessary. */
3446 if (parser->fully_implicit_function_template_p)
3447 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3449 while (true)
3451 cp_token *token = cp_lexer_peek_token (parser->lexer);
3453 switch (token->type)
3455 case CPP_EOF:
3456 case CPP_PRAGMA_EOL:
3457 /* If we've run out of tokens, stop. */
3458 return;
3460 case CPP_SEMICOLON:
3461 /* If the next token is a `;', we have reached the end of the
3462 statement. */
3463 if (!nesting_depth)
3464 return;
3465 break;
3467 case CPP_CLOSE_BRACE:
3468 /* If this is a non-nested '}', stop before consuming it.
3469 That way, when confronted with something like:
3471 { 3 + }
3473 we stop before consuming the closing '}', even though we
3474 have not yet reached a `;'. */
3475 if (nesting_depth == 0)
3476 return;
3478 /* If it is the closing '}' for a block that we have
3479 scanned, stop -- but only after consuming the token.
3480 That way given:
3482 void f g () { ... }
3483 typedef int I;
3485 we will stop after the body of the erroneously declared
3486 function, but before consuming the following `typedef'
3487 declaration. */
3488 if (--nesting_depth == 0)
3490 cp_lexer_consume_token (parser->lexer);
3491 return;
3493 /* XXX Really fallthru? */
3494 /* FALLTHRU */
3496 case CPP_OPEN_BRACE:
3497 ++nesting_depth;
3498 break;
3500 default:
3501 break;
3504 /* Consume the token. */
3505 cp_lexer_consume_token (parser->lexer);
3509 /* This function is called at the end of a statement or declaration.
3510 If the next token is a semicolon, it is consumed; otherwise, error
3511 recovery is attempted. */
3513 static void
3514 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3516 /* Look for the trailing `;'. */
3517 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3519 /* If there is additional (erroneous) input, skip to the end of
3520 the statement. */
3521 cp_parser_skip_to_end_of_statement (parser);
3522 /* If the next token is now a `;', consume it. */
3523 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3524 cp_lexer_consume_token (parser->lexer);
3528 /* Skip tokens until we have consumed an entire block, or until we
3529 have consumed a non-nested `;'. */
3531 static void
3532 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3534 int nesting_depth = 0;
3536 /* Unwind generic function template scope if necessary. */
3537 if (parser->fully_implicit_function_template_p)
3538 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3540 while (nesting_depth >= 0)
3542 cp_token *token = cp_lexer_peek_token (parser->lexer);
3544 switch (token->type)
3546 case CPP_EOF:
3547 case CPP_PRAGMA_EOL:
3548 /* If we've run out of tokens, stop. */
3549 return;
3551 case CPP_SEMICOLON:
3552 /* Stop if this is an unnested ';'. */
3553 if (!nesting_depth)
3554 nesting_depth = -1;
3555 break;
3557 case CPP_CLOSE_BRACE:
3558 /* Stop if this is an unnested '}', or closes the outermost
3559 nesting level. */
3560 nesting_depth--;
3561 if (nesting_depth < 0)
3562 return;
3563 if (!nesting_depth)
3564 nesting_depth = -1;
3565 break;
3567 case CPP_OPEN_BRACE:
3568 /* Nest. */
3569 nesting_depth++;
3570 break;
3572 default:
3573 break;
3576 /* Consume the token. */
3577 cp_lexer_consume_token (parser->lexer);
3581 /* Skip tokens until a non-nested closing curly brace is the next
3582 token, or there are no more tokens. Return true in the first case,
3583 false otherwise. */
3585 static bool
3586 cp_parser_skip_to_closing_brace (cp_parser *parser)
3588 unsigned nesting_depth = 0;
3590 while (true)
3592 cp_token *token = cp_lexer_peek_token (parser->lexer);
3594 switch (token->type)
3596 case CPP_EOF:
3597 case CPP_PRAGMA_EOL:
3598 /* If we've run out of tokens, stop. */
3599 return false;
3601 case CPP_CLOSE_BRACE:
3602 /* If the next token is a non-nested `}', then we have reached
3603 the end of the current block. */
3604 if (nesting_depth-- == 0)
3605 return true;
3606 break;
3608 case CPP_OPEN_BRACE:
3609 /* If it the next token is a `{', then we are entering a new
3610 block. Consume the entire block. */
3611 ++nesting_depth;
3612 break;
3614 default:
3615 break;
3618 /* Consume the token. */
3619 cp_lexer_consume_token (parser->lexer);
3623 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3624 parameter is the PRAGMA token, allowing us to purge the entire pragma
3625 sequence. */
3627 static void
3628 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3630 cp_token *token;
3632 parser->lexer->in_pragma = false;
3635 token = cp_lexer_consume_token (parser->lexer);
3636 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3638 /* Ensure that the pragma is not parsed again. */
3639 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3642 /* Require pragma end of line, resyncing with it as necessary. The
3643 arguments are as for cp_parser_skip_to_pragma_eol. */
3645 static void
3646 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3648 parser->lexer->in_pragma = false;
3649 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3650 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3653 /* This is a simple wrapper around make_typename_type. When the id is
3654 an unresolved identifier node, we can provide a superior diagnostic
3655 using cp_parser_diagnose_invalid_type_name. */
3657 static tree
3658 cp_parser_make_typename_type (cp_parser *parser, tree id,
3659 location_t id_location)
3661 tree result;
3662 if (identifier_p (id))
3664 result = make_typename_type (parser->scope, id, typename_type,
3665 /*complain=*/tf_none);
3666 if (result == error_mark_node)
3667 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3668 return result;
3670 return make_typename_type (parser->scope, id, typename_type, tf_error);
3673 /* This is a wrapper around the
3674 make_{pointer,ptrmem,reference}_declarator functions that decides
3675 which one to call based on the CODE and CLASS_TYPE arguments. The
3676 CODE argument should be one of the values returned by
3677 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3678 appertain to the pointer or reference. */
3680 static cp_declarator *
3681 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3682 cp_cv_quals cv_qualifiers,
3683 cp_declarator *target,
3684 tree attributes)
3686 if (code == ERROR_MARK)
3687 return cp_error_declarator;
3689 if (code == INDIRECT_REF)
3690 if (class_type == NULL_TREE)
3691 return make_pointer_declarator (cv_qualifiers, target, attributes);
3692 else
3693 return make_ptrmem_declarator (cv_qualifiers, class_type,
3694 target, attributes);
3695 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3696 return make_reference_declarator (cv_qualifiers, target,
3697 false, attributes);
3698 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3699 return make_reference_declarator (cv_qualifiers, target,
3700 true, attributes);
3701 gcc_unreachable ();
3704 /* Create a new C++ parser. */
3706 static cp_parser *
3707 cp_parser_new (void)
3709 cp_parser *parser;
3710 cp_lexer *lexer;
3711 unsigned i;
3713 /* cp_lexer_new_main is called before doing GC allocation because
3714 cp_lexer_new_main might load a PCH file. */
3715 lexer = cp_lexer_new_main ();
3717 /* Initialize the binops_by_token so that we can get the tree
3718 directly from the token. */
3719 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3720 binops_by_token[binops[i].token_type] = binops[i];
3722 parser = ggc_cleared_alloc<cp_parser> ();
3723 parser->lexer = lexer;
3724 parser->context = cp_parser_context_new (NULL);
3726 /* For now, we always accept GNU extensions. */
3727 parser->allow_gnu_extensions_p = 1;
3729 /* The `>' token is a greater-than operator, not the end of a
3730 template-id. */
3731 parser->greater_than_is_operator_p = true;
3733 parser->default_arg_ok_p = true;
3735 /* We are not parsing a constant-expression. */
3736 parser->integral_constant_expression_p = false;
3737 parser->allow_non_integral_constant_expression_p = false;
3738 parser->non_integral_constant_expression_p = false;
3740 /* Local variable names are not forbidden. */
3741 parser->local_variables_forbidden_p = false;
3743 /* We are not processing an `extern "C"' declaration. */
3744 parser->in_unbraced_linkage_specification_p = false;
3746 /* We are not processing a declarator. */
3747 parser->in_declarator_p = false;
3749 /* We are not processing a template-argument-list. */
3750 parser->in_template_argument_list_p = false;
3752 /* We are not in an iteration statement. */
3753 parser->in_statement = 0;
3755 /* We are not in a switch statement. */
3756 parser->in_switch_statement_p = false;
3758 /* We are not parsing a type-id inside an expression. */
3759 parser->in_type_id_in_expr_p = false;
3761 /* Declarations aren't implicitly extern "C". */
3762 parser->implicit_extern_c = false;
3764 /* String literals should be translated to the execution character set. */
3765 parser->translate_strings_p = true;
3767 /* We are not parsing a function body. */
3768 parser->in_function_body = false;
3770 /* We can correct until told otherwise. */
3771 parser->colon_corrects_to_scope_p = true;
3773 /* The unparsed function queue is empty. */
3774 push_unparsed_function_queues (parser);
3776 /* There are no classes being defined. */
3777 parser->num_classes_being_defined = 0;
3779 /* No template parameters apply. */
3780 parser->num_template_parameter_lists = 0;
3782 /* Special parsing data structures. */
3783 parser->omp_declare_simd = NULL;
3784 parser->cilk_simd_fn_info = NULL;
3785 parser->oacc_routine = NULL;
3787 /* Not declaring an implicit function template. */
3788 parser->auto_is_implicit_function_template_parm_p = false;
3789 parser->fully_implicit_function_template_p = false;
3790 parser->implicit_template_parms = 0;
3791 parser->implicit_template_scope = 0;
3793 /* Allow constrained-type-specifiers. */
3794 parser->prevent_constrained_type_specifiers = 0;
3796 return parser;
3799 /* Create a cp_lexer structure which will emit the tokens in CACHE
3800 and push it onto the parser's lexer stack. This is used for delayed
3801 parsing of in-class method bodies and default arguments, and should
3802 not be confused with tentative parsing. */
3803 static void
3804 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3806 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3807 lexer->next = parser->lexer;
3808 parser->lexer = lexer;
3810 /* Move the current source position to that of the first token in the
3811 new lexer. */
3812 cp_lexer_set_source_position_from_token (lexer->next_token);
3815 /* Pop the top lexer off the parser stack. This is never used for the
3816 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3817 static void
3818 cp_parser_pop_lexer (cp_parser *parser)
3820 cp_lexer *lexer = parser->lexer;
3821 parser->lexer = lexer->next;
3822 cp_lexer_destroy (lexer);
3824 /* Put the current source position back where it was before this
3825 lexer was pushed. */
3826 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3829 /* Lexical conventions [gram.lex] */
3831 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3832 identifier. */
3834 static cp_expr
3835 cp_parser_identifier (cp_parser* parser)
3837 cp_token *token;
3839 /* Look for the identifier. */
3840 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3841 /* Return the value. */
3842 if (token)
3843 return cp_expr (token->u.value, token->location);
3844 else
3845 return error_mark_node;
3848 /* Parse a sequence of adjacent string constants. Returns a
3849 TREE_STRING representing the combined, nul-terminated string
3850 constant. If TRANSLATE is true, translate the string to the
3851 execution character set. If WIDE_OK is true, a wide string is
3852 invalid here.
3854 C++98 [lex.string] says that if a narrow string literal token is
3855 adjacent to a wide string literal token, the behavior is undefined.
3856 However, C99 6.4.5p4 says that this results in a wide string literal.
3857 We follow C99 here, for consistency with the C front end.
3859 This code is largely lifted from lex_string() in c-lex.c.
3861 FUTURE: ObjC++ will need to handle @-strings here. */
3862 static cp_expr
3863 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3864 bool lookup_udlit = true)
3866 tree value;
3867 size_t count;
3868 struct obstack str_ob;
3869 cpp_string str, istr, *strs;
3870 cp_token *tok;
3871 enum cpp_ttype type, curr_type;
3872 int have_suffix_p = 0;
3873 tree string_tree;
3874 tree suffix_id = NULL_TREE;
3875 bool curr_tok_is_userdef_p = false;
3877 tok = cp_lexer_peek_token (parser->lexer);
3878 if (!cp_parser_is_string_literal (tok))
3880 cp_parser_error (parser, "expected string-literal");
3881 return error_mark_node;
3884 location_t loc = tok->location;
3886 if (cpp_userdef_string_p (tok->type))
3888 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3889 curr_type = cpp_userdef_string_remove_type (tok->type);
3890 curr_tok_is_userdef_p = true;
3892 else
3894 string_tree = tok->u.value;
3895 curr_type = tok->type;
3897 type = curr_type;
3899 /* Try to avoid the overhead of creating and destroying an obstack
3900 for the common case of just one string. */
3901 if (!cp_parser_is_string_literal
3902 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3904 cp_lexer_consume_token (parser->lexer);
3906 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3907 str.len = TREE_STRING_LENGTH (string_tree);
3908 count = 1;
3910 if (curr_tok_is_userdef_p)
3912 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3913 have_suffix_p = 1;
3914 curr_type = cpp_userdef_string_remove_type (tok->type);
3916 else
3917 curr_type = tok->type;
3919 strs = &str;
3921 else
3923 location_t last_tok_loc = tok->location;
3924 gcc_obstack_init (&str_ob);
3925 count = 0;
3929 cp_lexer_consume_token (parser->lexer);
3930 count++;
3931 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3932 str.len = TREE_STRING_LENGTH (string_tree);
3934 if (curr_tok_is_userdef_p)
3936 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3937 if (have_suffix_p == 0)
3939 suffix_id = curr_suffix_id;
3940 have_suffix_p = 1;
3942 else if (have_suffix_p == 1
3943 && curr_suffix_id != suffix_id)
3945 error ("inconsistent user-defined literal suffixes"
3946 " %qD and %qD in string literal",
3947 suffix_id, curr_suffix_id);
3948 have_suffix_p = -1;
3950 curr_type = cpp_userdef_string_remove_type (tok->type);
3952 else
3953 curr_type = tok->type;
3955 if (type != curr_type)
3957 if (type == CPP_STRING)
3958 type = curr_type;
3959 else if (curr_type != CPP_STRING)
3961 rich_location rich_loc (line_table, tok->location);
3962 rich_loc.add_range (last_tok_loc, false);
3963 error_at_rich_loc (&rich_loc,
3964 "unsupported non-standard concatenation "
3965 "of string literals");
3969 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3971 last_tok_loc = tok->location;
3973 tok = cp_lexer_peek_token (parser->lexer);
3974 if (cpp_userdef_string_p (tok->type))
3976 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3977 curr_type = cpp_userdef_string_remove_type (tok->type);
3978 curr_tok_is_userdef_p = true;
3980 else
3982 string_tree = tok->u.value;
3983 curr_type = tok->type;
3984 curr_tok_is_userdef_p = false;
3987 while (cp_parser_is_string_literal (tok));
3989 /* A string literal built by concatenation has its caret=start at
3990 the start of the initial string, and its finish at the finish of
3991 the final string literal. */
3992 loc = make_location (loc, loc, get_finish (last_tok_loc));
3994 strs = (cpp_string *) obstack_finish (&str_ob);
3997 if (type != CPP_STRING && !wide_ok)
3999 cp_parser_error (parser, "a wide string is invalid in this context");
4000 type = CPP_STRING;
4003 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4004 (parse_in, strs, count, &istr, type))
4006 value = build_string (istr.len, (const char *)istr.text);
4007 free (CONST_CAST (unsigned char *, istr.text));
4009 switch (type)
4011 default:
4012 case CPP_STRING:
4013 case CPP_UTF8STRING:
4014 TREE_TYPE (value) = char_array_type_node;
4015 break;
4016 case CPP_STRING16:
4017 TREE_TYPE (value) = char16_array_type_node;
4018 break;
4019 case CPP_STRING32:
4020 TREE_TYPE (value) = char32_array_type_node;
4021 break;
4022 case CPP_WSTRING:
4023 TREE_TYPE (value) = wchar_array_type_node;
4024 break;
4027 value = fix_string_type (value);
4029 if (have_suffix_p)
4031 tree literal = build_userdef_literal (suffix_id, value,
4032 OT_NONE, NULL_TREE);
4033 if (lookup_udlit)
4034 value = cp_parser_userdef_string_literal (literal);
4035 else
4036 value = literal;
4039 else
4040 /* cpp_interpret_string has issued an error. */
4041 value = error_mark_node;
4043 if (count > 1)
4044 obstack_free (&str_ob, 0);
4046 return cp_expr (value, loc);
4049 /* Look up a literal operator with the name and the exact arguments. */
4051 static tree
4052 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4054 tree decl, fns;
4055 decl = lookup_name (name);
4056 if (!decl || !is_overloaded_fn (decl))
4057 return error_mark_node;
4059 for (fns = decl; fns; fns = OVL_NEXT (fns))
4061 unsigned int ix;
4062 bool found = true;
4063 tree fn = OVL_CURRENT (fns);
4064 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4065 if (parmtypes != NULL_TREE)
4067 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4068 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4070 tree tparm = TREE_VALUE (parmtypes);
4071 tree targ = TREE_TYPE ((*args)[ix]);
4072 bool ptr = TYPE_PTR_P (tparm);
4073 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4074 if ((ptr || arr || !same_type_p (tparm, targ))
4075 && (!ptr || !arr
4076 || !same_type_p (TREE_TYPE (tparm),
4077 TREE_TYPE (targ))))
4078 found = false;
4080 if (found
4081 && ix == vec_safe_length (args)
4082 /* May be this should be sufficient_parms_p instead,
4083 depending on how exactly should user-defined literals
4084 work in presence of default arguments on the literal
4085 operator parameters. */
4086 && parmtypes == void_list_node)
4087 return decl;
4091 return error_mark_node;
4094 /* Parse a user-defined char constant. Returns a call to a user-defined
4095 literal operator taking the character as an argument. */
4097 static cp_expr
4098 cp_parser_userdef_char_literal (cp_parser *parser)
4100 cp_token *token = cp_lexer_consume_token (parser->lexer);
4101 tree literal = token->u.value;
4102 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4103 tree value = USERDEF_LITERAL_VALUE (literal);
4104 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4105 tree decl, result;
4107 /* Build up a call to the user-defined operator */
4108 /* Lookup the name we got back from the id-expression. */
4109 vec<tree, va_gc> *args = make_tree_vector ();
4110 vec_safe_push (args, value);
4111 decl = lookup_literal_operator (name, args);
4112 if (!decl || decl == error_mark_node)
4114 error ("unable to find character literal operator %qD with %qT argument",
4115 name, TREE_TYPE (value));
4116 release_tree_vector (args);
4117 return error_mark_node;
4119 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4120 release_tree_vector (args);
4121 return result;
4124 /* A subroutine of cp_parser_userdef_numeric_literal to
4125 create a char... template parameter pack from a string node. */
4127 static tree
4128 make_char_string_pack (tree value)
4130 tree charvec;
4131 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4132 const char *str = TREE_STRING_POINTER (value);
4133 int i, len = TREE_STRING_LENGTH (value) - 1;
4134 tree argvec = make_tree_vec (1);
4136 /* Fill in CHARVEC with all of the parameters. */
4137 charvec = make_tree_vec (len);
4138 for (i = 0; i < len; ++i)
4139 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4141 /* Build the argument packs. */
4142 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4143 TREE_TYPE (argpack) = char_type_node;
4145 TREE_VEC_ELT (argvec, 0) = argpack;
4147 return argvec;
4150 /* A subroutine of cp_parser_userdef_numeric_literal to
4151 create a char... template parameter pack from a string node. */
4153 static tree
4154 make_string_pack (tree value)
4156 tree charvec;
4157 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4158 const unsigned char *str
4159 = (const unsigned char *) TREE_STRING_POINTER (value);
4160 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4161 int len = TREE_STRING_LENGTH (value) / sz - 1;
4162 tree argvec = make_tree_vec (2);
4164 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4165 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4167 /* First template parm is character type. */
4168 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4170 /* Fill in CHARVEC with all of the parameters. */
4171 charvec = make_tree_vec (len);
4172 for (int i = 0; i < len; ++i)
4173 TREE_VEC_ELT (charvec, i)
4174 = double_int_to_tree (str_char_type_node,
4175 double_int::from_buffer (str + i * sz, sz));
4177 /* Build the argument packs. */
4178 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4179 TREE_TYPE (argpack) = str_char_type_node;
4181 TREE_VEC_ELT (argvec, 1) = argpack;
4183 return argvec;
4186 /* Parse a user-defined numeric constant. returns a call to a user-defined
4187 literal operator. */
4189 static cp_expr
4190 cp_parser_userdef_numeric_literal (cp_parser *parser)
4192 cp_token *token = cp_lexer_consume_token (parser->lexer);
4193 tree literal = token->u.value;
4194 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4195 tree value = USERDEF_LITERAL_VALUE (literal);
4196 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4197 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4198 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4199 tree decl, result;
4200 vec<tree, va_gc> *args;
4202 /* Look for a literal operator taking the exact type of numeric argument
4203 as the literal value. */
4204 args = make_tree_vector ();
4205 vec_safe_push (args, value);
4206 decl = lookup_literal_operator (name, args);
4207 if (decl && decl != error_mark_node)
4209 result = finish_call_expr (decl, &args, false, true,
4210 tf_warning_or_error);
4212 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4214 warning_at (token->location, OPT_Woverflow,
4215 "integer literal exceeds range of %qT type",
4216 long_long_unsigned_type_node);
4218 else
4220 if (overflow > 0)
4221 warning_at (token->location, OPT_Woverflow,
4222 "floating literal exceeds range of %qT type",
4223 long_double_type_node);
4224 else if (overflow < 0)
4225 warning_at (token->location, OPT_Woverflow,
4226 "floating literal truncated to zero");
4229 release_tree_vector (args);
4230 return result;
4232 release_tree_vector (args);
4234 /* If the numeric argument didn't work, look for a raw literal
4235 operator taking a const char* argument consisting of the number
4236 in string format. */
4237 args = make_tree_vector ();
4238 vec_safe_push (args, num_string);
4239 decl = lookup_literal_operator (name, args);
4240 if (decl && decl != error_mark_node)
4242 result = finish_call_expr (decl, &args, false, true,
4243 tf_warning_or_error);
4244 release_tree_vector (args);
4245 return result;
4247 release_tree_vector (args);
4249 /* If the raw literal didn't work, look for a non-type template
4250 function with parameter pack char.... Call the function with
4251 template parameter characters representing the number. */
4252 args = make_tree_vector ();
4253 decl = lookup_literal_operator (name, args);
4254 if (decl && decl != error_mark_node)
4256 tree tmpl_args = make_char_string_pack (num_string);
4257 decl = lookup_template_function (decl, tmpl_args);
4258 result = finish_call_expr (decl, &args, false, true,
4259 tf_warning_or_error);
4260 release_tree_vector (args);
4261 return result;
4264 release_tree_vector (args);
4266 error ("unable to find numeric literal operator %qD", name);
4267 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4268 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4269 "to enable more built-in suffixes");
4270 return error_mark_node;
4273 /* Parse a user-defined string constant. Returns a call to a user-defined
4274 literal operator taking a character pointer and the length of the string
4275 as arguments. */
4277 static tree
4278 cp_parser_userdef_string_literal (tree literal)
4280 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4281 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4282 tree value = USERDEF_LITERAL_VALUE (literal);
4283 int len = TREE_STRING_LENGTH (value)
4284 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4285 tree decl, result;
4286 vec<tree, va_gc> *args;
4288 /* Build up a call to the user-defined operator. */
4289 /* Lookup the name we got back from the id-expression. */
4290 args = make_tree_vector ();
4291 vec_safe_push (args, value);
4292 vec_safe_push (args, build_int_cst (size_type_node, len));
4293 decl = lookup_literal_operator (name, args);
4295 if (decl && decl != error_mark_node)
4297 result = finish_call_expr (decl, &args, false, true,
4298 tf_warning_or_error);
4299 release_tree_vector (args);
4300 return result;
4302 release_tree_vector (args);
4304 /* Look for a template function with typename parameter CharT
4305 and parameter pack CharT... Call the function with
4306 template parameter characters representing the string. */
4307 args = make_tree_vector ();
4308 decl = lookup_literal_operator (name, args);
4309 if (decl && decl != error_mark_node)
4311 tree tmpl_args = make_string_pack (value);
4312 decl = lookup_template_function (decl, tmpl_args);
4313 result = finish_call_expr (decl, &args, false, true,
4314 tf_warning_or_error);
4315 release_tree_vector (args);
4316 return result;
4318 release_tree_vector (args);
4320 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4321 name, TREE_TYPE (value), size_type_node);
4322 return error_mark_node;
4326 /* Basic concepts [gram.basic] */
4328 /* Parse a translation-unit.
4330 translation-unit:
4331 declaration-seq [opt]
4333 Returns TRUE if all went well. */
4335 static bool
4336 cp_parser_translation_unit (cp_parser* parser)
4338 /* The address of the first non-permanent object on the declarator
4339 obstack. */
4340 static void *declarator_obstack_base;
4342 bool success;
4344 /* Create the declarator obstack, if necessary. */
4345 if (!cp_error_declarator)
4347 gcc_obstack_init (&declarator_obstack);
4348 /* Create the error declarator. */
4349 cp_error_declarator = make_declarator (cdk_error);
4350 /* Create the empty parameter list. */
4351 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4352 /* Remember where the base of the declarator obstack lies. */
4353 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4356 cp_parser_declaration_seq_opt (parser);
4358 /* If there are no tokens left then all went well. */
4359 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4361 /* Get rid of the token array; we don't need it any more. */
4362 cp_lexer_destroy (parser->lexer);
4363 parser->lexer = NULL;
4365 /* This file might have been a context that's implicitly extern
4366 "C". If so, pop the lang context. (Only relevant for PCH.) */
4367 if (parser->implicit_extern_c)
4369 pop_lang_context ();
4370 parser->implicit_extern_c = false;
4373 /* Finish up. */
4374 finish_translation_unit ();
4376 success = true;
4378 else
4380 cp_parser_error (parser, "expected declaration");
4381 success = false;
4384 /* Make sure the declarator obstack was fully cleaned up. */
4385 gcc_assert (obstack_next_free (&declarator_obstack)
4386 == declarator_obstack_base);
4388 /* All went well. */
4389 return success;
4392 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4393 decltype context. */
4395 static inline tsubst_flags_t
4396 complain_flags (bool decltype_p)
4398 tsubst_flags_t complain = tf_warning_or_error;
4399 if (decltype_p)
4400 complain |= tf_decltype;
4401 return complain;
4404 /* We're about to parse a collection of statements. If we're currently
4405 parsing tentatively, set up a firewall so that any nested
4406 cp_parser_commit_to_tentative_parse won't affect the current context. */
4408 static cp_token_position
4409 cp_parser_start_tentative_firewall (cp_parser *parser)
4411 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4412 return 0;
4414 cp_parser_parse_tentatively (parser);
4415 cp_parser_commit_to_topmost_tentative_parse (parser);
4416 return cp_lexer_token_position (parser->lexer, false);
4419 /* We've finished parsing the collection of statements. Wrap up the
4420 firewall and replace the relevant tokens with the parsed form. */
4422 static void
4423 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4424 tree expr)
4426 if (!start)
4427 return;
4429 /* Finish the firewall level. */
4430 cp_parser_parse_definitely (parser);
4431 /* And remember the result of the parse for when we try again. */
4432 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4433 token->type = CPP_PREPARSED_EXPR;
4434 token->u.value = expr;
4435 token->keyword = RID_MAX;
4436 cp_lexer_purge_tokens_after (parser->lexer, start);
4439 /* Like the above functions, but let the user modify the tokens. Used by
4440 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4441 later parses, so it makes sense to localize the effects of
4442 cp_parser_commit_to_tentative_parse. */
4444 struct tentative_firewall
4446 cp_parser *parser;
4447 bool set;
4449 tentative_firewall (cp_parser *p): parser(p)
4451 /* If we're currently parsing tentatively, start a committed level as a
4452 firewall and then an inner tentative parse. */
4453 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4455 cp_parser_parse_tentatively (parser);
4456 cp_parser_commit_to_topmost_tentative_parse (parser);
4457 cp_parser_parse_tentatively (parser);
4461 ~tentative_firewall()
4463 if (set)
4465 /* Finish the inner tentative parse and the firewall, propagating any
4466 uncommitted error state to the outer tentative parse. */
4467 bool err = cp_parser_error_occurred (parser);
4468 cp_parser_parse_definitely (parser);
4469 cp_parser_parse_definitely (parser);
4470 if (err)
4471 cp_parser_simulate_error (parser);
4476 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4477 enclosing parentheses. */
4479 static cp_expr
4480 cp_parser_statement_expr (cp_parser *parser)
4482 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4484 /* Consume the '('. */
4485 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4486 cp_lexer_consume_token (parser->lexer);
4487 /* Start the statement-expression. */
4488 tree expr = begin_stmt_expr ();
4489 /* Parse the compound-statement. */
4490 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4491 /* Finish up. */
4492 expr = finish_stmt_expr (expr, false);
4493 /* Consume the ')'. */
4494 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4495 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4496 cp_parser_skip_to_end_of_statement (parser);
4498 cp_parser_end_tentative_firewall (parser, start, expr);
4499 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4500 return cp_expr (expr, combined_loc);
4503 /* Expressions [gram.expr] */
4505 /* Parse a fold-operator.
4507 fold-operator:
4508 - * / % ^ & | = < > << >>
4509 = -= *= /= %= ^= &= |= <<= >>=
4510 == != <= >= && || , .* ->*
4512 This returns the tree code corresponding to the matched operator
4513 as an int. When the current token matches a compound assignment
4514 opertor, the resulting tree code is the negative value of the
4515 non-assignment operator. */
4517 static int
4518 cp_parser_fold_operator (cp_token *token)
4520 switch (token->type)
4522 case CPP_PLUS: return PLUS_EXPR;
4523 case CPP_MINUS: return MINUS_EXPR;
4524 case CPP_MULT: return MULT_EXPR;
4525 case CPP_DIV: return TRUNC_DIV_EXPR;
4526 case CPP_MOD: return TRUNC_MOD_EXPR;
4527 case CPP_XOR: return BIT_XOR_EXPR;
4528 case CPP_AND: return BIT_AND_EXPR;
4529 case CPP_OR: return BIT_IOR_EXPR;
4530 case CPP_LSHIFT: return LSHIFT_EXPR;
4531 case CPP_RSHIFT: return RSHIFT_EXPR;
4533 case CPP_EQ: return -NOP_EXPR;
4534 case CPP_PLUS_EQ: return -PLUS_EXPR;
4535 case CPP_MINUS_EQ: return -MINUS_EXPR;
4536 case CPP_MULT_EQ: return -MULT_EXPR;
4537 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4538 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4539 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4540 case CPP_AND_EQ: return -BIT_AND_EXPR;
4541 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4542 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4543 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4545 case CPP_EQ_EQ: return EQ_EXPR;
4546 case CPP_NOT_EQ: return NE_EXPR;
4547 case CPP_LESS: return LT_EXPR;
4548 case CPP_GREATER: return GT_EXPR;
4549 case CPP_LESS_EQ: return LE_EXPR;
4550 case CPP_GREATER_EQ: return GE_EXPR;
4552 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4553 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4555 case CPP_COMMA: return COMPOUND_EXPR;
4557 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4558 case CPP_DEREF_STAR: return MEMBER_REF;
4560 default: return ERROR_MARK;
4564 /* Returns true if CODE indicates a binary expression, which is not allowed in
4565 the LHS of a fold-expression. More codes will need to be added to use this
4566 function in other contexts. */
4568 static bool
4569 is_binary_op (tree_code code)
4571 switch (code)
4573 case PLUS_EXPR:
4574 case POINTER_PLUS_EXPR:
4575 case MINUS_EXPR:
4576 case MULT_EXPR:
4577 case TRUNC_DIV_EXPR:
4578 case TRUNC_MOD_EXPR:
4579 case BIT_XOR_EXPR:
4580 case BIT_AND_EXPR:
4581 case BIT_IOR_EXPR:
4582 case LSHIFT_EXPR:
4583 case RSHIFT_EXPR:
4585 case MODOP_EXPR:
4587 case EQ_EXPR:
4588 case NE_EXPR:
4589 case LE_EXPR:
4590 case GE_EXPR:
4591 case LT_EXPR:
4592 case GT_EXPR:
4594 case TRUTH_ANDIF_EXPR:
4595 case TRUTH_ORIF_EXPR:
4597 case COMPOUND_EXPR:
4599 case DOTSTAR_EXPR:
4600 case MEMBER_REF:
4601 return true;
4603 default:
4604 return false;
4608 /* If the next token is a suitable fold operator, consume it and return as
4609 the function above. */
4611 static int
4612 cp_parser_fold_operator (cp_parser *parser)
4614 cp_token* token = cp_lexer_peek_token (parser->lexer);
4615 int code = cp_parser_fold_operator (token);
4616 if (code != ERROR_MARK)
4617 cp_lexer_consume_token (parser->lexer);
4618 return code;
4621 /* Parse a fold-expression.
4623 fold-expression:
4624 ( ... folding-operator cast-expression)
4625 ( cast-expression folding-operator ... )
4626 ( cast-expression folding operator ... folding-operator cast-expression)
4628 Note that the '(' and ')' are matched in primary expression. */
4630 static cp_expr
4631 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4633 cp_id_kind pidk;
4635 // Left fold.
4636 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4638 cp_lexer_consume_token (parser->lexer);
4639 int op = cp_parser_fold_operator (parser);
4640 if (op == ERROR_MARK)
4642 cp_parser_error (parser, "expected binary operator");
4643 return error_mark_node;
4646 tree expr = cp_parser_cast_expression (parser, false, false,
4647 false, &pidk);
4648 if (expr == error_mark_node)
4649 return error_mark_node;
4650 return finish_left_unary_fold_expr (expr, op);
4653 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4654 int op = cp_parser_fold_operator (parser);
4655 if (op == ERROR_MARK)
4657 cp_parser_error (parser, "expected binary operator");
4658 return error_mark_node;
4661 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4663 cp_parser_error (parser, "expected ...");
4664 return error_mark_node;
4666 cp_lexer_consume_token (parser->lexer);
4668 /* The operands of a fold-expression are cast-expressions, so binary or
4669 conditional expressions are not allowed. We check this here to avoid
4670 tentative parsing. */
4671 if (is_binary_op (TREE_CODE (expr1)))
4672 error_at (location_of (expr1),
4673 "binary expression in operand of fold-expression");
4674 else if (TREE_CODE (expr1) == COND_EXPR)
4675 error_at (location_of (expr1),
4676 "conditional expression in operand of fold-expression");
4678 // Right fold.
4679 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4680 return finish_right_unary_fold_expr (expr1, op);
4682 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4684 cp_parser_error (parser, "mismatched operator in fold-expression");
4685 return error_mark_node;
4687 cp_lexer_consume_token (parser->lexer);
4689 // Binary left or right fold.
4690 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4691 if (expr2 == error_mark_node)
4692 return error_mark_node;
4693 return finish_binary_fold_expr (expr1, expr2, op);
4696 /* Parse a primary-expression.
4698 primary-expression:
4699 literal
4700 this
4701 ( expression )
4702 id-expression
4703 lambda-expression (C++11)
4705 GNU Extensions:
4707 primary-expression:
4708 ( compound-statement )
4709 __builtin_va_arg ( assignment-expression , type-id )
4710 __builtin_offsetof ( type-id , offsetof-expression )
4712 C++ Extensions:
4713 __has_nothrow_assign ( type-id )
4714 __has_nothrow_constructor ( type-id )
4715 __has_nothrow_copy ( type-id )
4716 __has_trivial_assign ( type-id )
4717 __has_trivial_constructor ( type-id )
4718 __has_trivial_copy ( type-id )
4719 __has_trivial_destructor ( type-id )
4720 __has_virtual_destructor ( type-id )
4721 __is_abstract ( type-id )
4722 __is_base_of ( type-id , type-id )
4723 __is_class ( type-id )
4724 __is_empty ( type-id )
4725 __is_enum ( type-id )
4726 __is_final ( type-id )
4727 __is_literal_type ( type-id )
4728 __is_pod ( type-id )
4729 __is_polymorphic ( type-id )
4730 __is_std_layout ( type-id )
4731 __is_trivial ( type-id )
4732 __is_union ( type-id )
4734 Objective-C++ Extension:
4736 primary-expression:
4737 objc-expression
4739 literal:
4740 __null
4742 ADDRESS_P is true iff this expression was immediately preceded by
4743 "&" and therefore might denote a pointer-to-member. CAST_P is true
4744 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4745 true iff this expression is a template argument.
4747 Returns a representation of the expression. Upon return, *IDK
4748 indicates what kind of id-expression (if any) was present. */
4750 static cp_expr
4751 cp_parser_primary_expression (cp_parser *parser,
4752 bool address_p,
4753 bool cast_p,
4754 bool template_arg_p,
4755 bool decltype_p,
4756 cp_id_kind *idk)
4758 cp_token *token = NULL;
4760 /* Assume the primary expression is not an id-expression. */
4761 *idk = CP_ID_KIND_NONE;
4763 /* Peek at the next token. */
4764 token = cp_lexer_peek_token (parser->lexer);
4765 switch ((int) token->type)
4767 /* literal:
4768 integer-literal
4769 character-literal
4770 floating-literal
4771 string-literal
4772 boolean-literal
4773 pointer-literal
4774 user-defined-literal */
4775 case CPP_CHAR:
4776 case CPP_CHAR16:
4777 case CPP_CHAR32:
4778 case CPP_WCHAR:
4779 case CPP_UTF8CHAR:
4780 case CPP_NUMBER:
4781 case CPP_PREPARSED_EXPR:
4782 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4783 return cp_parser_userdef_numeric_literal (parser);
4784 token = cp_lexer_consume_token (parser->lexer);
4785 if (TREE_CODE (token->u.value) == FIXED_CST)
4787 error_at (token->location,
4788 "fixed-point types not supported in C++");
4789 return error_mark_node;
4791 /* Floating-point literals are only allowed in an integral
4792 constant expression if they are cast to an integral or
4793 enumeration type. */
4794 if (TREE_CODE (token->u.value) == REAL_CST
4795 && parser->integral_constant_expression_p
4796 && pedantic)
4798 /* CAST_P will be set even in invalid code like "int(2.7 +
4799 ...)". Therefore, we have to check that the next token
4800 is sure to end the cast. */
4801 if (cast_p)
4803 cp_token *next_token;
4805 next_token = cp_lexer_peek_token (parser->lexer);
4806 if (/* The comma at the end of an
4807 enumerator-definition. */
4808 next_token->type != CPP_COMMA
4809 /* The curly brace at the end of an enum-specifier. */
4810 && next_token->type != CPP_CLOSE_BRACE
4811 /* The end of a statement. */
4812 && next_token->type != CPP_SEMICOLON
4813 /* The end of the cast-expression. */
4814 && next_token->type != CPP_CLOSE_PAREN
4815 /* The end of an array bound. */
4816 && next_token->type != CPP_CLOSE_SQUARE
4817 /* The closing ">" in a template-argument-list. */
4818 && (next_token->type != CPP_GREATER
4819 || parser->greater_than_is_operator_p)
4820 /* C++0x only: A ">>" treated like two ">" tokens,
4821 in a template-argument-list. */
4822 && (next_token->type != CPP_RSHIFT
4823 || (cxx_dialect == cxx98)
4824 || parser->greater_than_is_operator_p))
4825 cast_p = false;
4828 /* If we are within a cast, then the constraint that the
4829 cast is to an integral or enumeration type will be
4830 checked at that point. If we are not within a cast, then
4831 this code is invalid. */
4832 if (!cast_p)
4833 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4835 return cp_expr (token->u.value, token->location);
4837 case CPP_CHAR_USERDEF:
4838 case CPP_CHAR16_USERDEF:
4839 case CPP_CHAR32_USERDEF:
4840 case CPP_WCHAR_USERDEF:
4841 case CPP_UTF8CHAR_USERDEF:
4842 return cp_parser_userdef_char_literal (parser);
4844 case CPP_STRING:
4845 case CPP_STRING16:
4846 case CPP_STRING32:
4847 case CPP_WSTRING:
4848 case CPP_UTF8STRING:
4849 case CPP_STRING_USERDEF:
4850 case CPP_STRING16_USERDEF:
4851 case CPP_STRING32_USERDEF:
4852 case CPP_WSTRING_USERDEF:
4853 case CPP_UTF8STRING_USERDEF:
4854 /* ??? Should wide strings be allowed when parser->translate_strings_p
4855 is false (i.e. in attributes)? If not, we can kill the third
4856 argument to cp_parser_string_literal. */
4857 return cp_parser_string_literal (parser,
4858 parser->translate_strings_p,
4859 true);
4861 case CPP_OPEN_PAREN:
4862 /* If we see `( { ' then we are looking at the beginning of
4863 a GNU statement-expression. */
4864 if (cp_parser_allow_gnu_extensions_p (parser)
4865 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4867 /* Statement-expressions are not allowed by the standard. */
4868 pedwarn (token->location, OPT_Wpedantic,
4869 "ISO C++ forbids braced-groups within expressions");
4871 /* And they're not allowed outside of a function-body; you
4872 cannot, for example, write:
4874 int i = ({ int j = 3; j + 1; });
4876 at class or namespace scope. */
4877 if (!parser->in_function_body
4878 || parser->in_template_argument_list_p)
4880 error_at (token->location,
4881 "statement-expressions are not allowed outside "
4882 "functions nor in template-argument lists");
4883 cp_parser_skip_to_end_of_block_or_statement (parser);
4884 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4885 cp_lexer_consume_token (parser->lexer);
4886 return error_mark_node;
4888 else
4889 return cp_parser_statement_expr (parser);
4891 /* Otherwise it's a normal parenthesized expression. */
4893 cp_expr expr;
4894 bool saved_greater_than_is_operator_p;
4896 location_t open_paren_loc = token->location;
4898 /* Consume the `('. */
4899 cp_lexer_consume_token (parser->lexer);
4900 /* Within a parenthesized expression, a `>' token is always
4901 the greater-than operator. */
4902 saved_greater_than_is_operator_p
4903 = parser->greater_than_is_operator_p;
4904 parser->greater_than_is_operator_p = true;
4906 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4907 /* Left fold expression. */
4908 expr = NULL_TREE;
4909 else
4910 /* Parse the parenthesized expression. */
4911 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4913 token = cp_lexer_peek_token (parser->lexer);
4914 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
4916 expr = cp_parser_fold_expression (parser, expr);
4917 if (expr != error_mark_node
4918 && cxx_dialect < cxx1z
4919 && !in_system_header_at (input_location))
4920 pedwarn (input_location, 0, "fold-expressions only available "
4921 "with -std=c++1z or -std=gnu++1z");
4923 else
4924 /* Let the front end know that this expression was
4925 enclosed in parentheses. This matters in case, for
4926 example, the expression is of the form `A::B', since
4927 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4928 not. */
4929 expr = finish_parenthesized_expr (expr);
4931 /* DR 705: Wrapping an unqualified name in parentheses
4932 suppresses arg-dependent lookup. We want to pass back
4933 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4934 (c++/37862), but none of the others. */
4935 if (*idk != CP_ID_KIND_QUALIFIED)
4936 *idk = CP_ID_KIND_NONE;
4938 /* The `>' token might be the end of a template-id or
4939 template-parameter-list now. */
4940 parser->greater_than_is_operator_p
4941 = saved_greater_than_is_operator_p;
4943 /* Consume the `)'. */
4944 token = cp_lexer_peek_token (parser->lexer);
4945 location_t close_paren_loc = token->location;
4946 expr.set_range (open_paren_loc, close_paren_loc);
4947 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)
4948 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4949 cp_parser_skip_to_end_of_statement (parser);
4951 return expr;
4954 case CPP_OPEN_SQUARE:
4956 if (c_dialect_objc ())
4958 /* We might have an Objective-C++ message. */
4959 cp_parser_parse_tentatively (parser);
4960 tree msg = cp_parser_objc_message_expression (parser);
4961 /* If that works out, we're done ... */
4962 if (cp_parser_parse_definitely (parser))
4963 return msg;
4964 /* ... else, fall though to see if it's a lambda. */
4966 cp_expr lam = cp_parser_lambda_expression (parser);
4967 /* Don't warn about a failed tentative parse. */
4968 if (cp_parser_error_occurred (parser))
4969 return error_mark_node;
4970 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4971 return lam;
4974 case CPP_OBJC_STRING:
4975 if (c_dialect_objc ())
4976 /* We have an Objective-C++ string literal. */
4977 return cp_parser_objc_expression (parser);
4978 cp_parser_error (parser, "expected primary-expression");
4979 return error_mark_node;
4981 case CPP_KEYWORD:
4982 switch (token->keyword)
4984 /* These two are the boolean literals. */
4985 case RID_TRUE:
4986 cp_lexer_consume_token (parser->lexer);
4987 return cp_expr (boolean_true_node, token->location);
4988 case RID_FALSE:
4989 cp_lexer_consume_token (parser->lexer);
4990 return cp_expr (boolean_false_node, token->location);
4992 /* The `__null' literal. */
4993 case RID_NULL:
4994 cp_lexer_consume_token (parser->lexer);
4995 return cp_expr (null_node, token->location);
4997 /* The `nullptr' literal. */
4998 case RID_NULLPTR:
4999 cp_lexer_consume_token (parser->lexer);
5000 return cp_expr (nullptr_node, token->location);
5002 /* Recognize the `this' keyword. */
5003 case RID_THIS:
5004 cp_lexer_consume_token (parser->lexer);
5005 if (parser->local_variables_forbidden_p)
5007 error_at (token->location,
5008 "%<this%> may not be used in this context");
5009 return error_mark_node;
5011 /* Pointers cannot appear in constant-expressions. */
5012 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5013 return error_mark_node;
5014 return cp_expr (finish_this_expr (), token->location);
5016 /* The `operator' keyword can be the beginning of an
5017 id-expression. */
5018 case RID_OPERATOR:
5019 goto id_expression;
5021 case RID_FUNCTION_NAME:
5022 case RID_PRETTY_FUNCTION_NAME:
5023 case RID_C99_FUNCTION_NAME:
5025 non_integral_constant name;
5027 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5028 __func__ are the names of variables -- but they are
5029 treated specially. Therefore, they are handled here,
5030 rather than relying on the generic id-expression logic
5031 below. Grammatically, these names are id-expressions.
5033 Consume the token. */
5034 token = cp_lexer_consume_token (parser->lexer);
5036 switch (token->keyword)
5038 case RID_FUNCTION_NAME:
5039 name = NIC_FUNC_NAME;
5040 break;
5041 case RID_PRETTY_FUNCTION_NAME:
5042 name = NIC_PRETTY_FUNC;
5043 break;
5044 case RID_C99_FUNCTION_NAME:
5045 name = NIC_C99_FUNC;
5046 break;
5047 default:
5048 gcc_unreachable ();
5051 if (cp_parser_non_integral_constant_expression (parser, name))
5052 return error_mark_node;
5054 /* Look up the name. */
5055 return finish_fname (token->u.value);
5058 case RID_VA_ARG:
5060 tree expression;
5061 tree type;
5062 source_location type_location;
5063 location_t start_loc
5064 = cp_lexer_peek_token (parser->lexer)->location;
5065 /* The `__builtin_va_arg' construct is used to handle
5066 `va_arg'. Consume the `__builtin_va_arg' token. */
5067 cp_lexer_consume_token (parser->lexer);
5068 /* Look for the opening `('. */
5069 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5070 /* Now, parse the assignment-expression. */
5071 expression = cp_parser_assignment_expression (parser);
5072 /* Look for the `,'. */
5073 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5074 type_location = cp_lexer_peek_token (parser->lexer)->location;
5075 /* Parse the type-id. */
5077 type_id_in_expr_sentinel s (parser);
5078 type = cp_parser_type_id (parser);
5080 /* Look for the closing `)'. */
5081 location_t finish_loc
5082 = cp_lexer_peek_token (parser->lexer)->location;
5083 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5084 /* Using `va_arg' in a constant-expression is not
5085 allowed. */
5086 if (cp_parser_non_integral_constant_expression (parser,
5087 NIC_VA_ARG))
5088 return error_mark_node;
5089 /* Construct a location of the form:
5090 __builtin_va_arg (v, int)
5091 ~~~~~~~~~~~~~~~~~~~~~^~~~
5092 with the caret at the type, ranging from the start of the
5093 "__builtin_va_arg" token to the close paren. */
5094 location_t combined_loc
5095 = make_location (type_location, start_loc, finish_loc);
5096 return build_x_va_arg (combined_loc, expression, type);
5099 case RID_OFFSETOF:
5100 return cp_parser_builtin_offsetof (parser);
5102 case RID_HAS_NOTHROW_ASSIGN:
5103 case RID_HAS_NOTHROW_CONSTRUCTOR:
5104 case RID_HAS_NOTHROW_COPY:
5105 case RID_HAS_TRIVIAL_ASSIGN:
5106 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5107 case RID_HAS_TRIVIAL_COPY:
5108 case RID_HAS_TRIVIAL_DESTRUCTOR:
5109 case RID_HAS_VIRTUAL_DESTRUCTOR:
5110 case RID_IS_ABSTRACT:
5111 case RID_IS_BASE_OF:
5112 case RID_IS_CLASS:
5113 case RID_IS_EMPTY:
5114 case RID_IS_ENUM:
5115 case RID_IS_FINAL:
5116 case RID_IS_LITERAL_TYPE:
5117 case RID_IS_POD:
5118 case RID_IS_POLYMORPHIC:
5119 case RID_IS_SAME_AS:
5120 case RID_IS_STD_LAYOUT:
5121 case RID_IS_TRIVIAL:
5122 case RID_IS_TRIVIALLY_ASSIGNABLE:
5123 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5124 case RID_IS_TRIVIALLY_COPYABLE:
5125 case RID_IS_UNION:
5126 return cp_parser_trait_expr (parser, token->keyword);
5128 // C++ concepts
5129 case RID_REQUIRES:
5130 return cp_parser_requires_expression (parser);
5132 /* Objective-C++ expressions. */
5133 case RID_AT_ENCODE:
5134 case RID_AT_PROTOCOL:
5135 case RID_AT_SELECTOR:
5136 return cp_parser_objc_expression (parser);
5138 case RID_TEMPLATE:
5139 if (parser->in_function_body
5140 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5141 == CPP_LESS))
5143 error_at (token->location,
5144 "a template declaration cannot appear at block scope");
5145 cp_parser_skip_to_end_of_block_or_statement (parser);
5146 return error_mark_node;
5148 /* FALLTHRU */
5149 default:
5150 cp_parser_error (parser, "expected primary-expression");
5151 return error_mark_node;
5154 /* An id-expression can start with either an identifier, a
5155 `::' as the beginning of a qualified-id, or the "operator"
5156 keyword. */
5157 case CPP_NAME:
5158 case CPP_SCOPE:
5159 case CPP_TEMPLATE_ID:
5160 case CPP_NESTED_NAME_SPECIFIER:
5162 id_expression:
5163 cp_expr id_expression;
5164 cp_expr decl;
5165 const char *error_msg;
5166 bool template_p;
5167 bool done;
5168 cp_token *id_expr_token;
5170 /* Parse the id-expression. */
5171 id_expression
5172 = cp_parser_id_expression (parser,
5173 /*template_keyword_p=*/false,
5174 /*check_dependency_p=*/true,
5175 &template_p,
5176 /*declarator_p=*/false,
5177 /*optional_p=*/false);
5178 if (id_expression == error_mark_node)
5179 return error_mark_node;
5180 id_expr_token = token;
5181 token = cp_lexer_peek_token (parser->lexer);
5182 done = (token->type != CPP_OPEN_SQUARE
5183 && token->type != CPP_OPEN_PAREN
5184 && token->type != CPP_DOT
5185 && token->type != CPP_DEREF
5186 && token->type != CPP_PLUS_PLUS
5187 && token->type != CPP_MINUS_MINUS);
5188 /* If we have a template-id, then no further lookup is
5189 required. If the template-id was for a template-class, we
5190 will sometimes have a TYPE_DECL at this point. */
5191 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5192 || TREE_CODE (id_expression) == TYPE_DECL)
5193 decl = id_expression;
5194 /* Look up the name. */
5195 else
5197 tree ambiguous_decls;
5199 /* If we already know that this lookup is ambiguous, then
5200 we've already issued an error message; there's no reason
5201 to check again. */
5202 if (id_expr_token->type == CPP_NAME
5203 && id_expr_token->error_reported)
5205 cp_parser_simulate_error (parser);
5206 return error_mark_node;
5209 decl = cp_parser_lookup_name (parser, id_expression,
5210 none_type,
5211 template_p,
5212 /*is_namespace=*/false,
5213 /*check_dependency=*/true,
5214 &ambiguous_decls,
5215 id_expr_token->location);
5216 /* If the lookup was ambiguous, an error will already have
5217 been issued. */
5218 if (ambiguous_decls)
5219 return error_mark_node;
5221 /* In Objective-C++, we may have an Objective-C 2.0
5222 dot-syntax for classes here. */
5223 if (c_dialect_objc ()
5224 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5225 && TREE_CODE (decl) == TYPE_DECL
5226 && objc_is_class_name (decl))
5228 tree component;
5229 cp_lexer_consume_token (parser->lexer);
5230 component = cp_parser_identifier (parser);
5231 if (component == error_mark_node)
5232 return error_mark_node;
5234 tree result = objc_build_class_component_ref (id_expression,
5235 component);
5236 /* Build a location of the form:
5237 expr.component
5238 ~~~~~^~~~~~~~~
5239 with caret at the start of the component name (at
5240 input_location), ranging from the start of the id_expression
5241 to the end of the component name. */
5242 location_t combined_loc
5243 = make_location (input_location, id_expression.get_start (),
5244 get_finish (input_location));
5245 protected_set_expr_location (result, combined_loc);
5246 return result;
5249 /* In Objective-C++, an instance variable (ivar) may be preferred
5250 to whatever cp_parser_lookup_name() found.
5251 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5252 rest of c-family, we have to do a little extra work to preserve
5253 any location information in cp_expr "decl". Given that
5254 objc_lookup_ivar is implemented in "c-family" and "objc", we
5255 have a trip through the pure "tree" type, rather than cp_expr.
5256 Naively copying it back to "decl" would implicitly give the
5257 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5258 store an EXPR_LOCATION. Hence we only update "decl" (and
5259 hence its location_t) if we get back a different tree node. */
5260 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5261 id_expression);
5262 if (decl_tree != decl.get_value ())
5263 decl = cp_expr (decl_tree);
5265 /* If name lookup gives us a SCOPE_REF, then the
5266 qualifying scope was dependent. */
5267 if (TREE_CODE (decl) == SCOPE_REF)
5269 /* At this point, we do not know if DECL is a valid
5270 integral constant expression. We assume that it is
5271 in fact such an expression, so that code like:
5273 template <int N> struct A {
5274 int a[B<N>::i];
5277 is accepted. At template-instantiation time, we
5278 will check that B<N>::i is actually a constant. */
5279 return decl;
5281 /* Check to see if DECL is a local variable in a context
5282 where that is forbidden. */
5283 if (parser->local_variables_forbidden_p
5284 && local_variable_p (decl))
5286 /* It might be that we only found DECL because we are
5287 trying to be generous with pre-ISO scoping rules.
5288 For example, consider:
5290 int i;
5291 void g() {
5292 for (int i = 0; i < 10; ++i) {}
5293 extern void f(int j = i);
5296 Here, name look up will originally find the out
5297 of scope `i'. We need to issue a warning message,
5298 but then use the global `i'. */
5299 decl = check_for_out_of_scope_variable (decl);
5300 if (local_variable_p (decl))
5302 error_at (id_expr_token->location,
5303 "local variable %qD may not appear in this context",
5304 decl.get_value ());
5305 return error_mark_node;
5310 decl = (finish_id_expression
5311 (id_expression, decl, parser->scope,
5312 idk,
5313 parser->integral_constant_expression_p,
5314 parser->allow_non_integral_constant_expression_p,
5315 &parser->non_integral_constant_expression_p,
5316 template_p, done, address_p,
5317 template_arg_p,
5318 &error_msg,
5319 id_expr_token->location));
5320 if (error_msg)
5321 cp_parser_error (parser, error_msg);
5322 decl.set_location (id_expr_token->location);
5323 return decl;
5326 /* Anything else is an error. */
5327 default:
5328 cp_parser_error (parser, "expected primary-expression");
5329 return error_mark_node;
5333 static inline cp_expr
5334 cp_parser_primary_expression (cp_parser *parser,
5335 bool address_p,
5336 bool cast_p,
5337 bool template_arg_p,
5338 cp_id_kind *idk)
5340 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5341 /*decltype*/false, idk);
5344 /* Parse an id-expression.
5346 id-expression:
5347 unqualified-id
5348 qualified-id
5350 qualified-id:
5351 :: [opt] nested-name-specifier template [opt] unqualified-id
5352 :: identifier
5353 :: operator-function-id
5354 :: template-id
5356 Return a representation of the unqualified portion of the
5357 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5358 a `::' or nested-name-specifier.
5360 Often, if the id-expression was a qualified-id, the caller will
5361 want to make a SCOPE_REF to represent the qualified-id. This
5362 function does not do this in order to avoid wastefully creating
5363 SCOPE_REFs when they are not required.
5365 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5366 `template' keyword.
5368 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5369 uninstantiated templates.
5371 If *TEMPLATE_P is non-NULL, it is set to true iff the
5372 `template' keyword is used to explicitly indicate that the entity
5373 named is a template.
5375 If DECLARATOR_P is true, the id-expression is appearing as part of
5376 a declarator, rather than as part of an expression. */
5378 static cp_expr
5379 cp_parser_id_expression (cp_parser *parser,
5380 bool template_keyword_p,
5381 bool check_dependency_p,
5382 bool *template_p,
5383 bool declarator_p,
5384 bool optional_p)
5386 bool global_scope_p;
5387 bool nested_name_specifier_p;
5389 /* Assume the `template' keyword was not used. */
5390 if (template_p)
5391 *template_p = template_keyword_p;
5393 /* Look for the optional `::' operator. */
5394 global_scope_p
5395 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
5396 != NULL_TREE);
5397 /* Look for the optional nested-name-specifier. */
5398 nested_name_specifier_p
5399 = (cp_parser_nested_name_specifier_opt (parser,
5400 /*typename_keyword_p=*/false,
5401 check_dependency_p,
5402 /*type_p=*/false,
5403 declarator_p)
5404 != NULL_TREE);
5405 /* If there is a nested-name-specifier, then we are looking at
5406 the first qualified-id production. */
5407 if (nested_name_specifier_p)
5409 tree saved_scope;
5410 tree saved_object_scope;
5411 tree saved_qualifying_scope;
5412 tree unqualified_id;
5413 bool is_template;
5415 /* See if the next token is the `template' keyword. */
5416 if (!template_p)
5417 template_p = &is_template;
5418 *template_p = cp_parser_optional_template_keyword (parser);
5419 /* Name lookup we do during the processing of the
5420 unqualified-id might obliterate SCOPE. */
5421 saved_scope = parser->scope;
5422 saved_object_scope = parser->object_scope;
5423 saved_qualifying_scope = parser->qualifying_scope;
5424 /* Process the final unqualified-id. */
5425 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5426 check_dependency_p,
5427 declarator_p,
5428 /*optional_p=*/false);
5429 /* Restore the SAVED_SCOPE for our caller. */
5430 parser->scope = saved_scope;
5431 parser->object_scope = saved_object_scope;
5432 parser->qualifying_scope = saved_qualifying_scope;
5434 return unqualified_id;
5436 /* Otherwise, if we are in global scope, then we are looking at one
5437 of the other qualified-id productions. */
5438 else if (global_scope_p)
5440 cp_token *token;
5441 tree id;
5443 /* Peek at the next token. */
5444 token = cp_lexer_peek_token (parser->lexer);
5446 /* If it's an identifier, and the next token is not a "<", then
5447 we can avoid the template-id case. This is an optimization
5448 for this common case. */
5449 if (token->type == CPP_NAME
5450 && !cp_parser_nth_token_starts_template_argument_list_p
5451 (parser, 2))
5452 return cp_parser_identifier (parser);
5454 cp_parser_parse_tentatively (parser);
5455 /* Try a template-id. */
5456 id = cp_parser_template_id (parser,
5457 /*template_keyword_p=*/false,
5458 /*check_dependency_p=*/true,
5459 none_type,
5460 declarator_p);
5461 /* If that worked, we're done. */
5462 if (cp_parser_parse_definitely (parser))
5463 return id;
5465 /* Peek at the next token. (Changes in the token buffer may
5466 have invalidated the pointer obtained above.) */
5467 token = cp_lexer_peek_token (parser->lexer);
5469 switch (token->type)
5471 case CPP_NAME:
5472 return cp_parser_identifier (parser);
5474 case CPP_KEYWORD:
5475 if (token->keyword == RID_OPERATOR)
5476 return cp_parser_operator_function_id (parser);
5477 /* Fall through. */
5479 default:
5480 cp_parser_error (parser, "expected id-expression");
5481 return error_mark_node;
5484 else
5485 return cp_parser_unqualified_id (parser, template_keyword_p,
5486 /*check_dependency_p=*/true,
5487 declarator_p,
5488 optional_p);
5491 /* Parse an unqualified-id.
5493 unqualified-id:
5494 identifier
5495 operator-function-id
5496 conversion-function-id
5497 ~ class-name
5498 template-id
5500 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5501 keyword, in a construct like `A::template ...'.
5503 Returns a representation of unqualified-id. For the `identifier'
5504 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5505 production a BIT_NOT_EXPR is returned; the operand of the
5506 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5507 other productions, see the documentation accompanying the
5508 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5509 names are looked up in uninstantiated templates. If DECLARATOR_P
5510 is true, the unqualified-id is appearing as part of a declarator,
5511 rather than as part of an expression. */
5513 static cp_expr
5514 cp_parser_unqualified_id (cp_parser* parser,
5515 bool template_keyword_p,
5516 bool check_dependency_p,
5517 bool declarator_p,
5518 bool optional_p)
5520 cp_token *token;
5522 /* Peek at the next token. */
5523 token = cp_lexer_peek_token (parser->lexer);
5525 switch ((int) token->type)
5527 case CPP_NAME:
5529 tree id;
5531 /* We don't know yet whether or not this will be a
5532 template-id. */
5533 cp_parser_parse_tentatively (parser);
5534 /* Try a template-id. */
5535 id = cp_parser_template_id (parser, template_keyword_p,
5536 check_dependency_p,
5537 none_type,
5538 declarator_p);
5539 /* If it worked, we're done. */
5540 if (cp_parser_parse_definitely (parser))
5541 return id;
5542 /* Otherwise, it's an ordinary identifier. */
5543 return cp_parser_identifier (parser);
5546 case CPP_TEMPLATE_ID:
5547 return cp_parser_template_id (parser, template_keyword_p,
5548 check_dependency_p,
5549 none_type,
5550 declarator_p);
5552 case CPP_COMPL:
5554 tree type_decl;
5555 tree qualifying_scope;
5556 tree object_scope;
5557 tree scope;
5558 bool done;
5560 /* Consume the `~' token. */
5561 cp_lexer_consume_token (parser->lexer);
5562 /* Parse the class-name. The standard, as written, seems to
5563 say that:
5565 template <typename T> struct S { ~S (); };
5566 template <typename T> S<T>::~S() {}
5568 is invalid, since `~' must be followed by a class-name, but
5569 `S<T>' is dependent, and so not known to be a class.
5570 That's not right; we need to look in uninstantiated
5571 templates. A further complication arises from:
5573 template <typename T> void f(T t) {
5574 t.T::~T();
5577 Here, it is not possible to look up `T' in the scope of `T'
5578 itself. We must look in both the current scope, and the
5579 scope of the containing complete expression.
5581 Yet another issue is:
5583 struct S {
5584 int S;
5585 ~S();
5588 S::~S() {}
5590 The standard does not seem to say that the `S' in `~S'
5591 should refer to the type `S' and not the data member
5592 `S::S'. */
5594 /* DR 244 says that we look up the name after the "~" in the
5595 same scope as we looked up the qualifying name. That idea
5596 isn't fully worked out; it's more complicated than that. */
5597 scope = parser->scope;
5598 object_scope = parser->object_scope;
5599 qualifying_scope = parser->qualifying_scope;
5601 /* Check for invalid scopes. */
5602 if (scope == error_mark_node)
5604 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5605 cp_lexer_consume_token (parser->lexer);
5606 return error_mark_node;
5608 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5610 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5611 error_at (token->location,
5612 "scope %qT before %<~%> is not a class-name",
5613 scope);
5614 cp_parser_simulate_error (parser);
5615 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5616 cp_lexer_consume_token (parser->lexer);
5617 return error_mark_node;
5619 gcc_assert (!scope || TYPE_P (scope));
5621 /* If the name is of the form "X::~X" it's OK even if X is a
5622 typedef. */
5623 token = cp_lexer_peek_token (parser->lexer);
5624 if (scope
5625 && token->type == CPP_NAME
5626 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5627 != CPP_LESS)
5628 && (token->u.value == TYPE_IDENTIFIER (scope)
5629 || (CLASS_TYPE_P (scope)
5630 && constructor_name_p (token->u.value, scope))))
5632 cp_lexer_consume_token (parser->lexer);
5633 return build_nt (BIT_NOT_EXPR, scope);
5636 /* ~auto means the destructor of whatever the object is. */
5637 if (cp_parser_is_keyword (token, RID_AUTO))
5639 if (cxx_dialect < cxx14)
5640 pedwarn (input_location, 0,
5641 "%<~auto%> only available with "
5642 "-std=c++14 or -std=gnu++14");
5643 cp_lexer_consume_token (parser->lexer);
5644 return build_nt (BIT_NOT_EXPR, make_auto ());
5647 /* If there was an explicit qualification (S::~T), first look
5648 in the scope given by the qualification (i.e., S).
5650 Note: in the calls to cp_parser_class_name below we pass
5651 typename_type so that lookup finds the injected-class-name
5652 rather than the constructor. */
5653 done = false;
5654 type_decl = NULL_TREE;
5655 if (scope)
5657 cp_parser_parse_tentatively (parser);
5658 type_decl = cp_parser_class_name (parser,
5659 /*typename_keyword_p=*/false,
5660 /*template_keyword_p=*/false,
5661 typename_type,
5662 /*check_dependency=*/false,
5663 /*class_head_p=*/false,
5664 declarator_p);
5665 if (cp_parser_parse_definitely (parser))
5666 done = true;
5668 /* In "N::S::~S", look in "N" as well. */
5669 if (!done && scope && qualifying_scope)
5671 cp_parser_parse_tentatively (parser);
5672 parser->scope = qualifying_scope;
5673 parser->object_scope = NULL_TREE;
5674 parser->qualifying_scope = NULL_TREE;
5675 type_decl
5676 = cp_parser_class_name (parser,
5677 /*typename_keyword_p=*/false,
5678 /*template_keyword_p=*/false,
5679 typename_type,
5680 /*check_dependency=*/false,
5681 /*class_head_p=*/false,
5682 declarator_p);
5683 if (cp_parser_parse_definitely (parser))
5684 done = true;
5686 /* In "p->S::~T", look in the scope given by "*p" as well. */
5687 else if (!done && object_scope)
5689 cp_parser_parse_tentatively (parser);
5690 parser->scope = object_scope;
5691 parser->object_scope = NULL_TREE;
5692 parser->qualifying_scope = NULL_TREE;
5693 type_decl
5694 = cp_parser_class_name (parser,
5695 /*typename_keyword_p=*/false,
5696 /*template_keyword_p=*/false,
5697 typename_type,
5698 /*check_dependency=*/false,
5699 /*class_head_p=*/false,
5700 declarator_p);
5701 if (cp_parser_parse_definitely (parser))
5702 done = true;
5704 /* Look in the surrounding context. */
5705 if (!done)
5707 parser->scope = NULL_TREE;
5708 parser->object_scope = NULL_TREE;
5709 parser->qualifying_scope = NULL_TREE;
5710 if (processing_template_decl)
5711 cp_parser_parse_tentatively (parser);
5712 type_decl
5713 = cp_parser_class_name (parser,
5714 /*typename_keyword_p=*/false,
5715 /*template_keyword_p=*/false,
5716 typename_type,
5717 /*check_dependency=*/false,
5718 /*class_head_p=*/false,
5719 declarator_p);
5720 if (processing_template_decl
5721 && ! cp_parser_parse_definitely (parser))
5723 /* We couldn't find a type with this name. If we're parsing
5724 tentatively, fail and try something else. */
5725 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5727 cp_parser_simulate_error (parser);
5728 return error_mark_node;
5730 /* Otherwise, accept it and check for a match at instantiation
5731 time. */
5732 type_decl = cp_parser_identifier (parser);
5733 if (type_decl != error_mark_node)
5734 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5735 return type_decl;
5738 /* If an error occurred, assume that the name of the
5739 destructor is the same as the name of the qualifying
5740 class. That allows us to keep parsing after running
5741 into ill-formed destructor names. */
5742 if (type_decl == error_mark_node && scope)
5743 return build_nt (BIT_NOT_EXPR, scope);
5744 else if (type_decl == error_mark_node)
5745 return error_mark_node;
5747 /* Check that destructor name and scope match. */
5748 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5750 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5751 error_at (token->location,
5752 "declaration of %<~%T%> as member of %qT",
5753 type_decl, scope);
5754 cp_parser_simulate_error (parser);
5755 return error_mark_node;
5758 /* [class.dtor]
5760 A typedef-name that names a class shall not be used as the
5761 identifier in the declarator for a destructor declaration. */
5762 if (declarator_p
5763 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5764 && !DECL_SELF_REFERENCE_P (type_decl)
5765 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5766 error_at (token->location,
5767 "typedef-name %qD used as destructor declarator",
5768 type_decl);
5770 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5773 case CPP_KEYWORD:
5774 if (token->keyword == RID_OPERATOR)
5776 cp_expr id;
5778 /* This could be a template-id, so we try that first. */
5779 cp_parser_parse_tentatively (parser);
5780 /* Try a template-id. */
5781 id = cp_parser_template_id (parser, template_keyword_p,
5782 /*check_dependency_p=*/true,
5783 none_type,
5784 declarator_p);
5785 /* If that worked, we're done. */
5786 if (cp_parser_parse_definitely (parser))
5787 return id;
5788 /* We still don't know whether we're looking at an
5789 operator-function-id or a conversion-function-id. */
5790 cp_parser_parse_tentatively (parser);
5791 /* Try an operator-function-id. */
5792 id = cp_parser_operator_function_id (parser);
5793 /* If that didn't work, try a conversion-function-id. */
5794 if (!cp_parser_parse_definitely (parser))
5795 id = cp_parser_conversion_function_id (parser);
5796 else if (UDLIT_OPER_P (id))
5798 /* 17.6.3.3.5 */
5799 const char *name = UDLIT_OP_SUFFIX (id);
5800 if (name[0] != '_' && !in_system_header_at (input_location)
5801 && declarator_p)
5802 warning (0, "literal operator suffixes not preceded by %<_%>"
5803 " are reserved for future standardization");
5806 return id;
5808 /* Fall through. */
5810 default:
5811 if (optional_p)
5812 return NULL_TREE;
5813 cp_parser_error (parser, "expected unqualified-id");
5814 return error_mark_node;
5818 /* Parse an (optional) nested-name-specifier.
5820 nested-name-specifier: [C++98]
5821 class-or-namespace-name :: nested-name-specifier [opt]
5822 class-or-namespace-name :: template nested-name-specifier [opt]
5824 nested-name-specifier: [C++0x]
5825 type-name ::
5826 namespace-name ::
5827 nested-name-specifier identifier ::
5828 nested-name-specifier template [opt] simple-template-id ::
5830 PARSER->SCOPE should be set appropriately before this function is
5831 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5832 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5833 in name lookups.
5835 Sets PARSER->SCOPE to the class (TYPE) or namespace
5836 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5837 it unchanged if there is no nested-name-specifier. Returns the new
5838 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5840 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5841 part of a declaration and/or decl-specifier. */
5843 static tree
5844 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5845 bool typename_keyword_p,
5846 bool check_dependency_p,
5847 bool type_p,
5848 bool is_declaration)
5850 bool success = false;
5851 cp_token_position start = 0;
5852 cp_token *token;
5854 /* Remember where the nested-name-specifier starts. */
5855 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5857 start = cp_lexer_token_position (parser->lexer, false);
5858 push_deferring_access_checks (dk_deferred);
5861 while (true)
5863 tree new_scope;
5864 tree old_scope;
5865 tree saved_qualifying_scope;
5866 bool template_keyword_p;
5868 /* Spot cases that cannot be the beginning of a
5869 nested-name-specifier. */
5870 token = cp_lexer_peek_token (parser->lexer);
5872 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5873 the already parsed nested-name-specifier. */
5874 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5876 /* Grab the nested-name-specifier and continue the loop. */
5877 cp_parser_pre_parsed_nested_name_specifier (parser);
5878 /* If we originally encountered this nested-name-specifier
5879 with IS_DECLARATION set to false, we will not have
5880 resolved TYPENAME_TYPEs, so we must do so here. */
5881 if (is_declaration
5882 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5884 new_scope = resolve_typename_type (parser->scope,
5885 /*only_current_p=*/false);
5886 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5887 parser->scope = new_scope;
5889 success = true;
5890 continue;
5893 /* Spot cases that cannot be the beginning of a
5894 nested-name-specifier. On the second and subsequent times
5895 through the loop, we look for the `template' keyword. */
5896 if (success && token->keyword == RID_TEMPLATE)
5898 /* A template-id can start a nested-name-specifier. */
5899 else if (token->type == CPP_TEMPLATE_ID)
5901 /* DR 743: decltype can be used in a nested-name-specifier. */
5902 else if (token_is_decltype (token))
5904 else
5906 /* If the next token is not an identifier, then it is
5907 definitely not a type-name or namespace-name. */
5908 if (token->type != CPP_NAME)
5909 break;
5910 /* If the following token is neither a `<' (to begin a
5911 template-id), nor a `::', then we are not looking at a
5912 nested-name-specifier. */
5913 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5915 if (token->type == CPP_COLON
5916 && parser->colon_corrects_to_scope_p
5917 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5919 error_at (token->location,
5920 "found %<:%> in nested-name-specifier, expected %<::%>");
5921 token->type = CPP_SCOPE;
5924 if (token->type != CPP_SCOPE
5925 && !cp_parser_nth_token_starts_template_argument_list_p
5926 (parser, 2))
5927 break;
5930 /* The nested-name-specifier is optional, so we parse
5931 tentatively. */
5932 cp_parser_parse_tentatively (parser);
5934 /* Look for the optional `template' keyword, if this isn't the
5935 first time through the loop. */
5936 if (success)
5937 template_keyword_p = cp_parser_optional_template_keyword (parser);
5938 else
5939 template_keyword_p = false;
5941 /* Save the old scope since the name lookup we are about to do
5942 might destroy it. */
5943 old_scope = parser->scope;
5944 saved_qualifying_scope = parser->qualifying_scope;
5945 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5946 look up names in "X<T>::I" in order to determine that "Y" is
5947 a template. So, if we have a typename at this point, we make
5948 an effort to look through it. */
5949 if (is_declaration
5950 && !typename_keyword_p
5951 && parser->scope
5952 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5953 parser->scope = resolve_typename_type (parser->scope,
5954 /*only_current_p=*/false);
5955 /* Parse the qualifying entity. */
5956 new_scope
5957 = cp_parser_qualifying_entity (parser,
5958 typename_keyword_p,
5959 template_keyword_p,
5960 check_dependency_p,
5961 type_p,
5962 is_declaration);
5963 /* Look for the `::' token. */
5964 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5966 /* If we found what we wanted, we keep going; otherwise, we're
5967 done. */
5968 if (!cp_parser_parse_definitely (parser))
5970 bool error_p = false;
5972 /* Restore the OLD_SCOPE since it was valid before the
5973 failed attempt at finding the last
5974 class-or-namespace-name. */
5975 parser->scope = old_scope;
5976 parser->qualifying_scope = saved_qualifying_scope;
5978 /* If the next token is a decltype, and the one after that is a
5979 `::', then the decltype has failed to resolve to a class or
5980 enumeration type. Give this error even when parsing
5981 tentatively since it can't possibly be valid--and we're going
5982 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5983 won't get another chance.*/
5984 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5985 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5986 == CPP_SCOPE))
5988 token = cp_lexer_consume_token (parser->lexer);
5989 error_at (token->location, "decltype evaluates to %qT, "
5990 "which is not a class or enumeration type",
5991 token->u.tree_check_value->value);
5992 parser->scope = error_mark_node;
5993 error_p = true;
5994 /* As below. */
5995 success = true;
5996 cp_lexer_consume_token (parser->lexer);
5999 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6000 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6002 /* If we have a non-type template-id followed by ::, it can't
6003 possibly be valid. */
6004 token = cp_lexer_peek_token (parser->lexer);
6005 tree tid = token->u.tree_check_value->value;
6006 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6007 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6009 tree tmpl = NULL_TREE;
6010 if (is_overloaded_fn (tid))
6012 tree fns = get_fns (tid);
6013 if (!OVL_CHAIN (fns))
6014 tmpl = OVL_CURRENT (fns);
6015 error_at (token->location, "function template-id %qD "
6016 "in nested-name-specifier", tid);
6018 else
6020 /* Variable template. */
6021 tmpl = TREE_OPERAND (tid, 0);
6022 gcc_assert (variable_template_p (tmpl));
6023 error_at (token->location, "variable template-id %qD "
6024 "in nested-name-specifier", tid);
6026 if (tmpl)
6027 inform (DECL_SOURCE_LOCATION (tmpl),
6028 "%qD declared here", tmpl);
6030 parser->scope = error_mark_node;
6031 error_p = true;
6032 /* As below. */
6033 success = true;
6034 cp_lexer_consume_token (parser->lexer);
6035 cp_lexer_consume_token (parser->lexer);
6039 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6040 break;
6041 /* If the next token is an identifier, and the one after
6042 that is a `::', then any valid interpretation would have
6043 found a class-or-namespace-name. */
6044 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6045 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6046 == CPP_SCOPE)
6047 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6048 != CPP_COMPL))
6050 token = cp_lexer_consume_token (parser->lexer);
6051 if (!error_p)
6053 if (!token->error_reported)
6055 tree decl;
6056 tree ambiguous_decls;
6058 decl = cp_parser_lookup_name (parser, token->u.value,
6059 none_type,
6060 /*is_template=*/false,
6061 /*is_namespace=*/false,
6062 /*check_dependency=*/true,
6063 &ambiguous_decls,
6064 token->location);
6065 if (TREE_CODE (decl) == TEMPLATE_DECL)
6066 error_at (token->location,
6067 "%qD used without template parameters",
6068 decl);
6069 else if (ambiguous_decls)
6071 // cp_parser_lookup_name has the same diagnostic,
6072 // thus make sure to emit it at most once.
6073 if (cp_parser_uncommitted_to_tentative_parse_p
6074 (parser))
6076 error_at (token->location,
6077 "reference to %qD is ambiguous",
6078 token->u.value);
6079 print_candidates (ambiguous_decls);
6081 decl = error_mark_node;
6083 else
6085 if (cxx_dialect != cxx98)
6086 cp_parser_name_lookup_error
6087 (parser, token->u.value, decl, NLE_NOT_CXX98,
6088 token->location);
6089 else
6090 cp_parser_name_lookup_error
6091 (parser, token->u.value, decl, NLE_CXX98,
6092 token->location);
6095 parser->scope = error_mark_node;
6096 error_p = true;
6097 /* Treat this as a successful nested-name-specifier
6098 due to:
6100 [basic.lookup.qual]
6102 If the name found is not a class-name (clause
6103 _class_) or namespace-name (_namespace.def_), the
6104 program is ill-formed. */
6105 success = true;
6107 cp_lexer_consume_token (parser->lexer);
6109 break;
6111 /* We've found one valid nested-name-specifier. */
6112 success = true;
6113 /* Name lookup always gives us a DECL. */
6114 if (TREE_CODE (new_scope) == TYPE_DECL)
6115 new_scope = TREE_TYPE (new_scope);
6116 /* Uses of "template" must be followed by actual templates. */
6117 if (template_keyword_p
6118 && !(CLASS_TYPE_P (new_scope)
6119 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6120 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6121 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6122 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6123 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6124 == TEMPLATE_ID_EXPR)))
6125 permerror (input_location, TYPE_P (new_scope)
6126 ? G_("%qT is not a template")
6127 : G_("%qD is not a template"),
6128 new_scope);
6129 /* If it is a class scope, try to complete it; we are about to
6130 be looking up names inside the class. */
6131 if (TYPE_P (new_scope)
6132 /* Since checking types for dependency can be expensive,
6133 avoid doing it if the type is already complete. */
6134 && !COMPLETE_TYPE_P (new_scope)
6135 /* Do not try to complete dependent types. */
6136 && !dependent_type_p (new_scope))
6138 new_scope = complete_type (new_scope);
6139 /* If it is a typedef to current class, use the current
6140 class instead, as the typedef won't have any names inside
6141 it yet. */
6142 if (!COMPLETE_TYPE_P (new_scope)
6143 && currently_open_class (new_scope))
6144 new_scope = TYPE_MAIN_VARIANT (new_scope);
6146 /* Make sure we look in the right scope the next time through
6147 the loop. */
6148 parser->scope = new_scope;
6151 /* If parsing tentatively, replace the sequence of tokens that makes
6152 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6153 token. That way, should we re-parse the token stream, we will
6154 not have to repeat the effort required to do the parse, nor will
6155 we issue duplicate error messages. */
6156 if (success && start)
6158 cp_token *token;
6160 token = cp_lexer_token_at (parser->lexer, start);
6161 /* Reset the contents of the START token. */
6162 token->type = CPP_NESTED_NAME_SPECIFIER;
6163 /* Retrieve any deferred checks. Do not pop this access checks yet
6164 so the memory will not be reclaimed during token replacing below. */
6165 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6166 token->u.tree_check_value->value = parser->scope;
6167 token->u.tree_check_value->checks = get_deferred_access_checks ();
6168 token->u.tree_check_value->qualifying_scope =
6169 parser->qualifying_scope;
6170 token->keyword = RID_MAX;
6172 /* Purge all subsequent tokens. */
6173 cp_lexer_purge_tokens_after (parser->lexer, start);
6176 if (start)
6177 pop_to_parent_deferring_access_checks ();
6179 return success ? parser->scope : NULL_TREE;
6182 /* Parse a nested-name-specifier. See
6183 cp_parser_nested_name_specifier_opt for details. This function
6184 behaves identically, except that it will an issue an error if no
6185 nested-name-specifier is present. */
6187 static tree
6188 cp_parser_nested_name_specifier (cp_parser *parser,
6189 bool typename_keyword_p,
6190 bool check_dependency_p,
6191 bool type_p,
6192 bool is_declaration)
6194 tree scope;
6196 /* Look for the nested-name-specifier. */
6197 scope = cp_parser_nested_name_specifier_opt (parser,
6198 typename_keyword_p,
6199 check_dependency_p,
6200 type_p,
6201 is_declaration);
6202 /* If it was not present, issue an error message. */
6203 if (!scope)
6205 cp_parser_error (parser, "expected nested-name-specifier");
6206 parser->scope = NULL_TREE;
6209 return scope;
6212 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6213 this is either a class-name or a namespace-name (which corresponds
6214 to the class-or-namespace-name production in the grammar). For
6215 C++0x, it can also be a type-name that refers to an enumeration
6216 type or a simple-template-id.
6218 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6219 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6220 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6221 TYPE_P is TRUE iff the next name should be taken as a class-name,
6222 even the same name is declared to be another entity in the same
6223 scope.
6225 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6226 specified by the class-or-namespace-name. If neither is found the
6227 ERROR_MARK_NODE is returned. */
6229 static tree
6230 cp_parser_qualifying_entity (cp_parser *parser,
6231 bool typename_keyword_p,
6232 bool template_keyword_p,
6233 bool check_dependency_p,
6234 bool type_p,
6235 bool is_declaration)
6237 tree saved_scope;
6238 tree saved_qualifying_scope;
6239 tree saved_object_scope;
6240 tree scope;
6241 bool only_class_p;
6242 bool successful_parse_p;
6244 /* DR 743: decltype can appear in a nested-name-specifier. */
6245 if (cp_lexer_next_token_is_decltype (parser->lexer))
6247 scope = cp_parser_decltype (parser);
6248 if (TREE_CODE (scope) != ENUMERAL_TYPE
6249 && !MAYBE_CLASS_TYPE_P (scope))
6251 cp_parser_simulate_error (parser);
6252 return error_mark_node;
6254 if (TYPE_NAME (scope))
6255 scope = TYPE_NAME (scope);
6256 return scope;
6259 /* Before we try to parse the class-name, we must save away the
6260 current PARSER->SCOPE since cp_parser_class_name will destroy
6261 it. */
6262 saved_scope = parser->scope;
6263 saved_qualifying_scope = parser->qualifying_scope;
6264 saved_object_scope = parser->object_scope;
6265 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6266 there is no need to look for a namespace-name. */
6267 only_class_p = template_keyword_p
6268 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6269 if (!only_class_p)
6270 cp_parser_parse_tentatively (parser);
6271 scope = cp_parser_class_name (parser,
6272 typename_keyword_p,
6273 template_keyword_p,
6274 type_p ? class_type : none_type,
6275 check_dependency_p,
6276 /*class_head_p=*/false,
6277 is_declaration,
6278 /*enum_ok=*/cxx_dialect > cxx98);
6279 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6280 /* If that didn't work, try for a namespace-name. */
6281 if (!only_class_p && !successful_parse_p)
6283 /* Restore the saved scope. */
6284 parser->scope = saved_scope;
6285 parser->qualifying_scope = saved_qualifying_scope;
6286 parser->object_scope = saved_object_scope;
6287 /* If we are not looking at an identifier followed by the scope
6288 resolution operator, then this is not part of a
6289 nested-name-specifier. (Note that this function is only used
6290 to parse the components of a nested-name-specifier.) */
6291 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6292 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6293 return error_mark_node;
6294 scope = cp_parser_namespace_name (parser);
6297 return scope;
6300 /* Return true if we are looking at a compound-literal, false otherwise. */
6302 static bool
6303 cp_parser_compound_literal_p (cp_parser *parser)
6305 /* Consume the `('. */
6306 cp_lexer_consume_token (parser->lexer);
6308 cp_lexer_save_tokens (parser->lexer);
6310 /* Skip tokens until the next token is a closing parenthesis.
6311 If we find the closing `)', and the next token is a `{', then
6312 we are looking at a compound-literal. */
6313 bool compound_literal_p
6314 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6315 /*consume_paren=*/true)
6316 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6318 /* Roll back the tokens we skipped. */
6319 cp_lexer_rollback_tokens (parser->lexer);
6321 return compound_literal_p;
6324 /* Parse a postfix-expression.
6326 postfix-expression:
6327 primary-expression
6328 postfix-expression [ expression ]
6329 postfix-expression ( expression-list [opt] )
6330 simple-type-specifier ( expression-list [opt] )
6331 typename :: [opt] nested-name-specifier identifier
6332 ( expression-list [opt] )
6333 typename :: [opt] nested-name-specifier template [opt] template-id
6334 ( expression-list [opt] )
6335 postfix-expression . template [opt] id-expression
6336 postfix-expression -> template [opt] id-expression
6337 postfix-expression . pseudo-destructor-name
6338 postfix-expression -> pseudo-destructor-name
6339 postfix-expression ++
6340 postfix-expression --
6341 dynamic_cast < type-id > ( expression )
6342 static_cast < type-id > ( expression )
6343 reinterpret_cast < type-id > ( expression )
6344 const_cast < type-id > ( expression )
6345 typeid ( expression )
6346 typeid ( type-id )
6348 GNU Extension:
6350 postfix-expression:
6351 ( type-id ) { initializer-list , [opt] }
6353 This extension is a GNU version of the C99 compound-literal
6354 construct. (The C99 grammar uses `type-name' instead of `type-id',
6355 but they are essentially the same concept.)
6357 If ADDRESS_P is true, the postfix expression is the operand of the
6358 `&' operator. CAST_P is true if this expression is the target of a
6359 cast.
6361 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6362 class member access expressions [expr.ref].
6364 Returns a representation of the expression. */
6366 static cp_expr
6367 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6368 bool member_access_only_p, bool decltype_p,
6369 cp_id_kind * pidk_return)
6371 cp_token *token;
6372 location_t loc;
6373 enum rid keyword;
6374 cp_id_kind idk = CP_ID_KIND_NONE;
6375 cp_expr postfix_expression = NULL_TREE;
6376 bool is_member_access = false;
6377 int saved_in_statement = -1;
6379 /* Peek at the next token. */
6380 token = cp_lexer_peek_token (parser->lexer);
6381 loc = token->location;
6382 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6384 /* Some of the productions are determined by keywords. */
6385 keyword = token->keyword;
6386 switch (keyword)
6388 case RID_DYNCAST:
6389 case RID_STATCAST:
6390 case RID_REINTCAST:
6391 case RID_CONSTCAST:
6393 tree type;
6394 cp_expr expression;
6395 const char *saved_message;
6396 bool saved_in_type_id_in_expr_p;
6398 /* All of these can be handled in the same way from the point
6399 of view of parsing. Begin by consuming the token
6400 identifying the cast. */
6401 cp_lexer_consume_token (parser->lexer);
6403 /* New types cannot be defined in the cast. */
6404 saved_message = parser->type_definition_forbidden_message;
6405 parser->type_definition_forbidden_message
6406 = G_("types may not be defined in casts");
6408 /* Look for the opening `<'. */
6409 cp_parser_require (parser, CPP_LESS, RT_LESS);
6410 /* Parse the type to which we are casting. */
6411 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6412 parser->in_type_id_in_expr_p = true;
6413 type = cp_parser_type_id (parser);
6414 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6415 /* Look for the closing `>'. */
6416 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6417 /* Restore the old message. */
6418 parser->type_definition_forbidden_message = saved_message;
6420 bool saved_greater_than_is_operator_p
6421 = parser->greater_than_is_operator_p;
6422 parser->greater_than_is_operator_p = true;
6424 /* And the expression which is being cast. */
6425 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6426 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6427 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6428 RT_CLOSE_PAREN);
6429 location_t end_loc = close_paren ?
6430 close_paren->location : UNKNOWN_LOCATION;
6432 parser->greater_than_is_operator_p
6433 = saved_greater_than_is_operator_p;
6435 /* Only type conversions to integral or enumeration types
6436 can be used in constant-expressions. */
6437 if (!cast_valid_in_integral_constant_expression_p (type)
6438 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6439 return error_mark_node;
6441 switch (keyword)
6443 case RID_DYNCAST:
6444 postfix_expression
6445 = build_dynamic_cast (type, expression, tf_warning_or_error);
6446 break;
6447 case RID_STATCAST:
6448 postfix_expression
6449 = build_static_cast (type, expression, tf_warning_or_error);
6450 break;
6451 case RID_REINTCAST:
6452 postfix_expression
6453 = build_reinterpret_cast (type, expression,
6454 tf_warning_or_error);
6455 break;
6456 case RID_CONSTCAST:
6457 postfix_expression
6458 = build_const_cast (type, expression, tf_warning_or_error);
6459 break;
6460 default:
6461 gcc_unreachable ();
6464 /* Construct a location e.g. :
6465 reinterpret_cast <int *> (expr)
6466 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6467 ranging from the start of the "*_cast" token to the final closing
6468 paren, with the caret at the start. */
6469 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6470 postfix_expression.set_location (cp_cast_loc);
6472 break;
6474 case RID_TYPEID:
6476 tree type;
6477 const char *saved_message;
6478 bool saved_in_type_id_in_expr_p;
6480 /* Consume the `typeid' token. */
6481 cp_lexer_consume_token (parser->lexer);
6482 /* Look for the `(' token. */
6483 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6484 /* Types cannot be defined in a `typeid' expression. */
6485 saved_message = parser->type_definition_forbidden_message;
6486 parser->type_definition_forbidden_message
6487 = G_("types may not be defined in a %<typeid%> expression");
6488 /* We can't be sure yet whether we're looking at a type-id or an
6489 expression. */
6490 cp_parser_parse_tentatively (parser);
6491 /* Try a type-id first. */
6492 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6493 parser->in_type_id_in_expr_p = true;
6494 type = cp_parser_type_id (parser);
6495 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6496 /* Look for the `)' token. Otherwise, we can't be sure that
6497 we're not looking at an expression: consider `typeid (int
6498 (3))', for example. */
6499 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6500 /* If all went well, simply lookup the type-id. */
6501 if (cp_parser_parse_definitely (parser))
6502 postfix_expression = get_typeid (type, tf_warning_or_error);
6503 /* Otherwise, fall back to the expression variant. */
6504 else
6506 tree expression;
6508 /* Look for an expression. */
6509 expression = cp_parser_expression (parser, & idk);
6510 /* Compute its typeid. */
6511 postfix_expression = build_typeid (expression, tf_warning_or_error);
6512 /* Look for the `)' token. */
6513 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6515 /* Restore the saved message. */
6516 parser->type_definition_forbidden_message = saved_message;
6517 /* `typeid' may not appear in an integral constant expression. */
6518 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6519 return error_mark_node;
6521 break;
6523 case RID_TYPENAME:
6525 tree type;
6526 /* The syntax permitted here is the same permitted for an
6527 elaborated-type-specifier. */
6528 ++parser->prevent_constrained_type_specifiers;
6529 type = cp_parser_elaborated_type_specifier (parser,
6530 /*is_friend=*/false,
6531 /*is_declaration=*/false);
6532 --parser->prevent_constrained_type_specifiers;
6533 postfix_expression = cp_parser_functional_cast (parser, type);
6535 break;
6537 case RID_CILK_SPAWN:
6539 location_t cilk_spawn_loc
6540 = cp_lexer_peek_token (parser->lexer)->location;
6541 cp_lexer_consume_token (parser->lexer);
6542 token = cp_lexer_peek_token (parser->lexer);
6543 if (token->type == CPP_SEMICOLON)
6545 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6546 "an expression");
6547 postfix_expression = error_mark_node;
6548 break;
6550 else if (!current_function_decl)
6552 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6553 "inside a function");
6554 postfix_expression = error_mark_node;
6555 break;
6557 else
6559 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6560 saved_in_statement = parser->in_statement;
6561 parser->in_statement |= IN_CILK_SPAWN;
6563 cfun->calls_cilk_spawn = 1;
6564 postfix_expression =
6565 cp_parser_postfix_expression (parser, false, false,
6566 false, false, &idk);
6567 if (!flag_cilkplus)
6569 error_at (token->location, "-fcilkplus must be enabled to use"
6570 " %<_Cilk_spawn%>");
6571 cfun->calls_cilk_spawn = 0;
6573 else if (saved_in_statement & IN_CILK_SPAWN)
6575 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6576 "are not permitted");
6577 postfix_expression = error_mark_node;
6578 cfun->calls_cilk_spawn = 0;
6580 else
6582 location_t loc = postfix_expression.get_location ();
6583 postfix_expression = build_cilk_spawn (token->location,
6584 postfix_expression);
6585 /* Build a location of the form:
6586 _Cilk_spawn expr
6587 ~~~~~~~~~~~~^~~~
6588 with caret at the expr, ranging from the start of the
6589 _Cilk_spawn token to the end of the expression. */
6590 location_t combined_loc =
6591 make_location (loc, cilk_spawn_loc, get_finish (loc));
6592 postfix_expression.set_location (combined_loc);
6593 if (postfix_expression != error_mark_node)
6594 SET_EXPR_LOCATION (postfix_expression, input_location);
6595 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6597 break;
6600 case RID_BUILTIN_SHUFFLE:
6602 vec<tree, va_gc> *vec;
6603 unsigned int i;
6604 tree p;
6606 cp_lexer_consume_token (parser->lexer);
6607 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6608 /*cast_p=*/false, /*allow_expansion_p=*/true,
6609 /*non_constant_p=*/NULL);
6610 if (vec == NULL)
6611 return error_mark_node;
6613 FOR_EACH_VEC_ELT (*vec, i, p)
6614 mark_exp_read (p);
6616 if (vec->length () == 2)
6617 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6618 tf_warning_or_error);
6619 else if (vec->length () == 3)
6620 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6621 tf_warning_or_error);
6622 else
6624 error_at (loc, "wrong number of arguments to "
6625 "%<__builtin_shuffle%>");
6626 return error_mark_node;
6628 break;
6631 default:
6633 tree type;
6635 /* If the next thing is a simple-type-specifier, we may be
6636 looking at a functional cast. We could also be looking at
6637 an id-expression. So, we try the functional cast, and if
6638 that doesn't work we fall back to the primary-expression. */
6639 cp_parser_parse_tentatively (parser);
6640 /* Look for the simple-type-specifier. */
6641 ++parser->prevent_constrained_type_specifiers;
6642 type = cp_parser_simple_type_specifier (parser,
6643 /*decl_specs=*/NULL,
6644 CP_PARSER_FLAGS_NONE);
6645 --parser->prevent_constrained_type_specifiers;
6646 /* Parse the cast itself. */
6647 if (!cp_parser_error_occurred (parser))
6648 postfix_expression
6649 = cp_parser_functional_cast (parser, type);
6650 /* If that worked, we're done. */
6651 if (cp_parser_parse_definitely (parser))
6652 break;
6654 /* If the functional-cast didn't work out, try a
6655 compound-literal. */
6656 if (cp_parser_allow_gnu_extensions_p (parser)
6657 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6659 cp_expr initializer = NULL_TREE;
6661 cp_parser_parse_tentatively (parser);
6663 /* Avoid calling cp_parser_type_id pointlessly, see comment
6664 in cp_parser_cast_expression about c++/29234. */
6665 if (!cp_parser_compound_literal_p (parser))
6666 cp_parser_simulate_error (parser);
6667 else
6669 /* Parse the type. */
6670 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6671 parser->in_type_id_in_expr_p = true;
6672 type = cp_parser_type_id (parser);
6673 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6674 /* Look for the `)'. */
6675 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6678 /* If things aren't going well, there's no need to
6679 keep going. */
6680 if (!cp_parser_error_occurred (parser))
6682 bool non_constant_p;
6683 /* Parse the brace-enclosed initializer list. */
6684 initializer = cp_parser_braced_list (parser,
6685 &non_constant_p);
6687 /* If that worked, we're definitely looking at a
6688 compound-literal expression. */
6689 if (cp_parser_parse_definitely (parser))
6691 /* Warn the user that a compound literal is not
6692 allowed in standard C++. */
6693 pedwarn (input_location, OPT_Wpedantic,
6694 "ISO C++ forbids compound-literals");
6695 /* For simplicity, we disallow compound literals in
6696 constant-expressions. We could
6697 allow compound literals of integer type, whose
6698 initializer was a constant, in constant
6699 expressions. Permitting that usage, as a further
6700 extension, would not change the meaning of any
6701 currently accepted programs. (Of course, as
6702 compound literals are not part of ISO C++, the
6703 standard has nothing to say.) */
6704 if (cp_parser_non_integral_constant_expression (parser,
6705 NIC_NCC))
6707 postfix_expression = error_mark_node;
6708 break;
6710 /* Form the representation of the compound-literal. */
6711 postfix_expression
6712 = finish_compound_literal (type, initializer,
6713 tf_warning_or_error);
6714 postfix_expression.set_location (initializer.get_location ());
6715 break;
6719 /* It must be a primary-expression. */
6720 postfix_expression
6721 = cp_parser_primary_expression (parser, address_p, cast_p,
6722 /*template_arg_p=*/false,
6723 decltype_p,
6724 &idk);
6726 break;
6729 /* Note that we don't need to worry about calling build_cplus_new on a
6730 class-valued CALL_EXPR in decltype when it isn't the end of the
6731 postfix-expression; unary_complex_lvalue will take care of that for
6732 all these cases. */
6734 /* Keep looping until the postfix-expression is complete. */
6735 while (true)
6737 if (idk == CP_ID_KIND_UNQUALIFIED
6738 && identifier_p (postfix_expression)
6739 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6740 /* It is not a Koenig lookup function call. */
6741 postfix_expression
6742 = unqualified_name_lookup_error (postfix_expression);
6744 /* Peek at the next token. */
6745 token = cp_lexer_peek_token (parser->lexer);
6747 switch (token->type)
6749 case CPP_OPEN_SQUARE:
6750 if (cp_next_tokens_can_be_std_attribute_p (parser))
6752 cp_parser_error (parser,
6753 "two consecutive %<[%> shall "
6754 "only introduce an attribute");
6755 return error_mark_node;
6757 postfix_expression
6758 = cp_parser_postfix_open_square_expression (parser,
6759 postfix_expression,
6760 false,
6761 decltype_p);
6762 postfix_expression.set_range (start_loc,
6763 postfix_expression.get_location ());
6765 idk = CP_ID_KIND_NONE;
6766 is_member_access = false;
6767 break;
6769 case CPP_OPEN_PAREN:
6770 /* postfix-expression ( expression-list [opt] ) */
6772 bool koenig_p;
6773 bool is_builtin_constant_p;
6774 bool saved_integral_constant_expression_p = false;
6775 bool saved_non_integral_constant_expression_p = false;
6776 tsubst_flags_t complain = complain_flags (decltype_p);
6777 vec<tree, va_gc> *args;
6778 location_t close_paren_loc = UNKNOWN_LOCATION;
6780 is_member_access = false;
6782 is_builtin_constant_p
6783 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6784 if (is_builtin_constant_p)
6786 /* The whole point of __builtin_constant_p is to allow
6787 non-constant expressions to appear as arguments. */
6788 saved_integral_constant_expression_p
6789 = parser->integral_constant_expression_p;
6790 saved_non_integral_constant_expression_p
6791 = parser->non_integral_constant_expression_p;
6792 parser->integral_constant_expression_p = false;
6794 args = (cp_parser_parenthesized_expression_list
6795 (parser, non_attr,
6796 /*cast_p=*/false, /*allow_expansion_p=*/true,
6797 /*non_constant_p=*/NULL,
6798 /*close_paren_loc=*/&close_paren_loc));
6799 if (is_builtin_constant_p)
6801 parser->integral_constant_expression_p
6802 = saved_integral_constant_expression_p;
6803 parser->non_integral_constant_expression_p
6804 = saved_non_integral_constant_expression_p;
6807 if (args == NULL)
6809 postfix_expression = error_mark_node;
6810 break;
6813 /* Function calls are not permitted in
6814 constant-expressions. */
6815 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6816 && cp_parser_non_integral_constant_expression (parser,
6817 NIC_FUNC_CALL))
6819 postfix_expression = error_mark_node;
6820 release_tree_vector (args);
6821 break;
6824 koenig_p = false;
6825 if (idk == CP_ID_KIND_UNQUALIFIED
6826 || idk == CP_ID_KIND_TEMPLATE_ID)
6828 if (identifier_p (postfix_expression))
6830 if (!args->is_empty ())
6832 koenig_p = true;
6833 if (!any_type_dependent_arguments_p (args))
6834 postfix_expression
6835 = perform_koenig_lookup (postfix_expression, args,
6836 complain);
6838 else
6839 postfix_expression
6840 = unqualified_fn_lookup_error (postfix_expression);
6842 /* We do not perform argument-dependent lookup if
6843 normal lookup finds a non-function, in accordance
6844 with the expected resolution of DR 218. */
6845 else if (!args->is_empty ()
6846 && is_overloaded_fn (postfix_expression))
6848 tree fn = get_first_fn (postfix_expression);
6849 fn = STRIP_TEMPLATE (fn);
6851 /* Do not do argument dependent lookup if regular
6852 lookup finds a member function or a block-scope
6853 function declaration. [basic.lookup.argdep]/3 */
6854 if (!DECL_FUNCTION_MEMBER_P (fn)
6855 && !DECL_LOCAL_FUNCTION_P (fn))
6857 koenig_p = true;
6858 if (!any_type_dependent_arguments_p (args))
6859 postfix_expression
6860 = perform_koenig_lookup (postfix_expression, args,
6861 complain);
6866 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6867 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6868 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6869 && vec_safe_length (args) == 3)
6871 tree arg0 = (*args)[0];
6872 tree arg1 = (*args)[1];
6873 tree arg2 = (*args)[2];
6874 int literal_mask = ((!!integer_zerop (arg1) << 1)
6875 | (!!integer_zerop (arg2) << 2));
6876 if (TREE_CODE (arg2) == CONST_DECL)
6877 arg2 = DECL_INITIAL (arg2);
6878 warn_for_memset (input_location, arg0, arg2, literal_mask);
6881 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6883 tree instance = TREE_OPERAND (postfix_expression, 0);
6884 tree fn = TREE_OPERAND (postfix_expression, 1);
6886 if (processing_template_decl
6887 && (type_dependent_object_expression_p (instance)
6888 || (!BASELINK_P (fn)
6889 && TREE_CODE (fn) != FIELD_DECL)
6890 || type_dependent_expression_p (fn)
6891 || any_type_dependent_arguments_p (args)))
6893 postfix_expression
6894 = build_nt_call_vec (postfix_expression, args);
6895 release_tree_vector (args);
6896 break;
6899 if (BASELINK_P (fn))
6901 postfix_expression
6902 = (build_new_method_call
6903 (instance, fn, &args, NULL_TREE,
6904 (idk == CP_ID_KIND_QUALIFIED
6905 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6906 : LOOKUP_NORMAL),
6907 /*fn_p=*/NULL,
6908 complain));
6910 else
6911 postfix_expression
6912 = finish_call_expr (postfix_expression, &args,
6913 /*disallow_virtual=*/false,
6914 /*koenig_p=*/false,
6915 complain);
6917 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6918 || TREE_CODE (postfix_expression) == MEMBER_REF
6919 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6920 postfix_expression = (build_offset_ref_call_from_tree
6921 (postfix_expression, &args,
6922 complain));
6923 else if (idk == CP_ID_KIND_QUALIFIED)
6924 /* A call to a static class member, or a namespace-scope
6925 function. */
6926 postfix_expression
6927 = finish_call_expr (postfix_expression, &args,
6928 /*disallow_virtual=*/true,
6929 koenig_p,
6930 complain);
6931 else
6932 /* All other function calls. */
6933 postfix_expression
6934 = finish_call_expr (postfix_expression, &args,
6935 /*disallow_virtual=*/false,
6936 koenig_p,
6937 complain);
6939 if (close_paren_loc != UNKNOWN_LOCATION)
6941 location_t combined_loc = make_location (token->location,
6942 start_loc,
6943 close_paren_loc);
6944 postfix_expression.set_location (combined_loc);
6947 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6948 idk = CP_ID_KIND_NONE;
6950 release_tree_vector (args);
6952 break;
6954 case CPP_DOT:
6955 case CPP_DEREF:
6956 /* postfix-expression . template [opt] id-expression
6957 postfix-expression . pseudo-destructor-name
6958 postfix-expression -> template [opt] id-expression
6959 postfix-expression -> pseudo-destructor-name */
6961 /* Consume the `.' or `->' operator. */
6962 cp_lexer_consume_token (parser->lexer);
6964 postfix_expression
6965 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6966 postfix_expression,
6967 false, &idk, loc);
6969 is_member_access = true;
6970 break;
6972 case CPP_PLUS_PLUS:
6973 /* postfix-expression ++ */
6974 /* Consume the `++' token. */
6975 cp_lexer_consume_token (parser->lexer);
6976 /* Generate a representation for the complete expression. */
6977 postfix_expression
6978 = finish_increment_expr (postfix_expression,
6979 POSTINCREMENT_EXPR);
6980 /* Increments may not appear in constant-expressions. */
6981 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6982 postfix_expression = error_mark_node;
6983 idk = CP_ID_KIND_NONE;
6984 is_member_access = false;
6985 break;
6987 case CPP_MINUS_MINUS:
6988 /* postfix-expression -- */
6989 /* Consume the `--' token. */
6990 cp_lexer_consume_token (parser->lexer);
6991 /* Generate a representation for the complete expression. */
6992 postfix_expression
6993 = finish_increment_expr (postfix_expression,
6994 POSTDECREMENT_EXPR);
6995 /* Decrements may not appear in constant-expressions. */
6996 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6997 postfix_expression = error_mark_node;
6998 idk = CP_ID_KIND_NONE;
6999 is_member_access = false;
7000 break;
7002 default:
7003 if (pidk_return != NULL)
7004 * pidk_return = idk;
7005 if (member_access_only_p)
7006 return is_member_access
7007 ? postfix_expression
7008 : cp_expr (error_mark_node);
7009 else
7010 return postfix_expression;
7014 /* We should never get here. */
7015 gcc_unreachable ();
7016 return error_mark_node;
7019 /* This function parses Cilk Plus array notations. If a normal array expr. is
7020 parsed then the array index is passed back to the caller through *INIT_INDEX
7021 and the function returns a NULL_TREE. If array notation expr. is parsed,
7022 then *INIT_INDEX is ignored by the caller and the function returns
7023 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
7024 error_mark_node. */
7026 static tree
7027 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
7028 tree array_value)
7030 cp_token *token = NULL;
7031 tree length_index, stride = NULL_TREE, value_tree, array_type;
7032 if (!array_value || array_value == error_mark_node)
7034 cp_parser_skip_to_end_of_statement (parser);
7035 return error_mark_node;
7038 array_type = TREE_TYPE (array_value);
7040 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7041 parser->colon_corrects_to_scope_p = false;
7042 token = cp_lexer_peek_token (parser->lexer);
7044 if (!token)
7046 cp_parser_error (parser, "expected %<:%> or numeral");
7047 return error_mark_node;
7049 else if (token->type == CPP_COLON)
7051 /* Consume the ':'. */
7052 cp_lexer_consume_token (parser->lexer);
7054 /* If we are here, then we have a case like this A[:]. */
7055 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7057 cp_parser_error (parser, "expected %<]%>");
7058 cp_parser_skip_to_end_of_statement (parser);
7059 return error_mark_node;
7061 *init_index = NULL_TREE;
7062 stride = NULL_TREE;
7063 length_index = NULL_TREE;
7065 else
7067 /* If we are here, then there are three valid possibilities:
7068 1. ARRAY [ EXP ]
7069 2. ARRAY [ EXP : EXP ]
7070 3. ARRAY [ EXP : EXP : EXP ] */
7072 *init_index = cp_parser_expression (parser);
7073 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7075 /* This indicates that we have a normal array expression. */
7076 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7077 return NULL_TREE;
7080 /* Consume the ':'. */
7081 cp_lexer_consume_token (parser->lexer);
7082 length_index = cp_parser_expression (parser);
7083 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7085 cp_lexer_consume_token (parser->lexer);
7086 stride = cp_parser_expression (parser);
7089 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7091 if (*init_index == error_mark_node || length_index == error_mark_node
7092 || stride == error_mark_node || array_type == error_mark_node)
7094 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7095 cp_lexer_consume_token (parser->lexer);
7096 return error_mark_node;
7098 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7100 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7101 length_index, stride, array_type);
7102 return value_tree;
7105 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7106 by cp_parser_builtin_offsetof. We're looking for
7108 postfix-expression [ expression ]
7109 postfix-expression [ braced-init-list ] (C++11)
7111 FOR_OFFSETOF is set if we're being called in that context, which
7112 changes how we deal with integer constant expressions. */
7114 static tree
7115 cp_parser_postfix_open_square_expression (cp_parser *parser,
7116 tree postfix_expression,
7117 bool for_offsetof,
7118 bool decltype_p)
7120 tree index = NULL_TREE;
7121 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7122 bool saved_greater_than_is_operator_p;
7124 /* Consume the `[' token. */
7125 cp_lexer_consume_token (parser->lexer);
7127 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7128 parser->greater_than_is_operator_p = true;
7130 /* Parse the index expression. */
7131 /* ??? For offsetof, there is a question of what to allow here. If
7132 offsetof is not being used in an integral constant expression context,
7133 then we *could* get the right answer by computing the value at runtime.
7134 If we are in an integral constant expression context, then we might
7135 could accept any constant expression; hard to say without analysis.
7136 Rather than open the barn door too wide right away, allow only integer
7137 constant expressions here. */
7138 if (for_offsetof)
7139 index = cp_parser_constant_expression (parser);
7140 else
7142 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7144 bool expr_nonconst_p;
7145 cp_lexer_set_source_position (parser->lexer);
7146 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7147 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7148 if (flag_cilkplus
7149 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7151 error_at (cp_lexer_peek_token (parser->lexer)->location,
7152 "braced list index is not allowed with array "
7153 "notation");
7154 cp_parser_skip_to_end_of_statement (parser);
7155 return error_mark_node;
7158 else if (flag_cilkplus)
7160 /* Here are have these two options:
7161 ARRAY[EXP : EXP] - Array notation expr with default
7162 stride of 1.
7163 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7164 stride. */
7165 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7166 postfix_expression);
7167 if (an_exp)
7168 return an_exp;
7170 else
7171 index = cp_parser_expression (parser);
7174 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7176 /* Look for the closing `]'. */
7177 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7179 /* Build the ARRAY_REF. */
7180 postfix_expression = grok_array_decl (loc, postfix_expression,
7181 index, decltype_p);
7183 /* When not doing offsetof, array references are not permitted in
7184 constant-expressions. */
7185 if (!for_offsetof
7186 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7187 postfix_expression = error_mark_node;
7189 return postfix_expression;
7192 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7193 by cp_parser_builtin_offsetof. We're looking for
7195 postfix-expression . template [opt] id-expression
7196 postfix-expression . pseudo-destructor-name
7197 postfix-expression -> template [opt] id-expression
7198 postfix-expression -> pseudo-destructor-name
7200 FOR_OFFSETOF is set if we're being called in that context. That sorta
7201 limits what of the above we'll actually accept, but nevermind.
7202 TOKEN_TYPE is the "." or "->" token, which will already have been
7203 removed from the stream. */
7205 static tree
7206 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7207 enum cpp_ttype token_type,
7208 cp_expr postfix_expression,
7209 bool for_offsetof, cp_id_kind *idk,
7210 location_t location)
7212 tree name;
7213 bool dependent_p;
7214 bool pseudo_destructor_p;
7215 tree scope = NULL_TREE;
7216 location_t start_loc = postfix_expression.get_start ();
7218 /* If this is a `->' operator, dereference the pointer. */
7219 if (token_type == CPP_DEREF)
7220 postfix_expression = build_x_arrow (location, postfix_expression,
7221 tf_warning_or_error);
7222 /* Check to see whether or not the expression is type-dependent and
7223 not the current instantiation. */
7224 dependent_p = type_dependent_object_expression_p (postfix_expression);
7225 /* The identifier following the `->' or `.' is not qualified. */
7226 parser->scope = NULL_TREE;
7227 parser->qualifying_scope = NULL_TREE;
7228 parser->object_scope = NULL_TREE;
7229 *idk = CP_ID_KIND_NONE;
7231 /* Enter the scope corresponding to the type of the object
7232 given by the POSTFIX_EXPRESSION. */
7233 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
7235 scope = TREE_TYPE (postfix_expression);
7236 /* According to the standard, no expression should ever have
7237 reference type. Unfortunately, we do not currently match
7238 the standard in this respect in that our internal representation
7239 of an expression may have reference type even when the standard
7240 says it does not. Therefore, we have to manually obtain the
7241 underlying type here. */
7242 scope = non_reference (scope);
7243 /* The type of the POSTFIX_EXPRESSION must be complete. */
7244 /* Unlike the object expression in other contexts, *this is not
7245 required to be of complete type for purposes of class member
7246 access (5.2.5) outside the member function body. */
7247 if (postfix_expression != current_class_ref
7248 && !(processing_template_decl
7249 && current_class_type
7250 && (same_type_ignoring_top_level_qualifiers_p
7251 (scope, current_class_type))))
7252 scope = complete_type_or_else (scope, postfix_expression);
7253 /* Let the name lookup machinery know that we are processing a
7254 class member access expression. */
7255 parser->context->object_type = scope;
7256 /* If something went wrong, we want to be able to discern that case,
7257 as opposed to the case where there was no SCOPE due to the type
7258 of expression being dependent. */
7259 if (!scope)
7260 scope = error_mark_node;
7261 /* If the SCOPE was erroneous, make the various semantic analysis
7262 functions exit quickly -- and without issuing additional error
7263 messages. */
7264 if (scope == error_mark_node)
7265 postfix_expression = error_mark_node;
7267 else
7268 /* Tell cp_parser_lookup_name that there was an object, even though it's
7269 type-dependent. */
7270 parser->context->object_type = unknown_type_node;
7272 /* Assume this expression is not a pseudo-destructor access. */
7273 pseudo_destructor_p = false;
7275 /* If the SCOPE is a scalar type, then, if this is a valid program,
7276 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7277 is type dependent, it can be pseudo-destructor-name or something else.
7278 Try to parse it as pseudo-destructor-name first. */
7279 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7281 tree s;
7282 tree type;
7284 cp_parser_parse_tentatively (parser);
7285 /* Parse the pseudo-destructor-name. */
7286 s = NULL_TREE;
7287 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7288 &s, &type);
7289 if (dependent_p
7290 && (cp_parser_error_occurred (parser)
7291 || !SCALAR_TYPE_P (type)))
7292 cp_parser_abort_tentative_parse (parser);
7293 else if (cp_parser_parse_definitely (parser))
7295 pseudo_destructor_p = true;
7296 postfix_expression
7297 = finish_pseudo_destructor_expr (postfix_expression,
7298 s, type, location);
7302 if (!pseudo_destructor_p)
7304 /* If the SCOPE is not a scalar type, we are looking at an
7305 ordinary class member access expression, rather than a
7306 pseudo-destructor-name. */
7307 bool template_p;
7308 cp_token *token = cp_lexer_peek_token (parser->lexer);
7309 /* Parse the id-expression. */
7310 name = (cp_parser_id_expression
7311 (parser,
7312 cp_parser_optional_template_keyword (parser),
7313 /*check_dependency_p=*/true,
7314 &template_p,
7315 /*declarator_p=*/false,
7316 /*optional_p=*/false));
7317 /* In general, build a SCOPE_REF if the member name is qualified.
7318 However, if the name was not dependent and has already been
7319 resolved; there is no need to build the SCOPE_REF. For example;
7321 struct X { void f(); };
7322 template <typename T> void f(T* t) { t->X::f(); }
7324 Even though "t" is dependent, "X::f" is not and has been resolved
7325 to a BASELINK; there is no need to include scope information. */
7327 /* But we do need to remember that there was an explicit scope for
7328 virtual function calls. */
7329 if (parser->scope)
7330 *idk = CP_ID_KIND_QUALIFIED;
7332 /* If the name is a template-id that names a type, we will get a
7333 TYPE_DECL here. That is invalid code. */
7334 if (TREE_CODE (name) == TYPE_DECL)
7336 error_at (token->location, "invalid use of %qD", name);
7337 postfix_expression = error_mark_node;
7339 else
7341 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7343 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7345 error_at (token->location, "%<%D::%D%> is not a class member",
7346 parser->scope, name);
7347 postfix_expression = error_mark_node;
7349 else
7350 name = build_qualified_name (/*type=*/NULL_TREE,
7351 parser->scope,
7352 name,
7353 template_p);
7354 parser->scope = NULL_TREE;
7355 parser->qualifying_scope = NULL_TREE;
7356 parser->object_scope = NULL_TREE;
7358 if (parser->scope && name && BASELINK_P (name))
7359 adjust_result_of_qualified_name_lookup
7360 (name, parser->scope, scope);
7361 postfix_expression
7362 = finish_class_member_access_expr (postfix_expression, name,
7363 template_p,
7364 tf_warning_or_error);
7365 /* Build a location e.g.:
7366 ptr->access_expr
7367 ~~~^~~~~~~~~~~~~
7368 where the caret is at the deref token, ranging from
7369 the start of postfix_expression to the end of the access expr. */
7370 location_t end_loc
7371 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7372 location_t combined_loc
7373 = make_location (input_location, start_loc, end_loc);
7374 protected_set_expr_location (postfix_expression, combined_loc);
7378 /* We no longer need to look up names in the scope of the object on
7379 the left-hand side of the `.' or `->' operator. */
7380 parser->context->object_type = NULL_TREE;
7382 /* Outside of offsetof, these operators may not appear in
7383 constant-expressions. */
7384 if (!for_offsetof
7385 && (cp_parser_non_integral_constant_expression
7386 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7387 postfix_expression = error_mark_node;
7389 return postfix_expression;
7392 /* Parse a parenthesized expression-list.
7394 expression-list:
7395 assignment-expression
7396 expression-list, assignment-expression
7398 attribute-list:
7399 expression-list
7400 identifier
7401 identifier, expression-list
7403 CAST_P is true if this expression is the target of a cast.
7405 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7406 argument pack.
7408 Returns a vector of trees. Each element is a representation of an
7409 assignment-expression. NULL is returned if the ( and or ) are
7410 missing. An empty, but allocated, vector is returned on no
7411 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7412 if we are parsing an attribute list for an attribute that wants a
7413 plain identifier argument, normal_attr for an attribute that wants
7414 an expression, or non_attr if we aren't parsing an attribute list. If
7415 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7416 not all of the expressions in the list were constant.
7417 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7418 will be written to with the location of the closing parenthesis. If
7419 an error occurs, it may or may not be written to. */
7421 static vec<tree, va_gc> *
7422 cp_parser_parenthesized_expression_list (cp_parser* parser,
7423 int is_attribute_list,
7424 bool cast_p,
7425 bool allow_expansion_p,
7426 bool *non_constant_p,
7427 location_t *close_paren_loc)
7429 vec<tree, va_gc> *expression_list;
7430 bool fold_expr_p = is_attribute_list != non_attr;
7431 tree identifier = NULL_TREE;
7432 bool saved_greater_than_is_operator_p;
7434 /* Assume all the expressions will be constant. */
7435 if (non_constant_p)
7436 *non_constant_p = false;
7438 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7439 return NULL;
7441 expression_list = make_tree_vector ();
7443 /* Within a parenthesized expression, a `>' token is always
7444 the greater-than operator. */
7445 saved_greater_than_is_operator_p
7446 = parser->greater_than_is_operator_p;
7447 parser->greater_than_is_operator_p = true;
7449 /* Consume expressions until there are no more. */
7450 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7451 while (true)
7453 tree expr;
7455 /* At the beginning of attribute lists, check to see if the
7456 next token is an identifier. */
7457 if (is_attribute_list == id_attr
7458 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7460 cp_token *token;
7462 /* Consume the identifier. */
7463 token = cp_lexer_consume_token (parser->lexer);
7464 /* Save the identifier. */
7465 identifier = token->u.value;
7467 else
7469 bool expr_non_constant_p;
7471 /* Parse the next assignment-expression. */
7472 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7474 /* A braced-init-list. */
7475 cp_lexer_set_source_position (parser->lexer);
7476 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7477 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7478 if (non_constant_p && expr_non_constant_p)
7479 *non_constant_p = true;
7481 else if (non_constant_p)
7483 expr = (cp_parser_constant_expression
7484 (parser, /*allow_non_constant_p=*/true,
7485 &expr_non_constant_p));
7486 if (expr_non_constant_p)
7487 *non_constant_p = true;
7489 else
7490 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7491 cast_p);
7493 if (fold_expr_p)
7494 expr = instantiate_non_dependent_expr (expr);
7496 /* If we have an ellipsis, then this is an expression
7497 expansion. */
7498 if (allow_expansion_p
7499 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7501 /* Consume the `...'. */
7502 cp_lexer_consume_token (parser->lexer);
7504 /* Build the argument pack. */
7505 expr = make_pack_expansion (expr);
7508 /* Add it to the list. We add error_mark_node
7509 expressions to the list, so that we can still tell if
7510 the correct form for a parenthesized expression-list
7511 is found. That gives better errors. */
7512 vec_safe_push (expression_list, expr);
7514 if (expr == error_mark_node)
7515 goto skip_comma;
7518 /* After the first item, attribute lists look the same as
7519 expression lists. */
7520 is_attribute_list = non_attr;
7522 get_comma:;
7523 /* If the next token isn't a `,', then we are done. */
7524 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7525 break;
7527 /* Otherwise, consume the `,' and keep going. */
7528 cp_lexer_consume_token (parser->lexer);
7531 if (close_paren_loc)
7532 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7534 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7536 int ending;
7538 skip_comma:;
7539 /* We try and resync to an unnested comma, as that will give the
7540 user better diagnostics. */
7541 ending = cp_parser_skip_to_closing_parenthesis (parser,
7542 /*recovering=*/true,
7543 /*or_comma=*/true,
7544 /*consume_paren=*/true);
7545 if (ending < 0)
7546 goto get_comma;
7547 if (!ending)
7549 parser->greater_than_is_operator_p
7550 = saved_greater_than_is_operator_p;
7551 return NULL;
7555 parser->greater_than_is_operator_p
7556 = saved_greater_than_is_operator_p;
7558 if (identifier)
7559 vec_safe_insert (expression_list, 0, identifier);
7561 return expression_list;
7564 /* Parse a pseudo-destructor-name.
7566 pseudo-destructor-name:
7567 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7568 :: [opt] nested-name-specifier template template-id :: ~ type-name
7569 :: [opt] nested-name-specifier [opt] ~ type-name
7571 If either of the first two productions is used, sets *SCOPE to the
7572 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7573 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7574 or ERROR_MARK_NODE if the parse fails. */
7576 static void
7577 cp_parser_pseudo_destructor_name (cp_parser* parser,
7578 tree object,
7579 tree* scope,
7580 tree* type)
7582 bool nested_name_specifier_p;
7584 /* Handle ~auto. */
7585 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7586 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7587 && !type_dependent_expression_p (object))
7589 if (cxx_dialect < cxx14)
7590 pedwarn (input_location, 0,
7591 "%<~auto%> only available with "
7592 "-std=c++14 or -std=gnu++14");
7593 cp_lexer_consume_token (parser->lexer);
7594 cp_lexer_consume_token (parser->lexer);
7595 *scope = NULL_TREE;
7596 *type = TREE_TYPE (object);
7597 return;
7600 /* Assume that things will not work out. */
7601 *type = error_mark_node;
7603 /* Look for the optional `::' operator. */
7604 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7605 /* Look for the optional nested-name-specifier. */
7606 nested_name_specifier_p
7607 = (cp_parser_nested_name_specifier_opt (parser,
7608 /*typename_keyword_p=*/false,
7609 /*check_dependency_p=*/true,
7610 /*type_p=*/false,
7611 /*is_declaration=*/false)
7612 != NULL_TREE);
7613 /* Now, if we saw a nested-name-specifier, we might be doing the
7614 second production. */
7615 if (nested_name_specifier_p
7616 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7618 /* Consume the `template' keyword. */
7619 cp_lexer_consume_token (parser->lexer);
7620 /* Parse the template-id. */
7621 cp_parser_template_id (parser,
7622 /*template_keyword_p=*/true,
7623 /*check_dependency_p=*/false,
7624 class_type,
7625 /*is_declaration=*/true);
7626 /* Look for the `::' token. */
7627 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7629 /* If the next token is not a `~', then there might be some
7630 additional qualification. */
7631 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7633 /* At this point, we're looking for "type-name :: ~". The type-name
7634 must not be a class-name, since this is a pseudo-destructor. So,
7635 it must be either an enum-name, or a typedef-name -- both of which
7636 are just identifiers. So, we peek ahead to check that the "::"
7637 and "~" tokens are present; if they are not, then we can avoid
7638 calling type_name. */
7639 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7640 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7641 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7643 cp_parser_error (parser, "non-scalar type");
7644 return;
7647 /* Look for the type-name. */
7648 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7649 if (*scope == error_mark_node)
7650 return;
7652 /* Look for the `::' token. */
7653 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7655 else
7656 *scope = NULL_TREE;
7658 /* Look for the `~'. */
7659 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7661 /* Once we see the ~, this has to be a pseudo-destructor. */
7662 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7663 cp_parser_commit_to_topmost_tentative_parse (parser);
7665 /* Look for the type-name again. We are not responsible for
7666 checking that it matches the first type-name. */
7667 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7670 /* Parse a unary-expression.
7672 unary-expression:
7673 postfix-expression
7674 ++ cast-expression
7675 -- cast-expression
7676 unary-operator cast-expression
7677 sizeof unary-expression
7678 sizeof ( type-id )
7679 alignof ( type-id ) [C++0x]
7680 new-expression
7681 delete-expression
7683 GNU Extensions:
7685 unary-expression:
7686 __extension__ cast-expression
7687 __alignof__ unary-expression
7688 __alignof__ ( type-id )
7689 alignof unary-expression [C++0x]
7690 __real__ cast-expression
7691 __imag__ cast-expression
7692 && identifier
7693 sizeof ( type-id ) { initializer-list , [opt] }
7694 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7695 __alignof__ ( type-id ) { initializer-list , [opt] }
7697 ADDRESS_P is true iff the unary-expression is appearing as the
7698 operand of the `&' operator. CAST_P is true if this expression is
7699 the target of a cast.
7701 Returns a representation of the expression. */
7703 static cp_expr
7704 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7705 bool address_p, bool cast_p, bool decltype_p)
7707 cp_token *token;
7708 enum tree_code unary_operator;
7710 /* Peek at the next token. */
7711 token = cp_lexer_peek_token (parser->lexer);
7712 /* Some keywords give away the kind of expression. */
7713 if (token->type == CPP_KEYWORD)
7715 enum rid keyword = token->keyword;
7717 switch (keyword)
7719 case RID_ALIGNOF:
7720 case RID_SIZEOF:
7722 tree operand, ret;
7723 enum tree_code op;
7724 location_t first_loc;
7726 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7727 /* Consume the token. */
7728 cp_lexer_consume_token (parser->lexer);
7729 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7730 /* Parse the operand. */
7731 operand = cp_parser_sizeof_operand (parser, keyword);
7733 if (TYPE_P (operand))
7734 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7735 else
7737 /* ISO C++ defines alignof only with types, not with
7738 expressions. So pedwarn if alignof is used with a non-
7739 type expression. However, __alignof__ is ok. */
7740 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7741 pedwarn (token->location, OPT_Wpedantic,
7742 "ISO C++ does not allow %<alignof%> "
7743 "with a non-type");
7745 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7747 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7748 SIZEOF_EXPR with the original operand. */
7749 if (op == SIZEOF_EXPR && ret != error_mark_node)
7751 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7753 if (!processing_template_decl && TYPE_P (operand))
7755 ret = build_min (SIZEOF_EXPR, size_type_node,
7756 build1 (NOP_EXPR, operand,
7757 error_mark_node));
7758 SIZEOF_EXPR_TYPE_P (ret) = 1;
7760 else
7761 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7762 TREE_SIDE_EFFECTS (ret) = 0;
7763 TREE_READONLY (ret) = 1;
7765 SET_EXPR_LOCATION (ret, first_loc);
7767 return ret;
7770 case RID_NEW:
7771 return cp_parser_new_expression (parser);
7773 case RID_DELETE:
7774 return cp_parser_delete_expression (parser);
7776 case RID_EXTENSION:
7778 /* The saved value of the PEDANTIC flag. */
7779 int saved_pedantic;
7780 tree expr;
7782 /* Save away the PEDANTIC flag. */
7783 cp_parser_extension_opt (parser, &saved_pedantic);
7784 /* Parse the cast-expression. */
7785 expr = cp_parser_simple_cast_expression (parser);
7786 /* Restore the PEDANTIC flag. */
7787 pedantic = saved_pedantic;
7789 return expr;
7792 case RID_REALPART:
7793 case RID_IMAGPART:
7795 tree expression;
7797 /* Consume the `__real__' or `__imag__' token. */
7798 cp_lexer_consume_token (parser->lexer);
7799 /* Parse the cast-expression. */
7800 expression = cp_parser_simple_cast_expression (parser);
7801 /* Create the complete representation. */
7802 return build_x_unary_op (token->location,
7803 (keyword == RID_REALPART
7804 ? REALPART_EXPR : IMAGPART_EXPR),
7805 expression,
7806 tf_warning_or_error);
7808 break;
7810 case RID_TRANSACTION_ATOMIC:
7811 case RID_TRANSACTION_RELAXED:
7812 return cp_parser_transaction_expression (parser, keyword);
7814 case RID_NOEXCEPT:
7816 tree expr;
7817 const char *saved_message;
7818 bool saved_integral_constant_expression_p;
7819 bool saved_non_integral_constant_expression_p;
7820 bool saved_greater_than_is_operator_p;
7822 cp_lexer_consume_token (parser->lexer);
7823 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7825 saved_message = parser->type_definition_forbidden_message;
7826 parser->type_definition_forbidden_message
7827 = G_("types may not be defined in %<noexcept%> expressions");
7829 saved_integral_constant_expression_p
7830 = parser->integral_constant_expression_p;
7831 saved_non_integral_constant_expression_p
7832 = parser->non_integral_constant_expression_p;
7833 parser->integral_constant_expression_p = false;
7835 saved_greater_than_is_operator_p
7836 = parser->greater_than_is_operator_p;
7837 parser->greater_than_is_operator_p = true;
7839 ++cp_unevaluated_operand;
7840 ++c_inhibit_evaluation_warnings;
7841 ++cp_noexcept_operand;
7842 expr = cp_parser_expression (parser);
7843 --cp_noexcept_operand;
7844 --c_inhibit_evaluation_warnings;
7845 --cp_unevaluated_operand;
7847 parser->greater_than_is_operator_p
7848 = saved_greater_than_is_operator_p;
7850 parser->integral_constant_expression_p
7851 = saved_integral_constant_expression_p;
7852 parser->non_integral_constant_expression_p
7853 = saved_non_integral_constant_expression_p;
7855 parser->type_definition_forbidden_message = saved_message;
7857 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7858 return finish_noexcept_expr (expr, tf_warning_or_error);
7861 default:
7862 break;
7866 /* Look for the `:: new' and `:: delete', which also signal the
7867 beginning of a new-expression, or delete-expression,
7868 respectively. If the next token is `::', then it might be one of
7869 these. */
7870 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7872 enum rid keyword;
7874 /* See if the token after the `::' is one of the keywords in
7875 which we're interested. */
7876 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7877 /* If it's `new', we have a new-expression. */
7878 if (keyword == RID_NEW)
7879 return cp_parser_new_expression (parser);
7880 /* Similarly, for `delete'. */
7881 else if (keyword == RID_DELETE)
7882 return cp_parser_delete_expression (parser);
7885 /* Look for a unary operator. */
7886 unary_operator = cp_parser_unary_operator (token);
7887 /* The `++' and `--' operators can be handled similarly, even though
7888 they are not technically unary-operators in the grammar. */
7889 if (unary_operator == ERROR_MARK)
7891 if (token->type == CPP_PLUS_PLUS)
7892 unary_operator = PREINCREMENT_EXPR;
7893 else if (token->type == CPP_MINUS_MINUS)
7894 unary_operator = PREDECREMENT_EXPR;
7895 /* Handle the GNU address-of-label extension. */
7896 else if (cp_parser_allow_gnu_extensions_p (parser)
7897 && token->type == CPP_AND_AND)
7899 tree identifier;
7900 tree expression;
7901 location_t start_loc = token->location;
7903 /* Consume the '&&' token. */
7904 cp_lexer_consume_token (parser->lexer);
7905 /* Look for the identifier. */
7906 location_t finish_loc
7907 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
7908 identifier = cp_parser_identifier (parser);
7909 /* Construct a location of the form:
7910 &&label
7911 ^~~~~~~
7912 with caret==start at the "&&", finish at the end of the label. */
7913 location_t combined_loc
7914 = make_location (start_loc, start_loc, finish_loc);
7915 /* Create an expression representing the address. */
7916 expression = finish_label_address_expr (identifier, combined_loc);
7917 if (cp_parser_non_integral_constant_expression (parser,
7918 NIC_ADDR_LABEL))
7919 expression = error_mark_node;
7920 return expression;
7923 if (unary_operator != ERROR_MARK)
7925 cp_expr cast_expression;
7926 cp_expr expression = error_mark_node;
7927 non_integral_constant non_constant_p = NIC_NONE;
7928 location_t loc = token->location;
7929 tsubst_flags_t complain = complain_flags (decltype_p);
7931 /* Consume the operator token. */
7932 token = cp_lexer_consume_token (parser->lexer);
7933 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
7935 /* Parse the cast-expression. */
7936 cast_expression
7937 = cp_parser_cast_expression (parser,
7938 unary_operator == ADDR_EXPR,
7939 /*cast_p=*/false,
7940 /*decltype*/false,
7941 pidk);
7943 /* Make a location:
7944 OP_TOKEN CAST_EXPRESSION
7945 ^~~~~~~~~~~~~~~~~~~~~~~~~
7946 with start==caret at the operator token, and
7947 extending to the end of the cast_expression. */
7948 loc = make_location (loc, loc, cast_expression.get_finish ());
7950 /* Now, build an appropriate representation. */
7951 switch (unary_operator)
7953 case INDIRECT_REF:
7954 non_constant_p = NIC_STAR;
7955 expression = build_x_indirect_ref (loc, cast_expression,
7956 RO_UNARY_STAR,
7957 complain);
7958 /* TODO: build_x_indirect_ref does not always honor the
7959 location, so ensure it is set. */
7960 expression.set_location (loc);
7961 break;
7963 case ADDR_EXPR:
7964 non_constant_p = NIC_ADDR;
7965 /* Fall through. */
7966 case BIT_NOT_EXPR:
7967 expression = build_x_unary_op (loc, unary_operator,
7968 cast_expression,
7969 complain);
7970 /* TODO: build_x_unary_op does not always honor the location,
7971 so ensure it is set. */
7972 expression.set_location (loc);
7973 break;
7975 case PREINCREMENT_EXPR:
7976 case PREDECREMENT_EXPR:
7977 non_constant_p = unary_operator == PREINCREMENT_EXPR
7978 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7979 /* Fall through. */
7980 case NEGATE_EXPR:
7981 /* Immediately fold negation of a constant, unless the constant is 0
7982 (since -0 == 0) or it would overflow. */
7983 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
7984 && CONSTANT_CLASS_P (cast_expression)
7985 && !integer_zerop (cast_expression)
7986 && !TREE_OVERFLOW (cast_expression))
7988 tree folded = fold_build1 (unary_operator,
7989 TREE_TYPE (cast_expression),
7990 cast_expression);
7991 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
7993 expression = cp_expr (folded, loc);
7994 break;
7997 /* Fall through. */
7998 case UNARY_PLUS_EXPR:
7999 case TRUTH_NOT_EXPR:
8000 expression = finish_unary_op_expr (loc, unary_operator,
8001 cast_expression, complain);
8002 break;
8004 default:
8005 gcc_unreachable ();
8008 if (non_constant_p != NIC_NONE
8009 && cp_parser_non_integral_constant_expression (parser,
8010 non_constant_p))
8011 expression = error_mark_node;
8013 return expression;
8016 return cp_parser_postfix_expression (parser, address_p, cast_p,
8017 /*member_access_only_p=*/false,
8018 decltype_p,
8019 pidk);
8022 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8023 unary-operator, the corresponding tree code is returned. */
8025 static enum tree_code
8026 cp_parser_unary_operator (cp_token* token)
8028 switch (token->type)
8030 case CPP_MULT:
8031 return INDIRECT_REF;
8033 case CPP_AND:
8034 return ADDR_EXPR;
8036 case CPP_PLUS:
8037 return UNARY_PLUS_EXPR;
8039 case CPP_MINUS:
8040 return NEGATE_EXPR;
8042 case CPP_NOT:
8043 return TRUTH_NOT_EXPR;
8045 case CPP_COMPL:
8046 return BIT_NOT_EXPR;
8048 default:
8049 return ERROR_MARK;
8053 /* Parse a new-expression.
8055 new-expression:
8056 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8057 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8059 Returns a representation of the expression. */
8061 static tree
8062 cp_parser_new_expression (cp_parser* parser)
8064 bool global_scope_p;
8065 vec<tree, va_gc> *placement;
8066 tree type;
8067 vec<tree, va_gc> *initializer;
8068 tree nelts = NULL_TREE;
8069 tree ret;
8071 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8073 /* Look for the optional `::' operator. */
8074 global_scope_p
8075 = (cp_parser_global_scope_opt (parser,
8076 /*current_scope_valid_p=*/false)
8077 != NULL_TREE);
8078 /* Look for the `new' operator. */
8079 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8080 /* There's no easy way to tell a new-placement from the
8081 `( type-id )' construct. */
8082 cp_parser_parse_tentatively (parser);
8083 /* Look for a new-placement. */
8084 placement = cp_parser_new_placement (parser);
8085 /* If that didn't work out, there's no new-placement. */
8086 if (!cp_parser_parse_definitely (parser))
8088 if (placement != NULL)
8089 release_tree_vector (placement);
8090 placement = NULL;
8093 /* If the next token is a `(', then we have a parenthesized
8094 type-id. */
8095 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8097 cp_token *token;
8098 const char *saved_message = parser->type_definition_forbidden_message;
8100 /* Consume the `('. */
8101 cp_lexer_consume_token (parser->lexer);
8103 /* Parse the type-id. */
8104 parser->type_definition_forbidden_message
8105 = G_("types may not be defined in a new-expression");
8107 type_id_in_expr_sentinel s (parser);
8108 type = cp_parser_type_id (parser);
8110 parser->type_definition_forbidden_message = saved_message;
8112 /* Look for the closing `)'. */
8113 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8114 token = cp_lexer_peek_token (parser->lexer);
8115 /* There should not be a direct-new-declarator in this production,
8116 but GCC used to allowed this, so we check and emit a sensible error
8117 message for this case. */
8118 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8120 error_at (token->location,
8121 "array bound forbidden after parenthesized type-id");
8122 inform (token->location,
8123 "try removing the parentheses around the type-id");
8124 cp_parser_direct_new_declarator (parser);
8127 /* Otherwise, there must be a new-type-id. */
8128 else
8129 type = cp_parser_new_type_id (parser, &nelts);
8131 /* If the next token is a `(' or '{', then we have a new-initializer. */
8132 cp_token *token = cp_lexer_peek_token (parser->lexer);
8133 if (token->type == CPP_OPEN_PAREN
8134 || token->type == CPP_OPEN_BRACE)
8135 initializer = cp_parser_new_initializer (parser);
8136 else
8137 initializer = NULL;
8139 /* A new-expression may not appear in an integral constant
8140 expression. */
8141 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8142 ret = error_mark_node;
8143 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8144 of a new-type-id or type-id of a new-expression, the new-expression shall
8145 contain a new-initializer of the form ( assignment-expression )".
8146 Additionally, consistently with the spirit of DR 1467, we want to accept
8147 'new auto { 2 }' too. */
8148 else if (type_uses_auto (type)
8149 && (vec_safe_length (initializer) != 1
8150 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8151 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8153 error_at (token->location,
8154 "initialization of new-expression for type %<auto%> "
8155 "requires exactly one element");
8156 ret = error_mark_node;
8158 else
8160 /* Construct a location e.g.:
8161 ptr = new int[100]
8162 ^~~~~~~~~~~~
8163 with caret == start at the start of the "new" token, and the end
8164 at the end of the final token we consumed. */
8165 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8166 location_t end_loc = get_finish (end_tok->location);
8167 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8169 /* Create a representation of the new-expression. */
8170 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8171 tf_warning_or_error);
8172 protected_set_expr_location (ret, combined_loc);
8175 if (placement != NULL)
8176 release_tree_vector (placement);
8177 if (initializer != NULL)
8178 release_tree_vector (initializer);
8180 return ret;
8183 /* Parse a new-placement.
8185 new-placement:
8186 ( expression-list )
8188 Returns the same representation as for an expression-list. */
8190 static vec<tree, va_gc> *
8191 cp_parser_new_placement (cp_parser* parser)
8193 vec<tree, va_gc> *expression_list;
8195 /* Parse the expression-list. */
8196 expression_list = (cp_parser_parenthesized_expression_list
8197 (parser, non_attr, /*cast_p=*/false,
8198 /*allow_expansion_p=*/true,
8199 /*non_constant_p=*/NULL));
8201 if (expression_list && expression_list->is_empty ())
8202 error ("expected expression-list or type-id");
8204 return expression_list;
8207 /* Parse a new-type-id.
8209 new-type-id:
8210 type-specifier-seq new-declarator [opt]
8212 Returns the TYPE allocated. If the new-type-id indicates an array
8213 type, *NELTS is set to the number of elements in the last array
8214 bound; the TYPE will not include the last array bound. */
8216 static tree
8217 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8219 cp_decl_specifier_seq type_specifier_seq;
8220 cp_declarator *new_declarator;
8221 cp_declarator *declarator;
8222 cp_declarator *outer_declarator;
8223 const char *saved_message;
8225 /* The type-specifier sequence must not contain type definitions.
8226 (It cannot contain declarations of new types either, but if they
8227 are not definitions we will catch that because they are not
8228 complete.) */
8229 saved_message = parser->type_definition_forbidden_message;
8230 parser->type_definition_forbidden_message
8231 = G_("types may not be defined in a new-type-id");
8232 /* Parse the type-specifier-seq. */
8233 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8234 /*is_trailing_return=*/false,
8235 &type_specifier_seq);
8236 /* Restore the old message. */
8237 parser->type_definition_forbidden_message = saved_message;
8239 if (type_specifier_seq.type == error_mark_node)
8240 return error_mark_node;
8242 /* Parse the new-declarator. */
8243 new_declarator = cp_parser_new_declarator_opt (parser);
8245 /* Determine the number of elements in the last array dimension, if
8246 any. */
8247 *nelts = NULL_TREE;
8248 /* Skip down to the last array dimension. */
8249 declarator = new_declarator;
8250 outer_declarator = NULL;
8251 while (declarator && (declarator->kind == cdk_pointer
8252 || declarator->kind == cdk_ptrmem))
8254 outer_declarator = declarator;
8255 declarator = declarator->declarator;
8257 while (declarator
8258 && declarator->kind == cdk_array
8259 && declarator->declarator
8260 && declarator->declarator->kind == cdk_array)
8262 outer_declarator = declarator;
8263 declarator = declarator->declarator;
8266 if (declarator && declarator->kind == cdk_array)
8268 *nelts = declarator->u.array.bounds;
8269 if (*nelts == error_mark_node)
8270 *nelts = integer_one_node;
8272 if (outer_declarator)
8273 outer_declarator->declarator = declarator->declarator;
8274 else
8275 new_declarator = NULL;
8278 return groktypename (&type_specifier_seq, new_declarator, false);
8281 /* Parse an (optional) new-declarator.
8283 new-declarator:
8284 ptr-operator new-declarator [opt]
8285 direct-new-declarator
8287 Returns the declarator. */
8289 static cp_declarator *
8290 cp_parser_new_declarator_opt (cp_parser* parser)
8292 enum tree_code code;
8293 tree type, std_attributes = NULL_TREE;
8294 cp_cv_quals cv_quals;
8296 /* We don't know if there's a ptr-operator next, or not. */
8297 cp_parser_parse_tentatively (parser);
8298 /* Look for a ptr-operator. */
8299 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8300 /* If that worked, look for more new-declarators. */
8301 if (cp_parser_parse_definitely (parser))
8303 cp_declarator *declarator;
8305 /* Parse another optional declarator. */
8306 declarator = cp_parser_new_declarator_opt (parser);
8308 declarator = cp_parser_make_indirect_declarator
8309 (code, type, cv_quals, declarator, std_attributes);
8311 return declarator;
8314 /* If the next token is a `[', there is a direct-new-declarator. */
8315 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8316 return cp_parser_direct_new_declarator (parser);
8318 return NULL;
8321 /* Parse a direct-new-declarator.
8323 direct-new-declarator:
8324 [ expression ]
8325 direct-new-declarator [constant-expression]
8329 static cp_declarator *
8330 cp_parser_direct_new_declarator (cp_parser* parser)
8332 cp_declarator *declarator = NULL;
8334 while (true)
8336 tree expression;
8337 cp_token *token;
8339 /* Look for the opening `['. */
8340 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8342 token = cp_lexer_peek_token (parser->lexer);
8343 expression = cp_parser_expression (parser);
8344 /* The standard requires that the expression have integral
8345 type. DR 74 adds enumeration types. We believe that the
8346 real intent is that these expressions be handled like the
8347 expression in a `switch' condition, which also allows
8348 classes with a single conversion to integral or
8349 enumeration type. */
8350 if (!processing_template_decl)
8352 expression
8353 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8354 expression,
8355 /*complain=*/true);
8356 if (!expression)
8358 error_at (token->location,
8359 "expression in new-declarator must have integral "
8360 "or enumeration type");
8361 expression = error_mark_node;
8365 /* Look for the closing `]'. */
8366 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8368 /* Add this bound to the declarator. */
8369 declarator = make_array_declarator (declarator, expression);
8371 /* If the next token is not a `[', then there are no more
8372 bounds. */
8373 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8374 break;
8377 return declarator;
8380 /* Parse a new-initializer.
8382 new-initializer:
8383 ( expression-list [opt] )
8384 braced-init-list
8386 Returns a representation of the expression-list. */
8388 static vec<tree, va_gc> *
8389 cp_parser_new_initializer (cp_parser* parser)
8391 vec<tree, va_gc> *expression_list;
8393 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8395 tree t;
8396 bool expr_non_constant_p;
8397 cp_lexer_set_source_position (parser->lexer);
8398 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8399 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8400 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8401 expression_list = make_tree_vector_single (t);
8403 else
8404 expression_list = (cp_parser_parenthesized_expression_list
8405 (parser, non_attr, /*cast_p=*/false,
8406 /*allow_expansion_p=*/true,
8407 /*non_constant_p=*/NULL));
8409 return expression_list;
8412 /* Parse a delete-expression.
8414 delete-expression:
8415 :: [opt] delete cast-expression
8416 :: [opt] delete [ ] cast-expression
8418 Returns a representation of the expression. */
8420 static tree
8421 cp_parser_delete_expression (cp_parser* parser)
8423 bool global_scope_p;
8424 bool array_p;
8425 tree expression;
8427 /* Look for the optional `::' operator. */
8428 global_scope_p
8429 = (cp_parser_global_scope_opt (parser,
8430 /*current_scope_valid_p=*/false)
8431 != NULL_TREE);
8432 /* Look for the `delete' keyword. */
8433 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8434 /* See if the array syntax is in use. */
8435 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8437 /* Consume the `[' token. */
8438 cp_lexer_consume_token (parser->lexer);
8439 /* Look for the `]' token. */
8440 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8441 /* Remember that this is the `[]' construct. */
8442 array_p = true;
8444 else
8445 array_p = false;
8447 /* Parse the cast-expression. */
8448 expression = cp_parser_simple_cast_expression (parser);
8450 /* A delete-expression may not appear in an integral constant
8451 expression. */
8452 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8453 return error_mark_node;
8455 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8456 tf_warning_or_error);
8459 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8460 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8461 0 otherwise. */
8463 static int
8464 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8466 cp_token *token = cp_lexer_peek_token (parser->lexer);
8467 switch (token->type)
8469 case CPP_COMMA:
8470 case CPP_SEMICOLON:
8471 case CPP_QUERY:
8472 case CPP_COLON:
8473 case CPP_CLOSE_SQUARE:
8474 case CPP_CLOSE_PAREN:
8475 case CPP_CLOSE_BRACE:
8476 case CPP_OPEN_BRACE:
8477 case CPP_DOT:
8478 case CPP_DOT_STAR:
8479 case CPP_DEREF:
8480 case CPP_DEREF_STAR:
8481 case CPP_DIV:
8482 case CPP_MOD:
8483 case CPP_LSHIFT:
8484 case CPP_RSHIFT:
8485 case CPP_LESS:
8486 case CPP_GREATER:
8487 case CPP_LESS_EQ:
8488 case CPP_GREATER_EQ:
8489 case CPP_EQ_EQ:
8490 case CPP_NOT_EQ:
8491 case CPP_EQ:
8492 case CPP_MULT_EQ:
8493 case CPP_DIV_EQ:
8494 case CPP_MOD_EQ:
8495 case CPP_PLUS_EQ:
8496 case CPP_MINUS_EQ:
8497 case CPP_RSHIFT_EQ:
8498 case CPP_LSHIFT_EQ:
8499 case CPP_AND_EQ:
8500 case CPP_XOR_EQ:
8501 case CPP_OR_EQ:
8502 case CPP_XOR:
8503 case CPP_OR:
8504 case CPP_OR_OR:
8505 case CPP_EOF:
8506 case CPP_ELLIPSIS:
8507 return 0;
8509 case CPP_OPEN_PAREN:
8510 /* In ((type ()) () the last () isn't a valid cast-expression,
8511 so the whole must be parsed as postfix-expression. */
8512 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8513 != CPP_CLOSE_PAREN;
8515 case CPP_OPEN_SQUARE:
8516 /* '[' may start a primary-expression in obj-c++ and in C++11,
8517 as a lambda-expression, eg, '(void)[]{}'. */
8518 if (cxx_dialect >= cxx11)
8519 return -1;
8520 return c_dialect_objc ();
8522 case CPP_PLUS_PLUS:
8523 case CPP_MINUS_MINUS:
8524 /* '++' and '--' may or may not start a cast-expression:
8526 struct T { void operator++(int); };
8527 void f() { (T())++; }
8531 int a;
8532 (int)++a; */
8533 return -1;
8535 default:
8536 return 1;
8540 /* Parse a cast-expression.
8542 cast-expression:
8543 unary-expression
8544 ( type-id ) cast-expression
8546 ADDRESS_P is true iff the unary-expression is appearing as the
8547 operand of the `&' operator. CAST_P is true if this expression is
8548 the target of a cast.
8550 Returns a representation of the expression. */
8552 static cp_expr
8553 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8554 bool decltype_p, cp_id_kind * pidk)
8556 /* If it's a `(', then we might be looking at a cast. */
8557 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8559 tree type = NULL_TREE;
8560 cp_expr expr (NULL_TREE);
8561 int cast_expression = 0;
8562 const char *saved_message;
8564 /* There's no way to know yet whether or not this is a cast.
8565 For example, `(int (3))' is a unary-expression, while `(int)
8566 3' is a cast. So, we resort to parsing tentatively. */
8567 cp_parser_parse_tentatively (parser);
8568 /* Types may not be defined in a cast. */
8569 saved_message = parser->type_definition_forbidden_message;
8570 parser->type_definition_forbidden_message
8571 = G_("types may not be defined in casts");
8572 /* Consume the `('. */
8573 cp_token *open_paren = cp_lexer_consume_token (parser->lexer);
8574 location_t open_paren_loc = open_paren->location;
8576 /* A very tricky bit is that `(struct S) { 3 }' is a
8577 compound-literal (which we permit in C++ as an extension).
8578 But, that construct is not a cast-expression -- it is a
8579 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8580 is legal; if the compound-literal were a cast-expression,
8581 you'd need an extra set of parentheses.) But, if we parse
8582 the type-id, and it happens to be a class-specifier, then we
8583 will commit to the parse at that point, because we cannot
8584 undo the action that is done when creating a new class. So,
8585 then we cannot back up and do a postfix-expression.
8587 Another tricky case is the following (c++/29234):
8589 struct S { void operator () (); };
8591 void foo ()
8593 ( S()() );
8596 As a type-id we parse the parenthesized S()() as a function
8597 returning a function, groktypename complains and we cannot
8598 back up in this case either.
8600 Therefore, we scan ahead to the closing `)', and check to see
8601 if the tokens after the `)' can start a cast-expression. Otherwise
8602 we are dealing with an unary-expression, a postfix-expression
8603 or something else.
8605 Yet another tricky case, in C++11, is the following (c++/54891):
8607 (void)[]{};
8609 The issue is that usually, besides the case of lambda-expressions,
8610 the parenthesized type-id cannot be followed by '[', and, eg, we
8611 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8612 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8613 we don't commit, we try a cast-expression, then an unary-expression.
8615 Save tokens so that we can put them back. */
8616 cp_lexer_save_tokens (parser->lexer);
8618 /* We may be looking at a cast-expression. */
8619 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8620 /*consume_paren=*/true))
8621 cast_expression
8622 = cp_parser_tokens_start_cast_expression (parser);
8624 /* Roll back the tokens we skipped. */
8625 cp_lexer_rollback_tokens (parser->lexer);
8626 /* If we aren't looking at a cast-expression, simulate an error so
8627 that the call to cp_parser_error_occurred below returns true. */
8628 if (!cast_expression)
8629 cp_parser_simulate_error (parser);
8630 else
8632 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8633 parser->in_type_id_in_expr_p = true;
8634 /* Look for the type-id. */
8635 type = cp_parser_type_id (parser);
8636 /* Look for the closing `)'. */
8637 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8638 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8641 /* Restore the saved message. */
8642 parser->type_definition_forbidden_message = saved_message;
8644 /* At this point this can only be either a cast or a
8645 parenthesized ctor such as `(T ())' that looks like a cast to
8646 function returning T. */
8647 if (!cp_parser_error_occurred (parser))
8649 /* Only commit if the cast-expression doesn't start with
8650 '++', '--', or '[' in C++11. */
8651 if (cast_expression > 0)
8652 cp_parser_commit_to_topmost_tentative_parse (parser);
8654 expr = cp_parser_cast_expression (parser,
8655 /*address_p=*/false,
8656 /*cast_p=*/true,
8657 /*decltype_p=*/false,
8658 pidk);
8660 if (cp_parser_parse_definitely (parser))
8662 /* Warn about old-style casts, if so requested. */
8663 if (warn_old_style_cast
8664 && !in_system_header_at (input_location)
8665 && !VOID_TYPE_P (type)
8666 && current_lang_name != lang_name_c)
8667 warning (OPT_Wold_style_cast, "use of old-style cast");
8669 /* Only type conversions to integral or enumeration types
8670 can be used in constant-expressions. */
8671 if (!cast_valid_in_integral_constant_expression_p (type)
8672 && cp_parser_non_integral_constant_expression (parser,
8673 NIC_CAST))
8674 return error_mark_node;
8676 /* Perform the cast. */
8677 /* Make a location:
8678 (TYPE) EXPR
8679 ^~~~~~~~~~~
8680 with start==caret at the open paren, extending to the
8681 end of "expr". */
8682 location_t cast_loc = make_location (open_paren_loc,
8683 open_paren_loc,
8684 expr.get_finish ());
8685 expr = build_c_cast (cast_loc, type, expr);
8686 return expr;
8689 else
8690 cp_parser_abort_tentative_parse (parser);
8693 /* If we get here, then it's not a cast, so it must be a
8694 unary-expression. */
8695 return cp_parser_unary_expression (parser, pidk, address_p,
8696 cast_p, decltype_p);
8699 /* Parse a binary expression of the general form:
8701 pm-expression:
8702 cast-expression
8703 pm-expression .* cast-expression
8704 pm-expression ->* cast-expression
8706 multiplicative-expression:
8707 pm-expression
8708 multiplicative-expression * pm-expression
8709 multiplicative-expression / pm-expression
8710 multiplicative-expression % pm-expression
8712 additive-expression:
8713 multiplicative-expression
8714 additive-expression + multiplicative-expression
8715 additive-expression - multiplicative-expression
8717 shift-expression:
8718 additive-expression
8719 shift-expression << additive-expression
8720 shift-expression >> additive-expression
8722 relational-expression:
8723 shift-expression
8724 relational-expression < shift-expression
8725 relational-expression > shift-expression
8726 relational-expression <= shift-expression
8727 relational-expression >= shift-expression
8729 GNU Extension:
8731 relational-expression:
8732 relational-expression <? shift-expression
8733 relational-expression >? shift-expression
8735 equality-expression:
8736 relational-expression
8737 equality-expression == relational-expression
8738 equality-expression != relational-expression
8740 and-expression:
8741 equality-expression
8742 and-expression & equality-expression
8744 exclusive-or-expression:
8745 and-expression
8746 exclusive-or-expression ^ and-expression
8748 inclusive-or-expression:
8749 exclusive-or-expression
8750 inclusive-or-expression | exclusive-or-expression
8752 logical-and-expression:
8753 inclusive-or-expression
8754 logical-and-expression && inclusive-or-expression
8756 logical-or-expression:
8757 logical-and-expression
8758 logical-or-expression || logical-and-expression
8760 All these are implemented with a single function like:
8762 binary-expression:
8763 simple-cast-expression
8764 binary-expression <token> binary-expression
8766 CAST_P is true if this expression is the target of a cast.
8768 The binops_by_token map is used to get the tree codes for each <token> type.
8769 binary-expressions are associated according to a precedence table. */
8771 #define TOKEN_PRECEDENCE(token) \
8772 (((token->type == CPP_GREATER \
8773 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8774 && !parser->greater_than_is_operator_p) \
8775 ? PREC_NOT_OPERATOR \
8776 : binops_by_token[token->type].prec)
8778 static cp_expr
8779 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8780 bool no_toplevel_fold_p,
8781 bool decltype_p,
8782 enum cp_parser_prec prec,
8783 cp_id_kind * pidk)
8785 cp_parser_expression_stack stack;
8786 cp_parser_expression_stack_entry *sp = &stack[0];
8787 cp_parser_expression_stack_entry current;
8788 cp_expr rhs;
8789 cp_token *token;
8790 enum tree_code rhs_type;
8791 enum cp_parser_prec new_prec, lookahead_prec;
8792 tree overload;
8794 /* Parse the first expression. */
8795 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8796 ? TRUTH_NOT_EXPR : ERROR_MARK);
8797 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8798 cast_p, decltype_p, pidk);
8799 current.prec = prec;
8801 if (cp_parser_error_occurred (parser))
8802 return error_mark_node;
8804 for (;;)
8806 /* Get an operator token. */
8807 token = cp_lexer_peek_token (parser->lexer);
8809 if (warn_cxx11_compat
8810 && token->type == CPP_RSHIFT
8811 && !parser->greater_than_is_operator_p)
8813 if (warning_at (token->location, OPT_Wc__11_compat,
8814 "%<>>%> operator is treated"
8815 " as two right angle brackets in C++11"))
8816 inform (token->location,
8817 "suggest parentheses around %<>>%> expression");
8820 new_prec = TOKEN_PRECEDENCE (token);
8821 if (new_prec != PREC_NOT_OPERATOR
8822 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8823 /* This is a fold-expression; handle it later. */
8824 new_prec = PREC_NOT_OPERATOR;
8826 /* Popping an entry off the stack means we completed a subexpression:
8827 - either we found a token which is not an operator (`>' where it is not
8828 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8829 will happen repeatedly;
8830 - or, we found an operator which has lower priority. This is the case
8831 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8832 parsing `3 * 4'. */
8833 if (new_prec <= current.prec)
8835 if (sp == stack)
8836 break;
8837 else
8838 goto pop;
8841 get_rhs:
8842 current.tree_type = binops_by_token[token->type].tree_type;
8843 current.loc = token->location;
8845 /* We used the operator token. */
8846 cp_lexer_consume_token (parser->lexer);
8848 /* For "false && x" or "true || x", x will never be executed;
8849 disable warnings while evaluating it. */
8850 if (current.tree_type == TRUTH_ANDIF_EXPR)
8851 c_inhibit_evaluation_warnings +=
8852 cp_fully_fold (current.lhs) == truthvalue_false_node;
8853 else if (current.tree_type == TRUTH_ORIF_EXPR)
8854 c_inhibit_evaluation_warnings +=
8855 cp_fully_fold (current.lhs) == truthvalue_true_node;
8857 /* Extract another operand. It may be the RHS of this expression
8858 or the LHS of a new, higher priority expression. */
8859 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8860 ? TRUTH_NOT_EXPR : ERROR_MARK);
8861 rhs = cp_parser_simple_cast_expression (parser);
8863 /* Get another operator token. Look up its precedence to avoid
8864 building a useless (immediately popped) stack entry for common
8865 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8866 token = cp_lexer_peek_token (parser->lexer);
8867 lookahead_prec = TOKEN_PRECEDENCE (token);
8868 if (lookahead_prec != PREC_NOT_OPERATOR
8869 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8870 lookahead_prec = PREC_NOT_OPERATOR;
8871 if (lookahead_prec > new_prec)
8873 /* ... and prepare to parse the RHS of the new, higher priority
8874 expression. Since precedence levels on the stack are
8875 monotonically increasing, we do not have to care about
8876 stack overflows. */
8877 *sp = current;
8878 ++sp;
8879 current.lhs = rhs;
8880 current.lhs_type = rhs_type;
8881 current.prec = new_prec;
8882 new_prec = lookahead_prec;
8883 goto get_rhs;
8885 pop:
8886 lookahead_prec = new_prec;
8887 /* If the stack is not empty, we have parsed into LHS the right side
8888 (`4' in the example above) of an expression we had suspended.
8889 We can use the information on the stack to recover the LHS (`3')
8890 from the stack together with the tree code (`MULT_EXPR'), and
8891 the precedence of the higher level subexpression
8892 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8893 which will be used to actually build the additive expression. */
8894 rhs = current.lhs;
8895 rhs_type = current.lhs_type;
8896 --sp;
8897 current = *sp;
8900 /* Undo the disabling of warnings done above. */
8901 if (current.tree_type == TRUTH_ANDIF_EXPR)
8902 c_inhibit_evaluation_warnings -=
8903 cp_fully_fold (current.lhs) == truthvalue_false_node;
8904 else if (current.tree_type == TRUTH_ORIF_EXPR)
8905 c_inhibit_evaluation_warnings -=
8906 cp_fully_fold (current.lhs) == truthvalue_true_node;
8908 if (warn_logical_not_paren
8909 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8910 && current.lhs_type == TRUTH_NOT_EXPR
8911 /* Avoid warning for !!x == y. */
8912 && (TREE_CODE (current.lhs) != NE_EXPR
8913 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8914 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8915 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8916 /* Avoid warning for !b == y where b is boolean. */
8917 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8918 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8919 != BOOLEAN_TYPE))))
8920 /* Avoid warning for !!b == y where b is boolean. */
8921 && (!DECL_P (current.lhs)
8922 || TREE_TYPE (current.lhs) == NULL_TREE
8923 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8924 warn_logical_not_parentheses (current.loc, current.tree_type,
8925 current.lhs, maybe_constant_value (rhs));
8927 overload = NULL;
8929 location_t combined_loc = make_location (current.loc,
8930 current.lhs.get_start (),
8931 rhs.get_finish ());
8933 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8934 ERROR_MARK for everything that is not a binary expression.
8935 This makes warn_about_parentheses miss some warnings that
8936 involve unary operators. For unary expressions we should
8937 pass the correct tree_code unless the unary expression was
8938 surrounded by parentheses.
8940 if (no_toplevel_fold_p
8941 && lookahead_prec <= current.prec
8942 && sp == stack)
8943 current.lhs = build2_loc (combined_loc,
8944 current.tree_type,
8945 TREE_CODE_CLASS (current.tree_type)
8946 == tcc_comparison
8947 ? boolean_type_node : TREE_TYPE (current.lhs),
8948 current.lhs, rhs);
8949 else
8951 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
8952 current.lhs, current.lhs_type,
8953 rhs, rhs_type, &overload,
8954 complain_flags (decltype_p));
8955 /* TODO: build_x_binary_op doesn't always honor the location. */
8956 current.lhs.set_location (combined_loc);
8958 current.lhs_type = current.tree_type;
8960 /* If the binary operator required the use of an overloaded operator,
8961 then this expression cannot be an integral constant-expression.
8962 An overloaded operator can be used even if both operands are
8963 otherwise permissible in an integral constant-expression if at
8964 least one of the operands is of enumeration type. */
8966 if (overload
8967 && cp_parser_non_integral_constant_expression (parser,
8968 NIC_OVERLOADED))
8969 return error_mark_node;
8972 return current.lhs;
8975 static cp_expr
8976 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8977 bool no_toplevel_fold_p,
8978 enum cp_parser_prec prec,
8979 cp_id_kind * pidk)
8981 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8982 /*decltype*/false, prec, pidk);
8985 /* Parse the `? expression : assignment-expression' part of a
8986 conditional-expression. The LOGICAL_OR_EXPR is the
8987 logical-or-expression that started the conditional-expression.
8988 Returns a representation of the entire conditional-expression.
8990 This routine is used by cp_parser_assignment_expression.
8992 ? expression : assignment-expression
8994 GNU Extensions:
8996 ? : assignment-expression */
8998 static tree
8999 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9001 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9002 cp_expr assignment_expr;
9003 struct cp_token *token;
9004 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9006 /* Consume the `?' token. */
9007 cp_lexer_consume_token (parser->lexer);
9008 token = cp_lexer_peek_token (parser->lexer);
9009 if (cp_parser_allow_gnu_extensions_p (parser)
9010 && token->type == CPP_COLON)
9012 pedwarn (token->location, OPT_Wpedantic,
9013 "ISO C++ does not allow ?: with omitted middle operand");
9014 /* Implicit true clause. */
9015 expr = NULL_TREE;
9016 c_inhibit_evaluation_warnings +=
9017 folded_logical_or_expr == truthvalue_true_node;
9018 warn_for_omitted_condop (token->location, logical_or_expr);
9020 else
9022 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9023 parser->colon_corrects_to_scope_p = false;
9024 /* Parse the expression. */
9025 c_inhibit_evaluation_warnings +=
9026 folded_logical_or_expr == truthvalue_false_node;
9027 expr = cp_parser_expression (parser);
9028 c_inhibit_evaluation_warnings +=
9029 ((folded_logical_or_expr == truthvalue_true_node)
9030 - (folded_logical_or_expr == truthvalue_false_node));
9031 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9034 /* The next token should be a `:'. */
9035 cp_parser_require (parser, CPP_COLON, RT_COLON);
9036 /* Parse the assignment-expression. */
9037 assignment_expr = cp_parser_assignment_expression (parser);
9038 c_inhibit_evaluation_warnings -=
9039 folded_logical_or_expr == truthvalue_true_node;
9041 /* Make a location:
9042 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9043 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9044 with the caret at the "?", ranging from the start of
9045 the logical_or_expr to the end of the assignment_expr. */
9046 loc = make_location (loc,
9047 logical_or_expr.get_start (),
9048 assignment_expr.get_finish ());
9050 /* Build the conditional-expression. */
9051 return build_x_conditional_expr (loc, logical_or_expr,
9052 expr,
9053 assignment_expr,
9054 tf_warning_or_error);
9057 /* Parse an assignment-expression.
9059 assignment-expression:
9060 conditional-expression
9061 logical-or-expression assignment-operator assignment_expression
9062 throw-expression
9064 CAST_P is true if this expression is the target of a cast.
9065 DECLTYPE_P is true if this expression is the operand of decltype.
9067 Returns a representation for the expression. */
9069 static cp_expr
9070 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9071 bool cast_p, bool decltype_p)
9073 cp_expr expr;
9075 /* If the next token is the `throw' keyword, then we're looking at
9076 a throw-expression. */
9077 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9078 expr = cp_parser_throw_expression (parser);
9079 /* Otherwise, it must be that we are looking at a
9080 logical-or-expression. */
9081 else
9083 /* Parse the binary expressions (logical-or-expression). */
9084 expr = cp_parser_binary_expression (parser, cast_p, false,
9085 decltype_p,
9086 PREC_NOT_OPERATOR, pidk);
9087 /* If the next token is a `?' then we're actually looking at a
9088 conditional-expression. */
9089 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9090 return cp_parser_question_colon_clause (parser, expr);
9091 else
9093 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9095 /* If it's an assignment-operator, we're using the second
9096 production. */
9097 enum tree_code assignment_operator
9098 = cp_parser_assignment_operator_opt (parser);
9099 if (assignment_operator != ERROR_MARK)
9101 bool non_constant_p;
9103 /* Parse the right-hand side of the assignment. */
9104 cp_expr rhs = cp_parser_initializer_clause (parser,
9105 &non_constant_p);
9107 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9108 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9110 /* An assignment may not appear in a
9111 constant-expression. */
9112 if (cp_parser_non_integral_constant_expression (parser,
9113 NIC_ASSIGNMENT))
9114 return error_mark_node;
9115 /* Build the assignment expression. Its default
9116 location:
9117 LHS = RHS
9118 ~~~~^~~~~
9119 is the location of the '=' token as the
9120 caret, ranging from the start of the lhs to the
9121 end of the rhs. */
9122 loc = make_location (loc,
9123 expr.get_start (),
9124 rhs.get_finish ());
9125 expr = build_x_modify_expr (loc, expr,
9126 assignment_operator,
9127 rhs,
9128 complain_flags (decltype_p));
9129 /* TODO: build_x_modify_expr doesn't honor the location,
9130 so we must set it here. */
9131 expr.set_location (loc);
9136 return expr;
9139 /* Parse an (optional) assignment-operator.
9141 assignment-operator: one of
9142 = *= /= %= += -= >>= <<= &= ^= |=
9144 GNU Extension:
9146 assignment-operator: one of
9147 <?= >?=
9149 If the next token is an assignment operator, the corresponding tree
9150 code is returned, and the token is consumed. For example, for
9151 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9152 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9153 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9154 operator, ERROR_MARK is returned. */
9156 static enum tree_code
9157 cp_parser_assignment_operator_opt (cp_parser* parser)
9159 enum tree_code op;
9160 cp_token *token;
9162 /* Peek at the next token. */
9163 token = cp_lexer_peek_token (parser->lexer);
9165 switch (token->type)
9167 case CPP_EQ:
9168 op = NOP_EXPR;
9169 break;
9171 case CPP_MULT_EQ:
9172 op = MULT_EXPR;
9173 break;
9175 case CPP_DIV_EQ:
9176 op = TRUNC_DIV_EXPR;
9177 break;
9179 case CPP_MOD_EQ:
9180 op = TRUNC_MOD_EXPR;
9181 break;
9183 case CPP_PLUS_EQ:
9184 op = PLUS_EXPR;
9185 break;
9187 case CPP_MINUS_EQ:
9188 op = MINUS_EXPR;
9189 break;
9191 case CPP_RSHIFT_EQ:
9192 op = RSHIFT_EXPR;
9193 break;
9195 case CPP_LSHIFT_EQ:
9196 op = LSHIFT_EXPR;
9197 break;
9199 case CPP_AND_EQ:
9200 op = BIT_AND_EXPR;
9201 break;
9203 case CPP_XOR_EQ:
9204 op = BIT_XOR_EXPR;
9205 break;
9207 case CPP_OR_EQ:
9208 op = BIT_IOR_EXPR;
9209 break;
9211 default:
9212 /* Nothing else is an assignment operator. */
9213 op = ERROR_MARK;
9216 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9217 if (op != ERROR_MARK
9218 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9219 op = ERROR_MARK;
9221 /* If it was an assignment operator, consume it. */
9222 if (op != ERROR_MARK)
9223 cp_lexer_consume_token (parser->lexer);
9225 return op;
9228 /* Parse an expression.
9230 expression:
9231 assignment-expression
9232 expression , assignment-expression
9234 CAST_P is true if this expression is the target of a cast.
9235 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9236 except possibly parenthesized or on the RHS of a comma (N3276).
9238 Returns a representation of the expression. */
9240 static cp_expr
9241 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9242 bool cast_p, bool decltype_p)
9244 cp_expr expression = NULL_TREE;
9245 location_t loc = UNKNOWN_LOCATION;
9247 while (true)
9249 cp_expr assignment_expression;
9251 /* Parse the next assignment-expression. */
9252 assignment_expression
9253 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9255 /* We don't create a temporary for a call that is the immediate operand
9256 of decltype or on the RHS of a comma. But when we see a comma, we
9257 need to create a temporary for a call on the LHS. */
9258 if (decltype_p && !processing_template_decl
9259 && TREE_CODE (assignment_expression) == CALL_EXPR
9260 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9261 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9262 assignment_expression
9263 = build_cplus_new (TREE_TYPE (assignment_expression),
9264 assignment_expression, tf_warning_or_error);
9266 /* If this is the first assignment-expression, we can just
9267 save it away. */
9268 if (!expression)
9269 expression = assignment_expression;
9270 else
9272 /* Create a location with caret at the comma, ranging
9273 from the start of the LHS to the end of the RHS. */
9274 loc = make_location (loc,
9275 expression.get_start (),
9276 assignment_expression.get_finish ());
9277 expression = build_x_compound_expr (loc, expression,
9278 assignment_expression,
9279 complain_flags (decltype_p));
9280 expression.set_location (loc);
9282 /* If the next token is not a comma, or we're in a fold-expression, then
9283 we are done with the expression. */
9284 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9285 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9286 break;
9287 /* Consume the `,'. */
9288 loc = cp_lexer_peek_token (parser->lexer)->location;
9289 cp_lexer_consume_token (parser->lexer);
9290 /* A comma operator cannot appear in a constant-expression. */
9291 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9292 expression = error_mark_node;
9295 return expression;
9298 /* Parse a constant-expression.
9300 constant-expression:
9301 conditional-expression
9303 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9304 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9305 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9306 is false, NON_CONSTANT_P should be NULL. */
9308 static cp_expr
9309 cp_parser_constant_expression (cp_parser* parser,
9310 bool allow_non_constant_p,
9311 bool *non_constant_p)
9313 bool saved_integral_constant_expression_p;
9314 bool saved_allow_non_integral_constant_expression_p;
9315 bool saved_non_integral_constant_expression_p;
9316 cp_expr expression;
9318 /* It might seem that we could simply parse the
9319 conditional-expression, and then check to see if it were
9320 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9321 one that the compiler can figure out is constant, possibly after
9322 doing some simplifications or optimizations. The standard has a
9323 precise definition of constant-expression, and we must honor
9324 that, even though it is somewhat more restrictive.
9326 For example:
9328 int i[(2, 3)];
9330 is not a legal declaration, because `(2, 3)' is not a
9331 constant-expression. The `,' operator is forbidden in a
9332 constant-expression. However, GCC's constant-folding machinery
9333 will fold this operation to an INTEGER_CST for `3'. */
9335 /* Save the old settings. */
9336 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9337 saved_allow_non_integral_constant_expression_p
9338 = parser->allow_non_integral_constant_expression_p;
9339 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9340 /* We are now parsing a constant-expression. */
9341 parser->integral_constant_expression_p = true;
9342 parser->allow_non_integral_constant_expression_p
9343 = (allow_non_constant_p || cxx_dialect >= cxx11);
9344 parser->non_integral_constant_expression_p = false;
9345 /* Although the grammar says "conditional-expression", we parse an
9346 "assignment-expression", which also permits "throw-expression"
9347 and the use of assignment operators. In the case that
9348 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9349 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9350 actually essential that we look for an assignment-expression.
9351 For example, cp_parser_initializer_clauses uses this function to
9352 determine whether a particular assignment-expression is in fact
9353 constant. */
9354 expression = cp_parser_assignment_expression (parser);
9355 /* Restore the old settings. */
9356 parser->integral_constant_expression_p
9357 = saved_integral_constant_expression_p;
9358 parser->allow_non_integral_constant_expression_p
9359 = saved_allow_non_integral_constant_expression_p;
9360 if (cxx_dialect >= cxx11)
9362 /* Require an rvalue constant expression here; that's what our
9363 callers expect. Reference constant expressions are handled
9364 separately in e.g. cp_parser_template_argument. */
9365 bool is_const = potential_rvalue_constant_expression (expression);
9366 parser->non_integral_constant_expression_p = !is_const;
9367 if (!is_const && !allow_non_constant_p)
9368 require_potential_rvalue_constant_expression (expression);
9370 if (allow_non_constant_p)
9371 *non_constant_p = parser->non_integral_constant_expression_p;
9372 parser->non_integral_constant_expression_p
9373 = saved_non_integral_constant_expression_p;
9375 return expression;
9378 /* Parse __builtin_offsetof.
9380 offsetof-expression:
9381 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9383 offsetof-member-designator:
9384 id-expression
9385 | offsetof-member-designator "." id-expression
9386 | offsetof-member-designator "[" expression "]"
9387 | offsetof-member-designator "->" id-expression */
9389 static cp_expr
9390 cp_parser_builtin_offsetof (cp_parser *parser)
9392 int save_ice_p, save_non_ice_p;
9393 tree type;
9394 cp_expr expr;
9395 cp_id_kind dummy;
9396 cp_token *token;
9397 location_t finish_loc;
9399 /* We're about to accept non-integral-constant things, but will
9400 definitely yield an integral constant expression. Save and
9401 restore these values around our local parsing. */
9402 save_ice_p = parser->integral_constant_expression_p;
9403 save_non_ice_p = parser->non_integral_constant_expression_p;
9405 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9407 /* Consume the "__builtin_offsetof" token. */
9408 cp_lexer_consume_token (parser->lexer);
9409 /* Consume the opening `('. */
9410 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9411 /* Parse the type-id. */
9412 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9413 type = cp_parser_type_id (parser);
9414 /* Look for the `,'. */
9415 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9416 token = cp_lexer_peek_token (parser->lexer);
9418 /* Build the (type *)null that begins the traditional offsetof macro. */
9419 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
9420 tf_warning_or_error);
9422 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9423 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
9424 true, &dummy, token->location);
9425 while (true)
9427 token = cp_lexer_peek_token (parser->lexer);
9428 switch (token->type)
9430 case CPP_OPEN_SQUARE:
9431 /* offsetof-member-designator "[" expression "]" */
9432 expr = cp_parser_postfix_open_square_expression (parser, expr,
9433 true, false);
9434 break;
9436 case CPP_DEREF:
9437 /* offsetof-member-designator "->" identifier */
9438 expr = grok_array_decl (token->location, expr,
9439 integer_zero_node, false);
9440 /* FALLTHRU */
9442 case CPP_DOT:
9443 /* offsetof-member-designator "." identifier */
9444 cp_lexer_consume_token (parser->lexer);
9445 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9446 expr, true, &dummy,
9447 token->location);
9448 break;
9450 case CPP_CLOSE_PAREN:
9451 /* Consume the ")" token. */
9452 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9453 cp_lexer_consume_token (parser->lexer);
9454 goto success;
9456 default:
9457 /* Error. We know the following require will fail, but
9458 that gives the proper error message. */
9459 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9460 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9461 expr = error_mark_node;
9462 goto failure;
9466 success:
9467 /* Make a location of the form:
9468 __builtin_offsetof (struct s, f)
9469 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9470 with caret at the type-id, ranging from the start of the
9471 "_builtin_offsetof" token to the close paren. */
9472 loc = make_location (loc, start_loc, finish_loc);
9473 /* The result will be an INTEGER_CST, so we need to explicitly
9474 preserve the location. */
9475 expr = cp_expr (finish_offsetof (expr, loc), loc);
9477 failure:
9478 parser->integral_constant_expression_p = save_ice_p;
9479 parser->non_integral_constant_expression_p = save_non_ice_p;
9481 return expr;
9484 /* Parse a trait expression.
9486 Returns a representation of the expression, the underlying type
9487 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9489 static tree
9490 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9492 cp_trait_kind kind;
9493 tree type1, type2 = NULL_TREE;
9494 bool binary = false;
9495 bool variadic = false;
9497 switch (keyword)
9499 case RID_HAS_NOTHROW_ASSIGN:
9500 kind = CPTK_HAS_NOTHROW_ASSIGN;
9501 break;
9502 case RID_HAS_NOTHROW_CONSTRUCTOR:
9503 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9504 break;
9505 case RID_HAS_NOTHROW_COPY:
9506 kind = CPTK_HAS_NOTHROW_COPY;
9507 break;
9508 case RID_HAS_TRIVIAL_ASSIGN:
9509 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9510 break;
9511 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9512 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9513 break;
9514 case RID_HAS_TRIVIAL_COPY:
9515 kind = CPTK_HAS_TRIVIAL_COPY;
9516 break;
9517 case RID_HAS_TRIVIAL_DESTRUCTOR:
9518 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9519 break;
9520 case RID_HAS_VIRTUAL_DESTRUCTOR:
9521 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9522 break;
9523 case RID_IS_ABSTRACT:
9524 kind = CPTK_IS_ABSTRACT;
9525 break;
9526 case RID_IS_BASE_OF:
9527 kind = CPTK_IS_BASE_OF;
9528 binary = true;
9529 break;
9530 case RID_IS_CLASS:
9531 kind = CPTK_IS_CLASS;
9532 break;
9533 case RID_IS_EMPTY:
9534 kind = CPTK_IS_EMPTY;
9535 break;
9536 case RID_IS_ENUM:
9537 kind = CPTK_IS_ENUM;
9538 break;
9539 case RID_IS_FINAL:
9540 kind = CPTK_IS_FINAL;
9541 break;
9542 case RID_IS_LITERAL_TYPE:
9543 kind = CPTK_IS_LITERAL_TYPE;
9544 break;
9545 case RID_IS_POD:
9546 kind = CPTK_IS_POD;
9547 break;
9548 case RID_IS_POLYMORPHIC:
9549 kind = CPTK_IS_POLYMORPHIC;
9550 break;
9551 case RID_IS_SAME_AS:
9552 kind = CPTK_IS_SAME_AS;
9553 binary = true;
9554 break;
9555 case RID_IS_STD_LAYOUT:
9556 kind = CPTK_IS_STD_LAYOUT;
9557 break;
9558 case RID_IS_TRIVIAL:
9559 kind = CPTK_IS_TRIVIAL;
9560 break;
9561 case RID_IS_TRIVIALLY_ASSIGNABLE:
9562 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9563 binary = true;
9564 break;
9565 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9566 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9567 variadic = true;
9568 break;
9569 case RID_IS_TRIVIALLY_COPYABLE:
9570 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9571 break;
9572 case RID_IS_UNION:
9573 kind = CPTK_IS_UNION;
9574 break;
9575 case RID_UNDERLYING_TYPE:
9576 kind = CPTK_UNDERLYING_TYPE;
9577 break;
9578 case RID_BASES:
9579 kind = CPTK_BASES;
9580 break;
9581 case RID_DIRECT_BASES:
9582 kind = CPTK_DIRECT_BASES;
9583 break;
9584 default:
9585 gcc_unreachable ();
9588 /* Consume the token. */
9589 cp_lexer_consume_token (parser->lexer);
9591 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9594 type_id_in_expr_sentinel s (parser);
9595 type1 = cp_parser_type_id (parser);
9598 if (type1 == error_mark_node)
9599 return error_mark_node;
9601 if (binary)
9603 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9606 type_id_in_expr_sentinel s (parser);
9607 type2 = cp_parser_type_id (parser);
9610 if (type2 == error_mark_node)
9611 return error_mark_node;
9613 else if (variadic)
9615 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9617 cp_lexer_consume_token (parser->lexer);
9618 tree elt = cp_parser_type_id (parser);
9619 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9621 cp_lexer_consume_token (parser->lexer);
9622 elt = make_pack_expansion (elt);
9624 if (elt == error_mark_node)
9625 return error_mark_node;
9626 type2 = tree_cons (NULL_TREE, elt, type2);
9630 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9632 /* Complete the trait expression, which may mean either processing
9633 the trait expr now or saving it for template instantiation. */
9634 switch(kind)
9636 case CPTK_UNDERLYING_TYPE:
9637 return finish_underlying_type (type1);
9638 case CPTK_BASES:
9639 return finish_bases (type1, false);
9640 case CPTK_DIRECT_BASES:
9641 return finish_bases (type1, true);
9642 default:
9643 return finish_trait_expr (kind, type1, type2);
9647 /* Lambdas that appear in variable initializer or default argument scope
9648 get that in their mangling, so we need to record it. We might as well
9649 use the count for function and namespace scopes as well. */
9650 static GTY(()) tree lambda_scope;
9651 static GTY(()) int lambda_count;
9652 struct GTY(()) tree_int
9654 tree t;
9655 int i;
9657 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9659 static void
9660 start_lambda_scope (tree decl)
9662 tree_int ti;
9663 gcc_assert (decl);
9664 /* Once we're inside a function, we ignore other scopes and just push
9665 the function again so that popping works properly. */
9666 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9667 decl = current_function_decl;
9668 ti.t = lambda_scope;
9669 ti.i = lambda_count;
9670 vec_safe_push (lambda_scope_stack, ti);
9671 if (lambda_scope != decl)
9673 /* Don't reset the count if we're still in the same function. */
9674 lambda_scope = decl;
9675 lambda_count = 0;
9679 static void
9680 record_lambda_scope (tree lambda)
9682 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9683 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9686 static void
9687 finish_lambda_scope (void)
9689 tree_int *p = &lambda_scope_stack->last ();
9690 if (lambda_scope != p->t)
9692 lambda_scope = p->t;
9693 lambda_count = p->i;
9695 lambda_scope_stack->pop ();
9698 /* Parse a lambda expression.
9700 lambda-expression:
9701 lambda-introducer lambda-declarator [opt] compound-statement
9703 Returns a representation of the expression. */
9705 static cp_expr
9706 cp_parser_lambda_expression (cp_parser* parser)
9708 tree lambda_expr = build_lambda_expr ();
9709 tree type;
9710 bool ok = true;
9711 cp_token *token = cp_lexer_peek_token (parser->lexer);
9712 cp_token_position start = 0;
9714 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9716 if (cp_unevaluated_operand)
9718 if (!token->error_reported)
9720 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9721 "lambda-expression in unevaluated context");
9722 token->error_reported = true;
9724 ok = false;
9726 else if (parser->in_template_argument_list_p)
9728 if (!token->error_reported)
9730 error_at (token->location, "lambda-expression in template-argument");
9731 token->error_reported = true;
9733 ok = false;
9736 /* We may be in the middle of deferred access check. Disable
9737 it now. */
9738 push_deferring_access_checks (dk_no_deferred);
9740 cp_parser_lambda_introducer (parser, lambda_expr);
9742 type = begin_lambda_type (lambda_expr);
9743 if (type == error_mark_node)
9744 return error_mark_node;
9746 record_lambda_scope (lambda_expr);
9748 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9749 determine_visibility (TYPE_NAME (type));
9751 /* Now that we've started the type, add the capture fields for any
9752 explicit captures. */
9753 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9756 /* Inside the class, surrounding template-parameter-lists do not apply. */
9757 unsigned int saved_num_template_parameter_lists
9758 = parser->num_template_parameter_lists;
9759 unsigned char in_statement = parser->in_statement;
9760 bool in_switch_statement_p = parser->in_switch_statement_p;
9761 bool fully_implicit_function_template_p
9762 = parser->fully_implicit_function_template_p;
9763 tree implicit_template_parms = parser->implicit_template_parms;
9764 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9765 bool auto_is_implicit_function_template_parm_p
9766 = parser->auto_is_implicit_function_template_parm_p;
9768 parser->num_template_parameter_lists = 0;
9769 parser->in_statement = 0;
9770 parser->in_switch_statement_p = false;
9771 parser->fully_implicit_function_template_p = false;
9772 parser->implicit_template_parms = 0;
9773 parser->implicit_template_scope = 0;
9774 parser->auto_is_implicit_function_template_parm_p = false;
9776 /* By virtue of defining a local class, a lambda expression has access to
9777 the private variables of enclosing classes. */
9779 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9781 if (ok && cp_parser_error_occurred (parser))
9782 ok = false;
9784 if (ok)
9786 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9787 && cp_parser_start_tentative_firewall (parser))
9788 start = token;
9789 cp_parser_lambda_body (parser, lambda_expr);
9791 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9793 if (cp_parser_skip_to_closing_brace (parser))
9794 cp_lexer_consume_token (parser->lexer);
9797 /* The capture list was built up in reverse order; fix that now. */
9798 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9799 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9801 if (ok)
9802 maybe_add_lambda_conv_op (type);
9804 type = finish_struct (type, /*attributes=*/NULL_TREE);
9806 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9807 parser->in_statement = in_statement;
9808 parser->in_switch_statement_p = in_switch_statement_p;
9809 parser->fully_implicit_function_template_p
9810 = fully_implicit_function_template_p;
9811 parser->implicit_template_parms = implicit_template_parms;
9812 parser->implicit_template_scope = implicit_template_scope;
9813 parser->auto_is_implicit_function_template_parm_p
9814 = auto_is_implicit_function_template_parm_p;
9817 /* This field is only used during parsing of the lambda. */
9818 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9820 /* This lambda shouldn't have any proxies left at this point. */
9821 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9822 /* And now that we're done, push proxies for an enclosing lambda. */
9823 insert_pending_capture_proxies ();
9825 if (ok)
9826 lambda_expr = build_lambda_object (lambda_expr);
9827 else
9828 lambda_expr = error_mark_node;
9830 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9832 pop_deferring_access_checks ();
9834 return lambda_expr;
9837 /* Parse the beginning of a lambda expression.
9839 lambda-introducer:
9840 [ lambda-capture [opt] ]
9842 LAMBDA_EXPR is the current representation of the lambda expression. */
9844 static void
9845 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9847 /* Need commas after the first capture. */
9848 bool first = true;
9850 /* Eat the leading `['. */
9851 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9853 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9854 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9855 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9856 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9857 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9858 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9860 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9862 cp_lexer_consume_token (parser->lexer);
9863 first = false;
9866 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9868 cp_token* capture_token;
9869 tree capture_id;
9870 tree capture_init_expr;
9871 cp_id_kind idk = CP_ID_KIND_NONE;
9872 bool explicit_init_p = false;
9874 enum capture_kind_type
9876 BY_COPY,
9877 BY_REFERENCE
9879 enum capture_kind_type capture_kind = BY_COPY;
9881 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9883 error ("expected end of capture-list");
9884 return;
9887 if (first)
9888 first = false;
9889 else
9890 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9892 /* Possibly capture `this'. */
9893 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9895 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9896 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9897 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9898 "with by-copy capture default");
9899 cp_lexer_consume_token (parser->lexer);
9900 add_capture (lambda_expr,
9901 /*id=*/this_identifier,
9902 /*initializer=*/finish_this_expr(),
9903 /*by_reference_p=*/false,
9904 explicit_init_p);
9905 continue;
9908 /* Remember whether we want to capture as a reference or not. */
9909 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9911 capture_kind = BY_REFERENCE;
9912 cp_lexer_consume_token (parser->lexer);
9915 /* Get the identifier. */
9916 capture_token = cp_lexer_peek_token (parser->lexer);
9917 capture_id = cp_parser_identifier (parser);
9919 if (capture_id == error_mark_node)
9920 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9921 delimiters, but I modified this to stop on unnested ']' as well. It
9922 was already changed to stop on unnested '}', so the
9923 "closing_parenthesis" name is no more misleading with my change. */
9925 cp_parser_skip_to_closing_parenthesis (parser,
9926 /*recovering=*/true,
9927 /*or_comma=*/true,
9928 /*consume_paren=*/true);
9929 break;
9932 /* Find the initializer for this capture. */
9933 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9934 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9935 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9937 bool direct, non_constant;
9938 /* An explicit initializer exists. */
9939 if (cxx_dialect < cxx14)
9940 pedwarn (input_location, 0,
9941 "lambda capture initializers "
9942 "only available with -std=c++14 or -std=gnu++14");
9943 capture_init_expr = cp_parser_initializer (parser, &direct,
9944 &non_constant);
9945 explicit_init_p = true;
9946 if (capture_init_expr == NULL_TREE)
9948 error ("empty initializer for lambda init-capture");
9949 capture_init_expr = error_mark_node;
9952 else
9954 const char* error_msg;
9956 /* Turn the identifier into an id-expression. */
9957 capture_init_expr
9958 = cp_parser_lookup_name_simple (parser, capture_id,
9959 capture_token->location);
9961 if (capture_init_expr == error_mark_node)
9963 unqualified_name_lookup_error (capture_id);
9964 continue;
9966 else if (DECL_P (capture_init_expr)
9967 && (!VAR_P (capture_init_expr)
9968 && TREE_CODE (capture_init_expr) != PARM_DECL))
9970 error_at (capture_token->location,
9971 "capture of non-variable %qD ",
9972 capture_init_expr);
9973 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9974 "%q#D declared here", capture_init_expr);
9975 continue;
9977 if (VAR_P (capture_init_expr)
9978 && decl_storage_duration (capture_init_expr) != dk_auto)
9980 if (pedwarn (capture_token->location, 0, "capture of variable "
9981 "%qD with non-automatic storage duration",
9982 capture_init_expr))
9983 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9984 "%q#D declared here", capture_init_expr);
9985 continue;
9988 capture_init_expr
9989 = finish_id_expression
9990 (capture_id,
9991 capture_init_expr,
9992 parser->scope,
9993 &idk,
9994 /*integral_constant_expression_p=*/false,
9995 /*allow_non_integral_constant_expression_p=*/false,
9996 /*non_integral_constant_expression_p=*/NULL,
9997 /*template_p=*/false,
9998 /*done=*/true,
9999 /*address_p=*/false,
10000 /*template_arg_p=*/false,
10001 &error_msg,
10002 capture_token->location);
10004 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10006 cp_lexer_consume_token (parser->lexer);
10007 capture_init_expr = make_pack_expansion (capture_init_expr);
10009 else
10010 check_for_bare_parameter_packs (capture_init_expr);
10013 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10014 && !explicit_init_p)
10016 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10017 && capture_kind == BY_COPY)
10018 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10019 "of %qD redundant with by-copy capture default",
10020 capture_id);
10021 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10022 && capture_kind == BY_REFERENCE)
10023 pedwarn (capture_token->location, 0, "explicit by-reference "
10024 "capture of %qD redundant with by-reference capture "
10025 "default", capture_id);
10028 add_capture (lambda_expr,
10029 capture_id,
10030 capture_init_expr,
10031 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10032 explicit_init_p);
10035 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10038 /* Parse the (optional) middle of a lambda expression.
10040 lambda-declarator:
10041 < template-parameter-list [opt] >
10042 ( parameter-declaration-clause [opt] )
10043 attribute-specifier [opt]
10044 decl-specifier-seq [opt]
10045 exception-specification [opt]
10046 lambda-return-type-clause [opt]
10048 LAMBDA_EXPR is the current representation of the lambda expression. */
10050 static bool
10051 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10053 /* 5.1.1.4 of the standard says:
10054 If a lambda-expression does not include a lambda-declarator, it is as if
10055 the lambda-declarator were ().
10056 This means an empty parameter list, no attributes, and no exception
10057 specification. */
10058 tree param_list = void_list_node;
10059 tree attributes = NULL_TREE;
10060 tree exception_spec = NULL_TREE;
10061 tree template_param_list = NULL_TREE;
10062 tree tx_qual = NULL_TREE;
10063 cp_decl_specifier_seq lambda_specs;
10064 clear_decl_specs (&lambda_specs);
10066 /* The template-parameter-list is optional, but must begin with
10067 an opening angle if present. */
10068 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10070 if (cxx_dialect < cxx14)
10071 pedwarn (parser->lexer->next_token->location, 0,
10072 "lambda templates are only available with "
10073 "-std=c++14 or -std=gnu++14");
10075 cp_lexer_consume_token (parser->lexer);
10077 template_param_list = cp_parser_template_parameter_list (parser);
10079 cp_parser_skip_to_end_of_template_parameter_list (parser);
10081 /* We just processed one more parameter list. */
10082 ++parser->num_template_parameter_lists;
10085 /* The parameter-declaration-clause is optional (unless
10086 template-parameter-list was given), but must begin with an
10087 opening parenthesis if present. */
10088 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10090 cp_lexer_consume_token (parser->lexer);
10092 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10094 /* Parse parameters. */
10095 param_list = cp_parser_parameter_declaration_clause (parser);
10097 /* Default arguments shall not be specified in the
10098 parameter-declaration-clause of a lambda-declarator. */
10099 for (tree t = param_list; t; t = TREE_CHAIN (t))
10100 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
10101 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10102 "default argument specified for lambda parameter");
10104 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10106 attributes = cp_parser_attributes_opt (parser);
10108 /* In the decl-specifier-seq of the lambda-declarator, each
10109 decl-specifier shall either be mutable or constexpr. */
10110 int declares_class_or_enum;
10111 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10112 cp_parser_decl_specifier_seq (parser,
10113 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10114 &lambda_specs, &declares_class_or_enum);
10115 if (lambda_specs.storage_class == sc_mutable)
10117 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10118 if (lambda_specs.conflicting_specifiers_p)
10119 error_at (lambda_specs.locations[ds_storage_class],
10120 "duplicate %<mutable%>");
10123 tx_qual = cp_parser_tx_qualifier_opt (parser);
10125 /* Parse optional exception specification. */
10126 exception_spec = cp_parser_exception_specification_opt (parser);
10128 /* Parse optional trailing return type. */
10129 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10131 cp_lexer_consume_token (parser->lexer);
10132 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10133 = cp_parser_trailing_type_id (parser);
10136 /* The function parameters must be in scope all the way until after the
10137 trailing-return-type in case of decltype. */
10138 pop_bindings_and_leave_scope ();
10140 else if (template_param_list != NULL_TREE) // generate diagnostic
10141 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10143 /* Create the function call operator.
10145 Messing with declarators like this is no uglier than building up the
10146 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10147 other code. */
10149 cp_decl_specifier_seq return_type_specs;
10150 cp_declarator* declarator;
10151 tree fco;
10152 int quals;
10153 void *p;
10155 clear_decl_specs (&return_type_specs);
10156 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10157 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
10158 else
10159 /* Maybe we will deduce the return type later. */
10160 return_type_specs.type = make_auto ();
10162 if (lambda_specs.locations[ds_constexpr])
10164 if (cxx_dialect >= cxx1z)
10165 return_type_specs.locations[ds_constexpr]
10166 = lambda_specs.locations[ds_constexpr];
10167 else
10168 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10169 "lambda only available with -std=c++1z or -std=gnu++1z");
10172 p = obstack_alloc (&declarator_obstack, 0);
10174 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
10175 sfk_none);
10177 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10178 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10179 declarator = make_call_declarator (declarator, param_list, quals,
10180 VIRT_SPEC_UNSPECIFIED,
10181 REF_QUAL_NONE,
10182 tx_qual,
10183 exception_spec,
10184 /*late_return_type=*/NULL_TREE,
10185 /*requires_clause*/NULL_TREE);
10186 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10188 fco = grokmethod (&return_type_specs,
10189 declarator,
10190 attributes);
10191 if (fco != error_mark_node)
10193 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10194 DECL_ARTIFICIAL (fco) = 1;
10195 /* Give the object parameter a different name. */
10196 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10197 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10198 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10200 if (template_param_list)
10202 fco = finish_member_template_decl (fco);
10203 finish_template_decl (template_param_list);
10204 --parser->num_template_parameter_lists;
10206 else if (parser->fully_implicit_function_template_p)
10207 fco = finish_fully_implicit_template (parser, fco);
10209 finish_member_declaration (fco);
10211 obstack_free (&declarator_obstack, p);
10213 return (fco != error_mark_node);
10217 /* Parse the body of a lambda expression, which is simply
10219 compound-statement
10221 but which requires special handling.
10222 LAMBDA_EXPR is the current representation of the lambda expression. */
10224 static void
10225 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10227 bool nested = (current_function_decl != NULL_TREE);
10228 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10229 if (nested)
10230 push_function_context ();
10231 else
10232 /* Still increment function_depth so that we don't GC in the
10233 middle of an expression. */
10234 ++function_depth;
10235 vec<tree> omp_privatization_save;
10236 save_omp_privatization_clauses (omp_privatization_save);
10237 /* Clear this in case we're in the middle of a default argument. */
10238 parser->local_variables_forbidden_p = false;
10240 /* Finish the function call operator
10241 - class_specifier
10242 + late_parsing_for_member
10243 + function_definition_after_declarator
10244 + ctor_initializer_opt_and_function_body */
10246 tree fco = lambda_function (lambda_expr);
10247 tree body;
10248 bool done = false;
10249 tree compound_stmt;
10250 tree cap;
10252 /* Let the front end know that we are going to be defining this
10253 function. */
10254 start_preparsed_function (fco,
10255 NULL_TREE,
10256 SF_PRE_PARSED | SF_INCLASS_INLINE);
10258 start_lambda_scope (fco);
10259 body = begin_function_body ();
10261 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10262 goto out;
10264 /* Push the proxies for any explicit captures. */
10265 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
10266 cap = TREE_CHAIN (cap))
10267 build_capture_proxy (TREE_PURPOSE (cap));
10269 compound_stmt = begin_compound_stmt (0);
10271 /* 5.1.1.4 of the standard says:
10272 If a lambda-expression does not include a trailing-return-type, it
10273 is as if the trailing-return-type denotes the following type:
10274 * if the compound-statement is of the form
10275 { return attribute-specifier [opt] expression ; }
10276 the type of the returned expression after lvalue-to-rvalue
10277 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10278 (_conv.array_ 4.2), and function-to-pointer conversion
10279 (_conv.func_ 4.3);
10280 * otherwise, void. */
10282 /* In a lambda that has neither a lambda-return-type-clause
10283 nor a deducible form, errors should be reported for return statements
10284 in the body. Since we used void as the placeholder return type, parsing
10285 the body as usual will give such desired behavior. */
10286 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10287 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10288 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10290 tree expr = NULL_TREE;
10291 cp_id_kind idk = CP_ID_KIND_NONE;
10293 /* Parse tentatively in case there's more after the initial return
10294 statement. */
10295 cp_parser_parse_tentatively (parser);
10297 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10299 expr = cp_parser_expression (parser, &idk);
10301 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10302 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10304 if (cp_parser_parse_definitely (parser))
10306 if (!processing_template_decl)
10308 tree type = lambda_return_type (expr);
10309 apply_deduced_return_type (fco, type);
10310 if (type == error_mark_node)
10311 expr = error_mark_node;
10314 /* Will get error here if type not deduced yet. */
10315 finish_return_stmt (expr);
10317 done = true;
10321 if (!done)
10323 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10324 cp_parser_label_declaration (parser);
10325 cp_parser_statement_seq_opt (parser, NULL_TREE);
10326 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10329 finish_compound_stmt (compound_stmt);
10331 out:
10332 finish_function_body (body);
10333 finish_lambda_scope ();
10335 /* Finish the function and generate code for it if necessary. */
10336 tree fn = finish_function (/*inline*/2);
10338 /* Only expand if the call op is not a template. */
10339 if (!DECL_TEMPLATE_INFO (fco))
10340 expand_or_defer_fn (fn);
10343 restore_omp_privatization_clauses (omp_privatization_save);
10344 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10345 if (nested)
10346 pop_function_context();
10347 else
10348 --function_depth;
10351 /* Statements [gram.stmt.stmt] */
10353 /* Parse a statement.
10355 statement:
10356 labeled-statement
10357 expression-statement
10358 compound-statement
10359 selection-statement
10360 iteration-statement
10361 jump-statement
10362 declaration-statement
10363 try-block
10365 C++11:
10367 statement:
10368 labeled-statement
10369 attribute-specifier-seq (opt) expression-statement
10370 attribute-specifier-seq (opt) compound-statement
10371 attribute-specifier-seq (opt) selection-statement
10372 attribute-specifier-seq (opt) iteration-statement
10373 attribute-specifier-seq (opt) jump-statement
10374 declaration-statement
10375 attribute-specifier-seq (opt) try-block
10377 TM Extension:
10379 statement:
10380 atomic-statement
10382 IN_COMPOUND is true when the statement is nested inside a
10383 cp_parser_compound_statement; this matters for certain pragmas.
10385 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10386 is a (possibly labeled) if statement which is not enclosed in braces
10387 and has an else clause. This is used to implement -Wparentheses.
10389 CHAIN is a vector of if-else-if conditions. */
10391 static void
10392 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10393 bool in_compound, bool *if_p, vec<tree> *chain)
10395 tree statement, std_attrs = NULL_TREE;
10396 cp_token *token;
10397 location_t statement_location, attrs_location;
10399 restart:
10400 if (if_p != NULL)
10401 *if_p = false;
10402 /* There is no statement yet. */
10403 statement = NULL_TREE;
10405 saved_token_sentinel saved_tokens (parser->lexer);
10406 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10407 if (c_dialect_objc ())
10408 /* In obj-c++, seeing '[[' might be the either the beginning of
10409 c++11 attributes, or a nested objc-message-expression. So
10410 let's parse the c++11 attributes tentatively. */
10411 cp_parser_parse_tentatively (parser);
10412 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10413 if (c_dialect_objc ())
10415 if (!cp_parser_parse_definitely (parser))
10416 std_attrs = NULL_TREE;
10419 /* Peek at the next token. */
10420 token = cp_lexer_peek_token (parser->lexer);
10421 /* Remember the location of the first token in the statement. */
10422 statement_location = token->location;
10423 /* If this is a keyword, then that will often determine what kind of
10424 statement we have. */
10425 if (token->type == CPP_KEYWORD)
10427 enum rid keyword = token->keyword;
10429 switch (keyword)
10431 case RID_CASE:
10432 case RID_DEFAULT:
10433 /* Looks like a labeled-statement with a case label.
10434 Parse the label, and then use tail recursion to parse
10435 the statement. */
10436 cp_parser_label_for_labeled_statement (parser, std_attrs);
10437 in_compound = false;
10438 goto restart;
10440 case RID_IF:
10441 case RID_SWITCH:
10442 statement = cp_parser_selection_statement (parser, if_p, chain);
10443 break;
10445 case RID_WHILE:
10446 case RID_DO:
10447 case RID_FOR:
10448 statement = cp_parser_iteration_statement (parser, if_p, false);
10449 break;
10451 case RID_CILK_FOR:
10452 if (!flag_cilkplus)
10454 error_at (cp_lexer_peek_token (parser->lexer)->location,
10455 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10456 cp_lexer_consume_token (parser->lexer);
10457 statement = error_mark_node;
10459 else
10460 statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10461 break;
10463 case RID_BREAK:
10464 case RID_CONTINUE:
10465 case RID_RETURN:
10466 case RID_GOTO:
10467 statement = cp_parser_jump_statement (parser);
10468 break;
10470 case RID_CILK_SYNC:
10471 cp_lexer_consume_token (parser->lexer);
10472 if (flag_cilkplus)
10474 tree sync_expr = build_cilk_sync ();
10475 SET_EXPR_LOCATION (sync_expr,
10476 token->location);
10477 statement = finish_expr_stmt (sync_expr);
10479 else
10481 error_at (token->location, "-fcilkplus must be enabled to use"
10482 " %<_Cilk_sync%>");
10483 statement = error_mark_node;
10485 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10486 break;
10488 /* Objective-C++ exception-handling constructs. */
10489 case RID_AT_TRY:
10490 case RID_AT_CATCH:
10491 case RID_AT_FINALLY:
10492 case RID_AT_SYNCHRONIZED:
10493 case RID_AT_THROW:
10494 statement = cp_parser_objc_statement (parser);
10495 break;
10497 case RID_TRY:
10498 statement = cp_parser_try_block (parser);
10499 break;
10501 case RID_NAMESPACE:
10502 /* This must be a namespace alias definition. */
10503 cp_parser_declaration_statement (parser);
10504 return;
10506 case RID_TRANSACTION_ATOMIC:
10507 case RID_TRANSACTION_RELAXED:
10508 case RID_SYNCHRONIZED:
10509 case RID_ATOMIC_NOEXCEPT:
10510 case RID_ATOMIC_CANCEL:
10511 statement = cp_parser_transaction (parser, token);
10512 break;
10513 case RID_TRANSACTION_CANCEL:
10514 statement = cp_parser_transaction_cancel (parser);
10515 break;
10517 default:
10518 /* It might be a keyword like `int' that can start a
10519 declaration-statement. */
10520 break;
10523 else if (token->type == CPP_NAME)
10525 /* If the next token is a `:', then we are looking at a
10526 labeled-statement. */
10527 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10528 if (token->type == CPP_COLON)
10530 /* Looks like a labeled-statement with an ordinary label.
10531 Parse the label, and then use tail recursion to parse
10532 the statement. */
10534 cp_parser_label_for_labeled_statement (parser, std_attrs);
10535 in_compound = false;
10536 goto restart;
10539 /* Anything that starts with a `{' must be a compound-statement. */
10540 else if (token->type == CPP_OPEN_BRACE)
10541 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10542 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10543 a statement all its own. */
10544 else if (token->type == CPP_PRAGMA)
10546 /* Only certain OpenMP pragmas are attached to statements, and thus
10547 are considered statements themselves. All others are not. In
10548 the context of a compound, accept the pragma as a "statement" and
10549 return so that we can check for a close brace. Otherwise we
10550 require a real statement and must go back and read one. */
10551 if (in_compound)
10552 cp_parser_pragma (parser, pragma_compound, if_p);
10553 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10554 goto restart;
10555 return;
10557 else if (token->type == CPP_EOF)
10559 cp_parser_error (parser, "expected statement");
10560 return;
10563 /* Everything else must be a declaration-statement or an
10564 expression-statement. Try for the declaration-statement
10565 first, unless we are looking at a `;', in which case we know that
10566 we have an expression-statement. */
10567 if (!statement)
10569 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10571 if (std_attrs != NULL_TREE)
10573 /* Attributes should be parsed as part of the the
10574 declaration, so let's un-parse them. */
10575 saved_tokens.rollback();
10576 std_attrs = NULL_TREE;
10579 cp_parser_parse_tentatively (parser);
10580 /* Try to parse the declaration-statement. */
10581 cp_parser_declaration_statement (parser);
10582 /* If that worked, we're done. */
10583 if (cp_parser_parse_definitely (parser))
10584 return;
10586 /* Look for an expression-statement instead. */
10587 statement = cp_parser_expression_statement (parser, in_statement_expr);
10590 /* Set the line number for the statement. */
10591 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10592 SET_EXPR_LOCATION (statement, statement_location);
10594 /* Note that for now, we don't do anything with c++11 statements
10595 parsed at this level. */
10596 if (std_attrs != NULL_TREE)
10597 warning_at (attrs_location,
10598 OPT_Wattributes,
10599 "attributes at the beginning of statement are ignored");
10602 /* Parse the label for a labeled-statement, i.e.
10604 identifier :
10605 case constant-expression :
10606 default :
10608 GNU Extension:
10609 case constant-expression ... constant-expression : statement
10611 When a label is parsed without errors, the label is added to the
10612 parse tree by the finish_* functions, so this function doesn't
10613 have to return the label. */
10615 static void
10616 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10618 cp_token *token;
10619 tree label = NULL_TREE;
10620 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10622 /* The next token should be an identifier. */
10623 token = cp_lexer_peek_token (parser->lexer);
10624 if (token->type != CPP_NAME
10625 && token->type != CPP_KEYWORD)
10627 cp_parser_error (parser, "expected labeled-statement");
10628 return;
10631 parser->colon_corrects_to_scope_p = false;
10632 switch (token->keyword)
10634 case RID_CASE:
10636 tree expr, expr_hi;
10637 cp_token *ellipsis;
10639 /* Consume the `case' token. */
10640 cp_lexer_consume_token (parser->lexer);
10641 /* Parse the constant-expression. */
10642 expr = cp_parser_constant_expression (parser);
10643 if (check_for_bare_parameter_packs (expr))
10644 expr = error_mark_node;
10646 ellipsis = cp_lexer_peek_token (parser->lexer);
10647 if (ellipsis->type == CPP_ELLIPSIS)
10649 /* Consume the `...' token. */
10650 cp_lexer_consume_token (parser->lexer);
10651 expr_hi = cp_parser_constant_expression (parser);
10652 if (check_for_bare_parameter_packs (expr_hi))
10653 expr_hi = error_mark_node;
10655 /* We don't need to emit warnings here, as the common code
10656 will do this for us. */
10658 else
10659 expr_hi = NULL_TREE;
10661 if (parser->in_switch_statement_p)
10662 finish_case_label (token->location, expr, expr_hi);
10663 else
10664 error_at (token->location,
10665 "case label %qE not within a switch statement",
10666 expr);
10668 break;
10670 case RID_DEFAULT:
10671 /* Consume the `default' token. */
10672 cp_lexer_consume_token (parser->lexer);
10674 if (parser->in_switch_statement_p)
10675 finish_case_label (token->location, NULL_TREE, NULL_TREE);
10676 else
10677 error_at (token->location, "case label not within a switch statement");
10678 break;
10680 default:
10681 /* Anything else must be an ordinary label. */
10682 label = finish_label_stmt (cp_parser_identifier (parser));
10683 break;
10686 /* Require the `:' token. */
10687 cp_parser_require (parser, CPP_COLON, RT_COLON);
10689 /* An ordinary label may optionally be followed by attributes.
10690 However, this is only permitted if the attributes are then
10691 followed by a semicolon. This is because, for backward
10692 compatibility, when parsing
10693 lab: __attribute__ ((unused)) int i;
10694 we want the attribute to attach to "i", not "lab". */
10695 if (label != NULL_TREE
10696 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10698 tree attrs;
10699 cp_parser_parse_tentatively (parser);
10700 attrs = cp_parser_gnu_attributes_opt (parser);
10701 if (attrs == NULL_TREE
10702 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10703 cp_parser_abort_tentative_parse (parser);
10704 else if (!cp_parser_parse_definitely (parser))
10706 else
10707 attributes = chainon (attributes, attrs);
10710 if (attributes != NULL_TREE)
10711 cplus_decl_attributes (&label, attributes, 0);
10713 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10716 /* Parse an expression-statement.
10718 expression-statement:
10719 expression [opt] ;
10721 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10722 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10723 indicates whether this expression-statement is part of an
10724 expression statement. */
10726 static tree
10727 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10729 tree statement = NULL_TREE;
10730 cp_token *token = cp_lexer_peek_token (parser->lexer);
10732 /* If the next token is a ';', then there is no expression
10733 statement. */
10734 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10736 statement = cp_parser_expression (parser);
10737 if (statement == error_mark_node
10738 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10740 cp_parser_skip_to_end_of_block_or_statement (parser);
10741 return error_mark_node;
10745 /* Give a helpful message for "A<T>::type t;" and the like. */
10746 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10747 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10749 if (TREE_CODE (statement) == SCOPE_REF)
10750 error_at (token->location, "need %<typename%> before %qE because "
10751 "%qT is a dependent scope",
10752 statement, TREE_OPERAND (statement, 0));
10753 else if (is_overloaded_fn (statement)
10754 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10756 /* A::A a; */
10757 tree fn = get_first_fn (statement);
10758 error_at (token->location,
10759 "%<%T::%D%> names the constructor, not the type",
10760 DECL_CONTEXT (fn), DECL_NAME (fn));
10764 /* Consume the final `;'. */
10765 cp_parser_consume_semicolon_at_end_of_statement (parser);
10767 if (in_statement_expr
10768 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10769 /* This is the final expression statement of a statement
10770 expression. */
10771 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10772 else if (statement)
10773 statement = finish_expr_stmt (statement);
10775 return statement;
10778 /* Parse a compound-statement.
10780 compound-statement:
10781 { statement-seq [opt] }
10783 GNU extension:
10785 compound-statement:
10786 { label-declaration-seq [opt] statement-seq [opt] }
10788 label-declaration-seq:
10789 label-declaration
10790 label-declaration-seq label-declaration
10792 Returns a tree representing the statement. */
10794 static tree
10795 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10796 int bcs_flags, bool function_body)
10798 tree compound_stmt;
10800 /* Consume the `{'. */
10801 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10802 return error_mark_node;
10803 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10804 && !function_body && cxx_dialect < cxx14)
10805 pedwarn (input_location, OPT_Wpedantic,
10806 "compound-statement in constexpr function");
10807 /* Begin the compound-statement. */
10808 compound_stmt = begin_compound_stmt (bcs_flags);
10809 /* If the next keyword is `__label__' we have a label declaration. */
10810 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10811 cp_parser_label_declaration (parser);
10812 /* Parse an (optional) statement-seq. */
10813 cp_parser_statement_seq_opt (parser, in_statement_expr);
10814 /* Finish the compound-statement. */
10815 finish_compound_stmt (compound_stmt);
10816 /* Consume the `}'. */
10817 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10819 return compound_stmt;
10822 /* Parse an (optional) statement-seq.
10824 statement-seq:
10825 statement
10826 statement-seq [opt] statement */
10828 static void
10829 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10831 /* Scan statements until there aren't any more. */
10832 while (true)
10834 cp_token *token = cp_lexer_peek_token (parser->lexer);
10836 /* If we are looking at a `}', then we have run out of
10837 statements; the same is true if we have reached the end
10838 of file, or have stumbled upon a stray '@end'. */
10839 if (token->type == CPP_CLOSE_BRACE
10840 || token->type == CPP_EOF
10841 || token->type == CPP_PRAGMA_EOL
10842 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10843 break;
10845 /* If we are in a compound statement and find 'else' then
10846 something went wrong. */
10847 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10849 if (parser->in_statement & IN_IF_STMT)
10850 break;
10851 else
10853 token = cp_lexer_consume_token (parser->lexer);
10854 error_at (token->location, "%<else%> without a previous %<if%>");
10858 /* Parse the statement. */
10859 cp_parser_statement (parser, in_statement_expr, true, NULL);
10863 /* Parse a selection-statement.
10865 selection-statement:
10866 if ( condition ) statement
10867 if ( condition ) statement else statement
10868 switch ( condition ) statement
10870 Returns the new IF_STMT or SWITCH_STMT.
10872 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10873 is a (possibly labeled) if statement which is not enclosed in
10874 braces and has an else clause. This is used to implement
10875 -Wparentheses.
10877 CHAIN is a vector of if-else-if conditions. This is used to implement
10878 -Wduplicated-cond. */
10880 static tree
10881 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
10882 vec<tree> *chain)
10884 cp_token *token;
10885 enum rid keyword;
10886 token_indent_info guard_tinfo;
10888 if (if_p != NULL)
10889 *if_p = false;
10891 /* Peek at the next token. */
10892 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10893 guard_tinfo = get_token_indent_info (token);
10895 /* See what kind of keyword it is. */
10896 keyword = token->keyword;
10897 switch (keyword)
10899 case RID_IF:
10900 case RID_SWITCH:
10902 tree statement;
10903 tree condition;
10905 bool cx = false;
10906 if (keyword == RID_IF
10907 && cp_lexer_next_token_is_keyword (parser->lexer,
10908 RID_CONSTEXPR))
10910 cx = true;
10911 cp_token *tok = cp_lexer_consume_token (parser->lexer);
10912 if (cxx_dialect < cxx1z && !in_system_header_at (tok->location))
10913 pedwarn (tok->location, 0, "%<if constexpr%> only available "
10914 "with -std=c++1z or -std=gnu++1z");
10917 /* Look for the `('. */
10918 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10920 cp_parser_skip_to_end_of_statement (parser);
10921 return error_mark_node;
10924 /* Begin the selection-statement. */
10925 if (keyword == RID_IF)
10927 statement = begin_if_stmt ();
10928 IF_STMT_CONSTEXPR_P (statement) = cx;
10930 else
10931 statement = begin_switch_stmt ();
10933 /* Parse the condition. */
10934 condition = cp_parser_condition (parser);
10935 /* Look for the `)'. */
10936 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10937 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10938 /*consume_paren=*/true);
10940 if (keyword == RID_IF)
10942 bool nested_if;
10943 unsigned char in_statement;
10945 /* Add the condition. */
10946 condition = finish_if_stmt_cond (condition, statement);
10948 if (warn_duplicated_cond)
10949 warn_duplicated_cond_add_or_warn (token->location, condition,
10950 &chain);
10952 /* Parse the then-clause. */
10953 in_statement = parser->in_statement;
10954 parser->in_statement |= IN_IF_STMT;
10956 /* Outside a template, the non-selected branch of a constexpr
10957 if is a 'discarded statement', i.e. unevaluated. */
10958 bool was_discarded = parser->in_discarded_stmt;
10959 bool discard_then = (cx && !processing_template_decl
10960 && integer_zerop (condition));
10961 if (discard_then)
10963 parser->in_discarded_stmt = true;
10964 ++c_inhibit_evaluation_warnings;
10967 cp_parser_implicitly_scoped_statement (parser, &nested_if,
10968 guard_tinfo);
10970 parser->in_statement = in_statement;
10972 finish_then_clause (statement);
10974 if (discard_then)
10976 THEN_CLAUSE (statement) = NULL_TREE;
10977 parser->in_discarded_stmt = was_discarded;
10978 --c_inhibit_evaluation_warnings;
10981 /* If the next token is `else', parse the else-clause. */
10982 if (cp_lexer_next_token_is_keyword (parser->lexer,
10983 RID_ELSE))
10985 bool discard_else = (cx && !processing_template_decl
10986 && integer_nonzerop (condition));
10987 if (discard_else)
10989 parser->in_discarded_stmt = true;
10990 ++c_inhibit_evaluation_warnings;
10993 guard_tinfo
10994 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
10995 /* Consume the `else' keyword. */
10996 cp_lexer_consume_token (parser->lexer);
10997 if (warn_duplicated_cond)
10999 if (cp_lexer_next_token_is_keyword (parser->lexer,
11000 RID_IF)
11001 && chain == NULL)
11003 /* We've got "if (COND) else if (COND2)". Start
11004 the condition chain and add COND as the first
11005 element. */
11006 chain = new vec<tree> ();
11007 if (!CONSTANT_CLASS_P (condition)
11008 && !TREE_SIDE_EFFECTS (condition))
11010 /* Wrap it in a NOP_EXPR so that we can set the
11011 location of the condition. */
11012 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11013 condition);
11014 SET_EXPR_LOCATION (e, token->location);
11015 chain->safe_push (e);
11018 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11019 RID_IF))
11021 /* This is if-else without subsequent if. Zap the
11022 condition chain; we would have already warned at
11023 this point. */
11024 delete chain;
11025 chain = NULL;
11028 begin_else_clause (statement);
11029 /* Parse the else-clause. */
11030 cp_parser_implicitly_scoped_statement (parser, NULL,
11031 guard_tinfo, chain);
11033 finish_else_clause (statement);
11035 /* If we are currently parsing a then-clause, then
11036 IF_P will not be NULL. We set it to true to
11037 indicate that this if statement has an else clause.
11038 This may trigger the Wparentheses warning below
11039 when we get back up to the parent if statement. */
11040 if (if_p != NULL)
11041 *if_p = true;
11043 if (discard_else)
11045 ELSE_CLAUSE (statement) = NULL_TREE;
11046 parser->in_discarded_stmt = was_discarded;
11047 --c_inhibit_evaluation_warnings;
11050 else
11052 /* This if statement does not have an else clause. If
11053 NESTED_IF is true, then the then-clause has an if
11054 statement which does have an else clause. We warn
11055 about the potential ambiguity. */
11056 if (nested_if)
11057 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11058 "suggest explicit braces to avoid ambiguous"
11059 " %<else%>");
11060 if (warn_duplicated_cond)
11062 /* We don't need the condition chain anymore. */
11063 delete chain;
11064 chain = NULL;
11068 /* Now we're all done with the if-statement. */
11069 finish_if_stmt (statement);
11071 else
11073 bool in_switch_statement_p;
11074 unsigned char in_statement;
11076 /* Add the condition. */
11077 finish_switch_cond (condition, statement);
11079 /* Parse the body of the switch-statement. */
11080 in_switch_statement_p = parser->in_switch_statement_p;
11081 in_statement = parser->in_statement;
11082 parser->in_switch_statement_p = true;
11083 parser->in_statement |= IN_SWITCH_STMT;
11084 cp_parser_implicitly_scoped_statement (parser, if_p,
11085 guard_tinfo);
11086 parser->in_switch_statement_p = in_switch_statement_p;
11087 parser->in_statement = in_statement;
11089 /* Now we're all done with the switch-statement. */
11090 finish_switch_stmt (statement);
11093 return statement;
11095 break;
11097 default:
11098 cp_parser_error (parser, "expected selection-statement");
11099 return error_mark_node;
11103 /* Parse a condition.
11105 condition:
11106 expression
11107 type-specifier-seq declarator = initializer-clause
11108 type-specifier-seq declarator braced-init-list
11110 GNU Extension:
11112 condition:
11113 type-specifier-seq declarator asm-specification [opt]
11114 attributes [opt] = assignment-expression
11116 Returns the expression that should be tested. */
11118 static tree
11119 cp_parser_condition (cp_parser* parser)
11121 cp_decl_specifier_seq type_specifiers;
11122 const char *saved_message;
11123 int declares_class_or_enum;
11125 /* Try the declaration first. */
11126 cp_parser_parse_tentatively (parser);
11127 /* New types are not allowed in the type-specifier-seq for a
11128 condition. */
11129 saved_message = parser->type_definition_forbidden_message;
11130 parser->type_definition_forbidden_message
11131 = G_("types may not be defined in conditions");
11132 /* Parse the type-specifier-seq. */
11133 cp_parser_decl_specifier_seq (parser,
11134 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11135 &type_specifiers,
11136 &declares_class_or_enum);
11137 /* Restore the saved message. */
11138 parser->type_definition_forbidden_message = saved_message;
11139 /* If all is well, we might be looking at a declaration. */
11140 if (!cp_parser_error_occurred (parser))
11142 tree decl;
11143 tree asm_specification;
11144 tree attributes;
11145 cp_declarator *declarator;
11146 tree initializer = NULL_TREE;
11148 /* Parse the declarator. */
11149 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11150 /*ctor_dtor_or_conv_p=*/NULL,
11151 /*parenthesized_p=*/NULL,
11152 /*member_p=*/false,
11153 /*friend_p=*/false);
11154 /* Parse the attributes. */
11155 attributes = cp_parser_attributes_opt (parser);
11156 /* Parse the asm-specification. */
11157 asm_specification = cp_parser_asm_specification_opt (parser);
11158 /* If the next token is not an `=' or '{', then we might still be
11159 looking at an expression. For example:
11161 if (A(a).x)
11163 looks like a decl-specifier-seq and a declarator -- but then
11164 there is no `=', so this is an expression. */
11165 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11166 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11167 cp_parser_simulate_error (parser);
11169 /* If we did see an `=' or '{', then we are looking at a declaration
11170 for sure. */
11171 if (cp_parser_parse_definitely (parser))
11173 tree pushed_scope;
11174 bool non_constant_p;
11175 bool flags = LOOKUP_ONLYCONVERTING;
11177 /* Create the declaration. */
11178 decl = start_decl (declarator, &type_specifiers,
11179 /*initialized_p=*/true,
11180 attributes, /*prefix_attributes=*/NULL_TREE,
11181 &pushed_scope);
11183 /* Parse the initializer. */
11184 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11186 initializer = cp_parser_braced_list (parser, &non_constant_p);
11187 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11188 flags = 0;
11190 else
11192 /* Consume the `='. */
11193 cp_parser_require (parser, CPP_EQ, RT_EQ);
11194 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11196 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11197 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11199 /* Process the initializer. */
11200 cp_finish_decl (decl,
11201 initializer, !non_constant_p,
11202 asm_specification,
11203 flags);
11205 if (pushed_scope)
11206 pop_scope (pushed_scope);
11208 return convert_from_reference (decl);
11211 /* If we didn't even get past the declarator successfully, we are
11212 definitely not looking at a declaration. */
11213 else
11214 cp_parser_abort_tentative_parse (parser);
11216 /* Otherwise, we are looking at an expression. */
11217 return cp_parser_expression (parser);
11220 /* Parses a for-statement or range-for-statement until the closing ')',
11221 not included. */
11223 static tree
11224 cp_parser_for (cp_parser *parser, bool ivdep)
11226 tree init, scope, decl;
11227 bool is_range_for;
11229 /* Begin the for-statement. */
11230 scope = begin_for_scope (&init);
11232 /* Parse the initialization. */
11233 is_range_for = cp_parser_for_init_statement (parser, &decl);
11235 if (is_range_for)
11236 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11237 else
11238 return cp_parser_c_for (parser, scope, init, ivdep);
11241 static tree
11242 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11244 /* Normal for loop */
11245 tree condition = NULL_TREE;
11246 tree expression = NULL_TREE;
11247 tree stmt;
11249 stmt = begin_for_stmt (scope, init);
11250 /* The for-init-statement has already been parsed in
11251 cp_parser_for_init_statement, so no work is needed here. */
11252 finish_for_init_stmt (stmt);
11254 /* If there's a condition, process it. */
11255 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11256 condition = cp_parser_condition (parser);
11257 else if (ivdep)
11259 cp_parser_error (parser, "missing loop condition in loop with "
11260 "%<GCC ivdep%> pragma");
11261 condition = error_mark_node;
11263 finish_for_cond (condition, stmt, ivdep);
11264 /* Look for the `;'. */
11265 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11267 /* If there's an expression, process it. */
11268 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11269 expression = cp_parser_expression (parser);
11270 finish_for_expr (expression, stmt);
11272 return stmt;
11275 /* Tries to parse a range-based for-statement:
11277 range-based-for:
11278 decl-specifier-seq declarator : expression
11280 The decl-specifier-seq declarator and the `:' are already parsed by
11281 cp_parser_for_init_statement. If processing_template_decl it returns a
11282 newly created RANGE_FOR_STMT; if not, it is converted to a
11283 regular FOR_STMT. */
11285 static tree
11286 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11287 bool ivdep)
11289 tree stmt, range_expr;
11290 cxx_binding *binding = NULL;
11291 tree name = NULL_TREE;
11293 /* Get the range declaration momentarily out of the way so that
11294 the range expression doesn't clash with it. */
11295 if (range_decl != error_mark_node)
11297 name = DECL_NAME (range_decl);
11298 binding = IDENTIFIER_BINDING (name);
11299 IDENTIFIER_BINDING (name) = binding->previous;
11302 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11304 bool expr_non_constant_p;
11305 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11307 else
11308 range_expr = cp_parser_expression (parser);
11310 /* Put the range declaration back into scope. */
11311 if (range_decl != error_mark_node)
11313 binding->previous = IDENTIFIER_BINDING (name);
11314 IDENTIFIER_BINDING (name) = binding;
11317 /* If in template, STMT is converted to a normal for-statement
11318 at instantiation. If not, it is done just ahead. */
11319 if (processing_template_decl)
11321 if (check_for_bare_parameter_packs (range_expr))
11322 range_expr = error_mark_node;
11323 stmt = begin_range_for_stmt (scope, init);
11324 if (ivdep)
11325 RANGE_FOR_IVDEP (stmt) = 1;
11326 finish_range_for_decl (stmt, range_decl, range_expr);
11327 if (!type_dependent_expression_p (range_expr)
11328 /* do_auto_deduction doesn't mess with template init-lists. */
11329 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11330 do_range_for_auto_deduction (range_decl, range_expr);
11332 else
11334 stmt = begin_for_stmt (scope, init);
11335 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
11337 return stmt;
11340 /* Subroutine of cp_convert_range_for: given the initializer expression,
11341 builds up the range temporary. */
11343 static tree
11344 build_range_temp (tree range_expr)
11346 tree range_type, range_temp;
11348 /* Find out the type deduced by the declaration
11349 `auto &&__range = range_expr'. */
11350 range_type = cp_build_reference_type (make_auto (), true);
11351 range_type = do_auto_deduction (range_type, range_expr,
11352 type_uses_auto (range_type));
11354 /* Create the __range variable. */
11355 range_temp = build_decl (input_location, VAR_DECL,
11356 get_identifier ("__for_range"), range_type);
11357 TREE_USED (range_temp) = 1;
11358 DECL_ARTIFICIAL (range_temp) = 1;
11360 return range_temp;
11363 /* Used by cp_parser_range_for in template context: we aren't going to
11364 do a full conversion yet, but we still need to resolve auto in the
11365 type of the for-range-declaration if present. This is basically
11366 a shortcut version of cp_convert_range_for. */
11368 static void
11369 do_range_for_auto_deduction (tree decl, tree range_expr)
11371 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11372 if (auto_node)
11374 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11375 range_temp = convert_from_reference (build_range_temp (range_expr));
11376 iter_type = (cp_parser_perform_range_for_lookup
11377 (range_temp, &begin_dummy, &end_dummy));
11378 if (iter_type)
11380 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11381 iter_type);
11382 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11383 tf_warning_or_error);
11384 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11385 iter_decl, auto_node);
11390 /* Converts a range-based for-statement into a normal
11391 for-statement, as per the definition.
11393 for (RANGE_DECL : RANGE_EXPR)
11394 BLOCK
11396 should be equivalent to:
11399 auto &&__range = RANGE_EXPR;
11400 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11401 __begin != __end;
11402 ++__begin)
11404 RANGE_DECL = *__begin;
11405 BLOCK
11409 If RANGE_EXPR is an array:
11410 BEGIN_EXPR = __range
11411 END_EXPR = __range + ARRAY_SIZE(__range)
11412 Else if RANGE_EXPR has a member 'begin' or 'end':
11413 BEGIN_EXPR = __range.begin()
11414 END_EXPR = __range.end()
11415 Else:
11416 BEGIN_EXPR = begin(__range)
11417 END_EXPR = end(__range);
11419 If __range has a member 'begin' but not 'end', or vice versa, we must
11420 still use the second alternative (it will surely fail, however).
11421 When calling begin()/end() in the third alternative we must use
11422 argument dependent lookup, but always considering 'std' as an associated
11423 namespace. */
11425 tree
11426 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11427 bool ivdep)
11429 tree begin, end;
11430 tree iter_type, begin_expr, end_expr;
11431 tree condition, expression;
11433 if (range_decl == error_mark_node || range_expr == error_mark_node)
11434 /* If an error happened previously do nothing or else a lot of
11435 unhelpful errors would be issued. */
11436 begin_expr = end_expr = iter_type = error_mark_node;
11437 else
11439 tree range_temp;
11441 if (VAR_P (range_expr)
11442 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11443 /* Can't bind a reference to an array of runtime bound. */
11444 range_temp = range_expr;
11445 else
11447 range_temp = build_range_temp (range_expr);
11448 pushdecl (range_temp);
11449 cp_finish_decl (range_temp, range_expr,
11450 /*is_constant_init*/false, NULL_TREE,
11451 LOOKUP_ONLYCONVERTING);
11452 range_temp = convert_from_reference (range_temp);
11454 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11455 &begin_expr, &end_expr);
11458 /* The new for initialization statement. */
11459 begin = build_decl (input_location, VAR_DECL,
11460 get_identifier ("__for_begin"), iter_type);
11461 TREE_USED (begin) = 1;
11462 DECL_ARTIFICIAL (begin) = 1;
11463 pushdecl (begin);
11464 cp_finish_decl (begin, begin_expr,
11465 /*is_constant_init*/false, NULL_TREE,
11466 LOOKUP_ONLYCONVERTING);
11468 if (cxx_dialect >= cxx1z)
11469 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11470 end = build_decl (input_location, VAR_DECL,
11471 get_identifier ("__for_end"), iter_type);
11472 TREE_USED (end) = 1;
11473 DECL_ARTIFICIAL (end) = 1;
11474 pushdecl (end);
11475 cp_finish_decl (end, end_expr,
11476 /*is_constant_init*/false, NULL_TREE,
11477 LOOKUP_ONLYCONVERTING);
11479 finish_for_init_stmt (statement);
11481 /* The new for condition. */
11482 condition = build_x_binary_op (input_location, NE_EXPR,
11483 begin, ERROR_MARK,
11484 end, ERROR_MARK,
11485 NULL, tf_warning_or_error);
11486 finish_for_cond (condition, statement, ivdep);
11488 /* The new increment expression. */
11489 expression = finish_unary_op_expr (input_location,
11490 PREINCREMENT_EXPR, begin,
11491 tf_warning_or_error);
11492 finish_for_expr (expression, statement);
11494 /* The declaration is initialized with *__begin inside the loop body. */
11495 cp_finish_decl (range_decl,
11496 build_x_indirect_ref (input_location, begin, RO_NULL,
11497 tf_warning_or_error),
11498 /*is_constant_init*/false, NULL_TREE,
11499 LOOKUP_ONLYCONVERTING);
11501 return statement;
11504 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11505 We need to solve both at the same time because the method used
11506 depends on the existence of members begin or end.
11507 Returns the type deduced for the iterator expression. */
11509 static tree
11510 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11512 if (error_operand_p (range))
11514 *begin = *end = error_mark_node;
11515 return error_mark_node;
11518 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11520 error ("range-based %<for%> expression of type %qT "
11521 "has incomplete type", TREE_TYPE (range));
11522 *begin = *end = error_mark_node;
11523 return error_mark_node;
11525 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11527 /* If RANGE is an array, we will use pointer arithmetic. */
11528 *begin = decay_conversion (range, tf_warning_or_error);
11529 *end = build_binary_op (input_location, PLUS_EXPR,
11530 range,
11531 array_type_nelts_top (TREE_TYPE (range)),
11533 return TREE_TYPE (*begin);
11535 else
11537 /* If it is not an array, we must do a bit of magic. */
11538 tree id_begin, id_end;
11539 tree member_begin, member_end;
11541 *begin = *end = error_mark_node;
11543 id_begin = get_identifier ("begin");
11544 id_end = get_identifier ("end");
11545 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11546 /*protect=*/2, /*want_type=*/false,
11547 tf_warning_or_error);
11548 member_end = lookup_member (TREE_TYPE (range), id_end,
11549 /*protect=*/2, /*want_type=*/false,
11550 tf_warning_or_error);
11552 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11554 /* Use the member functions. */
11555 if (member_begin != NULL_TREE)
11556 *begin = cp_parser_range_for_member_function (range, id_begin);
11557 else
11558 error ("range-based %<for%> expression of type %qT has an "
11559 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11561 if (member_end != NULL_TREE)
11562 *end = cp_parser_range_for_member_function (range, id_end);
11563 else
11564 error ("range-based %<for%> expression of type %qT has a "
11565 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11567 else
11569 /* Use global functions with ADL. */
11570 vec<tree, va_gc> *vec;
11571 vec = make_tree_vector ();
11573 vec_safe_push (vec, range);
11575 member_begin = perform_koenig_lookup (id_begin, vec,
11576 tf_warning_or_error);
11577 *begin = finish_call_expr (member_begin, &vec, false, true,
11578 tf_warning_or_error);
11579 member_end = perform_koenig_lookup (id_end, vec,
11580 tf_warning_or_error);
11581 *end = finish_call_expr (member_end, &vec, false, true,
11582 tf_warning_or_error);
11584 release_tree_vector (vec);
11587 /* Last common checks. */
11588 if (*begin == error_mark_node || *end == error_mark_node)
11590 /* If one of the expressions is an error do no more checks. */
11591 *begin = *end = error_mark_node;
11592 return error_mark_node;
11594 else if (type_dependent_expression_p (*begin)
11595 || type_dependent_expression_p (*end))
11596 /* Can happen, when, eg, in a template context, Koenig lookup
11597 can't resolve begin/end (c++/58503). */
11598 return NULL_TREE;
11599 else
11601 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11602 /* The unqualified type of the __begin and __end temporaries should
11603 be the same, as required by the multiple auto declaration. */
11604 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11606 if (cxx_dialect >= cxx1z
11607 && (build_x_binary_op (input_location, NE_EXPR,
11608 *begin, ERROR_MARK,
11609 *end, ERROR_MARK,
11610 NULL, tf_none)
11611 != error_mark_node))
11612 /* P0184R0 allows __begin and __end to have different types,
11613 but make sure they are comparable so we can give a better
11614 diagnostic. */;
11615 else
11616 error ("inconsistent begin/end types in range-based %<for%> "
11617 "statement: %qT and %qT",
11618 TREE_TYPE (*begin), TREE_TYPE (*end));
11620 return iter_type;
11625 /* Helper function for cp_parser_perform_range_for_lookup.
11626 Builds a tree for RANGE.IDENTIFIER(). */
11628 static tree
11629 cp_parser_range_for_member_function (tree range, tree identifier)
11631 tree member, res;
11632 vec<tree, va_gc> *vec;
11634 member = finish_class_member_access_expr (range, identifier,
11635 false, tf_warning_or_error);
11636 if (member == error_mark_node)
11637 return error_mark_node;
11639 vec = make_tree_vector ();
11640 res = finish_call_expr (member, &vec,
11641 /*disallow_virtual=*/false,
11642 /*koenig_p=*/false,
11643 tf_warning_or_error);
11644 release_tree_vector (vec);
11645 return res;
11648 /* Parse an iteration-statement.
11650 iteration-statement:
11651 while ( condition ) statement
11652 do statement while ( expression ) ;
11653 for ( for-init-statement condition [opt] ; expression [opt] )
11654 statement
11656 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11658 static tree
11659 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
11661 cp_token *token;
11662 enum rid keyword;
11663 tree statement;
11664 unsigned char in_statement;
11665 token_indent_info guard_tinfo;
11667 /* Peek at the next token. */
11668 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11669 if (!token)
11670 return error_mark_node;
11672 guard_tinfo = get_token_indent_info (token);
11674 /* Remember whether or not we are already within an iteration
11675 statement. */
11676 in_statement = parser->in_statement;
11678 /* See what kind of keyword it is. */
11679 keyword = token->keyword;
11680 switch (keyword)
11682 case RID_WHILE:
11684 tree condition;
11686 /* Begin the while-statement. */
11687 statement = begin_while_stmt ();
11688 /* Look for the `('. */
11689 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11690 /* Parse the condition. */
11691 condition = cp_parser_condition (parser);
11692 finish_while_stmt_cond (condition, statement, ivdep);
11693 /* Look for the `)'. */
11694 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11695 /* Parse the dependent statement. */
11696 parser->in_statement = IN_ITERATION_STMT;
11697 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11698 parser->in_statement = in_statement;
11699 /* We're done with the while-statement. */
11700 finish_while_stmt (statement);
11702 break;
11704 case RID_DO:
11706 tree expression;
11708 /* Begin the do-statement. */
11709 statement = begin_do_stmt ();
11710 /* Parse the body of the do-statement. */
11711 parser->in_statement = IN_ITERATION_STMT;
11712 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11713 parser->in_statement = in_statement;
11714 finish_do_body (statement);
11715 /* Look for the `while' keyword. */
11716 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11717 /* Look for the `('. */
11718 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11719 /* Parse the expression. */
11720 expression = cp_parser_expression (parser);
11721 /* We're done with the do-statement. */
11722 finish_do_stmt (expression, statement, ivdep);
11723 /* Look for the `)'. */
11724 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11725 /* Look for the `;'. */
11726 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11728 break;
11730 case RID_FOR:
11732 /* Look for the `('. */
11733 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11735 statement = cp_parser_for (parser, ivdep);
11737 /* Look for the `)'. */
11738 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11740 /* Parse the body of the for-statement. */
11741 parser->in_statement = IN_ITERATION_STMT;
11742 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11743 parser->in_statement = in_statement;
11745 /* We're done with the for-statement. */
11746 finish_for_stmt (statement);
11748 break;
11750 default:
11751 cp_parser_error (parser, "expected iteration-statement");
11752 statement = error_mark_node;
11753 break;
11756 return statement;
11759 /* Parse a for-init-statement or the declarator of a range-based-for.
11760 Returns true if a range-based-for declaration is seen.
11762 for-init-statement:
11763 expression-statement
11764 simple-declaration */
11766 static bool
11767 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
11769 /* If the next token is a `;', then we have an empty
11770 expression-statement. Grammatically, this is also a
11771 simple-declaration, but an invalid one, because it does not
11772 declare anything. Therefore, if we did not handle this case
11773 specially, we would issue an error message about an invalid
11774 declaration. */
11775 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11777 bool is_range_for = false;
11778 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11780 /* A colon is used in range-based for. */
11781 parser->colon_corrects_to_scope_p = false;
11783 /* We're going to speculatively look for a declaration, falling back
11784 to an expression, if necessary. */
11785 cp_parser_parse_tentatively (parser);
11786 /* Parse the declaration. */
11787 cp_parser_simple_declaration (parser,
11788 /*function_definition_allowed_p=*/false,
11789 decl);
11790 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11791 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11793 /* It is a range-for, consume the ':' */
11794 cp_lexer_consume_token (parser->lexer);
11795 is_range_for = true;
11796 if (cxx_dialect < cxx11)
11798 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11799 "range-based %<for%> loops only available with "
11800 "-std=c++11 or -std=gnu++11");
11801 *decl = error_mark_node;
11804 else
11805 /* The ';' is not consumed yet because we told
11806 cp_parser_simple_declaration not to. */
11807 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11809 if (cp_parser_parse_definitely (parser))
11810 return is_range_for;
11811 /* If the tentative parse failed, then we shall need to look for an
11812 expression-statement. */
11814 /* If we are here, it is an expression-statement. */
11815 cp_parser_expression_statement (parser, NULL_TREE);
11816 return false;
11819 /* Parse a jump-statement.
11821 jump-statement:
11822 break ;
11823 continue ;
11824 return expression [opt] ;
11825 return braced-init-list ;
11826 goto identifier ;
11828 GNU extension:
11830 jump-statement:
11831 goto * expression ;
11833 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
11835 static tree
11836 cp_parser_jump_statement (cp_parser* parser)
11838 tree statement = error_mark_node;
11839 cp_token *token;
11840 enum rid keyword;
11841 unsigned char in_statement;
11843 /* Peek at the next token. */
11844 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
11845 if (!token)
11846 return error_mark_node;
11848 /* See what kind of keyword it is. */
11849 keyword = token->keyword;
11850 switch (keyword)
11852 case RID_BREAK:
11853 in_statement = parser->in_statement & ~IN_IF_STMT;
11854 switch (in_statement)
11856 case 0:
11857 error_at (token->location, "break statement not within loop or switch");
11858 break;
11859 default:
11860 gcc_assert ((in_statement & IN_SWITCH_STMT)
11861 || in_statement == IN_ITERATION_STMT);
11862 statement = finish_break_stmt ();
11863 if (in_statement == IN_ITERATION_STMT)
11864 break_maybe_infinite_loop ();
11865 break;
11866 case IN_OMP_BLOCK:
11867 error_at (token->location, "invalid exit from OpenMP structured block");
11868 break;
11869 case IN_OMP_FOR:
11870 error_at (token->location, "break statement used with OpenMP for loop");
11871 break;
11872 case IN_CILK_SIMD_FOR:
11873 error_at (token->location, "break statement used with Cilk Plus for loop");
11874 break;
11876 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11877 break;
11879 case RID_CONTINUE:
11880 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11882 case 0:
11883 error_at (token->location, "continue statement not within a loop");
11884 break;
11885 case IN_CILK_SIMD_FOR:
11886 error_at (token->location,
11887 "continue statement within %<#pragma simd%> loop body");
11888 /* Fall through. */
11889 case IN_ITERATION_STMT:
11890 case IN_OMP_FOR:
11891 statement = finish_continue_stmt ();
11892 break;
11893 case IN_OMP_BLOCK:
11894 error_at (token->location, "invalid exit from OpenMP structured block");
11895 break;
11896 default:
11897 gcc_unreachable ();
11899 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11900 break;
11902 case RID_RETURN:
11904 tree expr;
11905 bool expr_non_constant_p;
11907 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11909 cp_lexer_set_source_position (parser->lexer);
11910 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11911 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11913 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11914 expr = cp_parser_expression (parser);
11915 else
11916 /* If the next token is a `;', then there is no
11917 expression. */
11918 expr = NULL_TREE;
11919 /* Build the return-statement. */
11920 if (current_function_auto_return_pattern && parser->in_discarded_stmt)
11921 /* Don't deduce from a discarded return statement. */;
11922 else
11923 statement = finish_return_stmt (expr);
11924 /* Look for the final `;'. */
11925 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11927 break;
11929 case RID_GOTO:
11930 if (parser->in_function_body
11931 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11933 error ("%<goto%> in %<constexpr%> function");
11934 cp_function_chain->invalid_constexpr = true;
11937 /* Create the goto-statement. */
11938 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11940 /* Issue a warning about this use of a GNU extension. */
11941 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11942 /* Consume the '*' token. */
11943 cp_lexer_consume_token (parser->lexer);
11944 /* Parse the dependent expression. */
11945 finish_goto_stmt (cp_parser_expression (parser));
11947 else
11948 finish_goto_stmt (cp_parser_identifier (parser));
11949 /* Look for the final `;'. */
11950 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11951 break;
11953 default:
11954 cp_parser_error (parser, "expected jump-statement");
11955 break;
11958 return statement;
11961 /* Parse a declaration-statement.
11963 declaration-statement:
11964 block-declaration */
11966 static void
11967 cp_parser_declaration_statement (cp_parser* parser)
11969 void *p;
11971 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11972 p = obstack_alloc (&declarator_obstack, 0);
11974 /* Parse the block-declaration. */
11975 cp_parser_block_declaration (parser, /*statement_p=*/true);
11977 /* Free any declarators allocated. */
11978 obstack_free (&declarator_obstack, p);
11981 /* Some dependent statements (like `if (cond) statement'), are
11982 implicitly in their own scope. In other words, if the statement is
11983 a single statement (as opposed to a compound-statement), it is
11984 none-the-less treated as if it were enclosed in braces. Any
11985 declarations appearing in the dependent statement are out of scope
11986 after control passes that point. This function parses a statement,
11987 but ensures that is in its own scope, even if it is not a
11988 compound-statement.
11990 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11991 is a (possibly labeled) if statement which is not enclosed in
11992 braces and has an else clause. This is used to implement
11993 -Wparentheses.
11995 CHAIN is a vector of if-else-if conditions. This is used to implement
11996 -Wduplicated-cond.
11998 Returns the new statement. */
12000 static tree
12001 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12002 const token_indent_info &guard_tinfo,
12003 vec<tree> *chain)
12005 tree statement;
12006 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12007 token_indent_info body_tinfo
12008 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12010 if (if_p != NULL)
12011 *if_p = false;
12013 /* Mark if () ; with a special NOP_EXPR. */
12014 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12016 cp_lexer_consume_token (parser->lexer);
12017 statement = add_stmt (build_empty_stmt (body_loc));
12019 if (guard_tinfo.keyword == RID_IF
12020 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12021 warning_at (body_loc, OPT_Wempty_body,
12022 "suggest braces around empty body in an %<if%> statement");
12023 else if (guard_tinfo.keyword == RID_ELSE)
12024 warning_at (body_loc, OPT_Wempty_body,
12025 "suggest braces around empty body in an %<else%> statement");
12027 /* if a compound is opened, we simply parse the statement directly. */
12028 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12029 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12030 /* If the token is not a `{', then we must take special action. */
12031 else
12033 /* Create a compound-statement. */
12034 statement = begin_compound_stmt (0);
12035 /* Parse the dependent-statement. */
12036 cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
12037 /* Finish the dummy compound-statement. */
12038 finish_compound_stmt (statement);
12041 token_indent_info next_tinfo
12042 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12043 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12045 /* Return the statement. */
12046 return statement;
12049 /* For some dependent statements (like `while (cond) statement'), we
12050 have already created a scope. Therefore, even if the dependent
12051 statement is a compound-statement, we do not want to create another
12052 scope. */
12054 static void
12055 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12056 const token_indent_info &guard_tinfo)
12058 /* If the token is a `{', then we must take special action. */
12059 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12061 token_indent_info body_tinfo
12062 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12064 cp_parser_statement (parser, NULL_TREE, false, if_p);
12065 token_indent_info next_tinfo
12066 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12067 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12069 else
12071 /* Avoid calling cp_parser_compound_statement, so that we
12072 don't create a new scope. Do everything else by hand. */
12073 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
12074 /* If the next keyword is `__label__' we have a label declaration. */
12075 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12076 cp_parser_label_declaration (parser);
12077 /* Parse an (optional) statement-seq. */
12078 cp_parser_statement_seq_opt (parser, NULL_TREE);
12079 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12083 /* Declarations [gram.dcl.dcl] */
12085 /* Parse an optional declaration-sequence.
12087 declaration-seq:
12088 declaration
12089 declaration-seq declaration */
12091 static void
12092 cp_parser_declaration_seq_opt (cp_parser* parser)
12094 while (true)
12096 cp_token *token;
12098 token = cp_lexer_peek_token (parser->lexer);
12100 if (token->type == CPP_CLOSE_BRACE
12101 || token->type == CPP_EOF
12102 || token->type == CPP_PRAGMA_EOL)
12103 break;
12105 if (token->type == CPP_SEMICOLON)
12107 /* A declaration consisting of a single semicolon is
12108 invalid. Allow it unless we're being pedantic. */
12109 cp_lexer_consume_token (parser->lexer);
12110 if (!in_system_header_at (input_location))
12111 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12112 continue;
12115 /* If we're entering or exiting a region that's implicitly
12116 extern "C", modify the lang context appropriately. */
12117 if (!parser->implicit_extern_c && token->implicit_extern_c)
12119 push_lang_context (lang_name_c);
12120 parser->implicit_extern_c = true;
12122 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12124 pop_lang_context ();
12125 parser->implicit_extern_c = false;
12128 if (token->type == CPP_PRAGMA)
12130 /* A top-level declaration can consist solely of a #pragma.
12131 A nested declaration cannot, so this is done here and not
12132 in cp_parser_declaration. (A #pragma at block scope is
12133 handled in cp_parser_statement.) */
12134 cp_parser_pragma (parser, pragma_external, NULL);
12135 continue;
12138 /* Parse the declaration itself. */
12139 cp_parser_declaration (parser);
12143 /* Parse a declaration.
12145 declaration:
12146 block-declaration
12147 function-definition
12148 template-declaration
12149 explicit-instantiation
12150 explicit-specialization
12151 linkage-specification
12152 namespace-definition
12154 GNU extension:
12156 declaration:
12157 __extension__ declaration */
12159 static void
12160 cp_parser_declaration (cp_parser* parser)
12162 cp_token token1;
12163 cp_token token2;
12164 int saved_pedantic;
12165 void *p;
12166 tree attributes = NULL_TREE;
12168 /* Check for the `__extension__' keyword. */
12169 if (cp_parser_extension_opt (parser, &saved_pedantic))
12171 /* Parse the qualified declaration. */
12172 cp_parser_declaration (parser);
12173 /* Restore the PEDANTIC flag. */
12174 pedantic = saved_pedantic;
12176 return;
12179 /* Try to figure out what kind of declaration is present. */
12180 token1 = *cp_lexer_peek_token (parser->lexer);
12182 if (token1.type != CPP_EOF)
12183 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12184 else
12186 token2.type = CPP_EOF;
12187 token2.keyword = RID_MAX;
12190 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12191 p = obstack_alloc (&declarator_obstack, 0);
12193 /* If the next token is `extern' and the following token is a string
12194 literal, then we have a linkage specification. */
12195 if (token1.keyword == RID_EXTERN
12196 && cp_parser_is_pure_string_literal (&token2))
12197 cp_parser_linkage_specification (parser);
12198 /* If the next token is `template', then we have either a template
12199 declaration, an explicit instantiation, or an explicit
12200 specialization. */
12201 else if (token1.keyword == RID_TEMPLATE)
12203 /* `template <>' indicates a template specialization. */
12204 if (token2.type == CPP_LESS
12205 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12206 cp_parser_explicit_specialization (parser);
12207 /* `template <' indicates a template declaration. */
12208 else if (token2.type == CPP_LESS)
12209 cp_parser_template_declaration (parser, /*member_p=*/false);
12210 /* Anything else must be an explicit instantiation. */
12211 else
12212 cp_parser_explicit_instantiation (parser);
12214 /* If the next token is `export', then we have a template
12215 declaration. */
12216 else if (token1.keyword == RID_EXPORT)
12217 cp_parser_template_declaration (parser, /*member_p=*/false);
12218 /* If the next token is `extern', 'static' or 'inline' and the one
12219 after that is `template', we have a GNU extended explicit
12220 instantiation directive. */
12221 else if (cp_parser_allow_gnu_extensions_p (parser)
12222 && (token1.keyword == RID_EXTERN
12223 || token1.keyword == RID_STATIC
12224 || token1.keyword == RID_INLINE)
12225 && token2.keyword == RID_TEMPLATE)
12226 cp_parser_explicit_instantiation (parser);
12227 /* If the next token is `namespace', check for a named or unnamed
12228 namespace definition. */
12229 else if (token1.keyword == RID_NAMESPACE
12230 && (/* A named namespace definition. */
12231 (token2.type == CPP_NAME
12232 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12233 != CPP_EQ))
12234 || (token2.type == CPP_OPEN_SQUARE
12235 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12236 == CPP_OPEN_SQUARE)
12237 /* An unnamed namespace definition. */
12238 || token2.type == CPP_OPEN_BRACE
12239 || token2.keyword == RID_ATTRIBUTE))
12240 cp_parser_namespace_definition (parser);
12241 /* An inline (associated) namespace definition. */
12242 else if (token1.keyword == RID_INLINE
12243 && token2.keyword == RID_NAMESPACE)
12244 cp_parser_namespace_definition (parser);
12245 /* Objective-C++ declaration/definition. */
12246 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12247 cp_parser_objc_declaration (parser, NULL_TREE);
12248 else if (c_dialect_objc ()
12249 && token1.keyword == RID_ATTRIBUTE
12250 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12251 cp_parser_objc_declaration (parser, attributes);
12252 /* At this point we may have a template declared by a concept
12253 introduction. */
12254 else if (flag_concepts
12255 && cp_parser_template_declaration_after_export (parser,
12256 /*member_p=*/false))
12257 /* We did. */;
12258 else
12259 /* Try to parse a block-declaration, or a function-definition. */
12260 cp_parser_block_declaration (parser, /*statement_p=*/false);
12262 /* Free any declarators allocated. */
12263 obstack_free (&declarator_obstack, p);
12266 /* Parse a block-declaration.
12268 block-declaration:
12269 simple-declaration
12270 asm-definition
12271 namespace-alias-definition
12272 using-declaration
12273 using-directive
12275 GNU Extension:
12277 block-declaration:
12278 __extension__ block-declaration
12280 C++0x Extension:
12282 block-declaration:
12283 static_assert-declaration
12285 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12286 part of a declaration-statement. */
12288 static void
12289 cp_parser_block_declaration (cp_parser *parser,
12290 bool statement_p)
12292 cp_token *token1;
12293 int saved_pedantic;
12295 /* Check for the `__extension__' keyword. */
12296 if (cp_parser_extension_opt (parser, &saved_pedantic))
12298 /* Parse the qualified declaration. */
12299 cp_parser_block_declaration (parser, statement_p);
12300 /* Restore the PEDANTIC flag. */
12301 pedantic = saved_pedantic;
12303 return;
12306 /* Peek at the next token to figure out which kind of declaration is
12307 present. */
12308 token1 = cp_lexer_peek_token (parser->lexer);
12310 /* If the next keyword is `asm', we have an asm-definition. */
12311 if (token1->keyword == RID_ASM)
12313 if (statement_p)
12314 cp_parser_commit_to_tentative_parse (parser);
12315 cp_parser_asm_definition (parser);
12317 /* If the next keyword is `namespace', we have a
12318 namespace-alias-definition. */
12319 else if (token1->keyword == RID_NAMESPACE)
12320 cp_parser_namespace_alias_definition (parser);
12321 /* If the next keyword is `using', we have a
12322 using-declaration, a using-directive, or an alias-declaration. */
12323 else if (token1->keyword == RID_USING)
12325 cp_token *token2;
12327 if (statement_p)
12328 cp_parser_commit_to_tentative_parse (parser);
12329 /* If the token after `using' is `namespace', then we have a
12330 using-directive. */
12331 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12332 if (token2->keyword == RID_NAMESPACE)
12333 cp_parser_using_directive (parser);
12334 /* If the second token after 'using' is '=', then we have an
12335 alias-declaration. */
12336 else if (cxx_dialect >= cxx11
12337 && token2->type == CPP_NAME
12338 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12339 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12340 cp_parser_alias_declaration (parser);
12341 /* Otherwise, it's a using-declaration. */
12342 else
12343 cp_parser_using_declaration (parser,
12344 /*access_declaration_p=*/false);
12346 /* If the next keyword is `__label__' we have a misplaced label
12347 declaration. */
12348 else if (token1->keyword == RID_LABEL)
12350 cp_lexer_consume_token (parser->lexer);
12351 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12352 cp_parser_skip_to_end_of_statement (parser);
12353 /* If the next token is now a `;', consume it. */
12354 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12355 cp_lexer_consume_token (parser->lexer);
12357 /* If the next token is `static_assert' we have a static assertion. */
12358 else if (token1->keyword == RID_STATIC_ASSERT)
12359 cp_parser_static_assert (parser, /*member_p=*/false);
12360 /* Anything else must be a simple-declaration. */
12361 else
12362 cp_parser_simple_declaration (parser, !statement_p,
12363 /*maybe_range_for_decl*/NULL);
12366 /* Parse a simple-declaration.
12368 simple-declaration:
12369 decl-specifier-seq [opt] init-declarator-list [opt] ;
12371 init-declarator-list:
12372 init-declarator
12373 init-declarator-list , init-declarator
12375 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12376 function-definition as a simple-declaration.
12378 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12379 parsed declaration if it is an uninitialized single declarator not followed
12380 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12381 if present, will not be consumed. */
12383 static void
12384 cp_parser_simple_declaration (cp_parser* parser,
12385 bool function_definition_allowed_p,
12386 tree *maybe_range_for_decl)
12388 cp_decl_specifier_seq decl_specifiers;
12389 int declares_class_or_enum;
12390 bool saw_declarator;
12391 location_t comma_loc = UNKNOWN_LOCATION;
12392 location_t init_loc = UNKNOWN_LOCATION;
12394 if (maybe_range_for_decl)
12395 *maybe_range_for_decl = NULL_TREE;
12397 /* Defer access checks until we know what is being declared; the
12398 checks for names appearing in the decl-specifier-seq should be
12399 done as if we were in the scope of the thing being declared. */
12400 push_deferring_access_checks (dk_deferred);
12402 /* Parse the decl-specifier-seq. We have to keep track of whether
12403 or not the decl-specifier-seq declares a named class or
12404 enumeration type, since that is the only case in which the
12405 init-declarator-list is allowed to be empty.
12407 [dcl.dcl]
12409 In a simple-declaration, the optional init-declarator-list can be
12410 omitted only when declaring a class or enumeration, that is when
12411 the decl-specifier-seq contains either a class-specifier, an
12412 elaborated-type-specifier, or an enum-specifier. */
12413 cp_parser_decl_specifier_seq (parser,
12414 CP_PARSER_FLAGS_OPTIONAL,
12415 &decl_specifiers,
12416 &declares_class_or_enum);
12417 /* We no longer need to defer access checks. */
12418 stop_deferring_access_checks ();
12420 /* In a block scope, a valid declaration must always have a
12421 decl-specifier-seq. By not trying to parse declarators, we can
12422 resolve the declaration/expression ambiguity more quickly. */
12423 if (!function_definition_allowed_p
12424 && !decl_specifiers.any_specifiers_p)
12426 cp_parser_error (parser, "expected declaration");
12427 goto done;
12430 /* If the next two tokens are both identifiers, the code is
12431 erroneous. The usual cause of this situation is code like:
12433 T t;
12435 where "T" should name a type -- but does not. */
12436 if (!decl_specifiers.any_type_specifiers_p
12437 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12439 /* If parsing tentatively, we should commit; we really are
12440 looking at a declaration. */
12441 cp_parser_commit_to_tentative_parse (parser);
12442 /* Give up. */
12443 goto done;
12446 /* If we have seen at least one decl-specifier, and the next token
12447 is not a parenthesis, then we must be looking at a declaration.
12448 (After "int (" we might be looking at a functional cast.) */
12449 if (decl_specifiers.any_specifiers_p
12450 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12451 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12452 && !cp_parser_error_occurred (parser))
12453 cp_parser_commit_to_tentative_parse (parser);
12455 tree last_type;
12457 last_type = NULL_TREE;
12459 /* Keep going until we hit the `;' at the end of the simple
12460 declaration. */
12461 saw_declarator = false;
12462 while (cp_lexer_next_token_is_not (parser->lexer,
12463 CPP_SEMICOLON))
12465 cp_token *token;
12466 bool function_definition_p;
12467 tree decl;
12468 tree auto_result = NULL_TREE;
12470 if (saw_declarator)
12472 /* If we are processing next declarator, comma is expected */
12473 token = cp_lexer_peek_token (parser->lexer);
12474 gcc_assert (token->type == CPP_COMMA);
12475 cp_lexer_consume_token (parser->lexer);
12476 if (maybe_range_for_decl)
12478 *maybe_range_for_decl = error_mark_node;
12479 if (comma_loc == UNKNOWN_LOCATION)
12480 comma_loc = token->location;
12483 else
12484 saw_declarator = true;
12486 /* Parse the init-declarator. */
12487 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12488 /*checks=*/NULL,
12489 function_definition_allowed_p,
12490 /*member_p=*/false,
12491 declares_class_or_enum,
12492 &function_definition_p,
12493 maybe_range_for_decl,
12494 &init_loc,
12495 &auto_result);
12496 /* If an error occurred while parsing tentatively, exit quickly.
12497 (That usually happens when in the body of a function; each
12498 statement is treated as a declaration-statement until proven
12499 otherwise.) */
12500 if (cp_parser_error_occurred (parser))
12501 goto done;
12503 if (auto_result)
12505 if (last_type && last_type != error_mark_node
12506 && !same_type_p (auto_result, last_type))
12508 /* If the list of declarators contains more than one declarator,
12509 the type of each declared variable is determined as described
12510 above. If the type deduced for the template parameter U is not
12511 the same in each deduction, the program is ill-formed. */
12512 error_at (decl_specifiers.locations[ds_type_spec],
12513 "inconsistent deduction for %qT: %qT and then %qT",
12514 decl_specifiers.type, last_type, auto_result);
12515 last_type = error_mark_node;
12517 else
12518 last_type = auto_result;
12521 /* Handle function definitions specially. */
12522 if (function_definition_p)
12524 /* If the next token is a `,', then we are probably
12525 processing something like:
12527 void f() {}, *p;
12529 which is erroneous. */
12530 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12532 cp_token *token = cp_lexer_peek_token (parser->lexer);
12533 error_at (token->location,
12534 "mixing"
12535 " declarations and function-definitions is forbidden");
12537 /* Otherwise, we're done with the list of declarators. */
12538 else
12540 pop_deferring_access_checks ();
12541 return;
12544 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12545 *maybe_range_for_decl = decl;
12546 /* The next token should be either a `,' or a `;'. */
12547 token = cp_lexer_peek_token (parser->lexer);
12548 /* If it's a `,', there are more declarators to come. */
12549 if (token->type == CPP_COMMA)
12550 /* will be consumed next time around */;
12551 /* If it's a `;', we are done. */
12552 else if (token->type == CPP_SEMICOLON)
12553 break;
12554 else if (maybe_range_for_decl)
12556 if (declares_class_or_enum && token->type == CPP_COLON)
12557 permerror (decl_specifiers.locations[ds_type_spec],
12558 "types may not be defined in a for-range-declaration");
12559 break;
12561 /* Anything else is an error. */
12562 else
12564 /* If we have already issued an error message we don't need
12565 to issue another one. */
12566 if ((decl != error_mark_node
12567 && DECL_INITIAL (decl) != error_mark_node)
12568 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12569 cp_parser_error (parser, "expected %<,%> or %<;%>");
12570 /* Skip tokens until we reach the end of the statement. */
12571 cp_parser_skip_to_end_of_statement (parser);
12572 /* If the next token is now a `;', consume it. */
12573 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12574 cp_lexer_consume_token (parser->lexer);
12575 goto done;
12577 /* After the first time around, a function-definition is not
12578 allowed -- even if it was OK at first. For example:
12580 int i, f() {}
12582 is not valid. */
12583 function_definition_allowed_p = false;
12586 /* Issue an error message if no declarators are present, and the
12587 decl-specifier-seq does not itself declare a class or
12588 enumeration: [dcl.dcl]/3. */
12589 if (!saw_declarator)
12591 if (cp_parser_declares_only_class_p (parser))
12593 if (!declares_class_or_enum
12594 && decl_specifiers.type
12595 && OVERLOAD_TYPE_P (decl_specifiers.type))
12596 /* Ensure an error is issued anyway when finish_decltype_type,
12597 called via cp_parser_decl_specifier_seq, returns a class or
12598 an enumeration (c++/51786). */
12599 decl_specifiers.type = NULL_TREE;
12600 shadow_tag (&decl_specifiers);
12602 /* Perform any deferred access checks. */
12603 perform_deferred_access_checks (tf_warning_or_error);
12606 /* Consume the `;'. */
12607 if (!maybe_range_for_decl)
12608 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12609 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12611 if (init_loc != UNKNOWN_LOCATION)
12612 error_at (init_loc, "initializer in range-based %<for%> loop");
12613 if (comma_loc != UNKNOWN_LOCATION)
12614 error_at (comma_loc,
12615 "multiple declarations in range-based %<for%> loop");
12618 done:
12619 pop_deferring_access_checks ();
12622 /* Parse a decl-specifier-seq.
12624 decl-specifier-seq:
12625 decl-specifier-seq [opt] decl-specifier
12626 decl-specifier attribute-specifier-seq [opt] (C++11)
12628 decl-specifier:
12629 storage-class-specifier
12630 type-specifier
12631 function-specifier
12632 friend
12633 typedef
12635 GNU Extension:
12637 decl-specifier:
12638 attributes
12640 Concepts Extension:
12642 decl-specifier:
12643 concept
12645 Set *DECL_SPECS to a representation of the decl-specifier-seq.
12647 The parser flags FLAGS is used to control type-specifier parsing.
12649 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
12650 flags:
12652 1: one of the decl-specifiers is an elaborated-type-specifier
12653 (i.e., a type declaration)
12654 2: one of the decl-specifiers is an enum-specifier or a
12655 class-specifier (i.e., a type definition)
12659 static void
12660 cp_parser_decl_specifier_seq (cp_parser* parser,
12661 cp_parser_flags flags,
12662 cp_decl_specifier_seq *decl_specs,
12663 int* declares_class_or_enum)
12665 bool constructor_possible_p = !parser->in_declarator_p;
12666 bool found_decl_spec = false;
12667 cp_token *start_token = NULL;
12668 cp_decl_spec ds;
12670 /* Clear DECL_SPECS. */
12671 clear_decl_specs (decl_specs);
12673 /* Assume no class or enumeration type is declared. */
12674 *declares_class_or_enum = 0;
12676 /* Keep reading specifiers until there are no more to read. */
12677 while (true)
12679 bool constructor_p;
12680 cp_token *token;
12681 ds = ds_last;
12683 /* Peek at the next token. */
12684 token = cp_lexer_peek_token (parser->lexer);
12686 /* Save the first token of the decl spec list for error
12687 reporting. */
12688 if (!start_token)
12689 start_token = token;
12690 /* Handle attributes. */
12691 if (cp_next_tokens_can_be_attribute_p (parser))
12693 /* Parse the attributes. */
12694 tree attrs = cp_parser_attributes_opt (parser);
12696 /* In a sequence of declaration specifiers, c++11 attributes
12697 appertain to the type that precede them. In that case
12698 [dcl.spec]/1 says:
12700 The attribute-specifier-seq affects the type only for
12701 the declaration it appears in, not other declarations
12702 involving the same type.
12704 But for now let's force the user to position the
12705 attribute either at the beginning of the declaration or
12706 after the declarator-id, which would clearly mean that it
12707 applies to the declarator. */
12708 if (cxx11_attribute_p (attrs))
12710 if (!found_decl_spec)
12711 /* The c++11 attribute is at the beginning of the
12712 declaration. It appertains to the entity being
12713 declared. */;
12714 else
12716 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
12718 /* This is an attribute following a
12719 class-specifier. */
12720 if (decl_specs->type_definition_p)
12721 warn_misplaced_attr_for_class_type (token->location,
12722 decl_specs->type);
12723 attrs = NULL_TREE;
12725 else
12727 decl_specs->std_attributes
12728 = chainon (decl_specs->std_attributes,
12729 attrs);
12730 if (decl_specs->locations[ds_std_attribute] == 0)
12731 decl_specs->locations[ds_std_attribute] = token->location;
12733 continue;
12737 decl_specs->attributes
12738 = chainon (decl_specs->attributes,
12739 attrs);
12740 if (decl_specs->locations[ds_attribute] == 0)
12741 decl_specs->locations[ds_attribute] = token->location;
12742 continue;
12744 /* Assume we will find a decl-specifier keyword. */
12745 found_decl_spec = true;
12746 /* If the next token is an appropriate keyword, we can simply
12747 add it to the list. */
12748 switch (token->keyword)
12750 /* decl-specifier:
12751 friend
12752 constexpr */
12753 case RID_FRIEND:
12754 if (!at_class_scope_p ())
12756 error_at (token->location, "%<friend%> used outside of class");
12757 cp_lexer_purge_token (parser->lexer);
12759 else
12761 ds = ds_friend;
12762 /* Consume the token. */
12763 cp_lexer_consume_token (parser->lexer);
12765 break;
12767 case RID_CONSTEXPR:
12768 ds = ds_constexpr;
12769 cp_lexer_consume_token (parser->lexer);
12770 break;
12772 case RID_CONCEPT:
12773 ds = ds_concept;
12774 cp_lexer_consume_token (parser->lexer);
12775 break;
12777 /* function-specifier:
12778 inline
12779 virtual
12780 explicit */
12781 case RID_INLINE:
12782 case RID_VIRTUAL:
12783 case RID_EXPLICIT:
12784 cp_parser_function_specifier_opt (parser, decl_specs);
12785 break;
12787 /* decl-specifier:
12788 typedef */
12789 case RID_TYPEDEF:
12790 ds = ds_typedef;
12791 /* Consume the token. */
12792 cp_lexer_consume_token (parser->lexer);
12793 /* A constructor declarator cannot appear in a typedef. */
12794 constructor_possible_p = false;
12795 /* The "typedef" keyword can only occur in a declaration; we
12796 may as well commit at this point. */
12797 cp_parser_commit_to_tentative_parse (parser);
12799 if (decl_specs->storage_class != sc_none)
12800 decl_specs->conflicting_specifiers_p = true;
12801 break;
12803 /* storage-class-specifier:
12804 auto
12805 register
12806 static
12807 extern
12808 mutable
12810 GNU Extension:
12811 thread */
12812 case RID_AUTO:
12813 if (cxx_dialect == cxx98)
12815 /* Consume the token. */
12816 cp_lexer_consume_token (parser->lexer);
12818 /* Complain about `auto' as a storage specifier, if
12819 we're complaining about C++0x compatibility. */
12820 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
12821 " changes meaning in C++11; please remove it");
12823 /* Set the storage class anyway. */
12824 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
12825 token);
12827 else
12828 /* C++0x auto type-specifier. */
12829 found_decl_spec = false;
12830 break;
12832 case RID_REGISTER:
12833 case RID_STATIC:
12834 case RID_EXTERN:
12835 case RID_MUTABLE:
12836 /* Consume the token. */
12837 cp_lexer_consume_token (parser->lexer);
12838 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
12839 token);
12840 break;
12841 case RID_THREAD:
12842 /* Consume the token. */
12843 ds = ds_thread;
12844 cp_lexer_consume_token (parser->lexer);
12845 break;
12847 default:
12848 /* We did not yet find a decl-specifier yet. */
12849 found_decl_spec = false;
12850 break;
12853 if (found_decl_spec
12854 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
12855 && token->keyword != RID_CONSTEXPR)
12856 error ("decl-specifier invalid in condition");
12858 if (found_decl_spec
12859 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
12860 && token->keyword != RID_MUTABLE
12861 && token->keyword != RID_CONSTEXPR)
12862 error_at (token->location, "%qD invalid in lambda",
12863 ridpointers[token->keyword]);
12865 if (ds != ds_last)
12866 set_and_check_decl_spec_loc (decl_specs, ds, token);
12868 /* Constructors are a special case. The `S' in `S()' is not a
12869 decl-specifier; it is the beginning of the declarator. */
12870 constructor_p
12871 = (!found_decl_spec
12872 && constructor_possible_p
12873 && (cp_parser_constructor_declarator_p
12874 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
12876 /* If we don't have a DECL_SPEC yet, then we must be looking at
12877 a type-specifier. */
12878 if (!found_decl_spec && !constructor_p)
12880 int decl_spec_declares_class_or_enum;
12881 bool is_cv_qualifier;
12882 tree type_spec;
12884 type_spec
12885 = cp_parser_type_specifier (parser, flags,
12886 decl_specs,
12887 /*is_declaration=*/true,
12888 &decl_spec_declares_class_or_enum,
12889 &is_cv_qualifier);
12890 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
12892 /* If this type-specifier referenced a user-defined type
12893 (a typedef, class-name, etc.), then we can't allow any
12894 more such type-specifiers henceforth.
12896 [dcl.spec]
12898 The longest sequence of decl-specifiers that could
12899 possibly be a type name is taken as the
12900 decl-specifier-seq of a declaration. The sequence shall
12901 be self-consistent as described below.
12903 [dcl.type]
12905 As a general rule, at most one type-specifier is allowed
12906 in the complete decl-specifier-seq of a declaration. The
12907 only exceptions are the following:
12909 -- const or volatile can be combined with any other
12910 type-specifier.
12912 -- signed or unsigned can be combined with char, long,
12913 short, or int.
12915 -- ..
12917 Example:
12919 typedef char* Pc;
12920 void g (const int Pc);
12922 Here, Pc is *not* part of the decl-specifier seq; it's
12923 the declarator. Therefore, once we see a type-specifier
12924 (other than a cv-qualifier), we forbid any additional
12925 user-defined types. We *do* still allow things like `int
12926 int' to be considered a decl-specifier-seq, and issue the
12927 error message later. */
12928 if (type_spec && !is_cv_qualifier)
12929 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12930 /* A constructor declarator cannot follow a type-specifier. */
12931 if (type_spec)
12933 constructor_possible_p = false;
12934 found_decl_spec = true;
12935 if (!is_cv_qualifier)
12936 decl_specs->any_type_specifiers_p = true;
12940 /* If we still do not have a DECL_SPEC, then there are no more
12941 decl-specifiers. */
12942 if (!found_decl_spec)
12943 break;
12945 decl_specs->any_specifiers_p = true;
12946 /* After we see one decl-specifier, further decl-specifiers are
12947 always optional. */
12948 flags |= CP_PARSER_FLAGS_OPTIONAL;
12951 /* Don't allow a friend specifier with a class definition. */
12952 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12953 && (*declares_class_or_enum & 2))
12954 error_at (decl_specs->locations[ds_friend],
12955 "class definition may not be declared a friend");
12958 /* Parse an (optional) storage-class-specifier.
12960 storage-class-specifier:
12961 auto
12962 register
12963 static
12964 extern
12965 mutable
12967 GNU Extension:
12969 storage-class-specifier:
12970 thread
12972 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12974 static tree
12975 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12977 switch (cp_lexer_peek_token (parser->lexer)->keyword)
12979 case RID_AUTO:
12980 if (cxx_dialect != cxx98)
12981 return NULL_TREE;
12982 /* Fall through for C++98. */
12984 case RID_REGISTER:
12985 case RID_STATIC:
12986 case RID_EXTERN:
12987 case RID_MUTABLE:
12988 case RID_THREAD:
12989 /* Consume the token. */
12990 return cp_lexer_consume_token (parser->lexer)->u.value;
12992 default:
12993 return NULL_TREE;
12997 /* Parse an (optional) function-specifier.
12999 function-specifier:
13000 inline
13001 virtual
13002 explicit
13004 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13005 Updates DECL_SPECS, if it is non-NULL. */
13007 static tree
13008 cp_parser_function_specifier_opt (cp_parser* parser,
13009 cp_decl_specifier_seq *decl_specs)
13011 cp_token *token = cp_lexer_peek_token (parser->lexer);
13012 switch (token->keyword)
13014 case RID_INLINE:
13015 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13016 break;
13018 case RID_VIRTUAL:
13019 /* 14.5.2.3 [temp.mem]
13021 A member function template shall not be virtual. */
13022 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13023 && current_class_type)
13024 error_at (token->location, "templates may not be %<virtual%>");
13025 else
13026 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13027 break;
13029 case RID_EXPLICIT:
13030 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13031 break;
13033 default:
13034 return NULL_TREE;
13037 /* Consume the token. */
13038 return cp_lexer_consume_token (parser->lexer)->u.value;
13041 /* Parse a linkage-specification.
13043 linkage-specification:
13044 extern string-literal { declaration-seq [opt] }
13045 extern string-literal declaration */
13047 static void
13048 cp_parser_linkage_specification (cp_parser* parser)
13050 tree linkage;
13052 /* Look for the `extern' keyword. */
13053 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13055 /* Look for the string-literal. */
13056 linkage = cp_parser_string_literal (parser, false, false);
13058 /* Transform the literal into an identifier. If the literal is a
13059 wide-character string, or contains embedded NULs, then we can't
13060 handle it as the user wants. */
13061 if (strlen (TREE_STRING_POINTER (linkage))
13062 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13064 cp_parser_error (parser, "invalid linkage-specification");
13065 /* Assume C++ linkage. */
13066 linkage = lang_name_cplusplus;
13068 else
13069 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13071 /* We're now using the new linkage. */
13072 push_lang_context (linkage);
13074 /* If the next token is a `{', then we're using the first
13075 production. */
13076 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13078 cp_ensure_no_omp_declare_simd (parser);
13079 cp_ensure_no_oacc_routine (parser);
13081 /* Consume the `{' token. */
13082 cp_lexer_consume_token (parser->lexer);
13083 /* Parse the declarations. */
13084 cp_parser_declaration_seq_opt (parser);
13085 /* Look for the closing `}'. */
13086 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13088 /* Otherwise, there's just one declaration. */
13089 else
13091 bool saved_in_unbraced_linkage_specification_p;
13093 saved_in_unbraced_linkage_specification_p
13094 = parser->in_unbraced_linkage_specification_p;
13095 parser->in_unbraced_linkage_specification_p = true;
13096 cp_parser_declaration (parser);
13097 parser->in_unbraced_linkage_specification_p
13098 = saved_in_unbraced_linkage_specification_p;
13101 /* We're done with the linkage-specification. */
13102 pop_lang_context ();
13105 /* Parse a static_assert-declaration.
13107 static_assert-declaration:
13108 static_assert ( constant-expression , string-literal ) ;
13109 static_assert ( constant-expression ) ; (C++1Z)
13111 If MEMBER_P, this static_assert is a class member. */
13113 static void
13114 cp_parser_static_assert(cp_parser *parser, bool member_p)
13116 tree condition;
13117 tree message;
13118 cp_token *token;
13119 location_t saved_loc;
13120 bool dummy;
13122 /* Peek at the `static_assert' token so we can keep track of exactly
13123 where the static assertion started. */
13124 token = cp_lexer_peek_token (parser->lexer);
13125 saved_loc = token->location;
13127 /* Look for the `static_assert' keyword. */
13128 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13129 RT_STATIC_ASSERT))
13130 return;
13132 /* We know we are in a static assertion; commit to any tentative
13133 parse. */
13134 if (cp_parser_parsing_tentatively (parser))
13135 cp_parser_commit_to_tentative_parse (parser);
13137 /* Parse the `(' starting the static assertion condition. */
13138 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
13140 /* Parse the constant-expression. Allow a non-constant expression
13141 here in order to give better diagnostics in finish_static_assert. */
13142 condition =
13143 cp_parser_constant_expression (parser,
13144 /*allow_non_constant_p=*/true,
13145 /*non_constant_p=*/&dummy);
13147 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13149 if (cxx_dialect < cxx1z)
13150 pedwarn (input_location, OPT_Wpedantic,
13151 "static_assert without a message "
13152 "only available with -std=c++1z or -std=gnu++1z");
13153 /* Eat the ')' */
13154 cp_lexer_consume_token (parser->lexer);
13155 message = build_string (1, "");
13156 TREE_TYPE (message) = char_array_type_node;
13157 fix_string_type (message);
13159 else
13161 /* Parse the separating `,'. */
13162 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13164 /* Parse the string-literal message. */
13165 message = cp_parser_string_literal (parser,
13166 /*translate=*/false,
13167 /*wide_ok=*/true);
13169 /* A `)' completes the static assertion. */
13170 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13171 cp_parser_skip_to_closing_parenthesis (parser,
13172 /*recovering=*/true,
13173 /*or_comma=*/false,
13174 /*consume_paren=*/true);
13177 /* A semicolon terminates the declaration. */
13178 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13180 /* Complete the static assertion, which may mean either processing
13181 the static assert now or saving it for template instantiation. */
13182 finish_static_assert (condition, message, saved_loc, member_p);
13185 /* Parse the expression in decltype ( expression ). */
13187 static tree
13188 cp_parser_decltype_expr (cp_parser *parser,
13189 bool &id_expression_or_member_access_p)
13191 cp_token *id_expr_start_token;
13192 tree expr;
13194 /* Since we're going to preserve any side-effects from this parse, set up a
13195 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13196 in the expression. */
13197 tentative_firewall firewall (parser);
13199 /* First, try parsing an id-expression. */
13200 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13201 cp_parser_parse_tentatively (parser);
13202 expr = cp_parser_id_expression (parser,
13203 /*template_keyword_p=*/false,
13204 /*check_dependency_p=*/true,
13205 /*template_p=*/NULL,
13206 /*declarator_p=*/false,
13207 /*optional_p=*/false);
13209 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13211 bool non_integral_constant_expression_p = false;
13212 tree id_expression = expr;
13213 cp_id_kind idk;
13214 const char *error_msg;
13216 if (identifier_p (expr))
13217 /* Lookup the name we got back from the id-expression. */
13218 expr = cp_parser_lookup_name_simple (parser, expr,
13219 id_expr_start_token->location);
13221 if (expr
13222 && expr != error_mark_node
13223 && TREE_CODE (expr) != TYPE_DECL
13224 && (TREE_CODE (expr) != BIT_NOT_EXPR
13225 || !TYPE_P (TREE_OPERAND (expr, 0)))
13226 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13228 /* Complete lookup of the id-expression. */
13229 expr = (finish_id_expression
13230 (id_expression, expr, parser->scope, &idk,
13231 /*integral_constant_expression_p=*/false,
13232 /*allow_non_integral_constant_expression_p=*/true,
13233 &non_integral_constant_expression_p,
13234 /*template_p=*/false,
13235 /*done=*/true,
13236 /*address_p=*/false,
13237 /*template_arg_p=*/false,
13238 &error_msg,
13239 id_expr_start_token->location));
13241 if (expr == error_mark_node)
13242 /* We found an id-expression, but it was something that we
13243 should not have found. This is an error, not something
13244 we can recover from, so note that we found an
13245 id-expression and we'll recover as gracefully as
13246 possible. */
13247 id_expression_or_member_access_p = true;
13250 if (expr
13251 && expr != error_mark_node
13252 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13253 /* We have an id-expression. */
13254 id_expression_or_member_access_p = true;
13257 if (!id_expression_or_member_access_p)
13259 /* Abort the id-expression parse. */
13260 cp_parser_abort_tentative_parse (parser);
13262 /* Parsing tentatively, again. */
13263 cp_parser_parse_tentatively (parser);
13265 /* Parse a class member access. */
13266 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13267 /*cast_p=*/false, /*decltype*/true,
13268 /*member_access_only_p=*/true, NULL);
13270 if (expr
13271 && expr != error_mark_node
13272 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13273 /* We have an id-expression. */
13274 id_expression_or_member_access_p = true;
13277 if (id_expression_or_member_access_p)
13278 /* We have parsed the complete id-expression or member access. */
13279 cp_parser_parse_definitely (parser);
13280 else
13282 /* Abort our attempt to parse an id-expression or member access
13283 expression. */
13284 cp_parser_abort_tentative_parse (parser);
13286 /* Parse a full expression. */
13287 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13288 /*decltype_p=*/true);
13291 return expr;
13294 /* Parse a `decltype' type. Returns the type.
13296 simple-type-specifier:
13297 decltype ( expression )
13298 C++14 proposal:
13299 decltype ( auto ) */
13301 static tree
13302 cp_parser_decltype (cp_parser *parser)
13304 tree expr;
13305 bool id_expression_or_member_access_p = false;
13306 const char *saved_message;
13307 bool saved_integral_constant_expression_p;
13308 bool saved_non_integral_constant_expression_p;
13309 bool saved_greater_than_is_operator_p;
13310 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13312 if (start_token->type == CPP_DECLTYPE)
13314 /* Already parsed. */
13315 cp_lexer_consume_token (parser->lexer);
13316 return saved_checks_value (start_token->u.tree_check_value);
13319 /* Look for the `decltype' token. */
13320 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13321 return error_mark_node;
13323 /* Parse the opening `('. */
13324 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13325 return error_mark_node;
13327 /* decltype (auto) */
13328 if (cxx_dialect >= cxx14
13329 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13331 cp_lexer_consume_token (parser->lexer);
13332 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13333 return error_mark_node;
13334 expr = make_decltype_auto ();
13335 AUTO_IS_DECLTYPE (expr) = true;
13336 goto rewrite;
13339 /* Types cannot be defined in a `decltype' expression. Save away the
13340 old message. */
13341 saved_message = parser->type_definition_forbidden_message;
13343 /* And create the new one. */
13344 parser->type_definition_forbidden_message
13345 = G_("types may not be defined in %<decltype%> expressions");
13347 /* The restrictions on constant-expressions do not apply inside
13348 decltype expressions. */
13349 saved_integral_constant_expression_p
13350 = parser->integral_constant_expression_p;
13351 saved_non_integral_constant_expression_p
13352 = parser->non_integral_constant_expression_p;
13353 parser->integral_constant_expression_p = false;
13355 /* Within a parenthesized expression, a `>' token is always
13356 the greater-than operator. */
13357 saved_greater_than_is_operator_p
13358 = parser->greater_than_is_operator_p;
13359 parser->greater_than_is_operator_p = true;
13361 /* Do not actually evaluate the expression. */
13362 ++cp_unevaluated_operand;
13364 /* Do not warn about problems with the expression. */
13365 ++c_inhibit_evaluation_warnings;
13367 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13369 /* Go back to evaluating expressions. */
13370 --cp_unevaluated_operand;
13371 --c_inhibit_evaluation_warnings;
13373 /* The `>' token might be the end of a template-id or
13374 template-parameter-list now. */
13375 parser->greater_than_is_operator_p
13376 = saved_greater_than_is_operator_p;
13378 /* Restore the old message and the integral constant expression
13379 flags. */
13380 parser->type_definition_forbidden_message = saved_message;
13381 parser->integral_constant_expression_p
13382 = saved_integral_constant_expression_p;
13383 parser->non_integral_constant_expression_p
13384 = saved_non_integral_constant_expression_p;
13386 /* Parse to the closing `)'. */
13387 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13389 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13390 /*consume_paren=*/true);
13391 return error_mark_node;
13394 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
13395 tf_warning_or_error);
13397 rewrite:
13398 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13399 it again. */
13400 start_token->type = CPP_DECLTYPE;
13401 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13402 start_token->u.tree_check_value->value = expr;
13403 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
13404 start_token->keyword = RID_MAX;
13405 cp_lexer_purge_tokens_after (parser->lexer, start_token);
13407 return expr;
13410 /* Special member functions [gram.special] */
13412 /* Parse a conversion-function-id.
13414 conversion-function-id:
13415 operator conversion-type-id
13417 Returns an IDENTIFIER_NODE representing the operator. */
13419 static tree
13420 cp_parser_conversion_function_id (cp_parser* parser)
13422 tree type;
13423 tree saved_scope;
13424 tree saved_qualifying_scope;
13425 tree saved_object_scope;
13426 tree pushed_scope = NULL_TREE;
13428 /* Look for the `operator' token. */
13429 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13430 return error_mark_node;
13431 /* When we parse the conversion-type-id, the current scope will be
13432 reset. However, we need that information in able to look up the
13433 conversion function later, so we save it here. */
13434 saved_scope = parser->scope;
13435 saved_qualifying_scope = parser->qualifying_scope;
13436 saved_object_scope = parser->object_scope;
13437 /* We must enter the scope of the class so that the names of
13438 entities declared within the class are available in the
13439 conversion-type-id. For example, consider:
13441 struct S {
13442 typedef int I;
13443 operator I();
13446 S::operator I() { ... }
13448 In order to see that `I' is a type-name in the definition, we
13449 must be in the scope of `S'. */
13450 if (saved_scope)
13451 pushed_scope = push_scope (saved_scope);
13452 /* Parse the conversion-type-id. */
13453 type = cp_parser_conversion_type_id (parser);
13454 /* Leave the scope of the class, if any. */
13455 if (pushed_scope)
13456 pop_scope (pushed_scope);
13457 /* Restore the saved scope. */
13458 parser->scope = saved_scope;
13459 parser->qualifying_scope = saved_qualifying_scope;
13460 parser->object_scope = saved_object_scope;
13461 /* If the TYPE is invalid, indicate failure. */
13462 if (type == error_mark_node)
13463 return error_mark_node;
13464 return mangle_conv_op_name_for_type (type);
13467 /* Parse a conversion-type-id:
13469 conversion-type-id:
13470 type-specifier-seq conversion-declarator [opt]
13472 Returns the TYPE specified. */
13474 static tree
13475 cp_parser_conversion_type_id (cp_parser* parser)
13477 tree attributes;
13478 cp_decl_specifier_seq type_specifiers;
13479 cp_declarator *declarator;
13480 tree type_specified;
13481 const char *saved_message;
13483 /* Parse the attributes. */
13484 attributes = cp_parser_attributes_opt (parser);
13486 saved_message = parser->type_definition_forbidden_message;
13487 parser->type_definition_forbidden_message
13488 = G_("types may not be defined in a conversion-type-id");
13490 /* Parse the type-specifiers. */
13491 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13492 /*is_trailing_return=*/false,
13493 &type_specifiers);
13495 parser->type_definition_forbidden_message = saved_message;
13497 /* If that didn't work, stop. */
13498 if (type_specifiers.type == error_mark_node)
13499 return error_mark_node;
13500 /* Parse the conversion-declarator. */
13501 declarator = cp_parser_conversion_declarator_opt (parser);
13503 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
13504 /*initialized=*/0, &attributes);
13505 if (attributes)
13506 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
13508 /* Don't give this error when parsing tentatively. This happens to
13509 work because we always parse this definitively once. */
13510 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
13511 && type_uses_auto (type_specified))
13513 if (cxx_dialect < cxx14)
13515 error ("invalid use of %<auto%> in conversion operator");
13516 return error_mark_node;
13518 else if (template_parm_scope_p ())
13519 warning (0, "use of %<auto%> in member template "
13520 "conversion operator can never be deduced");
13523 return type_specified;
13526 /* Parse an (optional) conversion-declarator.
13528 conversion-declarator:
13529 ptr-operator conversion-declarator [opt]
13533 static cp_declarator *
13534 cp_parser_conversion_declarator_opt (cp_parser* parser)
13536 enum tree_code code;
13537 tree class_type, std_attributes = NULL_TREE;
13538 cp_cv_quals cv_quals;
13540 /* We don't know if there's a ptr-operator next, or not. */
13541 cp_parser_parse_tentatively (parser);
13542 /* Try the ptr-operator. */
13543 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
13544 &std_attributes);
13545 /* If it worked, look for more conversion-declarators. */
13546 if (cp_parser_parse_definitely (parser))
13548 cp_declarator *declarator;
13550 /* Parse another optional declarator. */
13551 declarator = cp_parser_conversion_declarator_opt (parser);
13553 declarator = cp_parser_make_indirect_declarator
13554 (code, class_type, cv_quals, declarator, std_attributes);
13556 return declarator;
13559 return NULL;
13562 /* Parse an (optional) ctor-initializer.
13564 ctor-initializer:
13565 : mem-initializer-list
13567 Returns TRUE iff the ctor-initializer was actually present. */
13569 static bool
13570 cp_parser_ctor_initializer_opt (cp_parser* parser)
13572 /* If the next token is not a `:', then there is no
13573 ctor-initializer. */
13574 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13576 /* Do default initialization of any bases and members. */
13577 if (DECL_CONSTRUCTOR_P (current_function_decl))
13578 finish_mem_initializers (NULL_TREE);
13580 return false;
13583 /* Consume the `:' token. */
13584 cp_lexer_consume_token (parser->lexer);
13585 /* And the mem-initializer-list. */
13586 cp_parser_mem_initializer_list (parser);
13588 return true;
13591 /* Parse a mem-initializer-list.
13593 mem-initializer-list:
13594 mem-initializer ... [opt]
13595 mem-initializer ... [opt] , mem-initializer-list */
13597 static void
13598 cp_parser_mem_initializer_list (cp_parser* parser)
13600 tree mem_initializer_list = NULL_TREE;
13601 tree target_ctor = error_mark_node;
13602 cp_token *token = cp_lexer_peek_token (parser->lexer);
13604 /* Let the semantic analysis code know that we are starting the
13605 mem-initializer-list. */
13606 if (!DECL_CONSTRUCTOR_P (current_function_decl))
13607 error_at (token->location,
13608 "only constructors take member initializers");
13610 /* Loop through the list. */
13611 while (true)
13613 tree mem_initializer;
13615 token = cp_lexer_peek_token (parser->lexer);
13616 /* Parse the mem-initializer. */
13617 mem_initializer = cp_parser_mem_initializer (parser);
13618 /* If the next token is a `...', we're expanding member initializers. */
13619 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13621 /* Consume the `...'. */
13622 cp_lexer_consume_token (parser->lexer);
13624 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
13625 can be expanded but members cannot. */
13626 if (mem_initializer != error_mark_node
13627 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
13629 error_at (token->location,
13630 "cannot expand initializer for member %<%D%>",
13631 TREE_PURPOSE (mem_initializer));
13632 mem_initializer = error_mark_node;
13635 /* Construct the pack expansion type. */
13636 if (mem_initializer != error_mark_node)
13637 mem_initializer = make_pack_expansion (mem_initializer);
13639 if (target_ctor != error_mark_node
13640 && mem_initializer != error_mark_node)
13642 error ("mem-initializer for %qD follows constructor delegation",
13643 TREE_PURPOSE (mem_initializer));
13644 mem_initializer = error_mark_node;
13646 /* Look for a target constructor. */
13647 if (mem_initializer != error_mark_node
13648 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
13649 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
13651 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
13652 if (mem_initializer_list)
13654 error ("constructor delegation follows mem-initializer for %qD",
13655 TREE_PURPOSE (mem_initializer_list));
13656 mem_initializer = error_mark_node;
13658 target_ctor = mem_initializer;
13660 /* Add it to the list, unless it was erroneous. */
13661 if (mem_initializer != error_mark_node)
13663 TREE_CHAIN (mem_initializer) = mem_initializer_list;
13664 mem_initializer_list = mem_initializer;
13666 /* If the next token is not a `,', we're done. */
13667 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13668 break;
13669 /* Consume the `,' token. */
13670 cp_lexer_consume_token (parser->lexer);
13673 /* Perform semantic analysis. */
13674 if (DECL_CONSTRUCTOR_P (current_function_decl))
13675 finish_mem_initializers (mem_initializer_list);
13678 /* Parse a mem-initializer.
13680 mem-initializer:
13681 mem-initializer-id ( expression-list [opt] )
13682 mem-initializer-id braced-init-list
13684 GNU extension:
13686 mem-initializer:
13687 ( expression-list [opt] )
13689 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
13690 class) or FIELD_DECL (for a non-static data member) to initialize;
13691 the TREE_VALUE is the expression-list. An empty initialization
13692 list is represented by void_list_node. */
13694 static tree
13695 cp_parser_mem_initializer (cp_parser* parser)
13697 tree mem_initializer_id;
13698 tree expression_list;
13699 tree member;
13700 cp_token *token = cp_lexer_peek_token (parser->lexer);
13702 /* Find out what is being initialized. */
13703 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13705 permerror (token->location,
13706 "anachronistic old-style base class initializer");
13707 mem_initializer_id = NULL_TREE;
13709 else
13711 mem_initializer_id = cp_parser_mem_initializer_id (parser);
13712 if (mem_initializer_id == error_mark_node)
13713 return mem_initializer_id;
13715 member = expand_member_init (mem_initializer_id);
13716 if (member && !DECL_P (member))
13717 in_base_initializer = 1;
13719 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13721 bool expr_non_constant_p;
13722 cp_lexer_set_source_position (parser->lexer);
13723 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13724 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
13725 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
13726 expression_list = build_tree_list (NULL_TREE, expression_list);
13728 else
13730 vec<tree, va_gc> *vec;
13731 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
13732 /*cast_p=*/false,
13733 /*allow_expansion_p=*/true,
13734 /*non_constant_p=*/NULL);
13735 if (vec == NULL)
13736 return error_mark_node;
13737 expression_list = build_tree_list_vec (vec);
13738 release_tree_vector (vec);
13741 if (expression_list == error_mark_node)
13742 return error_mark_node;
13743 if (!expression_list)
13744 expression_list = void_type_node;
13746 in_base_initializer = 0;
13748 return member ? build_tree_list (member, expression_list) : error_mark_node;
13751 /* Parse a mem-initializer-id.
13753 mem-initializer-id:
13754 :: [opt] nested-name-specifier [opt] class-name
13755 decltype-specifier (C++11)
13756 identifier
13758 Returns a TYPE indicating the class to be initialized for the first
13759 production (and the second in C++11). Returns an IDENTIFIER_NODE
13760 indicating the data member to be initialized for the last production. */
13762 static tree
13763 cp_parser_mem_initializer_id (cp_parser* parser)
13765 bool global_scope_p;
13766 bool nested_name_specifier_p;
13767 bool template_p = false;
13768 tree id;
13770 cp_token *token = cp_lexer_peek_token (parser->lexer);
13772 /* `typename' is not allowed in this context ([temp.res]). */
13773 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13775 error_at (token->location,
13776 "keyword %<typename%> not allowed in this context (a qualified "
13777 "member initializer is implicitly a type)");
13778 cp_lexer_consume_token (parser->lexer);
13780 /* Look for the optional `::' operator. */
13781 global_scope_p
13782 = (cp_parser_global_scope_opt (parser,
13783 /*current_scope_valid_p=*/false)
13784 != NULL_TREE);
13785 /* Look for the optional nested-name-specifier. The simplest way to
13786 implement:
13788 [temp.res]
13790 The keyword `typename' is not permitted in a base-specifier or
13791 mem-initializer; in these contexts a qualified name that
13792 depends on a template-parameter is implicitly assumed to be a
13793 type name.
13795 is to assume that we have seen the `typename' keyword at this
13796 point. */
13797 nested_name_specifier_p
13798 = (cp_parser_nested_name_specifier_opt (parser,
13799 /*typename_keyword_p=*/true,
13800 /*check_dependency_p=*/true,
13801 /*type_p=*/true,
13802 /*is_declaration=*/true)
13803 != NULL_TREE);
13804 if (nested_name_specifier_p)
13805 template_p = cp_parser_optional_template_keyword (parser);
13806 /* If there is a `::' operator or a nested-name-specifier, then we
13807 are definitely looking for a class-name. */
13808 if (global_scope_p || nested_name_specifier_p)
13809 return cp_parser_class_name (parser,
13810 /*typename_keyword_p=*/true,
13811 /*template_keyword_p=*/template_p,
13812 typename_type,
13813 /*check_dependency_p=*/true,
13814 /*class_head_p=*/false,
13815 /*is_declaration=*/true);
13816 /* Otherwise, we could also be looking for an ordinary identifier. */
13817 cp_parser_parse_tentatively (parser);
13818 if (cp_lexer_next_token_is_decltype (parser->lexer))
13819 /* Try a decltype-specifier. */
13820 id = cp_parser_decltype (parser);
13821 else
13822 /* Otherwise, try a class-name. */
13823 id = cp_parser_class_name (parser,
13824 /*typename_keyword_p=*/true,
13825 /*template_keyword_p=*/false,
13826 none_type,
13827 /*check_dependency_p=*/true,
13828 /*class_head_p=*/false,
13829 /*is_declaration=*/true);
13830 /* If we found one, we're done. */
13831 if (cp_parser_parse_definitely (parser))
13832 return id;
13833 /* Otherwise, look for an ordinary identifier. */
13834 return cp_parser_identifier (parser);
13837 /* Overloading [gram.over] */
13839 /* Parse an operator-function-id.
13841 operator-function-id:
13842 operator operator
13844 Returns an IDENTIFIER_NODE for the operator which is a
13845 human-readable spelling of the identifier, e.g., `operator +'. */
13847 static cp_expr
13848 cp_parser_operator_function_id (cp_parser* parser)
13850 /* Look for the `operator' keyword. */
13851 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13852 return error_mark_node;
13853 /* And then the name of the operator itself. */
13854 return cp_parser_operator (parser);
13857 /* Return an identifier node for a user-defined literal operator.
13858 The suffix identifier is chained to the operator name identifier. */
13860 static tree
13861 cp_literal_operator_id (const char* name)
13863 tree identifier;
13864 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
13865 + strlen (name) + 10);
13866 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
13867 identifier = get_identifier (buffer);
13869 return identifier;
13872 /* Parse an operator.
13874 operator:
13875 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
13876 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
13877 || ++ -- , ->* -> () []
13879 GNU Extensions:
13881 operator:
13882 <? >? <?= >?=
13884 Returns an IDENTIFIER_NODE for the operator which is a
13885 human-readable spelling of the identifier, e.g., `operator +'. */
13887 static cp_expr
13888 cp_parser_operator (cp_parser* parser)
13890 tree id = NULL_TREE;
13891 cp_token *token;
13892 bool utf8 = false;
13894 /* Peek at the next token. */
13895 token = cp_lexer_peek_token (parser->lexer);
13897 location_t start_loc = token->location;
13899 /* Figure out which operator we have. */
13900 switch (token->type)
13902 case CPP_KEYWORD:
13904 enum tree_code op;
13906 /* The keyword should be either `new' or `delete'. */
13907 if (token->keyword == RID_NEW)
13908 op = NEW_EXPR;
13909 else if (token->keyword == RID_DELETE)
13910 op = DELETE_EXPR;
13911 else
13912 break;
13914 /* Consume the `new' or `delete' token. */
13915 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
13917 /* Peek at the next token. */
13918 token = cp_lexer_peek_token (parser->lexer);
13919 /* If it's a `[' token then this is the array variant of the
13920 operator. */
13921 if (token->type == CPP_OPEN_SQUARE)
13923 /* Consume the `[' token. */
13924 cp_lexer_consume_token (parser->lexer);
13925 /* Look for the `]' token. */
13926 if (cp_token *close_token
13927 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13928 end_loc = close_token->location;
13929 id = ansi_opname (op == NEW_EXPR
13930 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
13932 /* Otherwise, we have the non-array variant. */
13933 else
13934 id = ansi_opname (op);
13936 location_t loc = make_location (start_loc, start_loc, end_loc);
13938 return cp_expr (id, loc);
13941 case CPP_PLUS:
13942 id = ansi_opname (PLUS_EXPR);
13943 break;
13945 case CPP_MINUS:
13946 id = ansi_opname (MINUS_EXPR);
13947 break;
13949 case CPP_MULT:
13950 id = ansi_opname (MULT_EXPR);
13951 break;
13953 case CPP_DIV:
13954 id = ansi_opname (TRUNC_DIV_EXPR);
13955 break;
13957 case CPP_MOD:
13958 id = ansi_opname (TRUNC_MOD_EXPR);
13959 break;
13961 case CPP_XOR:
13962 id = ansi_opname (BIT_XOR_EXPR);
13963 break;
13965 case CPP_AND:
13966 id = ansi_opname (BIT_AND_EXPR);
13967 break;
13969 case CPP_OR:
13970 id = ansi_opname (BIT_IOR_EXPR);
13971 break;
13973 case CPP_COMPL:
13974 id = ansi_opname (BIT_NOT_EXPR);
13975 break;
13977 case CPP_NOT:
13978 id = ansi_opname (TRUTH_NOT_EXPR);
13979 break;
13981 case CPP_EQ:
13982 id = ansi_assopname (NOP_EXPR);
13983 break;
13985 case CPP_LESS:
13986 id = ansi_opname (LT_EXPR);
13987 break;
13989 case CPP_GREATER:
13990 id = ansi_opname (GT_EXPR);
13991 break;
13993 case CPP_PLUS_EQ:
13994 id = ansi_assopname (PLUS_EXPR);
13995 break;
13997 case CPP_MINUS_EQ:
13998 id = ansi_assopname (MINUS_EXPR);
13999 break;
14001 case CPP_MULT_EQ:
14002 id = ansi_assopname (MULT_EXPR);
14003 break;
14005 case CPP_DIV_EQ:
14006 id = ansi_assopname (TRUNC_DIV_EXPR);
14007 break;
14009 case CPP_MOD_EQ:
14010 id = ansi_assopname (TRUNC_MOD_EXPR);
14011 break;
14013 case CPP_XOR_EQ:
14014 id = ansi_assopname (BIT_XOR_EXPR);
14015 break;
14017 case CPP_AND_EQ:
14018 id = ansi_assopname (BIT_AND_EXPR);
14019 break;
14021 case CPP_OR_EQ:
14022 id = ansi_assopname (BIT_IOR_EXPR);
14023 break;
14025 case CPP_LSHIFT:
14026 id = ansi_opname (LSHIFT_EXPR);
14027 break;
14029 case CPP_RSHIFT:
14030 id = ansi_opname (RSHIFT_EXPR);
14031 break;
14033 case CPP_LSHIFT_EQ:
14034 id = ansi_assopname (LSHIFT_EXPR);
14035 break;
14037 case CPP_RSHIFT_EQ:
14038 id = ansi_assopname (RSHIFT_EXPR);
14039 break;
14041 case CPP_EQ_EQ:
14042 id = ansi_opname (EQ_EXPR);
14043 break;
14045 case CPP_NOT_EQ:
14046 id = ansi_opname (NE_EXPR);
14047 break;
14049 case CPP_LESS_EQ:
14050 id = ansi_opname (LE_EXPR);
14051 break;
14053 case CPP_GREATER_EQ:
14054 id = ansi_opname (GE_EXPR);
14055 break;
14057 case CPP_AND_AND:
14058 id = ansi_opname (TRUTH_ANDIF_EXPR);
14059 break;
14061 case CPP_OR_OR:
14062 id = ansi_opname (TRUTH_ORIF_EXPR);
14063 break;
14065 case CPP_PLUS_PLUS:
14066 id = ansi_opname (POSTINCREMENT_EXPR);
14067 break;
14069 case CPP_MINUS_MINUS:
14070 id = ansi_opname (PREDECREMENT_EXPR);
14071 break;
14073 case CPP_COMMA:
14074 id = ansi_opname (COMPOUND_EXPR);
14075 break;
14077 case CPP_DEREF_STAR:
14078 id = ansi_opname (MEMBER_REF);
14079 break;
14081 case CPP_DEREF:
14082 id = ansi_opname (COMPONENT_REF);
14083 break;
14085 case CPP_OPEN_PAREN:
14086 /* Consume the `('. */
14087 cp_lexer_consume_token (parser->lexer);
14088 /* Look for the matching `)'. */
14089 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14090 return ansi_opname (CALL_EXPR);
14092 case CPP_OPEN_SQUARE:
14093 /* Consume the `['. */
14094 cp_lexer_consume_token (parser->lexer);
14095 /* Look for the matching `]'. */
14096 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14097 return ansi_opname (ARRAY_REF);
14099 case CPP_UTF8STRING:
14100 case CPP_UTF8STRING_USERDEF:
14101 utf8 = true;
14102 /* FALLTHRU */
14103 case CPP_STRING:
14104 case CPP_WSTRING:
14105 case CPP_STRING16:
14106 case CPP_STRING32:
14107 case CPP_STRING_USERDEF:
14108 case CPP_WSTRING_USERDEF:
14109 case CPP_STRING16_USERDEF:
14110 case CPP_STRING32_USERDEF:
14112 tree str, string_tree;
14113 int sz, len;
14115 if (cxx_dialect == cxx98)
14116 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14118 /* Consume the string. */
14119 str = cp_parser_string_literal (parser, /*translate=*/true,
14120 /*wide_ok=*/true, /*lookup_udlit=*/false);
14121 if (str == error_mark_node)
14122 return error_mark_node;
14123 else if (TREE_CODE (str) == USERDEF_LITERAL)
14125 string_tree = USERDEF_LITERAL_VALUE (str);
14126 id = USERDEF_LITERAL_SUFFIX_ID (str);
14128 else
14130 string_tree = str;
14131 /* Look for the suffix identifier. */
14132 token = cp_lexer_peek_token (parser->lexer);
14133 if (token->type == CPP_NAME)
14134 id = cp_parser_identifier (parser);
14135 else if (token->type == CPP_KEYWORD)
14137 error ("unexpected keyword;"
14138 " remove space between quotes and suffix identifier");
14139 return error_mark_node;
14141 else
14143 error ("expected suffix identifier");
14144 return error_mark_node;
14147 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14148 (TREE_TYPE (TREE_TYPE (string_tree))));
14149 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14150 if (len != 0)
14152 error ("expected empty string after %<operator%> keyword");
14153 return error_mark_node;
14155 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14156 != char_type_node)
14158 error ("invalid encoding prefix in literal operator");
14159 return error_mark_node;
14161 if (id != error_mark_node)
14163 const char *name = IDENTIFIER_POINTER (id);
14164 id = cp_literal_operator_id (name);
14166 return id;
14169 default:
14170 /* Anything else is an error. */
14171 break;
14174 /* If we have selected an identifier, we need to consume the
14175 operator token. */
14176 if (id)
14177 cp_lexer_consume_token (parser->lexer);
14178 /* Otherwise, no valid operator name was present. */
14179 else
14181 cp_parser_error (parser, "expected operator");
14182 id = error_mark_node;
14185 return cp_expr (id, start_loc);
14188 /* Parse a template-declaration.
14190 template-declaration:
14191 export [opt] template < template-parameter-list > declaration
14193 If MEMBER_P is TRUE, this template-declaration occurs within a
14194 class-specifier.
14196 The grammar rule given by the standard isn't correct. What
14197 is really meant is:
14199 template-declaration:
14200 export [opt] template-parameter-list-seq
14201 decl-specifier-seq [opt] init-declarator [opt] ;
14202 export [opt] template-parameter-list-seq
14203 function-definition
14205 template-parameter-list-seq:
14206 template-parameter-list-seq [opt]
14207 template < template-parameter-list >
14209 Concept Extensions:
14211 template-parameter-list-seq:
14212 template < template-parameter-list > requires-clause [opt]
14214 requires-clause:
14215 requires logical-or-expression */
14217 static void
14218 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14220 /* Check for `export'. */
14221 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14223 /* Consume the `export' token. */
14224 cp_lexer_consume_token (parser->lexer);
14225 /* Warn that we do not support `export'. */
14226 warning (0, "keyword %<export%> not implemented, and will be ignored");
14229 cp_parser_template_declaration_after_export (parser, member_p);
14232 /* Parse a template-parameter-list.
14234 template-parameter-list:
14235 template-parameter
14236 template-parameter-list , template-parameter
14238 Returns a TREE_LIST. Each node represents a template parameter.
14239 The nodes are connected via their TREE_CHAINs. */
14241 static tree
14242 cp_parser_template_parameter_list (cp_parser* parser)
14244 tree parameter_list = NULL_TREE;
14246 begin_template_parm_list ();
14248 /* The loop below parses the template parms. We first need to know
14249 the total number of template parms to be able to compute proper
14250 canonical types of each dependent type. So after the loop, when
14251 we know the total number of template parms,
14252 end_template_parm_list computes the proper canonical types and
14253 fixes up the dependent types accordingly. */
14254 while (true)
14256 tree parameter;
14257 bool is_non_type;
14258 bool is_parameter_pack;
14259 location_t parm_loc;
14261 /* Parse the template-parameter. */
14262 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14263 parameter = cp_parser_template_parameter (parser,
14264 &is_non_type,
14265 &is_parameter_pack);
14266 /* Add it to the list. */
14267 if (parameter != error_mark_node)
14268 parameter_list = process_template_parm (parameter_list,
14269 parm_loc,
14270 parameter,
14271 is_non_type,
14272 is_parameter_pack);
14273 else
14275 tree err_parm = build_tree_list (parameter, parameter);
14276 parameter_list = chainon (parameter_list, err_parm);
14279 /* If the next token is not a `,', we're done. */
14280 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14281 break;
14282 /* Otherwise, consume the `,' token. */
14283 cp_lexer_consume_token (parser->lexer);
14286 return end_template_parm_list (parameter_list);
14289 /* Parse a introduction-list.
14291 introduction-list:
14292 introduced-parameter
14293 introduction-list , introduced-parameter
14295 introduced-parameter:
14296 ...[opt] identifier
14298 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14299 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14300 WILDCARD_DECL will also have DECL_NAME set and token location in
14301 DECL_SOURCE_LOCATION. */
14303 static tree
14304 cp_parser_introduction_list (cp_parser *parser)
14306 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14308 while (true)
14310 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14311 if (is_pack)
14312 cp_lexer_consume_token (parser->lexer);
14314 /* Build placeholder. */
14315 tree parm = build_nt (WILDCARD_DECL);
14316 DECL_SOURCE_LOCATION (parm)
14317 = cp_lexer_peek_token (parser->lexer)->location;
14318 DECL_NAME (parm) = cp_parser_identifier (parser);
14319 WILDCARD_PACK_P (parm) = is_pack;
14320 vec_safe_push (introduction_vec, parm);
14322 /* If the next token is not a `,', we're done. */
14323 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14324 break;
14325 /* Otherwise, consume the `,' token. */
14326 cp_lexer_consume_token (parser->lexer);
14329 /* Convert the vec into a TREE_VEC. */
14330 tree introduction_list = make_tree_vec (introduction_vec->length ());
14331 unsigned int n;
14332 tree parm;
14333 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14334 TREE_VEC_ELT (introduction_list, n) = parm;
14336 release_tree_vector (introduction_vec);
14337 return introduction_list;
14340 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14341 is an abstract declarator. */
14343 static inline cp_declarator*
14344 get_id_declarator (cp_declarator *declarator)
14346 cp_declarator *d = declarator;
14347 while (d && d->kind != cdk_id)
14348 d = d->declarator;
14349 return d;
14352 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14353 is an abstract declarator. */
14355 static inline tree
14356 get_unqualified_id (cp_declarator *declarator)
14358 declarator = get_id_declarator (declarator);
14359 if (declarator)
14360 return declarator->u.id.unqualified_name;
14361 else
14362 return NULL_TREE;
14365 /* Returns true if DECL represents a constrained-parameter. */
14367 static inline bool
14368 is_constrained_parameter (tree decl)
14370 return (decl
14371 && TREE_CODE (decl) == TYPE_DECL
14372 && CONSTRAINED_PARM_CONCEPT (decl)
14373 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14376 /* Returns true if PARM declares a constrained-parameter. */
14378 static inline bool
14379 is_constrained_parameter (cp_parameter_declarator *parm)
14381 return is_constrained_parameter (parm->decl_specifiers.type);
14384 /* Check that the type parameter is only a declarator-id, and that its
14385 type is not cv-qualified. */
14387 bool
14388 cp_parser_check_constrained_type_parm (cp_parser *parser,
14389 cp_parameter_declarator *parm)
14391 if (!parm->declarator)
14392 return true;
14394 if (parm->declarator->kind != cdk_id)
14396 cp_parser_error (parser, "invalid constrained type parameter");
14397 return false;
14400 /* Don't allow cv-qualified type parameters. */
14401 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
14402 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
14404 cp_parser_error (parser, "cv-qualified type parameter");
14405 return false;
14408 return true;
14411 /* Finish parsing/processing a template type parameter and checking
14412 various restrictions. */
14414 static inline tree
14415 cp_parser_constrained_type_template_parm (cp_parser *parser,
14416 tree id,
14417 cp_parameter_declarator* parmdecl)
14419 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
14420 return finish_template_type_parm (class_type_node, id);
14421 else
14422 return error_mark_node;
14425 static tree
14426 finish_constrained_template_template_parm (tree proto, tree id)
14428 /* FIXME: This should probably be copied, and we may need to adjust
14429 the template parameter depths. */
14430 tree saved_parms = current_template_parms;
14431 begin_template_parm_list ();
14432 current_template_parms = DECL_TEMPLATE_PARMS (proto);
14433 end_template_parm_list ();
14435 tree parm = finish_template_template_parm (class_type_node, id);
14436 current_template_parms = saved_parms;
14438 return parm;
14441 /* Finish parsing/processing a template template parameter by borrowing
14442 the template parameter list from the prototype parameter. */
14444 static tree
14445 cp_parser_constrained_template_template_parm (cp_parser *parser,
14446 tree proto,
14447 tree id,
14448 cp_parameter_declarator *parmdecl)
14450 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
14451 return error_mark_node;
14452 return finish_constrained_template_template_parm (proto, id);
14455 /* Create a new non-type template parameter from the given PARM
14456 declarator. */
14458 static tree
14459 constrained_non_type_template_parm (bool *is_non_type,
14460 cp_parameter_declarator *parm)
14462 *is_non_type = true;
14463 cp_declarator *decl = parm->declarator;
14464 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
14465 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
14466 return grokdeclarator (decl, specs, TPARM, 0, NULL);
14469 /* Build a constrained template parameter based on the PARMDECL
14470 declarator. The type of PARMDECL is the constrained type, which
14471 refers to the prototype template parameter that ultimately
14472 specifies the type of the declared parameter. */
14474 static tree
14475 finish_constrained_parameter (cp_parser *parser,
14476 cp_parameter_declarator *parmdecl,
14477 bool *is_non_type,
14478 bool *is_parameter_pack)
14480 tree decl = parmdecl->decl_specifiers.type;
14481 tree id = get_unqualified_id (parmdecl->declarator);
14482 tree def = parmdecl->default_argument;
14483 tree proto = DECL_INITIAL (decl);
14485 /* A template parameter constrained by a variadic concept shall also
14486 be declared as a template parameter pack. */
14487 bool is_variadic = template_parameter_pack_p (proto);
14488 if (is_variadic && !*is_parameter_pack)
14489 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
14491 /* Build the parameter. Return an error if the declarator was invalid. */
14492 tree parm;
14493 if (TREE_CODE (proto) == TYPE_DECL)
14494 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
14495 else if (TREE_CODE (proto) == TEMPLATE_DECL)
14496 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
14497 parmdecl);
14498 else
14499 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
14500 if (parm == error_mark_node)
14501 return error_mark_node;
14503 /* Finish the parameter decl and create a node attaching the
14504 default argument and constraint. */
14505 parm = build_tree_list (def, parm);
14506 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
14508 return parm;
14511 /* Returns true if the parsed type actually represents the declaration
14512 of a type template-parameter. */
14514 static inline bool
14515 declares_constrained_type_template_parameter (tree type)
14517 return (is_constrained_parameter (type)
14518 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
14522 /* Returns true if the parsed type actually represents the declaration of
14523 a template template-parameter. */
14525 static bool
14526 declares_constrained_template_template_parameter (tree type)
14528 return (is_constrained_parameter (type)
14529 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
14532 /* Parse a default argument for a type template-parameter.
14533 Note that diagnostics are handled in cp_parser_template_parameter. */
14535 static tree
14536 cp_parser_default_type_template_argument (cp_parser *parser)
14538 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14540 /* Consume the `=' token. */
14541 cp_lexer_consume_token (parser->lexer);
14543 cp_token *token = cp_lexer_peek_token (parser->lexer);
14545 /* Parse the default-argument. */
14546 push_deferring_access_checks (dk_no_deferred);
14547 tree default_argument = cp_parser_type_id (parser);
14548 pop_deferring_access_checks ();
14550 if (flag_concepts && type_uses_auto (default_argument))
14552 error_at (token->location,
14553 "invalid use of %<auto%> in default template argument");
14554 return error_mark_node;
14557 return default_argument;
14560 /* Parse a default argument for a template template-parameter. */
14562 static tree
14563 cp_parser_default_template_template_argument (cp_parser *parser)
14565 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14567 bool is_template;
14569 /* Consume the `='. */
14570 cp_lexer_consume_token (parser->lexer);
14571 /* Parse the id-expression. */
14572 push_deferring_access_checks (dk_no_deferred);
14573 /* save token before parsing the id-expression, for error
14574 reporting */
14575 const cp_token* token = cp_lexer_peek_token (parser->lexer);
14576 tree default_argument
14577 = cp_parser_id_expression (parser,
14578 /*template_keyword_p=*/false,
14579 /*check_dependency_p=*/true,
14580 /*template_p=*/&is_template,
14581 /*declarator_p=*/false,
14582 /*optional_p=*/false);
14583 if (TREE_CODE (default_argument) == TYPE_DECL)
14584 /* If the id-expression was a template-id that refers to
14585 a template-class, we already have the declaration here,
14586 so no further lookup is needed. */
14588 else
14589 /* Look up the name. */
14590 default_argument
14591 = cp_parser_lookup_name (parser, default_argument,
14592 none_type,
14593 /*is_template=*/is_template,
14594 /*is_namespace=*/false,
14595 /*check_dependency=*/true,
14596 /*ambiguous_decls=*/NULL,
14597 token->location);
14598 /* See if the default argument is valid. */
14599 default_argument = check_template_template_default_arg (default_argument);
14600 pop_deferring_access_checks ();
14601 return default_argument;
14604 /* Parse a template-parameter.
14606 template-parameter:
14607 type-parameter
14608 parameter-declaration
14610 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
14611 the parameter. The TREE_PURPOSE is the default value, if any.
14612 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
14613 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
14614 set to true iff this parameter is a parameter pack. */
14616 static tree
14617 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
14618 bool *is_parameter_pack)
14620 cp_token *token;
14621 cp_parameter_declarator *parameter_declarator;
14622 tree parm;
14624 /* Assume it is a type parameter or a template parameter. */
14625 *is_non_type = false;
14626 /* Assume it not a parameter pack. */
14627 *is_parameter_pack = false;
14628 /* Peek at the next token. */
14629 token = cp_lexer_peek_token (parser->lexer);
14630 /* If it is `class' or `template', we have a type-parameter. */
14631 if (token->keyword == RID_TEMPLATE)
14632 return cp_parser_type_parameter (parser, is_parameter_pack);
14633 /* If it is `class' or `typename' we do not know yet whether it is a
14634 type parameter or a non-type parameter. Consider:
14636 template <typename T, typename T::X X> ...
14640 template <class C, class D*> ...
14642 Here, the first parameter is a type parameter, and the second is
14643 a non-type parameter. We can tell by looking at the token after
14644 the identifier -- if it is a `,', `=', or `>' then we have a type
14645 parameter. */
14646 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
14648 /* Peek at the token after `class' or `typename'. */
14649 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14650 /* If it's an ellipsis, we have a template type parameter
14651 pack. */
14652 if (token->type == CPP_ELLIPSIS)
14653 return cp_parser_type_parameter (parser, is_parameter_pack);
14654 /* If it's an identifier, skip it. */
14655 if (token->type == CPP_NAME)
14656 token = cp_lexer_peek_nth_token (parser->lexer, 3);
14657 /* Now, see if the token looks like the end of a template
14658 parameter. */
14659 if (token->type == CPP_COMMA
14660 || token->type == CPP_EQ
14661 || token->type == CPP_GREATER)
14662 return cp_parser_type_parameter (parser, is_parameter_pack);
14665 /* Otherwise, it is a non-type parameter or a constrained parameter.
14667 [temp.param]
14669 When parsing a default template-argument for a non-type
14670 template-parameter, the first non-nested `>' is taken as the end
14671 of the template parameter-list rather than a greater-than
14672 operator. */
14673 parameter_declarator
14674 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
14675 /*parenthesized_p=*/NULL);
14677 if (!parameter_declarator)
14678 return error_mark_node;
14680 /* If the parameter declaration is marked as a parameter pack, set
14681 *IS_PARAMETER_PACK to notify the caller. */
14682 if (parameter_declarator->template_parameter_pack_p)
14683 *is_parameter_pack = true;
14685 if (parameter_declarator->default_argument)
14687 /* Can happen in some cases of erroneous input (c++/34892). */
14688 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14689 /* Consume the `...' for better error recovery. */
14690 cp_lexer_consume_token (parser->lexer);
14693 // The parameter may have been constrained.
14694 if (is_constrained_parameter (parameter_declarator))
14695 return finish_constrained_parameter (parser,
14696 parameter_declarator,
14697 is_non_type,
14698 is_parameter_pack);
14700 // Now we're sure that the parameter is a non-type parameter.
14701 *is_non_type = true;
14703 parm = grokdeclarator (parameter_declarator->declarator,
14704 &parameter_declarator->decl_specifiers,
14705 TPARM, /*initialized=*/0,
14706 /*attrlist=*/NULL);
14707 if (parm == error_mark_node)
14708 return error_mark_node;
14710 return build_tree_list (parameter_declarator->default_argument, parm);
14713 /* Parse a type-parameter.
14715 type-parameter:
14716 class identifier [opt]
14717 class identifier [opt] = type-id
14718 typename identifier [opt]
14719 typename identifier [opt] = type-id
14720 template < template-parameter-list > class identifier [opt]
14721 template < template-parameter-list > class identifier [opt]
14722 = id-expression
14724 GNU Extension (variadic templates):
14726 type-parameter:
14727 class ... identifier [opt]
14728 typename ... identifier [opt]
14730 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
14731 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
14732 the declaration of the parameter.
14734 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
14736 static tree
14737 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
14739 cp_token *token;
14740 tree parameter;
14742 /* Look for a keyword to tell us what kind of parameter this is. */
14743 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
14744 if (!token)
14745 return error_mark_node;
14747 switch (token->keyword)
14749 case RID_CLASS:
14750 case RID_TYPENAME:
14752 tree identifier;
14753 tree default_argument;
14755 /* If the next token is an ellipsis, we have a template
14756 argument pack. */
14757 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14759 /* Consume the `...' token. */
14760 cp_lexer_consume_token (parser->lexer);
14761 maybe_warn_variadic_templates ();
14763 *is_parameter_pack = true;
14766 /* If the next token is an identifier, then it names the
14767 parameter. */
14768 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14769 identifier = cp_parser_identifier (parser);
14770 else
14771 identifier = NULL_TREE;
14773 /* Create the parameter. */
14774 parameter = finish_template_type_parm (class_type_node, identifier);
14776 /* If the next token is an `=', we have a default argument. */
14777 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14779 default_argument
14780 = cp_parser_default_type_template_argument (parser);
14782 /* Template parameter packs cannot have default
14783 arguments. */
14784 if (*is_parameter_pack)
14786 if (identifier)
14787 error_at (token->location,
14788 "template parameter pack %qD cannot have a "
14789 "default argument", identifier);
14790 else
14791 error_at (token->location,
14792 "template parameter packs cannot have "
14793 "default arguments");
14794 default_argument = NULL_TREE;
14796 else if (check_for_bare_parameter_packs (default_argument))
14797 default_argument = error_mark_node;
14799 else
14800 default_argument = NULL_TREE;
14802 /* Create the combined representation of the parameter and the
14803 default argument. */
14804 parameter = build_tree_list (default_argument, parameter);
14806 break;
14808 case RID_TEMPLATE:
14810 tree identifier;
14811 tree default_argument;
14813 /* Look for the `<'. */
14814 cp_parser_require (parser, CPP_LESS, RT_LESS);
14815 /* Parse the template-parameter-list. */
14816 cp_parser_template_parameter_list (parser);
14817 /* Look for the `>'. */
14818 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14820 // If template requirements are present, parse them.
14821 if (flag_concepts)
14823 tree reqs = get_shorthand_constraints (current_template_parms);
14824 if (tree r = cp_parser_requires_clause_opt (parser))
14825 reqs = conjoin_constraints (reqs, normalize_expression (r));
14826 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
14829 /* Look for the `class' or 'typename' keywords. */
14830 cp_parser_type_parameter_key (parser);
14831 /* If the next token is an ellipsis, we have a template
14832 argument pack. */
14833 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14835 /* Consume the `...' token. */
14836 cp_lexer_consume_token (parser->lexer);
14837 maybe_warn_variadic_templates ();
14839 *is_parameter_pack = true;
14841 /* If the next token is an `=', then there is a
14842 default-argument. If the next token is a `>', we are at
14843 the end of the parameter-list. If the next token is a `,',
14844 then we are at the end of this parameter. */
14845 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
14846 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
14847 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14849 identifier = cp_parser_identifier (parser);
14850 /* Treat invalid names as if the parameter were nameless. */
14851 if (identifier == error_mark_node)
14852 identifier = NULL_TREE;
14854 else
14855 identifier = NULL_TREE;
14857 /* Create the template parameter. */
14858 parameter = finish_template_template_parm (class_type_node,
14859 identifier);
14861 /* If the next token is an `=', then there is a
14862 default-argument. */
14863 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14865 default_argument
14866 = cp_parser_default_template_template_argument (parser);
14868 /* Template parameter packs cannot have default
14869 arguments. */
14870 if (*is_parameter_pack)
14872 if (identifier)
14873 error_at (token->location,
14874 "template parameter pack %qD cannot "
14875 "have a default argument",
14876 identifier);
14877 else
14878 error_at (token->location, "template parameter packs cannot "
14879 "have default arguments");
14880 default_argument = NULL_TREE;
14883 else
14884 default_argument = NULL_TREE;
14886 /* Create the combined representation of the parameter and the
14887 default argument. */
14888 parameter = build_tree_list (default_argument, parameter);
14890 break;
14892 default:
14893 gcc_unreachable ();
14894 break;
14897 return parameter;
14900 /* Parse a template-id.
14902 template-id:
14903 template-name < template-argument-list [opt] >
14905 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
14906 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
14907 returned. Otherwise, if the template-name names a function, or set
14908 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
14909 names a class, returns a TYPE_DECL for the specialization.
14911 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
14912 uninstantiated templates. */
14914 static tree
14915 cp_parser_template_id (cp_parser *parser,
14916 bool template_keyword_p,
14917 bool check_dependency_p,
14918 enum tag_types tag_type,
14919 bool is_declaration)
14921 tree templ;
14922 tree arguments;
14923 tree template_id;
14924 cp_token_position start_of_id = 0;
14925 cp_token *next_token = NULL, *next_token_2 = NULL;
14926 bool is_identifier;
14928 /* If the next token corresponds to a template-id, there is no need
14929 to reparse it. */
14930 next_token = cp_lexer_peek_token (parser->lexer);
14931 if (next_token->type == CPP_TEMPLATE_ID)
14933 cp_lexer_consume_token (parser->lexer);
14934 return saved_checks_value (next_token->u.tree_check_value);
14937 /* Avoid performing name lookup if there is no possibility of
14938 finding a template-id. */
14939 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
14940 || (next_token->type == CPP_NAME
14941 && !cp_parser_nth_token_starts_template_argument_list_p
14942 (parser, 2)))
14944 cp_parser_error (parser, "expected template-id");
14945 return error_mark_node;
14948 /* Remember where the template-id starts. */
14949 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
14950 start_of_id = cp_lexer_token_position (parser->lexer, false);
14952 push_deferring_access_checks (dk_deferred);
14954 /* Parse the template-name. */
14955 is_identifier = false;
14956 templ = cp_parser_template_name (parser, template_keyword_p,
14957 check_dependency_p,
14958 is_declaration,
14959 tag_type,
14960 &is_identifier);
14961 if (templ == error_mark_node || is_identifier)
14963 pop_deferring_access_checks ();
14964 return templ;
14967 /* Since we're going to preserve any side-effects from this parse, set up a
14968 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14969 in the template arguments. */
14970 tentative_firewall firewall (parser);
14972 /* If we find the sequence `[:' after a template-name, it's probably
14973 a digraph-typo for `< ::'. Substitute the tokens and check if we can
14974 parse correctly the argument list. */
14975 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
14976 == CPP_OPEN_SQUARE)
14977 && next_token->flags & DIGRAPH
14978 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
14979 == CPP_COLON)
14980 && !(next_token_2->flags & PREV_WHITE))
14982 cp_parser_parse_tentatively (parser);
14983 /* Change `:' into `::'. */
14984 next_token_2->type = CPP_SCOPE;
14985 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
14986 CPP_LESS. */
14987 cp_lexer_consume_token (parser->lexer);
14989 /* Parse the arguments. */
14990 arguments = cp_parser_enclosed_template_argument_list (parser);
14991 if (!cp_parser_parse_definitely (parser))
14993 /* If we couldn't parse an argument list, then we revert our changes
14994 and return simply an error. Maybe this is not a template-id
14995 after all. */
14996 next_token_2->type = CPP_COLON;
14997 cp_parser_error (parser, "expected %<<%>");
14998 pop_deferring_access_checks ();
14999 return error_mark_node;
15001 /* Otherwise, emit an error about the invalid digraph, but continue
15002 parsing because we got our argument list. */
15003 if (permerror (next_token->location,
15004 "%<<::%> cannot begin a template-argument list"))
15006 static bool hint = false;
15007 inform (next_token->location,
15008 "%<<:%> is an alternate spelling for %<[%>."
15009 " Insert whitespace between %<<%> and %<::%>");
15010 if (!hint && !flag_permissive)
15012 inform (next_token->location, "(if you use %<-fpermissive%> "
15013 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15014 "accept your code)");
15015 hint = true;
15019 else
15021 /* Look for the `<' that starts the template-argument-list. */
15022 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15024 pop_deferring_access_checks ();
15025 return error_mark_node;
15027 /* Parse the arguments. */
15028 arguments = cp_parser_enclosed_template_argument_list (parser);
15031 /* Build a representation of the specialization. */
15032 if (identifier_p (templ))
15033 template_id = build_min_nt_loc (next_token->location,
15034 TEMPLATE_ID_EXPR,
15035 templ, arguments);
15036 else if (DECL_TYPE_TEMPLATE_P (templ)
15037 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15039 bool entering_scope;
15040 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15041 template (rather than some instantiation thereof) only if
15042 is not nested within some other construct. For example, in
15043 "template <typename T> void f(T) { A<T>::", A<T> is just an
15044 instantiation of A. */
15045 entering_scope = (template_parm_scope_p ()
15046 && cp_lexer_next_token_is (parser->lexer,
15047 CPP_SCOPE));
15048 template_id
15049 = finish_template_type (templ, arguments, entering_scope);
15051 /* A template-like identifier may be a partial concept id. */
15052 else if (flag_concepts
15053 && (template_id = (cp_parser_maybe_partial_concept_id
15054 (parser, templ, arguments))))
15055 return template_id;
15056 else if (variable_template_p (templ))
15058 template_id = lookup_template_variable (templ, arguments);
15059 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15060 SET_EXPR_LOCATION (template_id, next_token->location);
15062 else
15064 /* If it's not a class-template or a template-template, it should be
15065 a function-template. */
15066 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15067 || TREE_CODE (templ) == OVERLOAD
15068 || BASELINK_P (templ)));
15070 template_id = lookup_template_function (templ, arguments);
15071 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15072 SET_EXPR_LOCATION (template_id, next_token->location);
15075 /* If parsing tentatively, replace the sequence of tokens that makes
15076 up the template-id with a CPP_TEMPLATE_ID token. That way,
15077 should we re-parse the token stream, we will not have to repeat
15078 the effort required to do the parse, nor will we issue duplicate
15079 error messages about problems during instantiation of the
15080 template. */
15081 if (start_of_id
15082 /* Don't do this if we had a parse error in a declarator; re-parsing
15083 might succeed if a name changes meaning (60361). */
15084 && !(cp_parser_error_occurred (parser)
15085 && cp_parser_parsing_tentatively (parser)
15086 && parser->in_declarator_p))
15088 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
15090 /* Reset the contents of the START_OF_ID token. */
15091 token->type = CPP_TEMPLATE_ID;
15093 /* Update the location to be of the form:
15094 template-name < template-argument-list [opt] >
15095 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15096 with caret == start at the start of the template-name,
15097 ranging until the closing '>'. */
15098 location_t finish_loc
15099 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15100 location_t combined_loc
15101 = make_location (token->location, token->location, finish_loc);
15102 token->location = combined_loc;
15104 /* Retrieve any deferred checks. Do not pop this access checks yet
15105 so the memory will not be reclaimed during token replacing below. */
15106 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15107 token->u.tree_check_value->value = template_id;
15108 token->u.tree_check_value->checks = get_deferred_access_checks ();
15109 token->keyword = RID_MAX;
15111 /* Purge all subsequent tokens. */
15112 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15114 /* ??? Can we actually assume that, if template_id ==
15115 error_mark_node, we will have issued a diagnostic to the
15116 user, as opposed to simply marking the tentative parse as
15117 failed? */
15118 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15119 error_at (token->location, "parse error in template argument list");
15122 pop_to_parent_deferring_access_checks ();
15123 return template_id;
15126 /* Parse a template-name.
15128 template-name:
15129 identifier
15131 The standard should actually say:
15133 template-name:
15134 identifier
15135 operator-function-id
15137 A defect report has been filed about this issue.
15139 A conversion-function-id cannot be a template name because they cannot
15140 be part of a template-id. In fact, looking at this code:
15142 a.operator K<int>()
15144 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15145 It is impossible to call a templated conversion-function-id with an
15146 explicit argument list, since the only allowed template parameter is
15147 the type to which it is converting.
15149 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15150 `template' keyword, in a construction like:
15152 T::template f<3>()
15154 In that case `f' is taken to be a template-name, even though there
15155 is no way of knowing for sure.
15157 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15158 name refers to a set of overloaded functions, at least one of which
15159 is a template, or an IDENTIFIER_NODE with the name of the template,
15160 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15161 names are looked up inside uninstantiated templates. */
15163 static tree
15164 cp_parser_template_name (cp_parser* parser,
15165 bool template_keyword_p,
15166 bool check_dependency_p,
15167 bool is_declaration,
15168 enum tag_types tag_type,
15169 bool *is_identifier)
15171 tree identifier;
15172 tree decl;
15173 tree fns;
15174 cp_token *token = cp_lexer_peek_token (parser->lexer);
15176 /* If the next token is `operator', then we have either an
15177 operator-function-id or a conversion-function-id. */
15178 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15180 /* We don't know whether we're looking at an
15181 operator-function-id or a conversion-function-id. */
15182 cp_parser_parse_tentatively (parser);
15183 /* Try an operator-function-id. */
15184 identifier = cp_parser_operator_function_id (parser);
15185 /* If that didn't work, try a conversion-function-id. */
15186 if (!cp_parser_parse_definitely (parser))
15188 cp_parser_error (parser, "expected template-name");
15189 return error_mark_node;
15192 /* Look for the identifier. */
15193 else
15194 identifier = cp_parser_identifier (parser);
15196 /* If we didn't find an identifier, we don't have a template-id. */
15197 if (identifier == error_mark_node)
15198 return error_mark_node;
15200 /* If the name immediately followed the `template' keyword, then it
15201 is a template-name. However, if the next token is not `<', then
15202 we do not treat it as a template-name, since it is not being used
15203 as part of a template-id. This enables us to handle constructs
15204 like:
15206 template <typename T> struct S { S(); };
15207 template <typename T> S<T>::S();
15209 correctly. We would treat `S' as a template -- if it were `S<T>'
15210 -- but we do not if there is no `<'. */
15212 if (processing_template_decl
15213 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15215 /* In a declaration, in a dependent context, we pretend that the
15216 "template" keyword was present in order to improve error
15217 recovery. For example, given:
15219 template <typename T> void f(T::X<int>);
15221 we want to treat "X<int>" as a template-id. */
15222 if (is_declaration
15223 && !template_keyword_p
15224 && parser->scope && TYPE_P (parser->scope)
15225 && check_dependency_p
15226 && dependent_scope_p (parser->scope)
15227 /* Do not do this for dtors (or ctors), since they never
15228 need the template keyword before their name. */
15229 && !constructor_name_p (identifier, parser->scope))
15231 cp_token_position start = 0;
15233 /* Explain what went wrong. */
15234 error_at (token->location, "non-template %qD used as template",
15235 identifier);
15236 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15237 parser->scope, identifier);
15238 /* If parsing tentatively, find the location of the "<" token. */
15239 if (cp_parser_simulate_error (parser))
15240 start = cp_lexer_token_position (parser->lexer, true);
15241 /* Parse the template arguments so that we can issue error
15242 messages about them. */
15243 cp_lexer_consume_token (parser->lexer);
15244 cp_parser_enclosed_template_argument_list (parser);
15245 /* Skip tokens until we find a good place from which to
15246 continue parsing. */
15247 cp_parser_skip_to_closing_parenthesis (parser,
15248 /*recovering=*/true,
15249 /*or_comma=*/true,
15250 /*consume_paren=*/false);
15251 /* If parsing tentatively, permanently remove the
15252 template argument list. That will prevent duplicate
15253 error messages from being issued about the missing
15254 "template" keyword. */
15255 if (start)
15256 cp_lexer_purge_tokens_after (parser->lexer, start);
15257 if (is_identifier)
15258 *is_identifier = true;
15259 return identifier;
15262 /* If the "template" keyword is present, then there is generally
15263 no point in doing name-lookup, so we just return IDENTIFIER.
15264 But, if the qualifying scope is non-dependent then we can
15265 (and must) do name-lookup normally. */
15266 if (template_keyword_p
15267 && (!parser->scope
15268 || (TYPE_P (parser->scope)
15269 && dependent_type_p (parser->scope))))
15270 return identifier;
15273 /* Look up the name. */
15274 decl = cp_parser_lookup_name (parser, identifier,
15275 tag_type,
15276 /*is_template=*/true,
15277 /*is_namespace=*/false,
15278 check_dependency_p,
15279 /*ambiguous_decls=*/NULL,
15280 token->location);
15282 decl = strip_using_decl (decl);
15284 /* If DECL is a template, then the name was a template-name. */
15285 if (TREE_CODE (decl) == TEMPLATE_DECL)
15287 if (TREE_DEPRECATED (decl)
15288 && deprecated_state != DEPRECATED_SUPPRESS)
15289 warn_deprecated_use (decl, NULL_TREE);
15291 else
15293 tree fn = NULL_TREE;
15295 /* The standard does not explicitly indicate whether a name that
15296 names a set of overloaded declarations, some of which are
15297 templates, is a template-name. However, such a name should
15298 be a template-name; otherwise, there is no way to form a
15299 template-id for the overloaded templates. */
15300 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
15301 if (TREE_CODE (fns) == OVERLOAD)
15302 for (fn = fns; fn; fn = OVL_NEXT (fn))
15303 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
15304 break;
15306 if (!fn)
15308 /* The name does not name a template. */
15309 cp_parser_error (parser, "expected template-name");
15310 return error_mark_node;
15314 /* If DECL is dependent, and refers to a function, then just return
15315 its name; we will look it up again during template instantiation. */
15316 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15318 tree scope = ovl_scope (decl);
15319 if (TYPE_P (scope) && dependent_type_p (scope))
15320 return identifier;
15323 return decl;
15326 /* Parse a template-argument-list.
15328 template-argument-list:
15329 template-argument ... [opt]
15330 template-argument-list , template-argument ... [opt]
15332 Returns a TREE_VEC containing the arguments. */
15334 static tree
15335 cp_parser_template_argument_list (cp_parser* parser)
15337 tree fixed_args[10];
15338 unsigned n_args = 0;
15339 unsigned alloced = 10;
15340 tree *arg_ary = fixed_args;
15341 tree vec;
15342 bool saved_in_template_argument_list_p;
15343 bool saved_ice_p;
15344 bool saved_non_ice_p;
15346 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15347 parser->in_template_argument_list_p = true;
15348 /* Even if the template-id appears in an integral
15349 constant-expression, the contents of the argument list do
15350 not. */
15351 saved_ice_p = parser->integral_constant_expression_p;
15352 parser->integral_constant_expression_p = false;
15353 saved_non_ice_p = parser->non_integral_constant_expression_p;
15354 parser->non_integral_constant_expression_p = false;
15356 /* Parse the arguments. */
15359 tree argument;
15361 if (n_args)
15362 /* Consume the comma. */
15363 cp_lexer_consume_token (parser->lexer);
15365 /* Parse the template-argument. */
15366 argument = cp_parser_template_argument (parser);
15368 /* If the next token is an ellipsis, we're expanding a template
15369 argument pack. */
15370 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15372 if (argument == error_mark_node)
15374 cp_token *token = cp_lexer_peek_token (parser->lexer);
15375 error_at (token->location,
15376 "expected parameter pack before %<...%>");
15378 /* Consume the `...' token. */
15379 cp_lexer_consume_token (parser->lexer);
15381 /* Make the argument into a TYPE_PACK_EXPANSION or
15382 EXPR_PACK_EXPANSION. */
15383 argument = make_pack_expansion (argument);
15386 if (n_args == alloced)
15388 alloced *= 2;
15390 if (arg_ary == fixed_args)
15392 arg_ary = XNEWVEC (tree, alloced);
15393 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
15395 else
15396 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
15398 arg_ary[n_args++] = argument;
15400 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15402 vec = make_tree_vec (n_args);
15404 while (n_args--)
15405 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
15407 if (arg_ary != fixed_args)
15408 free (arg_ary);
15409 parser->non_integral_constant_expression_p = saved_non_ice_p;
15410 parser->integral_constant_expression_p = saved_ice_p;
15411 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
15412 if (CHECKING_P)
15413 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
15414 return vec;
15417 /* Parse a template-argument.
15419 template-argument:
15420 assignment-expression
15421 type-id
15422 id-expression
15424 The representation is that of an assignment-expression, type-id, or
15425 id-expression -- except that the qualified id-expression is
15426 evaluated, so that the value returned is either a DECL or an
15427 OVERLOAD.
15429 Although the standard says "assignment-expression", it forbids
15430 throw-expressions or assignments in the template argument.
15431 Therefore, we use "conditional-expression" instead. */
15433 static tree
15434 cp_parser_template_argument (cp_parser* parser)
15436 tree argument;
15437 bool template_p;
15438 bool address_p;
15439 bool maybe_type_id = false;
15440 cp_token *token = NULL, *argument_start_token = NULL;
15441 location_t loc = 0;
15442 cp_id_kind idk;
15444 /* There's really no way to know what we're looking at, so we just
15445 try each alternative in order.
15447 [temp.arg]
15449 In a template-argument, an ambiguity between a type-id and an
15450 expression is resolved to a type-id, regardless of the form of
15451 the corresponding template-parameter.
15453 Therefore, we try a type-id first. */
15454 cp_parser_parse_tentatively (parser);
15455 argument = cp_parser_template_type_arg (parser);
15456 /* If there was no error parsing the type-id but the next token is a
15457 '>>', our behavior depends on which dialect of C++ we're
15458 parsing. In C++98, we probably found a typo for '> >'. But there
15459 are type-id which are also valid expressions. For instance:
15461 struct X { int operator >> (int); };
15462 template <int V> struct Foo {};
15463 Foo<X () >> 5> r;
15465 Here 'X()' is a valid type-id of a function type, but the user just
15466 wanted to write the expression "X() >> 5". Thus, we remember that we
15467 found a valid type-id, but we still try to parse the argument as an
15468 expression to see what happens.
15470 In C++0x, the '>>' will be considered two separate '>'
15471 tokens. */
15472 if (!cp_parser_error_occurred (parser)
15473 && cxx_dialect == cxx98
15474 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15476 maybe_type_id = true;
15477 cp_parser_abort_tentative_parse (parser);
15479 else
15481 /* If the next token isn't a `,' or a `>', then this argument wasn't
15482 really finished. This means that the argument is not a valid
15483 type-id. */
15484 if (!cp_parser_next_token_ends_template_argument_p (parser))
15485 cp_parser_error (parser, "expected template-argument");
15486 /* If that worked, we're done. */
15487 if (cp_parser_parse_definitely (parser))
15488 return argument;
15490 /* We're still not sure what the argument will be. */
15491 cp_parser_parse_tentatively (parser);
15492 /* Try a template. */
15493 argument_start_token = cp_lexer_peek_token (parser->lexer);
15494 argument = cp_parser_id_expression (parser,
15495 /*template_keyword_p=*/false,
15496 /*check_dependency_p=*/true,
15497 &template_p,
15498 /*declarator_p=*/false,
15499 /*optional_p=*/false);
15500 /* If the next token isn't a `,' or a `>', then this argument wasn't
15501 really finished. */
15502 if (!cp_parser_next_token_ends_template_argument_p (parser))
15503 cp_parser_error (parser, "expected template-argument");
15504 if (!cp_parser_error_occurred (parser))
15506 /* Figure out what is being referred to. If the id-expression
15507 was for a class template specialization, then we will have a
15508 TYPE_DECL at this point. There is no need to do name lookup
15509 at this point in that case. */
15510 if (TREE_CODE (argument) != TYPE_DECL)
15511 argument = cp_parser_lookup_name (parser, argument,
15512 none_type,
15513 /*is_template=*/template_p,
15514 /*is_namespace=*/false,
15515 /*check_dependency=*/true,
15516 /*ambiguous_decls=*/NULL,
15517 argument_start_token->location);
15518 /* Handle a constrained-type-specifier for a non-type template
15519 parameter. */
15520 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
15521 argument = decl;
15522 else if (TREE_CODE (argument) != TEMPLATE_DECL
15523 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
15524 cp_parser_error (parser, "expected template-name");
15526 if (cp_parser_parse_definitely (parser))
15528 if (TREE_DEPRECATED (argument))
15529 warn_deprecated_use (argument, NULL_TREE);
15530 return argument;
15532 /* It must be a non-type argument. In C++17 any constant-expression is
15533 allowed. */
15534 if (cxx_dialect > cxx14)
15535 goto general_expr;
15537 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
15539 -- an integral constant-expression of integral or enumeration
15540 type; or
15542 -- the name of a non-type template-parameter; or
15544 -- the name of an object or function with external linkage...
15546 -- the address of an object or function with external linkage...
15548 -- a pointer to member... */
15549 /* Look for a non-type template parameter. */
15550 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15552 cp_parser_parse_tentatively (parser);
15553 argument = cp_parser_primary_expression (parser,
15554 /*address_p=*/false,
15555 /*cast_p=*/false,
15556 /*template_arg_p=*/true,
15557 &idk);
15558 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
15559 || !cp_parser_next_token_ends_template_argument_p (parser))
15560 cp_parser_simulate_error (parser);
15561 if (cp_parser_parse_definitely (parser))
15562 return argument;
15565 /* If the next token is "&", the argument must be the address of an
15566 object or function with external linkage. */
15567 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
15568 if (address_p)
15570 loc = cp_lexer_peek_token (parser->lexer)->location;
15571 cp_lexer_consume_token (parser->lexer);
15573 /* See if we might have an id-expression. */
15574 token = cp_lexer_peek_token (parser->lexer);
15575 if (token->type == CPP_NAME
15576 || token->keyword == RID_OPERATOR
15577 || token->type == CPP_SCOPE
15578 || token->type == CPP_TEMPLATE_ID
15579 || token->type == CPP_NESTED_NAME_SPECIFIER)
15581 cp_parser_parse_tentatively (parser);
15582 argument = cp_parser_primary_expression (parser,
15583 address_p,
15584 /*cast_p=*/false,
15585 /*template_arg_p=*/true,
15586 &idk);
15587 if (cp_parser_error_occurred (parser)
15588 || !cp_parser_next_token_ends_template_argument_p (parser))
15589 cp_parser_abort_tentative_parse (parser);
15590 else
15592 tree probe;
15594 if (INDIRECT_REF_P (argument))
15596 /* Strip the dereference temporarily. */
15597 gcc_assert (REFERENCE_REF_P (argument));
15598 argument = TREE_OPERAND (argument, 0);
15601 /* If we're in a template, we represent a qualified-id referring
15602 to a static data member as a SCOPE_REF even if the scope isn't
15603 dependent so that we can check access control later. */
15604 probe = argument;
15605 if (TREE_CODE (probe) == SCOPE_REF)
15606 probe = TREE_OPERAND (probe, 1);
15607 if (VAR_P (probe))
15609 /* A variable without external linkage might still be a
15610 valid constant-expression, so no error is issued here
15611 if the external-linkage check fails. */
15612 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
15613 cp_parser_simulate_error (parser);
15615 else if (is_overloaded_fn (argument))
15616 /* All overloaded functions are allowed; if the external
15617 linkage test does not pass, an error will be issued
15618 later. */
15620 else if (address_p
15621 && (TREE_CODE (argument) == OFFSET_REF
15622 || TREE_CODE (argument) == SCOPE_REF))
15623 /* A pointer-to-member. */
15625 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
15627 else
15628 cp_parser_simulate_error (parser);
15630 if (cp_parser_parse_definitely (parser))
15632 if (address_p)
15633 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
15634 tf_warning_or_error);
15635 else
15636 argument = convert_from_reference (argument);
15637 return argument;
15641 /* If the argument started with "&", there are no other valid
15642 alternatives at this point. */
15643 if (address_p)
15645 cp_parser_error (parser, "invalid non-type template argument");
15646 return error_mark_node;
15649 general_expr:
15650 /* If the argument wasn't successfully parsed as a type-id followed
15651 by '>>', the argument can only be a constant expression now.
15652 Otherwise, we try parsing the constant-expression tentatively,
15653 because the argument could really be a type-id. */
15654 if (maybe_type_id)
15655 cp_parser_parse_tentatively (parser);
15657 if (cxx_dialect <= cxx14)
15658 argument = cp_parser_constant_expression (parser);
15659 else
15661 /* With C++17 generalized non-type template arguments we need to handle
15662 lvalue constant expressions, too. */
15663 argument = cp_parser_assignment_expression (parser);
15664 require_potential_constant_expression (argument);
15667 if (!maybe_type_id)
15668 return argument;
15669 if (!cp_parser_next_token_ends_template_argument_p (parser))
15670 cp_parser_error (parser, "expected template-argument");
15671 if (cp_parser_parse_definitely (parser))
15672 return argument;
15673 /* We did our best to parse the argument as a non type-id, but that
15674 was the only alternative that matched (albeit with a '>' after
15675 it). We can assume it's just a typo from the user, and a
15676 diagnostic will then be issued. */
15677 return cp_parser_template_type_arg (parser);
15680 /* Parse an explicit-instantiation.
15682 explicit-instantiation:
15683 template declaration
15685 Although the standard says `declaration', what it really means is:
15687 explicit-instantiation:
15688 template decl-specifier-seq [opt] declarator [opt] ;
15690 Things like `template int S<int>::i = 5, int S<double>::j;' are not
15691 supposed to be allowed. A defect report has been filed about this
15692 issue.
15694 GNU Extension:
15696 explicit-instantiation:
15697 storage-class-specifier template
15698 decl-specifier-seq [opt] declarator [opt] ;
15699 function-specifier template
15700 decl-specifier-seq [opt] declarator [opt] ; */
15702 static void
15703 cp_parser_explicit_instantiation (cp_parser* parser)
15705 int declares_class_or_enum;
15706 cp_decl_specifier_seq decl_specifiers;
15707 tree extension_specifier = NULL_TREE;
15709 timevar_push (TV_TEMPLATE_INST);
15711 /* Look for an (optional) storage-class-specifier or
15712 function-specifier. */
15713 if (cp_parser_allow_gnu_extensions_p (parser))
15715 extension_specifier
15716 = cp_parser_storage_class_specifier_opt (parser);
15717 if (!extension_specifier)
15718 extension_specifier
15719 = cp_parser_function_specifier_opt (parser,
15720 /*decl_specs=*/NULL);
15723 /* Look for the `template' keyword. */
15724 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15725 /* Let the front end know that we are processing an explicit
15726 instantiation. */
15727 begin_explicit_instantiation ();
15728 /* [temp.explicit] says that we are supposed to ignore access
15729 control while processing explicit instantiation directives. */
15730 push_deferring_access_checks (dk_no_check);
15731 /* Parse a decl-specifier-seq. */
15732 cp_parser_decl_specifier_seq (parser,
15733 CP_PARSER_FLAGS_OPTIONAL,
15734 &decl_specifiers,
15735 &declares_class_or_enum);
15736 /* If there was exactly one decl-specifier, and it declared a class,
15737 and there's no declarator, then we have an explicit type
15738 instantiation. */
15739 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
15741 tree type;
15743 type = check_tag_decl (&decl_specifiers,
15744 /*explicit_type_instantiation_p=*/true);
15745 /* Turn access control back on for names used during
15746 template instantiation. */
15747 pop_deferring_access_checks ();
15748 if (type)
15749 do_type_instantiation (type, extension_specifier,
15750 /*complain=*/tf_error);
15752 else
15754 cp_declarator *declarator;
15755 tree decl;
15757 /* Parse the declarator. */
15758 declarator
15759 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15760 /*ctor_dtor_or_conv_p=*/NULL,
15761 /*parenthesized_p=*/NULL,
15762 /*member_p=*/false,
15763 /*friend_p=*/false);
15764 if (declares_class_or_enum & 2)
15765 cp_parser_check_for_definition_in_return_type (declarator,
15766 decl_specifiers.type,
15767 decl_specifiers.locations[ds_type_spec]);
15768 if (declarator != cp_error_declarator)
15770 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
15771 permerror (decl_specifiers.locations[ds_inline],
15772 "explicit instantiation shall not use"
15773 " %<inline%> specifier");
15774 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
15775 permerror (decl_specifiers.locations[ds_constexpr],
15776 "explicit instantiation shall not use"
15777 " %<constexpr%> specifier");
15779 decl = grokdeclarator (declarator, &decl_specifiers,
15780 NORMAL, 0, &decl_specifiers.attributes);
15781 /* Turn access control back on for names used during
15782 template instantiation. */
15783 pop_deferring_access_checks ();
15784 /* Do the explicit instantiation. */
15785 do_decl_instantiation (decl, extension_specifier);
15787 else
15789 pop_deferring_access_checks ();
15790 /* Skip the body of the explicit instantiation. */
15791 cp_parser_skip_to_end_of_statement (parser);
15794 /* We're done with the instantiation. */
15795 end_explicit_instantiation ();
15797 cp_parser_consume_semicolon_at_end_of_statement (parser);
15799 timevar_pop (TV_TEMPLATE_INST);
15802 /* Parse an explicit-specialization.
15804 explicit-specialization:
15805 template < > declaration
15807 Although the standard says `declaration', what it really means is:
15809 explicit-specialization:
15810 template <> decl-specifier [opt] init-declarator [opt] ;
15811 template <> function-definition
15812 template <> explicit-specialization
15813 template <> template-declaration */
15815 static void
15816 cp_parser_explicit_specialization (cp_parser* parser)
15818 bool need_lang_pop;
15819 cp_token *token = cp_lexer_peek_token (parser->lexer);
15821 /* Look for the `template' keyword. */
15822 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15823 /* Look for the `<'. */
15824 cp_parser_require (parser, CPP_LESS, RT_LESS);
15825 /* Look for the `>'. */
15826 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15827 /* We have processed another parameter list. */
15828 ++parser->num_template_parameter_lists;
15829 /* [temp]
15831 A template ... explicit specialization ... shall not have C
15832 linkage. */
15833 if (current_lang_name == lang_name_c)
15835 error_at (token->location, "template specialization with C linkage");
15836 /* Give it C++ linkage to avoid confusing other parts of the
15837 front end. */
15838 push_lang_context (lang_name_cplusplus);
15839 need_lang_pop = true;
15841 else
15842 need_lang_pop = false;
15843 /* Let the front end know that we are beginning a specialization. */
15844 if (!begin_specialization ())
15846 end_specialization ();
15847 return;
15850 /* If the next keyword is `template', we need to figure out whether
15851 or not we're looking a template-declaration. */
15852 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15854 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15855 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
15856 cp_parser_template_declaration_after_export (parser,
15857 /*member_p=*/false);
15858 else
15859 cp_parser_explicit_specialization (parser);
15861 else
15862 /* Parse the dependent declaration. */
15863 cp_parser_single_declaration (parser,
15864 /*checks=*/NULL,
15865 /*member_p=*/false,
15866 /*explicit_specialization_p=*/true,
15867 /*friend_p=*/NULL);
15868 /* We're done with the specialization. */
15869 end_specialization ();
15870 /* For the erroneous case of a template with C linkage, we pushed an
15871 implicit C++ linkage scope; exit that scope now. */
15872 if (need_lang_pop)
15873 pop_lang_context ();
15874 /* We're done with this parameter list. */
15875 --parser->num_template_parameter_lists;
15878 /* Parse a type-specifier.
15880 type-specifier:
15881 simple-type-specifier
15882 class-specifier
15883 enum-specifier
15884 elaborated-type-specifier
15885 cv-qualifier
15887 GNU Extension:
15889 type-specifier:
15890 __complex__
15892 Returns a representation of the type-specifier. For a
15893 class-specifier, enum-specifier, or elaborated-type-specifier, a
15894 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
15896 The parser flags FLAGS is used to control type-specifier parsing.
15898 If IS_DECLARATION is TRUE, then this type-specifier is appearing
15899 in a decl-specifier-seq.
15901 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
15902 class-specifier, enum-specifier, or elaborated-type-specifier, then
15903 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
15904 if a type is declared; 2 if it is defined. Otherwise, it is set to
15905 zero.
15907 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
15908 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
15909 is set to FALSE. */
15911 static tree
15912 cp_parser_type_specifier (cp_parser* parser,
15913 cp_parser_flags flags,
15914 cp_decl_specifier_seq *decl_specs,
15915 bool is_declaration,
15916 int* declares_class_or_enum,
15917 bool* is_cv_qualifier)
15919 tree type_spec = NULL_TREE;
15920 cp_token *token;
15921 enum rid keyword;
15922 cp_decl_spec ds = ds_last;
15924 /* Assume this type-specifier does not declare a new type. */
15925 if (declares_class_or_enum)
15926 *declares_class_or_enum = 0;
15927 /* And that it does not specify a cv-qualifier. */
15928 if (is_cv_qualifier)
15929 *is_cv_qualifier = false;
15930 /* Peek at the next token. */
15931 token = cp_lexer_peek_token (parser->lexer);
15933 /* If we're looking at a keyword, we can use that to guide the
15934 production we choose. */
15935 keyword = token->keyword;
15936 switch (keyword)
15938 case RID_ENUM:
15939 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15940 goto elaborated_type_specifier;
15942 /* Look for the enum-specifier. */
15943 type_spec = cp_parser_enum_specifier (parser);
15944 /* If that worked, we're done. */
15945 if (type_spec)
15947 if (declares_class_or_enum)
15948 *declares_class_or_enum = 2;
15949 if (decl_specs)
15950 cp_parser_set_decl_spec_type (decl_specs,
15951 type_spec,
15952 token,
15953 /*type_definition_p=*/true);
15954 return type_spec;
15956 else
15957 goto elaborated_type_specifier;
15959 /* Any of these indicate either a class-specifier, or an
15960 elaborated-type-specifier. */
15961 case RID_CLASS:
15962 case RID_STRUCT:
15963 case RID_UNION:
15964 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15965 goto elaborated_type_specifier;
15967 /* Parse tentatively so that we can back up if we don't find a
15968 class-specifier. */
15969 cp_parser_parse_tentatively (parser);
15970 /* Look for the class-specifier. */
15971 type_spec = cp_parser_class_specifier (parser);
15972 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
15973 /* If that worked, we're done. */
15974 if (cp_parser_parse_definitely (parser))
15976 if (declares_class_or_enum)
15977 *declares_class_or_enum = 2;
15978 if (decl_specs)
15979 cp_parser_set_decl_spec_type (decl_specs,
15980 type_spec,
15981 token,
15982 /*type_definition_p=*/true);
15983 return type_spec;
15986 /* Fall through. */
15987 elaborated_type_specifier:
15988 /* We're declaring (not defining) a class or enum. */
15989 if (declares_class_or_enum)
15990 *declares_class_or_enum = 1;
15992 /* Fall through. */
15993 case RID_TYPENAME:
15994 /* Look for an elaborated-type-specifier. */
15995 type_spec
15996 = (cp_parser_elaborated_type_specifier
15997 (parser,
15998 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
15999 is_declaration));
16000 if (decl_specs)
16001 cp_parser_set_decl_spec_type (decl_specs,
16002 type_spec,
16003 token,
16004 /*type_definition_p=*/false);
16005 return type_spec;
16007 case RID_CONST:
16008 ds = ds_const;
16009 if (is_cv_qualifier)
16010 *is_cv_qualifier = true;
16011 break;
16013 case RID_VOLATILE:
16014 ds = ds_volatile;
16015 if (is_cv_qualifier)
16016 *is_cv_qualifier = true;
16017 break;
16019 case RID_RESTRICT:
16020 ds = ds_restrict;
16021 if (is_cv_qualifier)
16022 *is_cv_qualifier = true;
16023 break;
16025 case RID_COMPLEX:
16026 /* The `__complex__' keyword is a GNU extension. */
16027 ds = ds_complex;
16028 break;
16030 default:
16031 break;
16034 /* Handle simple keywords. */
16035 if (ds != ds_last)
16037 if (decl_specs)
16039 set_and_check_decl_spec_loc (decl_specs, ds, token);
16040 decl_specs->any_specifiers_p = true;
16042 return cp_lexer_consume_token (parser->lexer)->u.value;
16045 /* If we do not already have a type-specifier, assume we are looking
16046 at a simple-type-specifier. */
16047 type_spec = cp_parser_simple_type_specifier (parser,
16048 decl_specs,
16049 flags);
16051 /* If we didn't find a type-specifier, and a type-specifier was not
16052 optional in this context, issue an error message. */
16053 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16055 cp_parser_error (parser, "expected type specifier");
16056 return error_mark_node;
16059 return type_spec;
16062 /* Parse a simple-type-specifier.
16064 simple-type-specifier:
16065 :: [opt] nested-name-specifier [opt] type-name
16066 :: [opt] nested-name-specifier template template-id
16067 char
16068 wchar_t
16069 bool
16070 short
16072 long
16073 signed
16074 unsigned
16075 float
16076 double
16077 void
16079 C++0x Extension:
16081 simple-type-specifier:
16082 auto
16083 decltype ( expression )
16084 char16_t
16085 char32_t
16086 __underlying_type ( type-id )
16088 GNU Extension:
16090 simple-type-specifier:
16091 __int128
16092 __typeof__ unary-expression
16093 __typeof__ ( type-id )
16094 __typeof__ ( type-id ) { initializer-list , [opt] }
16096 Concepts Extension:
16098 simple-type-specifier:
16099 constrained-type-specifier
16101 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16102 appropriately updated. */
16104 static tree
16105 cp_parser_simple_type_specifier (cp_parser* parser,
16106 cp_decl_specifier_seq *decl_specs,
16107 cp_parser_flags flags)
16109 tree type = NULL_TREE;
16110 cp_token *token;
16111 int idx;
16113 /* Peek at the next token. */
16114 token = cp_lexer_peek_token (parser->lexer);
16116 /* If we're looking at a keyword, things are easy. */
16117 switch (token->keyword)
16119 case RID_CHAR:
16120 if (decl_specs)
16121 decl_specs->explicit_char_p = true;
16122 type = char_type_node;
16123 break;
16124 case RID_CHAR16:
16125 type = char16_type_node;
16126 break;
16127 case RID_CHAR32:
16128 type = char32_type_node;
16129 break;
16130 case RID_WCHAR:
16131 type = wchar_type_node;
16132 break;
16133 case RID_BOOL:
16134 type = boolean_type_node;
16135 break;
16136 case RID_SHORT:
16137 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16138 type = short_integer_type_node;
16139 break;
16140 case RID_INT:
16141 if (decl_specs)
16142 decl_specs->explicit_int_p = true;
16143 type = integer_type_node;
16144 break;
16145 case RID_INT_N_0:
16146 case RID_INT_N_1:
16147 case RID_INT_N_2:
16148 case RID_INT_N_3:
16149 idx = token->keyword - RID_INT_N_0;
16150 if (! int_n_enabled_p [idx])
16151 break;
16152 if (decl_specs)
16154 decl_specs->explicit_intN_p = true;
16155 decl_specs->int_n_idx = idx;
16157 type = int_n_trees [idx].signed_type;
16158 break;
16159 case RID_LONG:
16160 if (decl_specs)
16161 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16162 type = long_integer_type_node;
16163 break;
16164 case RID_SIGNED:
16165 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16166 type = integer_type_node;
16167 break;
16168 case RID_UNSIGNED:
16169 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16170 type = unsigned_type_node;
16171 break;
16172 case RID_FLOAT:
16173 type = float_type_node;
16174 break;
16175 case RID_DOUBLE:
16176 type = double_type_node;
16177 break;
16178 case RID_VOID:
16179 type = void_type_node;
16180 break;
16182 case RID_AUTO:
16183 maybe_warn_cpp0x (CPP0X_AUTO);
16184 if (parser->auto_is_implicit_function_template_parm_p)
16186 /* The 'auto' might be the placeholder return type for a function decl
16187 with trailing return type. */
16188 bool have_trailing_return_fn_decl = false;
16190 cp_parser_parse_tentatively (parser);
16191 cp_lexer_consume_token (parser->lexer);
16192 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16193 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16194 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16195 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16197 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16199 cp_lexer_consume_token (parser->lexer);
16200 cp_parser_skip_to_closing_parenthesis (parser,
16201 /*recovering*/false,
16202 /*or_comma*/false,
16203 /*consume_paren*/true);
16204 continue;
16207 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16209 have_trailing_return_fn_decl = true;
16210 break;
16213 cp_lexer_consume_token (parser->lexer);
16215 cp_parser_abort_tentative_parse (parser);
16217 if (have_trailing_return_fn_decl)
16219 type = make_auto ();
16220 break;
16223 if (cxx_dialect >= cxx14)
16225 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16226 type = TREE_TYPE (type);
16228 else
16229 type = error_mark_node;
16231 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16233 if (cxx_dialect < cxx14)
16234 error_at (token->location,
16235 "use of %<auto%> in lambda parameter declaration "
16236 "only available with "
16237 "-std=c++14 or -std=gnu++14");
16239 else if (cxx_dialect < cxx14)
16240 error_at (token->location,
16241 "use of %<auto%> in parameter declaration "
16242 "only available with "
16243 "-std=c++14 or -std=gnu++14");
16244 else if (!flag_concepts)
16245 pedwarn (token->location, OPT_Wpedantic,
16246 "ISO C++ forbids use of %<auto%> in parameter "
16247 "declaration");
16249 else
16250 type = make_auto ();
16251 break;
16253 case RID_DECLTYPE:
16254 /* Since DR 743, decltype can either be a simple-type-specifier by
16255 itself or begin a nested-name-specifier. Parsing it will replace
16256 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16257 handling below decide what to do. */
16258 cp_parser_decltype (parser);
16259 cp_lexer_set_token_position (parser->lexer, token);
16260 break;
16262 case RID_TYPEOF:
16263 /* Consume the `typeof' token. */
16264 cp_lexer_consume_token (parser->lexer);
16265 /* Parse the operand to `typeof'. */
16266 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16267 /* If it is not already a TYPE, take its type. */
16268 if (!TYPE_P (type))
16269 type = finish_typeof (type);
16271 if (decl_specs)
16272 cp_parser_set_decl_spec_type (decl_specs, type,
16273 token,
16274 /*type_definition_p=*/false);
16276 return type;
16278 case RID_UNDERLYING_TYPE:
16279 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16280 if (decl_specs)
16281 cp_parser_set_decl_spec_type (decl_specs, type,
16282 token,
16283 /*type_definition_p=*/false);
16285 return type;
16287 case RID_BASES:
16288 case RID_DIRECT_BASES:
16289 type = cp_parser_trait_expr (parser, token->keyword);
16290 if (decl_specs)
16291 cp_parser_set_decl_spec_type (decl_specs, type,
16292 token,
16293 /*type_definition_p=*/false);
16294 return type;
16295 default:
16296 break;
16299 /* If token is an already-parsed decltype not followed by ::,
16300 it's a simple-type-specifier. */
16301 if (token->type == CPP_DECLTYPE
16302 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16304 type = saved_checks_value (token->u.tree_check_value);
16305 if (decl_specs)
16307 cp_parser_set_decl_spec_type (decl_specs, type,
16308 token,
16309 /*type_definition_p=*/false);
16310 /* Remember that we are handling a decltype in order to
16311 implement the resolution of DR 1510 when the argument
16312 isn't instantiation dependent. */
16313 decl_specs->decltype_p = true;
16315 cp_lexer_consume_token (parser->lexer);
16316 return type;
16319 /* If the type-specifier was for a built-in type, we're done. */
16320 if (type)
16322 /* Record the type. */
16323 if (decl_specs
16324 && (token->keyword != RID_SIGNED
16325 && token->keyword != RID_UNSIGNED
16326 && token->keyword != RID_SHORT
16327 && token->keyword != RID_LONG))
16328 cp_parser_set_decl_spec_type (decl_specs,
16329 type,
16330 token,
16331 /*type_definition_p=*/false);
16332 if (decl_specs)
16333 decl_specs->any_specifiers_p = true;
16335 /* Consume the token. */
16336 cp_lexer_consume_token (parser->lexer);
16338 if (type == error_mark_node)
16339 return error_mark_node;
16341 /* There is no valid C++ program where a non-template type is
16342 followed by a "<". That usually indicates that the user thought
16343 that the type was a template. */
16344 cp_parser_check_for_invalid_template_id (parser, type, none_type,
16345 token->location);
16347 return TYPE_NAME (type);
16350 /* The type-specifier must be a user-defined type. */
16351 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16353 bool qualified_p;
16354 bool global_p;
16356 /* Don't gobble tokens or issue error messages if this is an
16357 optional type-specifier. */
16358 if (flags & CP_PARSER_FLAGS_OPTIONAL)
16359 cp_parser_parse_tentatively (parser);
16361 /* Look for the optional `::' operator. */
16362 global_p
16363 = (cp_parser_global_scope_opt (parser,
16364 /*current_scope_valid_p=*/false)
16365 != NULL_TREE);
16366 /* Look for the nested-name specifier. */
16367 qualified_p
16368 = (cp_parser_nested_name_specifier_opt (parser,
16369 /*typename_keyword_p=*/false,
16370 /*check_dependency_p=*/true,
16371 /*type_p=*/false,
16372 /*is_declaration=*/false)
16373 != NULL_TREE);
16374 token = cp_lexer_peek_token (parser->lexer);
16375 /* If we have seen a nested-name-specifier, and the next token
16376 is `template', then we are using the template-id production. */
16377 if (parser->scope
16378 && cp_parser_optional_template_keyword (parser))
16380 /* Look for the template-id. */
16381 type = cp_parser_template_id (parser,
16382 /*template_keyword_p=*/true,
16383 /*check_dependency_p=*/true,
16384 none_type,
16385 /*is_declaration=*/false);
16386 /* If the template-id did not name a type, we are out of
16387 luck. */
16388 if (TREE_CODE (type) != TYPE_DECL)
16390 cp_parser_error (parser, "expected template-id for type");
16391 type = NULL_TREE;
16394 /* Otherwise, look for a type-name. */
16395 else
16396 type = cp_parser_type_name (parser);
16397 /* Keep track of all name-lookups performed in class scopes. */
16398 if (type
16399 && !global_p
16400 && !qualified_p
16401 && TREE_CODE (type) == TYPE_DECL
16402 && identifier_p (DECL_NAME (type)))
16403 maybe_note_name_used_in_class (DECL_NAME (type), type);
16404 /* If it didn't work out, we don't have a TYPE. */
16405 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
16406 && !cp_parser_parse_definitely (parser))
16407 type = NULL_TREE;
16408 if (type && decl_specs)
16409 cp_parser_set_decl_spec_type (decl_specs, type,
16410 token,
16411 /*type_definition_p=*/false);
16414 /* If we didn't get a type-name, issue an error message. */
16415 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16417 cp_parser_error (parser, "expected type-name");
16418 return error_mark_node;
16421 if (type && type != error_mark_node)
16423 /* See if TYPE is an Objective-C type, and if so, parse and
16424 accept any protocol references following it. Do this before
16425 the cp_parser_check_for_invalid_template_id() call, because
16426 Objective-C types can be followed by '<...>' which would
16427 enclose protocol names rather than template arguments, and so
16428 everything is fine. */
16429 if (c_dialect_objc () && !parser->scope
16430 && (objc_is_id (type) || objc_is_class_name (type)))
16432 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16433 tree qual_type = objc_get_protocol_qualified_type (type, protos);
16435 /* Clobber the "unqualified" type previously entered into
16436 DECL_SPECS with the new, improved protocol-qualified version. */
16437 if (decl_specs)
16438 decl_specs->type = qual_type;
16440 return qual_type;
16443 /* There is no valid C++ program where a non-template type is
16444 followed by a "<". That usually indicates that the user
16445 thought that the type was a template. */
16446 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
16447 none_type,
16448 token->location);
16451 return type;
16454 /* Parse a type-name.
16456 type-name:
16457 class-name
16458 enum-name
16459 typedef-name
16460 simple-template-id [in c++0x]
16462 enum-name:
16463 identifier
16465 typedef-name:
16466 identifier
16468 Concepts:
16470 type-name:
16471 concept-name
16472 partial-concept-id
16474 concept-name:
16475 identifier
16477 Returns a TYPE_DECL for the type. */
16479 static tree
16480 cp_parser_type_name (cp_parser* parser)
16482 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
16485 /* See above. */
16486 static tree
16487 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
16489 tree type_decl;
16491 /* We can't know yet whether it is a class-name or not. */
16492 cp_parser_parse_tentatively (parser);
16493 /* Try a class-name. */
16494 type_decl = cp_parser_class_name (parser,
16495 typename_keyword_p,
16496 /*template_keyword_p=*/false,
16497 none_type,
16498 /*check_dependency_p=*/true,
16499 /*class_head_p=*/false,
16500 /*is_declaration=*/false);
16501 /* If it's not a class-name, keep looking. */
16502 if (!cp_parser_parse_definitely (parser))
16504 if (cxx_dialect < cxx11)
16505 /* It must be a typedef-name or an enum-name. */
16506 return cp_parser_nonclass_name (parser);
16508 cp_parser_parse_tentatively (parser);
16509 /* It is either a simple-template-id representing an
16510 instantiation of an alias template... */
16511 type_decl = cp_parser_template_id (parser,
16512 /*template_keyword_p=*/false,
16513 /*check_dependency_p=*/true,
16514 none_type,
16515 /*is_declaration=*/false);
16516 /* Note that this must be an instantiation of an alias template
16517 because [temp.names]/6 says:
16519 A template-id that names an alias template specialization
16520 is a type-name.
16522 Whereas [temp.names]/7 says:
16524 A simple-template-id that names a class template
16525 specialization is a class-name.
16527 With concepts, this could also be a partial-concept-id that
16528 declares a non-type template parameter. */
16529 if (type_decl != NULL_TREE
16530 && TREE_CODE (type_decl) == TYPE_DECL
16531 && TYPE_DECL_ALIAS_P (type_decl))
16532 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
16533 else if (is_constrained_parameter (type_decl))
16534 /* Don't do anything. */ ;
16535 else
16536 cp_parser_simulate_error (parser);
16538 if (!cp_parser_parse_definitely (parser))
16539 /* ... Or a typedef-name or an enum-name. */
16540 return cp_parser_nonclass_name (parser);
16543 return type_decl;
16546 /* Check if DECL and ARGS can form a constrained-type-specifier.
16547 If ARGS is non-null, we try to form a concept check of the
16548 form DECL<?, ARGS> where ? is a wildcard that matches any
16549 kind of template argument. If ARGS is NULL, then we try to
16550 form a concept check of the form DECL<?>. */
16552 static tree
16553 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
16554 tree decl, tree args)
16556 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
16558 /* If we a constrained-type-specifier cannot be deduced. */
16559 if (parser->prevent_constrained_type_specifiers)
16560 return NULL_TREE;
16562 /* A constrained type specifier can only be found in an
16563 overload set or as a reference to a template declaration.
16565 FIXME: This might be masking a bug. It's possible that
16566 that the deduction below is causing template specializations
16567 to be formed with the wildcard as an argument. */
16568 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
16569 return NULL_TREE;
16571 /* Try to build a call expression that evaluates the
16572 concept. This can fail if the overload set refers
16573 only to non-templates. */
16574 tree placeholder = build_nt (WILDCARD_DECL);
16575 tree check = build_concept_check (decl, placeholder, args);
16576 if (check == error_mark_node)
16577 return NULL_TREE;
16579 /* Deduce the checked constraint and the prototype parameter.
16581 FIXME: In certain cases, failure to deduce should be a
16582 diagnosable error. */
16583 tree conc;
16584 tree proto;
16585 if (!deduce_constrained_parameter (check, conc, proto))
16586 return NULL_TREE;
16588 /* In template parameter scope, this results in a constrained
16589 parameter. Return a descriptor of that parm. */
16590 if (processing_template_parmlist)
16591 return build_constrained_parameter (conc, proto, args);
16593 /* In a parameter-declaration-clause, constrained-type
16594 specifiers result in invented template parameters. */
16595 if (parser->auto_is_implicit_function_template_parm_p)
16597 tree x = build_constrained_parameter (conc, proto, args);
16598 return synthesize_implicit_template_parm (parser, x);
16600 else
16602 /* Otherwise, we're in a context where the constrained
16603 type name is deduced and the constraint applies
16604 after deduction. */
16605 return make_constrained_auto (conc, args);
16608 return NULL_TREE;
16611 /* If DECL refers to a concept, return a TYPE_DECL representing
16612 the result of using the constrained type specifier in the
16613 current context. DECL refers to a concept if
16615 - it is an overload set containing a function concept taking a single
16616 type argument, or
16618 - it is a variable concept taking a single type argument. */
16620 static tree
16621 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
16623 if (flag_concepts
16624 && (TREE_CODE (decl) == OVERLOAD
16625 || BASELINK_P (decl)
16626 || variable_concept_p (decl)))
16627 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
16628 else
16629 return NULL_TREE;
16632 /* Check if DECL and ARGS form a partial-concept-id. If so,
16633 assign ID to the resulting constrained placeholder.
16635 Returns true if the partial-concept-id designates a placeholder
16636 and false otherwise. Note that *id is set to NULL_TREE in
16637 this case. */
16639 static tree
16640 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
16642 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
16645 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
16646 or a concept-name.
16648 enum-name:
16649 identifier
16651 typedef-name:
16652 identifier
16654 concept-name:
16655 identifier
16657 Returns a TYPE_DECL for the type. */
16659 static tree
16660 cp_parser_nonclass_name (cp_parser* parser)
16662 tree type_decl;
16663 tree identifier;
16665 cp_token *token = cp_lexer_peek_token (parser->lexer);
16666 identifier = cp_parser_identifier (parser);
16667 if (identifier == error_mark_node)
16668 return error_mark_node;
16670 /* Look up the type-name. */
16671 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
16673 type_decl = strip_using_decl (type_decl);
16675 /* If we found an overload set, then it may refer to a concept-name. */
16676 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
16677 type_decl = decl;
16679 if (TREE_CODE (type_decl) != TYPE_DECL
16680 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
16682 /* See if this is an Objective-C type. */
16683 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16684 tree type = objc_get_protocol_qualified_type (identifier, protos);
16685 if (type)
16686 type_decl = TYPE_NAME (type);
16689 /* Issue an error if we did not find a type-name. */
16690 if (TREE_CODE (type_decl) != TYPE_DECL
16691 /* In Objective-C, we have the complication that class names are
16692 normally type names and start declarations (eg, the
16693 "NSObject" in "NSObject *object;"), but can be used in an
16694 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
16695 is an expression. So, a classname followed by a dot is not a
16696 valid type-name. */
16697 || (objc_is_class_name (TREE_TYPE (type_decl))
16698 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
16700 if (!cp_parser_simulate_error (parser))
16701 cp_parser_name_lookup_error (parser, identifier, type_decl,
16702 NLE_TYPE, token->location);
16703 return error_mark_node;
16705 /* Remember that the name was used in the definition of the
16706 current class so that we can check later to see if the
16707 meaning would have been different after the class was
16708 entirely defined. */
16709 else if (type_decl != error_mark_node
16710 && !parser->scope)
16711 maybe_note_name_used_in_class (identifier, type_decl);
16713 return type_decl;
16716 /* Parse an elaborated-type-specifier. Note that the grammar given
16717 here incorporates the resolution to DR68.
16719 elaborated-type-specifier:
16720 class-key :: [opt] nested-name-specifier [opt] identifier
16721 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
16722 enum-key :: [opt] nested-name-specifier [opt] identifier
16723 typename :: [opt] nested-name-specifier identifier
16724 typename :: [opt] nested-name-specifier template [opt]
16725 template-id
16727 GNU extension:
16729 elaborated-type-specifier:
16730 class-key attributes :: [opt] nested-name-specifier [opt] identifier
16731 class-key attributes :: [opt] nested-name-specifier [opt]
16732 template [opt] template-id
16733 enum attributes :: [opt] nested-name-specifier [opt] identifier
16735 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
16736 declared `friend'. If IS_DECLARATION is TRUE, then this
16737 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
16738 something is being declared.
16740 Returns the TYPE specified. */
16742 static tree
16743 cp_parser_elaborated_type_specifier (cp_parser* parser,
16744 bool is_friend,
16745 bool is_declaration)
16747 enum tag_types tag_type;
16748 tree identifier;
16749 tree type = NULL_TREE;
16750 tree attributes = NULL_TREE;
16751 tree globalscope;
16752 cp_token *token = NULL;
16754 /* See if we're looking at the `enum' keyword. */
16755 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
16757 /* Consume the `enum' token. */
16758 cp_lexer_consume_token (parser->lexer);
16759 /* Remember that it's an enumeration type. */
16760 tag_type = enum_type;
16761 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
16762 enums) is used here. */
16763 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
16764 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
16766 pedwarn (input_location, 0, "elaborated-type-specifier "
16767 "for a scoped enum must not use the %<%D%> keyword",
16768 cp_lexer_peek_token (parser->lexer)->u.value);
16769 /* Consume the `struct' or `class' and parse it anyway. */
16770 cp_lexer_consume_token (parser->lexer);
16772 /* Parse the attributes. */
16773 attributes = cp_parser_attributes_opt (parser);
16775 /* Or, it might be `typename'. */
16776 else if (cp_lexer_next_token_is_keyword (parser->lexer,
16777 RID_TYPENAME))
16779 /* Consume the `typename' token. */
16780 cp_lexer_consume_token (parser->lexer);
16781 /* Remember that it's a `typename' type. */
16782 tag_type = typename_type;
16784 /* Otherwise it must be a class-key. */
16785 else
16787 tag_type = cp_parser_class_key (parser);
16788 if (tag_type == none_type)
16789 return error_mark_node;
16790 /* Parse the attributes. */
16791 attributes = cp_parser_attributes_opt (parser);
16794 /* Look for the `::' operator. */
16795 globalscope = cp_parser_global_scope_opt (parser,
16796 /*current_scope_valid_p=*/false);
16797 /* Look for the nested-name-specifier. */
16798 if (tag_type == typename_type && !globalscope)
16800 if (!cp_parser_nested_name_specifier (parser,
16801 /*typename_keyword_p=*/true,
16802 /*check_dependency_p=*/true,
16803 /*type_p=*/true,
16804 is_declaration))
16805 return error_mark_node;
16807 else
16808 /* Even though `typename' is not present, the proposed resolution
16809 to Core Issue 180 says that in `class A<T>::B', `B' should be
16810 considered a type-name, even if `A<T>' is dependent. */
16811 cp_parser_nested_name_specifier_opt (parser,
16812 /*typename_keyword_p=*/true,
16813 /*check_dependency_p=*/true,
16814 /*type_p=*/true,
16815 is_declaration);
16816 /* For everything but enumeration types, consider a template-id.
16817 For an enumeration type, consider only a plain identifier. */
16818 if (tag_type != enum_type)
16820 bool template_p = false;
16821 tree decl;
16823 /* Allow the `template' keyword. */
16824 template_p = cp_parser_optional_template_keyword (parser);
16825 /* If we didn't see `template', we don't know if there's a
16826 template-id or not. */
16827 if (!template_p)
16828 cp_parser_parse_tentatively (parser);
16829 /* Parse the template-id. */
16830 token = cp_lexer_peek_token (parser->lexer);
16831 decl = cp_parser_template_id (parser, template_p,
16832 /*check_dependency_p=*/true,
16833 tag_type,
16834 is_declaration);
16835 /* If we didn't find a template-id, look for an ordinary
16836 identifier. */
16837 if (!template_p && !cp_parser_parse_definitely (parser))
16839 /* We can get here when cp_parser_template_id, called by
16840 cp_parser_class_name with tag_type == none_type, succeeds
16841 and caches a BASELINK. Then, when called again here,
16842 instead of failing and returning an error_mark_node
16843 returns it (see template/typename17.C in C++11).
16844 ??? Could we diagnose this earlier? */
16845 else if (tag_type == typename_type && BASELINK_P (decl))
16847 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
16848 type = error_mark_node;
16850 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
16851 in effect, then we must assume that, upon instantiation, the
16852 template will correspond to a class. */
16853 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16854 && tag_type == typename_type)
16855 type = make_typename_type (parser->scope, decl,
16856 typename_type,
16857 /*complain=*/tf_error);
16858 /* If the `typename' keyword is in effect and DECL is not a type
16859 decl, then type is non existent. */
16860 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
16862 else if (TREE_CODE (decl) == TYPE_DECL)
16863 type = check_elaborated_type_specifier (tag_type, decl,
16864 /*allow_template_p=*/true);
16865 else if (decl == error_mark_node)
16866 type = error_mark_node;
16869 if (!type)
16871 token = cp_lexer_peek_token (parser->lexer);
16872 identifier = cp_parser_identifier (parser);
16874 if (identifier == error_mark_node)
16876 parser->scope = NULL_TREE;
16877 return error_mark_node;
16880 /* For a `typename', we needn't call xref_tag. */
16881 if (tag_type == typename_type
16882 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
16883 return cp_parser_make_typename_type (parser, identifier,
16884 token->location);
16886 /* Template parameter lists apply only if we are not within a
16887 function parameter list. */
16888 bool template_parm_lists_apply
16889 = parser->num_template_parameter_lists;
16890 if (template_parm_lists_apply)
16891 for (cp_binding_level *s = current_binding_level;
16892 s && s->kind != sk_template_parms;
16893 s = s->level_chain)
16894 if (s->kind == sk_function_parms)
16895 template_parm_lists_apply = false;
16897 /* Look up a qualified name in the usual way. */
16898 if (parser->scope)
16900 tree decl;
16901 tree ambiguous_decls;
16903 decl = cp_parser_lookup_name (parser, identifier,
16904 tag_type,
16905 /*is_template=*/false,
16906 /*is_namespace=*/false,
16907 /*check_dependency=*/true,
16908 &ambiguous_decls,
16909 token->location);
16911 /* If the lookup was ambiguous, an error will already have been
16912 issued. */
16913 if (ambiguous_decls)
16914 return error_mark_node;
16916 /* If we are parsing friend declaration, DECL may be a
16917 TEMPLATE_DECL tree node here. However, we need to check
16918 whether this TEMPLATE_DECL results in valid code. Consider
16919 the following example:
16921 namespace N {
16922 template <class T> class C {};
16924 class X {
16925 template <class T> friend class N::C; // #1, valid code
16927 template <class T> class Y {
16928 friend class N::C; // #2, invalid code
16931 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
16932 name lookup of `N::C'. We see that friend declaration must
16933 be template for the code to be valid. Note that
16934 processing_template_decl does not work here since it is
16935 always 1 for the above two cases. */
16937 decl = (cp_parser_maybe_treat_template_as_class
16938 (decl, /*tag_name_p=*/is_friend
16939 && template_parm_lists_apply));
16941 if (TREE_CODE (decl) != TYPE_DECL)
16943 cp_parser_diagnose_invalid_type_name (parser,
16944 identifier,
16945 token->location);
16946 return error_mark_node;
16949 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
16951 bool allow_template = (template_parm_lists_apply
16952 || DECL_SELF_REFERENCE_P (decl));
16953 type = check_elaborated_type_specifier (tag_type, decl,
16954 allow_template);
16956 if (type == error_mark_node)
16957 return error_mark_node;
16960 /* Forward declarations of nested types, such as
16962 class C1::C2;
16963 class C1::C2::C3;
16965 are invalid unless all components preceding the final '::'
16966 are complete. If all enclosing types are complete, these
16967 declarations become merely pointless.
16969 Invalid forward declarations of nested types are errors
16970 caught elsewhere in parsing. Those that are pointless arrive
16971 here. */
16973 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16974 && !is_friend && !processing_explicit_instantiation)
16975 warning (0, "declaration %qD does not declare anything", decl);
16977 type = TREE_TYPE (decl);
16979 else
16981 /* An elaborated-type-specifier sometimes introduces a new type and
16982 sometimes names an existing type. Normally, the rule is that it
16983 introduces a new type only if there is not an existing type of
16984 the same name already in scope. For example, given:
16986 struct S {};
16987 void f() { struct S s; }
16989 the `struct S' in the body of `f' is the same `struct S' as in
16990 the global scope; the existing definition is used. However, if
16991 there were no global declaration, this would introduce a new
16992 local class named `S'.
16994 An exception to this rule applies to the following code:
16996 namespace N { struct S; }
16998 Here, the elaborated-type-specifier names a new type
16999 unconditionally; even if there is already an `S' in the
17000 containing scope this declaration names a new type.
17001 This exception only applies if the elaborated-type-specifier
17002 forms the complete declaration:
17004 [class.name]
17006 A declaration consisting solely of `class-key identifier ;' is
17007 either a redeclaration of the name in the current scope or a
17008 forward declaration of the identifier as a class name. It
17009 introduces the name into the current scope.
17011 We are in this situation precisely when the next token is a `;'.
17013 An exception to the exception is that a `friend' declaration does
17014 *not* name a new type; i.e., given:
17016 struct S { friend struct T; };
17018 `T' is not a new type in the scope of `S'.
17020 Also, `new struct S' or `sizeof (struct S)' never results in the
17021 definition of a new type; a new type can only be declared in a
17022 declaration context. */
17024 tag_scope ts;
17025 bool template_p;
17027 if (is_friend)
17028 /* Friends have special name lookup rules. */
17029 ts = ts_within_enclosing_non_class;
17030 else if (is_declaration
17031 && cp_lexer_next_token_is (parser->lexer,
17032 CPP_SEMICOLON))
17033 /* This is a `class-key identifier ;' */
17034 ts = ts_current;
17035 else
17036 ts = ts_global;
17038 template_p =
17039 (template_parm_lists_apply
17040 && (cp_parser_next_token_starts_class_definition_p (parser)
17041 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17042 /* An unqualified name was used to reference this type, so
17043 there were no qualifying templates. */
17044 if (template_parm_lists_apply
17045 && !cp_parser_check_template_parameters (parser,
17046 /*num_templates=*/0,
17047 token->location,
17048 /*declarator=*/NULL))
17049 return error_mark_node;
17050 type = xref_tag (tag_type, identifier, ts, template_p);
17054 if (type == error_mark_node)
17055 return error_mark_node;
17057 /* Allow attributes on forward declarations of classes. */
17058 if (attributes)
17060 if (TREE_CODE (type) == TYPENAME_TYPE)
17061 warning (OPT_Wattributes,
17062 "attributes ignored on uninstantiated type");
17063 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17064 && ! processing_explicit_instantiation)
17065 warning (OPT_Wattributes,
17066 "attributes ignored on template instantiation");
17067 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17068 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17069 else
17070 warning (OPT_Wattributes,
17071 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17074 if (tag_type != enum_type)
17076 /* Indicate whether this class was declared as a `class' or as a
17077 `struct'. */
17078 if (CLASS_TYPE_P (type))
17079 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17080 cp_parser_check_class_key (tag_type, type);
17083 /* A "<" cannot follow an elaborated type specifier. If that
17084 happens, the user was probably trying to form a template-id. */
17085 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17086 token->location);
17088 return type;
17091 /* Parse an enum-specifier.
17093 enum-specifier:
17094 enum-head { enumerator-list [opt] }
17095 enum-head { enumerator-list , } [C++0x]
17097 enum-head:
17098 enum-key identifier [opt] enum-base [opt]
17099 enum-key nested-name-specifier identifier enum-base [opt]
17101 enum-key:
17102 enum
17103 enum class [C++0x]
17104 enum struct [C++0x]
17106 enum-base: [C++0x]
17107 : type-specifier-seq
17109 opaque-enum-specifier:
17110 enum-key identifier enum-base [opt] ;
17112 GNU Extensions:
17113 enum-key attributes[opt] identifier [opt] enum-base [opt]
17114 { enumerator-list [opt] }attributes[opt]
17115 enum-key attributes[opt] identifier [opt] enum-base [opt]
17116 { enumerator-list, }attributes[opt] [C++0x]
17118 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17119 if the token stream isn't an enum-specifier after all. */
17121 static tree
17122 cp_parser_enum_specifier (cp_parser* parser)
17124 tree identifier;
17125 tree type = NULL_TREE;
17126 tree prev_scope;
17127 tree nested_name_specifier = NULL_TREE;
17128 tree attributes;
17129 bool scoped_enum_p = false;
17130 bool has_underlying_type = false;
17131 bool nested_being_defined = false;
17132 bool new_value_list = false;
17133 bool is_new_type = false;
17134 bool is_unnamed = false;
17135 tree underlying_type = NULL_TREE;
17136 cp_token *type_start_token = NULL;
17137 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17139 parser->colon_corrects_to_scope_p = false;
17141 /* Parse tentatively so that we can back up if we don't find a
17142 enum-specifier. */
17143 cp_parser_parse_tentatively (parser);
17145 /* Caller guarantees that the current token is 'enum', an identifier
17146 possibly follows, and the token after that is an opening brace.
17147 If we don't have an identifier, fabricate an anonymous name for
17148 the enumeration being defined. */
17149 cp_lexer_consume_token (parser->lexer);
17151 /* Parse the "class" or "struct", which indicates a scoped
17152 enumeration type in C++0x. */
17153 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17154 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17156 if (cxx_dialect < cxx11)
17157 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17159 /* Consume the `struct' or `class' token. */
17160 cp_lexer_consume_token (parser->lexer);
17162 scoped_enum_p = true;
17165 attributes = cp_parser_attributes_opt (parser);
17167 /* Clear the qualification. */
17168 parser->scope = NULL_TREE;
17169 parser->qualifying_scope = NULL_TREE;
17170 parser->object_scope = NULL_TREE;
17172 /* Figure out in what scope the declaration is being placed. */
17173 prev_scope = current_scope ();
17175 type_start_token = cp_lexer_peek_token (parser->lexer);
17177 push_deferring_access_checks (dk_no_check);
17178 nested_name_specifier
17179 = cp_parser_nested_name_specifier_opt (parser,
17180 /*typename_keyword_p=*/true,
17181 /*check_dependency_p=*/false,
17182 /*type_p=*/false,
17183 /*is_declaration=*/false);
17185 if (nested_name_specifier)
17187 tree name;
17189 identifier = cp_parser_identifier (parser);
17190 name = cp_parser_lookup_name (parser, identifier,
17191 enum_type,
17192 /*is_template=*/false,
17193 /*is_namespace=*/false,
17194 /*check_dependency=*/true,
17195 /*ambiguous_decls=*/NULL,
17196 input_location);
17197 if (name && name != error_mark_node)
17199 type = TREE_TYPE (name);
17200 if (TREE_CODE (type) == TYPENAME_TYPE)
17202 /* Are template enums allowed in ISO? */
17203 if (template_parm_scope_p ())
17204 pedwarn (type_start_token->location, OPT_Wpedantic,
17205 "%qD is an enumeration template", name);
17206 /* ignore a typename reference, for it will be solved by name
17207 in start_enum. */
17208 type = NULL_TREE;
17211 else if (nested_name_specifier == error_mark_node)
17212 /* We already issued an error. */;
17213 else
17215 error_at (type_start_token->location,
17216 "%qD does not name an enumeration in %qT",
17217 identifier, nested_name_specifier);
17218 nested_name_specifier = error_mark_node;
17221 else
17223 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17224 identifier = cp_parser_identifier (parser);
17225 else
17227 identifier = make_anon_name ();
17228 is_unnamed = true;
17229 if (scoped_enum_p)
17230 error_at (type_start_token->location,
17231 "unnamed scoped enum is not allowed");
17234 pop_deferring_access_checks ();
17236 /* Check for the `:' that denotes a specified underlying type in C++0x.
17237 Note that a ':' could also indicate a bitfield width, however. */
17238 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17240 cp_decl_specifier_seq type_specifiers;
17242 /* Consume the `:'. */
17243 cp_lexer_consume_token (parser->lexer);
17245 /* Parse the type-specifier-seq. */
17246 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17247 /*is_trailing_return=*/false,
17248 &type_specifiers);
17250 /* At this point this is surely not elaborated type specifier. */
17251 if (!cp_parser_parse_definitely (parser))
17252 return NULL_TREE;
17254 if (cxx_dialect < cxx11)
17255 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17257 has_underlying_type = true;
17259 /* If that didn't work, stop. */
17260 if (type_specifiers.type != error_mark_node)
17262 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17263 /*initialized=*/0, NULL);
17264 if (underlying_type == error_mark_node
17265 || check_for_bare_parameter_packs (underlying_type))
17266 underlying_type = NULL_TREE;
17270 /* Look for the `{' but don't consume it yet. */
17271 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17273 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17275 cp_parser_error (parser, "expected %<{%>");
17276 if (has_underlying_type)
17278 type = NULL_TREE;
17279 goto out;
17282 /* An opaque-enum-specifier must have a ';' here. */
17283 if ((scoped_enum_p || underlying_type)
17284 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17286 cp_parser_error (parser, "expected %<;%> or %<{%>");
17287 if (has_underlying_type)
17289 type = NULL_TREE;
17290 goto out;
17295 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17296 return NULL_TREE;
17298 if (nested_name_specifier)
17300 if (CLASS_TYPE_P (nested_name_specifier))
17302 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
17303 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
17304 push_scope (nested_name_specifier);
17306 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17308 push_nested_namespace (nested_name_specifier);
17312 /* Issue an error message if type-definitions are forbidden here. */
17313 if (!cp_parser_check_type_definition (parser))
17314 type = error_mark_node;
17315 else
17316 /* Create the new type. We do this before consuming the opening
17317 brace so the enum will be recorded as being on the line of its
17318 tag (or the 'enum' keyword, if there is no tag). */
17319 type = start_enum (identifier, type, underlying_type,
17320 attributes, scoped_enum_p, &is_new_type);
17322 /* If the next token is not '{' it is an opaque-enum-specifier or an
17323 elaborated-type-specifier. */
17324 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17326 timevar_push (TV_PARSE_ENUM);
17327 if (nested_name_specifier
17328 && nested_name_specifier != error_mark_node)
17330 /* The following catches invalid code such as:
17331 enum class S<int>::E { A, B, C }; */
17332 if (!processing_specialization
17333 && CLASS_TYPE_P (nested_name_specifier)
17334 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
17335 error_at (type_start_token->location, "cannot add an enumerator "
17336 "list to a template instantiation");
17338 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
17340 error_at (type_start_token->location,
17341 "%<%T::%E%> has not been declared",
17342 TYPE_CONTEXT (nested_name_specifier),
17343 nested_name_specifier);
17344 type = error_mark_node;
17346 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
17347 && !CLASS_TYPE_P (nested_name_specifier))
17349 error_at (type_start_token->location, "nested name specifier "
17350 "%qT for enum declaration does not name a class "
17351 "or namespace", nested_name_specifier);
17352 type = error_mark_node;
17354 /* If that scope does not contain the scope in which the
17355 class was originally declared, the program is invalid. */
17356 else if (prev_scope && !is_ancestor (prev_scope,
17357 nested_name_specifier))
17359 if (at_namespace_scope_p ())
17360 error_at (type_start_token->location,
17361 "declaration of %qD in namespace %qD which does not "
17362 "enclose %qD",
17363 type, prev_scope, nested_name_specifier);
17364 else
17365 error_at (type_start_token->location,
17366 "declaration of %qD in %qD which does not "
17367 "enclose %qD",
17368 type, prev_scope, nested_name_specifier);
17369 type = error_mark_node;
17371 /* If that scope is the scope where the declaration is being placed
17372 the program is invalid. */
17373 else if (CLASS_TYPE_P (nested_name_specifier)
17374 && CLASS_TYPE_P (prev_scope)
17375 && same_type_p (nested_name_specifier, prev_scope))
17377 permerror (type_start_token->location,
17378 "extra qualification not allowed");
17379 nested_name_specifier = NULL_TREE;
17383 if (scoped_enum_p)
17384 begin_scope (sk_scoped_enum, type);
17386 /* Consume the opening brace. */
17387 cp_lexer_consume_token (parser->lexer);
17389 if (type == error_mark_node)
17390 ; /* Nothing to add */
17391 else if (OPAQUE_ENUM_P (type)
17392 || (cxx_dialect > cxx98 && processing_specialization))
17394 new_value_list = true;
17395 SET_OPAQUE_ENUM_P (type, false);
17396 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17398 else
17400 error_at (type_start_token->location,
17401 "multiple definition of %q#T", type);
17402 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
17403 "previous definition here");
17404 type = error_mark_node;
17407 if (type == error_mark_node)
17408 cp_parser_skip_to_end_of_block_or_statement (parser);
17409 /* If the next token is not '}', then there are some enumerators. */
17410 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17412 if (is_unnamed && !scoped_enum_p)
17413 pedwarn (type_start_token->location, OPT_Wpedantic,
17414 "ISO C++ forbids empty unnamed enum");
17416 else
17417 cp_parser_enumerator_list (parser, type);
17419 /* Consume the final '}'. */
17420 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17422 if (scoped_enum_p)
17423 finish_scope ();
17424 timevar_pop (TV_PARSE_ENUM);
17426 else
17428 /* If a ';' follows, then it is an opaque-enum-specifier
17429 and additional restrictions apply. */
17430 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17432 if (is_unnamed)
17433 error_at (type_start_token->location,
17434 "opaque-enum-specifier without name");
17435 else if (nested_name_specifier)
17436 error_at (type_start_token->location,
17437 "opaque-enum-specifier must use a simple identifier");
17441 /* Look for trailing attributes to apply to this enumeration, and
17442 apply them if appropriate. */
17443 if (cp_parser_allow_gnu_extensions_p (parser))
17445 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
17446 cplus_decl_attributes (&type,
17447 trailing_attr,
17448 (int) ATTR_FLAG_TYPE_IN_PLACE);
17451 /* Finish up the enumeration. */
17452 if (type != error_mark_node)
17454 if (new_value_list)
17455 finish_enum_value_list (type);
17456 if (is_new_type)
17457 finish_enum (type);
17460 if (nested_name_specifier)
17462 if (CLASS_TYPE_P (nested_name_specifier))
17464 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
17465 pop_scope (nested_name_specifier);
17467 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17469 pop_nested_namespace (nested_name_specifier);
17472 out:
17473 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17474 return type;
17477 /* Parse an enumerator-list. The enumerators all have the indicated
17478 TYPE.
17480 enumerator-list:
17481 enumerator-definition
17482 enumerator-list , enumerator-definition */
17484 static void
17485 cp_parser_enumerator_list (cp_parser* parser, tree type)
17487 while (true)
17489 /* Parse an enumerator-definition. */
17490 cp_parser_enumerator_definition (parser, type);
17492 /* If the next token is not a ',', we've reached the end of
17493 the list. */
17494 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17495 break;
17496 /* Otherwise, consume the `,' and keep going. */
17497 cp_lexer_consume_token (parser->lexer);
17498 /* If the next token is a `}', there is a trailing comma. */
17499 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17501 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
17502 pedwarn (input_location, OPT_Wpedantic,
17503 "comma at end of enumerator list");
17504 break;
17509 /* Parse an enumerator-definition. The enumerator has the indicated
17510 TYPE.
17512 enumerator-definition:
17513 enumerator
17514 enumerator = constant-expression
17516 enumerator:
17517 identifier
17519 GNU Extensions:
17521 enumerator-definition:
17522 enumerator attributes [opt]
17523 enumerator attributes [opt] = constant-expression */
17525 static void
17526 cp_parser_enumerator_definition (cp_parser* parser, tree type)
17528 tree identifier;
17529 tree value;
17530 location_t loc;
17532 /* Save the input location because we are interested in the location
17533 of the identifier and not the location of the explicit value. */
17534 loc = cp_lexer_peek_token (parser->lexer)->location;
17536 /* Look for the identifier. */
17537 identifier = cp_parser_identifier (parser);
17538 if (identifier == error_mark_node)
17539 return;
17541 /* Parse any specified attributes. */
17542 tree attrs = cp_parser_attributes_opt (parser);
17544 /* If the next token is an '=', then there is an explicit value. */
17545 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17547 /* Consume the `=' token. */
17548 cp_lexer_consume_token (parser->lexer);
17549 /* Parse the value. */
17550 value = cp_parser_constant_expression (parser);
17552 else
17553 value = NULL_TREE;
17555 /* If we are processing a template, make sure the initializer of the
17556 enumerator doesn't contain any bare template parameter pack. */
17557 if (check_for_bare_parameter_packs (value))
17558 value = error_mark_node;
17560 /* Create the enumerator. */
17561 build_enumerator (identifier, value, type, attrs, loc);
17564 /* Parse a namespace-name.
17566 namespace-name:
17567 original-namespace-name
17568 namespace-alias
17570 Returns the NAMESPACE_DECL for the namespace. */
17572 static tree
17573 cp_parser_namespace_name (cp_parser* parser)
17575 tree identifier;
17576 tree namespace_decl;
17578 cp_token *token = cp_lexer_peek_token (parser->lexer);
17580 /* Get the name of the namespace. */
17581 identifier = cp_parser_identifier (parser);
17582 if (identifier == error_mark_node)
17583 return error_mark_node;
17585 /* Look up the identifier in the currently active scope. Look only
17586 for namespaces, due to:
17588 [basic.lookup.udir]
17590 When looking up a namespace-name in a using-directive or alias
17591 definition, only namespace names are considered.
17593 And:
17595 [basic.lookup.qual]
17597 During the lookup of a name preceding the :: scope resolution
17598 operator, object, function, and enumerator names are ignored.
17600 (Note that cp_parser_qualifying_entity only calls this
17601 function if the token after the name is the scope resolution
17602 operator.) */
17603 namespace_decl = cp_parser_lookup_name (parser, identifier,
17604 none_type,
17605 /*is_template=*/false,
17606 /*is_namespace=*/true,
17607 /*check_dependency=*/true,
17608 /*ambiguous_decls=*/NULL,
17609 token->location);
17610 /* If it's not a namespace, issue an error. */
17611 if (namespace_decl == error_mark_node
17612 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
17614 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17615 error_at (token->location, "%qD is not a namespace-name", identifier);
17616 cp_parser_error (parser, "expected namespace-name");
17617 namespace_decl = error_mark_node;
17620 return namespace_decl;
17623 /* Parse a namespace-definition.
17625 namespace-definition:
17626 named-namespace-definition
17627 unnamed-namespace-definition
17629 named-namespace-definition:
17630 original-namespace-definition
17631 extension-namespace-definition
17633 original-namespace-definition:
17634 namespace identifier { namespace-body }
17636 extension-namespace-definition:
17637 namespace original-namespace-name { namespace-body }
17639 unnamed-namespace-definition:
17640 namespace { namespace-body } */
17642 static void
17643 cp_parser_namespace_definition (cp_parser* parser)
17645 tree identifier, attribs;
17646 bool has_visibility;
17647 bool is_inline;
17648 cp_token* token;
17649 int nested_definition_count = 0;
17651 cp_ensure_no_omp_declare_simd (parser);
17652 cp_ensure_no_oacc_routine (parser);
17653 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
17655 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
17656 is_inline = true;
17657 cp_lexer_consume_token (parser->lexer);
17659 else
17660 is_inline = false;
17662 /* Look for the `namespace' keyword. */
17663 token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17665 /* Parse any specified attributes before the identifier. */
17666 attribs = cp_parser_attributes_opt (parser);
17668 /* Get the name of the namespace. We do not attempt to distinguish
17669 between an original-namespace-definition and an
17670 extension-namespace-definition at this point. The semantic
17671 analysis routines are responsible for that. */
17672 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17673 identifier = cp_parser_identifier (parser);
17674 else
17675 identifier = NULL_TREE;
17677 /* Parse any specified attributes after the identifier. */
17678 tree post_ident_attribs = cp_parser_attributes_opt (parser);
17679 if (post_ident_attribs)
17681 if (attribs)
17682 attribs = chainon (attribs, post_ident_attribs);
17683 else
17684 attribs = post_ident_attribs;
17687 /* Start the namespace. */
17688 bool ok = push_namespace (identifier);
17690 /* Parse any nested namespace definition. */
17691 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17693 if (attribs)
17694 error_at (token->location, "a nested namespace definition cannot have attributes");
17695 if (cxx_dialect < cxx1z)
17696 pedwarn (input_location, OPT_Wpedantic,
17697 "nested namespace definitions only available with "
17698 "-std=c++1z or -std=gnu++1z");
17699 if (is_inline)
17700 error_at (token->location, "a nested namespace definition cannot be inline");
17701 while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17703 cp_lexer_consume_token (parser->lexer);
17704 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17705 identifier = cp_parser_identifier (parser);
17706 else
17708 cp_parser_error (parser, "nested identifier required");
17709 break;
17711 ++nested_definition_count;
17712 push_namespace (identifier);
17716 /* Look for the `{' to validate starting the namespace. */
17717 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
17719 /* "inline namespace" is equivalent to a stub namespace definition
17720 followed by a strong using directive. */
17721 if (is_inline && ok)
17723 tree name_space = current_namespace;
17724 /* Set up namespace association. */
17725 DECL_NAMESPACE_ASSOCIATIONS (name_space)
17726 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
17727 DECL_NAMESPACE_ASSOCIATIONS (name_space));
17728 /* Import the contents of the inline namespace. */
17729 pop_namespace ();
17730 do_using_directive (name_space);
17731 push_namespace (identifier);
17734 has_visibility = handle_namespace_attrs (current_namespace, attribs);
17736 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
17738 /* Parse the body of the namespace. */
17739 cp_parser_namespace_body (parser);
17741 if (has_visibility)
17742 pop_visibility (1);
17744 /* Finish the nested namespace definitions. */
17745 while (nested_definition_count--)
17746 pop_namespace ();
17748 /* Finish the namespace. */
17749 if (ok)
17750 pop_namespace ();
17751 /* Look for the final `}'. */
17752 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17755 /* Parse a namespace-body.
17757 namespace-body:
17758 declaration-seq [opt] */
17760 static void
17761 cp_parser_namespace_body (cp_parser* parser)
17763 cp_parser_declaration_seq_opt (parser);
17766 /* Parse a namespace-alias-definition.
17768 namespace-alias-definition:
17769 namespace identifier = qualified-namespace-specifier ; */
17771 static void
17772 cp_parser_namespace_alias_definition (cp_parser* parser)
17774 tree identifier;
17775 tree namespace_specifier;
17777 cp_token *token = cp_lexer_peek_token (parser->lexer);
17779 /* Look for the `namespace' keyword. */
17780 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17781 /* Look for the identifier. */
17782 identifier = cp_parser_identifier (parser);
17783 if (identifier == error_mark_node)
17784 return;
17785 /* Look for the `=' token. */
17786 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
17787 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17789 error_at (token->location, "%<namespace%> definition is not allowed here");
17790 /* Skip the definition. */
17791 cp_lexer_consume_token (parser->lexer);
17792 if (cp_parser_skip_to_closing_brace (parser))
17793 cp_lexer_consume_token (parser->lexer);
17794 return;
17796 cp_parser_require (parser, CPP_EQ, RT_EQ);
17797 /* Look for the qualified-namespace-specifier. */
17798 namespace_specifier
17799 = cp_parser_qualified_namespace_specifier (parser);
17800 /* Look for the `;' token. */
17801 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17803 /* Register the alias in the symbol table. */
17804 do_namespace_alias (identifier, namespace_specifier);
17807 /* Parse a qualified-namespace-specifier.
17809 qualified-namespace-specifier:
17810 :: [opt] nested-name-specifier [opt] namespace-name
17812 Returns a NAMESPACE_DECL corresponding to the specified
17813 namespace. */
17815 static tree
17816 cp_parser_qualified_namespace_specifier (cp_parser* parser)
17818 /* Look for the optional `::'. */
17819 cp_parser_global_scope_opt (parser,
17820 /*current_scope_valid_p=*/false);
17822 /* Look for the optional nested-name-specifier. */
17823 cp_parser_nested_name_specifier_opt (parser,
17824 /*typename_keyword_p=*/false,
17825 /*check_dependency_p=*/true,
17826 /*type_p=*/false,
17827 /*is_declaration=*/true);
17829 return cp_parser_namespace_name (parser);
17832 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
17833 access declaration.
17835 using-declaration:
17836 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
17837 using :: unqualified-id ;
17839 access-declaration:
17840 qualified-id ;
17844 static bool
17845 cp_parser_using_declaration (cp_parser* parser,
17846 bool access_declaration_p)
17848 cp_token *token;
17849 bool typename_p = false;
17850 bool global_scope_p;
17851 tree decl;
17852 tree identifier;
17853 tree qscope;
17854 int oldcount = errorcount;
17855 cp_token *diag_token = NULL;
17857 if (access_declaration_p)
17859 diag_token = cp_lexer_peek_token (parser->lexer);
17860 cp_parser_parse_tentatively (parser);
17862 else
17864 /* Look for the `using' keyword. */
17865 cp_parser_require_keyword (parser, RID_USING, RT_USING);
17867 /* Peek at the next token. */
17868 token = cp_lexer_peek_token (parser->lexer);
17869 /* See if it's `typename'. */
17870 if (token->keyword == RID_TYPENAME)
17872 /* Remember that we've seen it. */
17873 typename_p = true;
17874 /* Consume the `typename' token. */
17875 cp_lexer_consume_token (parser->lexer);
17879 /* Look for the optional global scope qualification. */
17880 global_scope_p
17881 = (cp_parser_global_scope_opt (parser,
17882 /*current_scope_valid_p=*/false)
17883 != NULL_TREE);
17885 /* If we saw `typename', or didn't see `::', then there must be a
17886 nested-name-specifier present. */
17887 if (typename_p || !global_scope_p)
17889 qscope = cp_parser_nested_name_specifier (parser, typename_p,
17890 /*check_dependency_p=*/true,
17891 /*type_p=*/false,
17892 /*is_declaration=*/true);
17893 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
17895 cp_parser_skip_to_end_of_block_or_statement (parser);
17896 return false;
17899 /* Otherwise, we could be in either of the two productions. In that
17900 case, treat the nested-name-specifier as optional. */
17901 else
17902 qscope = cp_parser_nested_name_specifier_opt (parser,
17903 /*typename_keyword_p=*/false,
17904 /*check_dependency_p=*/true,
17905 /*type_p=*/false,
17906 /*is_declaration=*/true);
17907 if (!qscope)
17908 qscope = global_namespace;
17909 else if (UNSCOPED_ENUM_P (qscope))
17910 qscope = CP_TYPE_CONTEXT (qscope);
17912 if (access_declaration_p && cp_parser_error_occurred (parser))
17913 /* Something has already gone wrong; there's no need to parse
17914 further. Since an error has occurred, the return value of
17915 cp_parser_parse_definitely will be false, as required. */
17916 return cp_parser_parse_definitely (parser);
17918 token = cp_lexer_peek_token (parser->lexer);
17919 /* Parse the unqualified-id. */
17920 identifier = cp_parser_unqualified_id (parser,
17921 /*template_keyword_p=*/false,
17922 /*check_dependency_p=*/true,
17923 /*declarator_p=*/true,
17924 /*optional_p=*/false);
17926 if (access_declaration_p)
17928 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17929 cp_parser_simulate_error (parser);
17930 if (!cp_parser_parse_definitely (parser))
17931 return false;
17934 /* The function we call to handle a using-declaration is different
17935 depending on what scope we are in. */
17936 if (qscope == error_mark_node || identifier == error_mark_node)
17938 else if (!identifier_p (identifier)
17939 && TREE_CODE (identifier) != BIT_NOT_EXPR)
17940 /* [namespace.udecl]
17942 A using declaration shall not name a template-id. */
17943 error_at (token->location,
17944 "a template-id may not appear in a using-declaration");
17945 else
17947 if (at_class_scope_p ())
17949 /* Create the USING_DECL. */
17950 decl = do_class_using_decl (parser->scope, identifier);
17952 if (decl && typename_p)
17953 USING_DECL_TYPENAME_P (decl) = 1;
17955 if (check_for_bare_parameter_packs (decl))
17957 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17958 return false;
17960 else
17961 /* Add it to the list of members in this class. */
17962 finish_member_declaration (decl);
17964 else
17966 decl = cp_parser_lookup_name_simple (parser,
17967 identifier,
17968 token->location);
17969 if (decl == error_mark_node)
17970 cp_parser_name_lookup_error (parser, identifier,
17971 decl, NLE_NULL,
17972 token->location);
17973 else if (check_for_bare_parameter_packs (decl))
17975 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17976 return false;
17978 else if (!at_namespace_scope_p ())
17979 do_local_using_decl (decl, qscope, identifier);
17980 else
17981 do_toplevel_using_decl (decl, qscope, identifier);
17985 /* Look for the final `;'. */
17986 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17988 if (access_declaration_p && errorcount == oldcount)
17989 warning_at (diag_token->location, OPT_Wdeprecated,
17990 "access declarations are deprecated "
17991 "in favour of using-declarations; "
17992 "suggestion: add the %<using%> keyword");
17994 return true;
17997 /* Parse an alias-declaration.
17999 alias-declaration:
18000 using identifier attribute-specifier-seq [opt] = type-id */
18002 static tree
18003 cp_parser_alias_declaration (cp_parser* parser)
18005 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18006 location_t id_location;
18007 cp_declarator *declarator;
18008 cp_decl_specifier_seq decl_specs;
18009 bool member_p;
18010 const char *saved_message = NULL;
18012 /* Look for the `using' keyword. */
18013 cp_token *using_token
18014 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18015 if (using_token == NULL)
18016 return error_mark_node;
18018 id_location = cp_lexer_peek_token (parser->lexer)->location;
18019 id = cp_parser_identifier (parser);
18020 if (id == error_mark_node)
18021 return error_mark_node;
18023 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18024 attributes = cp_parser_attributes_opt (parser);
18025 if (attributes == error_mark_node)
18026 return error_mark_node;
18028 cp_parser_require (parser, CPP_EQ, RT_EQ);
18030 if (cp_parser_error_occurred (parser))
18031 return error_mark_node;
18033 cp_parser_commit_to_tentative_parse (parser);
18035 /* Now we are going to parse the type-id of the declaration. */
18038 [dcl.type]/3 says:
18040 "A type-specifier-seq shall not define a class or enumeration
18041 unless it appears in the type-id of an alias-declaration (7.1.3) that
18042 is not the declaration of a template-declaration."
18044 In other words, if we currently are in an alias template, the
18045 type-id should not define a type.
18047 So let's set parser->type_definition_forbidden_message in that
18048 case; cp_parser_check_type_definition (called by
18049 cp_parser_class_specifier) will then emit an error if a type is
18050 defined in the type-id. */
18051 if (parser->num_template_parameter_lists)
18053 saved_message = parser->type_definition_forbidden_message;
18054 parser->type_definition_forbidden_message =
18055 G_("types may not be defined in alias template declarations");
18058 type = cp_parser_type_id (parser);
18060 /* Restore the error message if need be. */
18061 if (parser->num_template_parameter_lists)
18062 parser->type_definition_forbidden_message = saved_message;
18064 if (type == error_mark_node
18065 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18067 cp_parser_skip_to_end_of_block_or_statement (parser);
18068 return error_mark_node;
18071 /* A typedef-name can also be introduced by an alias-declaration. The
18072 identifier following the using keyword becomes a typedef-name. It has
18073 the same semantics as if it were introduced by the typedef
18074 specifier. In particular, it does not define a new type and it shall
18075 not appear in the type-id. */
18077 clear_decl_specs (&decl_specs);
18078 decl_specs.type = type;
18079 if (attributes != NULL_TREE)
18081 decl_specs.attributes = attributes;
18082 set_and_check_decl_spec_loc (&decl_specs,
18083 ds_attribute,
18084 attrs_token);
18086 set_and_check_decl_spec_loc (&decl_specs,
18087 ds_typedef,
18088 using_token);
18089 set_and_check_decl_spec_loc (&decl_specs,
18090 ds_alias,
18091 using_token);
18093 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18094 declarator->id_loc = id_location;
18096 member_p = at_class_scope_p ();
18097 if (member_p)
18098 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18099 NULL_TREE, attributes);
18100 else
18101 decl = start_decl (declarator, &decl_specs, 0,
18102 attributes, NULL_TREE, &pushed_scope);
18103 if (decl == error_mark_node)
18104 return decl;
18106 // Attach constraints to the alias declaration.
18107 if (flag_concepts && current_template_parms)
18109 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18110 tree constr = build_constraints (reqs, NULL_TREE);
18111 set_constraints (decl, constr);
18114 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18116 if (pushed_scope)
18117 pop_scope (pushed_scope);
18119 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18120 added into the symbol table; otherwise, return the TYPE_DECL. */
18121 if (DECL_LANG_SPECIFIC (decl)
18122 && DECL_TEMPLATE_INFO (decl)
18123 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18125 decl = DECL_TI_TEMPLATE (decl);
18126 if (member_p)
18127 check_member_template (decl);
18130 return decl;
18133 /* Parse a using-directive.
18135 using-directive:
18136 using namespace :: [opt] nested-name-specifier [opt]
18137 namespace-name ; */
18139 static void
18140 cp_parser_using_directive (cp_parser* parser)
18142 tree namespace_decl;
18143 tree attribs;
18145 /* Look for the `using' keyword. */
18146 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18147 /* And the `namespace' keyword. */
18148 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18149 /* Look for the optional `::' operator. */
18150 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18151 /* And the optional nested-name-specifier. */
18152 cp_parser_nested_name_specifier_opt (parser,
18153 /*typename_keyword_p=*/false,
18154 /*check_dependency_p=*/true,
18155 /*type_p=*/false,
18156 /*is_declaration=*/true);
18157 /* Get the namespace being used. */
18158 namespace_decl = cp_parser_namespace_name (parser);
18159 /* And any specified attributes. */
18160 attribs = cp_parser_attributes_opt (parser);
18161 /* Update the symbol table. */
18162 parse_using_directive (namespace_decl, attribs);
18163 /* Look for the final `;'. */
18164 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18167 /* Parse an asm-definition.
18169 asm-definition:
18170 asm ( string-literal ) ;
18172 GNU Extension:
18174 asm-definition:
18175 asm volatile [opt] ( string-literal ) ;
18176 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18177 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18178 : asm-operand-list [opt] ) ;
18179 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18180 : asm-operand-list [opt]
18181 : asm-clobber-list [opt] ) ;
18182 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18183 : asm-clobber-list [opt]
18184 : asm-goto-list ) ; */
18186 static void
18187 cp_parser_asm_definition (cp_parser* parser)
18189 tree string;
18190 tree outputs = NULL_TREE;
18191 tree inputs = NULL_TREE;
18192 tree clobbers = NULL_TREE;
18193 tree labels = NULL_TREE;
18194 tree asm_stmt;
18195 bool volatile_p = false;
18196 bool extended_p = false;
18197 bool invalid_inputs_p = false;
18198 bool invalid_outputs_p = false;
18199 bool goto_p = false;
18200 required_token missing = RT_NONE;
18202 /* Look for the `asm' keyword. */
18203 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18205 if (parser->in_function_body
18206 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18208 error ("%<asm%> in %<constexpr%> function");
18209 cp_function_chain->invalid_constexpr = true;
18212 /* See if the next token is `volatile'. */
18213 if (cp_parser_allow_gnu_extensions_p (parser)
18214 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18216 /* Remember that we saw the `volatile' keyword. */
18217 volatile_p = true;
18218 /* Consume the token. */
18219 cp_lexer_consume_token (parser->lexer);
18221 if (cp_parser_allow_gnu_extensions_p (parser)
18222 && parser->in_function_body
18223 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18225 /* Remember that we saw the `goto' keyword. */
18226 goto_p = true;
18227 /* Consume the token. */
18228 cp_lexer_consume_token (parser->lexer);
18230 /* Look for the opening `('. */
18231 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18232 return;
18233 /* Look for the string. */
18234 string = cp_parser_string_literal (parser, false, false);
18235 if (string == error_mark_node)
18237 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18238 /*consume_paren=*/true);
18239 return;
18242 /* If we're allowing GNU extensions, check for the extended assembly
18243 syntax. Unfortunately, the `:' tokens need not be separated by
18244 a space in C, and so, for compatibility, we tolerate that here
18245 too. Doing that means that we have to treat the `::' operator as
18246 two `:' tokens. */
18247 if (cp_parser_allow_gnu_extensions_p (parser)
18248 && parser->in_function_body
18249 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18250 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18252 bool inputs_p = false;
18253 bool clobbers_p = false;
18254 bool labels_p = false;
18256 /* The extended syntax was used. */
18257 extended_p = true;
18259 /* Look for outputs. */
18260 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18262 /* Consume the `:'. */
18263 cp_lexer_consume_token (parser->lexer);
18264 /* Parse the output-operands. */
18265 if (cp_lexer_next_token_is_not (parser->lexer,
18266 CPP_COLON)
18267 && cp_lexer_next_token_is_not (parser->lexer,
18268 CPP_SCOPE)
18269 && cp_lexer_next_token_is_not (parser->lexer,
18270 CPP_CLOSE_PAREN)
18271 && !goto_p)
18273 outputs = cp_parser_asm_operand_list (parser);
18274 if (outputs == error_mark_node)
18275 invalid_outputs_p = true;
18278 /* If the next token is `::', there are no outputs, and the
18279 next token is the beginning of the inputs. */
18280 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18281 /* The inputs are coming next. */
18282 inputs_p = true;
18284 /* Look for inputs. */
18285 if (inputs_p
18286 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18288 /* Consume the `:' or `::'. */
18289 cp_lexer_consume_token (parser->lexer);
18290 /* Parse the output-operands. */
18291 if (cp_lexer_next_token_is_not (parser->lexer,
18292 CPP_COLON)
18293 && cp_lexer_next_token_is_not (parser->lexer,
18294 CPP_SCOPE)
18295 && cp_lexer_next_token_is_not (parser->lexer,
18296 CPP_CLOSE_PAREN))
18298 inputs = cp_parser_asm_operand_list (parser);
18299 if (inputs == error_mark_node)
18300 invalid_inputs_p = true;
18303 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18304 /* The clobbers are coming next. */
18305 clobbers_p = true;
18307 /* Look for clobbers. */
18308 if (clobbers_p
18309 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18311 clobbers_p = true;
18312 /* Consume the `:' or `::'. */
18313 cp_lexer_consume_token (parser->lexer);
18314 /* Parse the clobbers. */
18315 if (cp_lexer_next_token_is_not (parser->lexer,
18316 CPP_COLON)
18317 && cp_lexer_next_token_is_not (parser->lexer,
18318 CPP_CLOSE_PAREN))
18319 clobbers = cp_parser_asm_clobber_list (parser);
18321 else if (goto_p
18322 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18323 /* The labels are coming next. */
18324 labels_p = true;
18326 /* Look for labels. */
18327 if (labels_p
18328 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
18330 labels_p = true;
18331 /* Consume the `:' or `::'. */
18332 cp_lexer_consume_token (parser->lexer);
18333 /* Parse the labels. */
18334 labels = cp_parser_asm_label_list (parser);
18337 if (goto_p && !labels_p)
18338 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
18340 else if (goto_p)
18341 missing = RT_COLON_SCOPE;
18343 /* Look for the closing `)'. */
18344 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
18345 missing ? missing : RT_CLOSE_PAREN))
18346 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18347 /*consume_paren=*/true);
18348 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18350 if (!invalid_inputs_p && !invalid_outputs_p)
18352 /* Create the ASM_EXPR. */
18353 if (parser->in_function_body)
18355 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
18356 inputs, clobbers, labels);
18357 /* If the extended syntax was not used, mark the ASM_EXPR. */
18358 if (!extended_p)
18360 tree temp = asm_stmt;
18361 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
18362 temp = TREE_OPERAND (temp, 0);
18364 ASM_INPUT_P (temp) = 1;
18367 else
18368 symtab->finalize_toplevel_asm (string);
18372 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
18373 type that comes from the decl-specifier-seq. */
18375 static tree
18376 strip_declarator_types (tree type, cp_declarator *declarator)
18378 for (cp_declarator *d = declarator; d;)
18379 switch (d->kind)
18381 case cdk_id:
18382 case cdk_error:
18383 d = NULL;
18384 break;
18386 default:
18387 if (TYPE_PTRMEMFUNC_P (type))
18388 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
18389 type = TREE_TYPE (type);
18390 d = d->declarator;
18391 break;
18394 return type;
18397 /* Declarators [gram.dcl.decl] */
18399 /* Parse an init-declarator.
18401 init-declarator:
18402 declarator initializer [opt]
18404 GNU Extension:
18406 init-declarator:
18407 declarator asm-specification [opt] attributes [opt] initializer [opt]
18409 function-definition:
18410 decl-specifier-seq [opt] declarator ctor-initializer [opt]
18411 function-body
18412 decl-specifier-seq [opt] declarator function-try-block
18414 GNU Extension:
18416 function-definition:
18417 __extension__ function-definition
18419 TM Extension:
18421 function-definition:
18422 decl-specifier-seq [opt] declarator function-transaction-block
18424 The DECL_SPECIFIERS apply to this declarator. Returns a
18425 representation of the entity declared. If MEMBER_P is TRUE, then
18426 this declarator appears in a class scope. The new DECL created by
18427 this declarator is returned.
18429 The CHECKS are access checks that should be performed once we know
18430 what entity is being declared (and, therefore, what classes have
18431 befriended it).
18433 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18434 for a function-definition here as well. If the declarator is a
18435 declarator for a function-definition, *FUNCTION_DEFINITION_P will
18436 be TRUE upon return. By that point, the function-definition will
18437 have been completely parsed.
18439 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18440 is FALSE.
18442 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18443 parsed declaration if it is an uninitialized single declarator not followed
18444 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18445 if present, will not be consumed. If returned, this declarator will be
18446 created with SD_INITIALIZED but will not call cp_finish_decl.
18448 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
18449 and there is an initializer, the pointed location_t is set to the
18450 location of the '=' or `(', or '{' in C++11 token introducing the
18451 initializer. */
18453 static tree
18454 cp_parser_init_declarator (cp_parser* parser,
18455 cp_decl_specifier_seq *decl_specifiers,
18456 vec<deferred_access_check, va_gc> *checks,
18457 bool function_definition_allowed_p,
18458 bool member_p,
18459 int declares_class_or_enum,
18460 bool* function_definition_p,
18461 tree* maybe_range_for_decl,
18462 location_t* init_loc,
18463 tree* auto_result)
18465 cp_token *token = NULL, *asm_spec_start_token = NULL,
18466 *attributes_start_token = NULL;
18467 cp_declarator *declarator;
18468 tree prefix_attributes;
18469 tree attributes = NULL;
18470 tree asm_specification;
18471 tree initializer;
18472 tree decl = NULL_TREE;
18473 tree scope;
18474 int is_initialized;
18475 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
18476 initialized with "= ..", CPP_OPEN_PAREN if initialized with
18477 "(...)". */
18478 enum cpp_ttype initialization_kind;
18479 bool is_direct_init = false;
18480 bool is_non_constant_init;
18481 int ctor_dtor_or_conv_p;
18482 bool friend_p = cp_parser_friend_p (decl_specifiers);
18483 tree pushed_scope = NULL_TREE;
18484 bool range_for_decl_p = false;
18485 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18486 location_t tmp_init_loc = UNKNOWN_LOCATION;
18488 /* Gather the attributes that were provided with the
18489 decl-specifiers. */
18490 prefix_attributes = decl_specifiers->attributes;
18492 /* Assume that this is not the declarator for a function
18493 definition. */
18494 if (function_definition_p)
18495 *function_definition_p = false;
18497 /* Default arguments are only permitted for function parameters. */
18498 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
18499 parser->default_arg_ok_p = false;
18501 /* Defer access checks while parsing the declarator; we cannot know
18502 what names are accessible until we know what is being
18503 declared. */
18504 resume_deferring_access_checks ();
18506 /* Parse the declarator. */
18507 token = cp_lexer_peek_token (parser->lexer);
18508 declarator
18509 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18510 &ctor_dtor_or_conv_p,
18511 /*parenthesized_p=*/NULL,
18512 member_p, friend_p);
18513 /* Gather up the deferred checks. */
18514 stop_deferring_access_checks ();
18516 parser->default_arg_ok_p = saved_default_arg_ok_p;
18518 /* If the DECLARATOR was erroneous, there's no need to go
18519 further. */
18520 if (declarator == cp_error_declarator)
18521 return error_mark_node;
18523 /* Check that the number of template-parameter-lists is OK. */
18524 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
18525 token->location))
18526 return error_mark_node;
18528 if (declares_class_or_enum & 2)
18529 cp_parser_check_for_definition_in_return_type (declarator,
18530 decl_specifiers->type,
18531 decl_specifiers->locations[ds_type_spec]);
18533 /* Figure out what scope the entity declared by the DECLARATOR is
18534 located in. `grokdeclarator' sometimes changes the scope, so
18535 we compute it now. */
18536 scope = get_scope_of_declarator (declarator);
18538 /* Perform any lookups in the declared type which were thought to be
18539 dependent, but are not in the scope of the declarator. */
18540 decl_specifiers->type
18541 = maybe_update_decl_type (decl_specifiers->type, scope);
18543 /* If we're allowing GNU extensions, look for an
18544 asm-specification. */
18545 if (cp_parser_allow_gnu_extensions_p (parser))
18547 /* Look for an asm-specification. */
18548 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
18549 asm_specification = cp_parser_asm_specification_opt (parser);
18551 else
18552 asm_specification = NULL_TREE;
18554 /* Look for attributes. */
18555 attributes_start_token = cp_lexer_peek_token (parser->lexer);
18556 attributes = cp_parser_attributes_opt (parser);
18558 /* Peek at the next token. */
18559 token = cp_lexer_peek_token (parser->lexer);
18561 bool bogus_implicit_tmpl = false;
18563 if (function_declarator_p (declarator))
18565 /* Check to see if the token indicates the start of a
18566 function-definition. */
18567 if (cp_parser_token_starts_function_definition_p (token))
18569 if (!function_definition_allowed_p)
18571 /* If a function-definition should not appear here, issue an
18572 error message. */
18573 cp_parser_error (parser,
18574 "a function-definition is not allowed here");
18575 return error_mark_node;
18578 location_t func_brace_location
18579 = cp_lexer_peek_token (parser->lexer)->location;
18581 /* Neither attributes nor an asm-specification are allowed
18582 on a function-definition. */
18583 if (asm_specification)
18584 error_at (asm_spec_start_token->location,
18585 "an asm-specification is not allowed "
18586 "on a function-definition");
18587 if (attributes)
18588 error_at (attributes_start_token->location,
18589 "attributes are not allowed "
18590 "on a function-definition");
18591 /* This is a function-definition. */
18592 *function_definition_p = true;
18594 /* Parse the function definition. */
18595 if (member_p)
18596 decl = cp_parser_save_member_function_body (parser,
18597 decl_specifiers,
18598 declarator,
18599 prefix_attributes);
18600 else
18601 decl =
18602 (cp_parser_function_definition_from_specifiers_and_declarator
18603 (parser, decl_specifiers, prefix_attributes, declarator));
18605 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
18607 /* This is where the prologue starts... */
18608 DECL_STRUCT_FUNCTION (decl)->function_start_locus
18609 = func_brace_location;
18612 return decl;
18615 else if (parser->fully_implicit_function_template_p)
18617 /* A non-template declaration involving a function parameter list
18618 containing an implicit template parameter will be made into a
18619 template. If the resulting declaration is not going to be an
18620 actual function then finish the template scope here to prevent it.
18621 An error message will be issued once we have a decl to talk about.
18623 FIXME probably we should do type deduction rather than create an
18624 implicit template, but the standard currently doesn't allow it. */
18625 bogus_implicit_tmpl = true;
18626 finish_fully_implicit_template (parser, NULL_TREE);
18629 /* [dcl.dcl]
18631 Only in function declarations for constructors, destructors, and
18632 type conversions can the decl-specifier-seq be omitted.
18634 We explicitly postpone this check past the point where we handle
18635 function-definitions because we tolerate function-definitions
18636 that are missing their return types in some modes. */
18637 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
18639 cp_parser_error (parser,
18640 "expected constructor, destructor, or type conversion");
18641 return error_mark_node;
18644 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
18645 if (token->type == CPP_EQ
18646 || token->type == CPP_OPEN_PAREN
18647 || token->type == CPP_OPEN_BRACE)
18649 is_initialized = SD_INITIALIZED;
18650 initialization_kind = token->type;
18651 if (maybe_range_for_decl)
18652 *maybe_range_for_decl = error_mark_node;
18653 tmp_init_loc = token->location;
18654 if (init_loc && *init_loc == UNKNOWN_LOCATION)
18655 *init_loc = tmp_init_loc;
18657 if (token->type == CPP_EQ
18658 && function_declarator_p (declarator))
18660 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
18661 if (t2->keyword == RID_DEFAULT)
18662 is_initialized = SD_DEFAULTED;
18663 else if (t2->keyword == RID_DELETE)
18664 is_initialized = SD_DELETED;
18667 else
18669 /* If the init-declarator isn't initialized and isn't followed by a
18670 `,' or `;', it's not a valid init-declarator. */
18671 if (token->type != CPP_COMMA
18672 && token->type != CPP_SEMICOLON)
18674 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
18675 range_for_decl_p = true;
18676 else
18678 if (!maybe_range_for_decl)
18679 cp_parser_error (parser, "expected initializer");
18680 return error_mark_node;
18683 is_initialized = SD_UNINITIALIZED;
18684 initialization_kind = CPP_EOF;
18687 /* Because start_decl has side-effects, we should only call it if we
18688 know we're going ahead. By this point, we know that we cannot
18689 possibly be looking at any other construct. */
18690 cp_parser_commit_to_tentative_parse (parser);
18692 /* Enter the newly declared entry in the symbol table. If we're
18693 processing a declaration in a class-specifier, we wait until
18694 after processing the initializer. */
18695 if (!member_p)
18697 if (parser->in_unbraced_linkage_specification_p)
18698 decl_specifiers->storage_class = sc_extern;
18699 decl = start_decl (declarator, decl_specifiers,
18700 range_for_decl_p? SD_INITIALIZED : is_initialized,
18701 attributes, prefix_attributes, &pushed_scope);
18702 cp_finalize_omp_declare_simd (parser, decl);
18703 cp_finalize_oacc_routine (parser, decl, false);
18704 /* Adjust location of decl if declarator->id_loc is more appropriate:
18705 set, and decl wasn't merged with another decl, in which case its
18706 location would be different from input_location, and more accurate. */
18707 if (DECL_P (decl)
18708 && declarator->id_loc != UNKNOWN_LOCATION
18709 && DECL_SOURCE_LOCATION (decl) == input_location)
18710 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
18712 else if (scope)
18713 /* Enter the SCOPE. That way unqualified names appearing in the
18714 initializer will be looked up in SCOPE. */
18715 pushed_scope = push_scope (scope);
18717 /* Perform deferred access control checks, now that we know in which
18718 SCOPE the declared entity resides. */
18719 if (!member_p && decl)
18721 tree saved_current_function_decl = NULL_TREE;
18723 /* If the entity being declared is a function, pretend that we
18724 are in its scope. If it is a `friend', it may have access to
18725 things that would not otherwise be accessible. */
18726 if (TREE_CODE (decl) == FUNCTION_DECL)
18728 saved_current_function_decl = current_function_decl;
18729 current_function_decl = decl;
18732 /* Perform access checks for template parameters. */
18733 cp_parser_perform_template_parameter_access_checks (checks);
18735 /* Perform the access control checks for the declarator and the
18736 decl-specifiers. */
18737 perform_deferred_access_checks (tf_warning_or_error);
18739 /* Restore the saved value. */
18740 if (TREE_CODE (decl) == FUNCTION_DECL)
18741 current_function_decl = saved_current_function_decl;
18744 /* Parse the initializer. */
18745 initializer = NULL_TREE;
18746 is_direct_init = false;
18747 is_non_constant_init = true;
18748 if (is_initialized)
18750 if (function_declarator_p (declarator))
18752 if (initialization_kind == CPP_EQ)
18753 initializer = cp_parser_pure_specifier (parser);
18754 else
18756 /* If the declaration was erroneous, we don't really
18757 know what the user intended, so just silently
18758 consume the initializer. */
18759 if (decl != error_mark_node)
18760 error_at (tmp_init_loc, "initializer provided for function");
18761 cp_parser_skip_to_closing_parenthesis (parser,
18762 /*recovering=*/true,
18763 /*or_comma=*/false,
18764 /*consume_paren=*/true);
18767 else
18769 /* We want to record the extra mangling scope for in-class
18770 initializers of class members and initializers of static data
18771 member templates. The former involves deferring
18772 parsing of the initializer until end of class as with default
18773 arguments. So right here we only handle the latter. */
18774 if (!member_p && processing_template_decl)
18775 start_lambda_scope (decl);
18776 initializer = cp_parser_initializer (parser,
18777 &is_direct_init,
18778 &is_non_constant_init);
18779 if (!member_p && processing_template_decl)
18780 finish_lambda_scope ();
18781 if (initializer == error_mark_node)
18782 cp_parser_skip_to_end_of_statement (parser);
18786 /* The old parser allows attributes to appear after a parenthesized
18787 initializer. Mark Mitchell proposed removing this functionality
18788 on the GCC mailing lists on 2002-08-13. This parser accepts the
18789 attributes -- but ignores them. */
18790 if (cp_parser_allow_gnu_extensions_p (parser)
18791 && initialization_kind == CPP_OPEN_PAREN)
18792 if (cp_parser_attributes_opt (parser))
18793 warning (OPT_Wattributes,
18794 "attributes after parenthesized initializer ignored");
18796 /* And now complain about a non-function implicit template. */
18797 if (bogus_implicit_tmpl && decl != error_mark_node)
18798 error_at (DECL_SOURCE_LOCATION (decl),
18799 "non-function %qD declared as implicit template", decl);
18801 /* For an in-class declaration, use `grokfield' to create the
18802 declaration. */
18803 if (member_p)
18805 if (pushed_scope)
18807 pop_scope (pushed_scope);
18808 pushed_scope = NULL_TREE;
18810 decl = grokfield (declarator, decl_specifiers,
18811 initializer, !is_non_constant_init,
18812 /*asmspec=*/NULL_TREE,
18813 chainon (attributes, prefix_attributes));
18814 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
18815 cp_parser_save_default_args (parser, decl);
18816 cp_finalize_omp_declare_simd (parser, decl);
18817 cp_finalize_oacc_routine (parser, decl, false);
18820 /* Finish processing the declaration. But, skip member
18821 declarations. */
18822 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
18824 cp_finish_decl (decl,
18825 initializer, !is_non_constant_init,
18826 asm_specification,
18827 /* If the initializer is in parentheses, then this is
18828 a direct-initialization, which means that an
18829 `explicit' constructor is OK. Otherwise, an
18830 `explicit' constructor cannot be used. */
18831 ((is_direct_init || !is_initialized)
18832 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
18834 else if ((cxx_dialect != cxx98) && friend_p
18835 && decl && TREE_CODE (decl) == FUNCTION_DECL)
18836 /* Core issue #226 (C++0x only): A default template-argument
18837 shall not be specified in a friend class template
18838 declaration. */
18839 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
18840 /*is_partial=*/false, /*is_friend_decl=*/1);
18842 if (!friend_p && pushed_scope)
18843 pop_scope (pushed_scope);
18845 if (function_declarator_p (declarator)
18846 && parser->fully_implicit_function_template_p)
18848 if (member_p)
18849 decl = finish_fully_implicit_template (parser, decl);
18850 else
18851 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
18854 if (auto_result && is_initialized && decl_specifiers->type
18855 && type_uses_auto (decl_specifiers->type))
18856 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
18858 return decl;
18861 /* Parse a declarator.
18863 declarator:
18864 direct-declarator
18865 ptr-operator declarator
18867 abstract-declarator:
18868 ptr-operator abstract-declarator [opt]
18869 direct-abstract-declarator
18871 GNU Extensions:
18873 declarator:
18874 attributes [opt] direct-declarator
18875 attributes [opt] ptr-operator declarator
18877 abstract-declarator:
18878 attributes [opt] ptr-operator abstract-declarator [opt]
18879 attributes [opt] direct-abstract-declarator
18881 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
18882 detect constructor, destructor or conversion operators. It is set
18883 to -1 if the declarator is a name, and +1 if it is a
18884 function. Otherwise it is set to zero. Usually you just want to
18885 test for >0, but internally the negative value is used.
18887 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
18888 a decl-specifier-seq unless it declares a constructor, destructor,
18889 or conversion. It might seem that we could check this condition in
18890 semantic analysis, rather than parsing, but that makes it difficult
18891 to handle something like `f()'. We want to notice that there are
18892 no decl-specifiers, and therefore realize that this is an
18893 expression, not a declaration.)
18895 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
18896 the declarator is a direct-declarator of the form "(...)".
18898 MEMBER_P is true iff this declarator is a member-declarator.
18900 FRIEND_P is true iff this declarator is a friend. */
18902 static cp_declarator *
18903 cp_parser_declarator (cp_parser* parser,
18904 cp_parser_declarator_kind dcl_kind,
18905 int* ctor_dtor_or_conv_p,
18906 bool* parenthesized_p,
18907 bool member_p, bool friend_p)
18909 cp_declarator *declarator;
18910 enum tree_code code;
18911 cp_cv_quals cv_quals;
18912 tree class_type;
18913 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
18915 /* Assume this is not a constructor, destructor, or type-conversion
18916 operator. */
18917 if (ctor_dtor_or_conv_p)
18918 *ctor_dtor_or_conv_p = 0;
18920 if (cp_parser_allow_gnu_extensions_p (parser))
18921 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
18923 /* Check for the ptr-operator production. */
18924 cp_parser_parse_tentatively (parser);
18925 /* Parse the ptr-operator. */
18926 code = cp_parser_ptr_operator (parser,
18927 &class_type,
18928 &cv_quals,
18929 &std_attributes);
18931 /* If that worked, then we have a ptr-operator. */
18932 if (cp_parser_parse_definitely (parser))
18934 /* If a ptr-operator was found, then this declarator was not
18935 parenthesized. */
18936 if (parenthesized_p)
18937 *parenthesized_p = true;
18938 /* The dependent declarator is optional if we are parsing an
18939 abstract-declarator. */
18940 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18941 cp_parser_parse_tentatively (parser);
18943 /* Parse the dependent declarator. */
18944 declarator = cp_parser_declarator (parser, dcl_kind,
18945 /*ctor_dtor_or_conv_p=*/NULL,
18946 /*parenthesized_p=*/NULL,
18947 /*member_p=*/false,
18948 friend_p);
18950 /* If we are parsing an abstract-declarator, we must handle the
18951 case where the dependent declarator is absent. */
18952 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
18953 && !cp_parser_parse_definitely (parser))
18954 declarator = NULL;
18956 declarator = cp_parser_make_indirect_declarator
18957 (code, class_type, cv_quals, declarator, std_attributes);
18959 /* Everything else is a direct-declarator. */
18960 else
18962 if (parenthesized_p)
18963 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
18964 CPP_OPEN_PAREN);
18965 declarator = cp_parser_direct_declarator (parser, dcl_kind,
18966 ctor_dtor_or_conv_p,
18967 member_p, friend_p);
18970 if (gnu_attributes && declarator && declarator != cp_error_declarator)
18971 declarator->attributes = gnu_attributes;
18972 return declarator;
18975 /* Parse a direct-declarator or direct-abstract-declarator.
18977 direct-declarator:
18978 declarator-id
18979 direct-declarator ( parameter-declaration-clause )
18980 cv-qualifier-seq [opt]
18981 ref-qualifier [opt]
18982 exception-specification [opt]
18983 direct-declarator [ constant-expression [opt] ]
18984 ( declarator )
18986 direct-abstract-declarator:
18987 direct-abstract-declarator [opt]
18988 ( parameter-declaration-clause )
18989 cv-qualifier-seq [opt]
18990 ref-qualifier [opt]
18991 exception-specification [opt]
18992 direct-abstract-declarator [opt] [ constant-expression [opt] ]
18993 ( abstract-declarator )
18995 Returns a representation of the declarator. DCL_KIND is
18996 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
18997 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
18998 we are parsing a direct-declarator. It is
18999 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19000 of ambiguity we prefer an abstract declarator, as per
19001 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19002 as for cp_parser_declarator. */
19004 static cp_declarator *
19005 cp_parser_direct_declarator (cp_parser* parser,
19006 cp_parser_declarator_kind dcl_kind,
19007 int* ctor_dtor_or_conv_p,
19008 bool member_p, bool friend_p)
19010 cp_token *token;
19011 cp_declarator *declarator = NULL;
19012 tree scope = NULL_TREE;
19013 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19014 bool saved_in_declarator_p = parser->in_declarator_p;
19015 bool first = true;
19016 tree pushed_scope = NULL_TREE;
19018 while (true)
19020 /* Peek at the next token. */
19021 token = cp_lexer_peek_token (parser->lexer);
19022 if (token->type == CPP_OPEN_PAREN)
19024 /* This is either a parameter-declaration-clause, or a
19025 parenthesized declarator. When we know we are parsing a
19026 named declarator, it must be a parenthesized declarator
19027 if FIRST is true. For instance, `(int)' is a
19028 parameter-declaration-clause, with an omitted
19029 direct-abstract-declarator. But `((*))', is a
19030 parenthesized abstract declarator. Finally, when T is a
19031 template parameter `(T)' is a
19032 parameter-declaration-clause, and not a parenthesized
19033 named declarator.
19035 We first try and parse a parameter-declaration-clause,
19036 and then try a nested declarator (if FIRST is true).
19038 It is not an error for it not to be a
19039 parameter-declaration-clause, even when FIRST is
19040 false. Consider,
19042 int i (int);
19043 int i (3);
19045 The first is the declaration of a function while the
19046 second is the definition of a variable, including its
19047 initializer.
19049 Having seen only the parenthesis, we cannot know which of
19050 these two alternatives should be selected. Even more
19051 complex are examples like:
19053 int i (int (a));
19054 int i (int (3));
19056 The former is a function-declaration; the latter is a
19057 variable initialization.
19059 Thus again, we try a parameter-declaration-clause, and if
19060 that fails, we back out and return. */
19062 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19064 tree params;
19065 bool is_declarator = false;
19067 /* In a member-declarator, the only valid interpretation
19068 of a parenthesis is the start of a
19069 parameter-declaration-clause. (It is invalid to
19070 initialize a static data member with a parenthesized
19071 initializer; only the "=" form of initialization is
19072 permitted.) */
19073 if (!member_p)
19074 cp_parser_parse_tentatively (parser);
19076 /* Consume the `('. */
19077 cp_lexer_consume_token (parser->lexer);
19078 if (first)
19080 /* If this is going to be an abstract declarator, we're
19081 in a declarator and we can't have default args. */
19082 parser->default_arg_ok_p = false;
19083 parser->in_declarator_p = true;
19086 begin_scope (sk_function_parms, NULL_TREE);
19088 /* Parse the parameter-declaration-clause. */
19089 params = cp_parser_parameter_declaration_clause (parser);
19091 /* Consume the `)'. */
19092 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19094 /* If all went well, parse the cv-qualifier-seq,
19095 ref-qualifier and the exception-specification. */
19096 if (member_p || cp_parser_parse_definitely (parser))
19098 cp_cv_quals cv_quals;
19099 cp_virt_specifiers virt_specifiers;
19100 cp_ref_qualifier ref_qual;
19101 tree exception_specification;
19102 tree late_return;
19103 tree attrs;
19104 bool memfn = (member_p || (pushed_scope
19105 && CLASS_TYPE_P (pushed_scope)));
19107 is_declarator = true;
19109 if (ctor_dtor_or_conv_p)
19110 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19111 first = false;
19113 /* Parse the cv-qualifier-seq. */
19114 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19115 /* Parse the ref-qualifier. */
19116 ref_qual = cp_parser_ref_qualifier_opt (parser);
19117 /* Parse the tx-qualifier. */
19118 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19119 /* And the exception-specification. */
19120 exception_specification
19121 = cp_parser_exception_specification_opt (parser);
19123 attrs = cp_parser_std_attribute_spec_seq (parser);
19125 /* In here, we handle cases where attribute is used after
19126 the function declaration. For example:
19127 void func (int x) __attribute__((vector(..))); */
19128 tree gnu_attrs = NULL_TREE;
19129 if (flag_cilkplus
19130 && cp_next_tokens_can_be_gnu_attribute_p (parser))
19132 cp_parser_parse_tentatively (parser);
19133 tree attr = cp_parser_gnu_attributes_opt (parser);
19134 if (cp_lexer_next_token_is_not (parser->lexer,
19135 CPP_SEMICOLON)
19136 && cp_lexer_next_token_is_not (parser->lexer,
19137 CPP_OPEN_BRACE))
19138 cp_parser_abort_tentative_parse (parser);
19139 else if (!cp_parser_parse_definitely (parser))
19141 else
19142 gnu_attrs = attr;
19144 tree requires_clause = NULL_TREE;
19145 late_return = (cp_parser_late_return_type_opt
19146 (parser, declarator, requires_clause,
19147 memfn ? cv_quals : -1));
19149 /* Parse the virt-specifier-seq. */
19150 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19152 /* Create the function-declarator. */
19153 declarator = make_call_declarator (declarator,
19154 params,
19155 cv_quals,
19156 virt_specifiers,
19157 ref_qual,
19158 tx_qual,
19159 exception_specification,
19160 late_return,
19161 requires_clause);
19162 declarator->std_attributes = attrs;
19163 declarator->attributes = gnu_attrs;
19164 /* Any subsequent parameter lists are to do with
19165 return type, so are not those of the declared
19166 function. */
19167 parser->default_arg_ok_p = false;
19170 /* Remove the function parms from scope. */
19171 pop_bindings_and_leave_scope ();
19173 if (is_declarator)
19174 /* Repeat the main loop. */
19175 continue;
19178 /* If this is the first, we can try a parenthesized
19179 declarator. */
19180 if (first)
19182 bool saved_in_type_id_in_expr_p;
19184 parser->default_arg_ok_p = saved_default_arg_ok_p;
19185 parser->in_declarator_p = saved_in_declarator_p;
19187 /* Consume the `('. */
19188 cp_lexer_consume_token (parser->lexer);
19189 /* Parse the nested declarator. */
19190 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19191 parser->in_type_id_in_expr_p = true;
19192 declarator
19193 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19194 /*parenthesized_p=*/NULL,
19195 member_p, friend_p);
19196 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19197 first = false;
19198 /* Expect a `)'. */
19199 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19200 declarator = cp_error_declarator;
19201 if (declarator == cp_error_declarator)
19202 break;
19204 goto handle_declarator;
19206 /* Otherwise, we must be done. */
19207 else
19208 break;
19210 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19211 && token->type == CPP_OPEN_SQUARE
19212 && !cp_next_tokens_can_be_attribute_p (parser))
19214 /* Parse an array-declarator. */
19215 tree bounds, attrs;
19217 if (ctor_dtor_or_conv_p)
19218 *ctor_dtor_or_conv_p = 0;
19220 first = false;
19221 parser->default_arg_ok_p = false;
19222 parser->in_declarator_p = true;
19223 /* Consume the `['. */
19224 cp_lexer_consume_token (parser->lexer);
19225 /* Peek at the next token. */
19226 token = cp_lexer_peek_token (parser->lexer);
19227 /* If the next token is `]', then there is no
19228 constant-expression. */
19229 if (token->type != CPP_CLOSE_SQUARE)
19231 bool non_constant_p;
19232 bounds
19233 = cp_parser_constant_expression (parser,
19234 /*allow_non_constant=*/true,
19235 &non_constant_p);
19236 if (!non_constant_p)
19237 /* OK */;
19238 else if (error_operand_p (bounds))
19239 /* Already gave an error. */;
19240 else if (!parser->in_function_body
19241 || current_binding_level->kind == sk_function_parms)
19243 /* Normally, the array bound must be an integral constant
19244 expression. However, as an extension, we allow VLAs
19245 in function scopes as long as they aren't part of a
19246 parameter declaration. */
19247 cp_parser_error (parser,
19248 "array bound is not an integer constant");
19249 bounds = error_mark_node;
19251 else if (processing_template_decl
19252 && !type_dependent_expression_p (bounds))
19254 /* Remember this wasn't a constant-expression. */
19255 bounds = build_nop (TREE_TYPE (bounds), bounds);
19256 TREE_SIDE_EFFECTS (bounds) = 1;
19259 else
19260 bounds = NULL_TREE;
19261 /* Look for the closing `]'. */
19262 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19264 declarator = cp_error_declarator;
19265 break;
19268 attrs = cp_parser_std_attribute_spec_seq (parser);
19269 declarator = make_array_declarator (declarator, bounds);
19270 declarator->std_attributes = attrs;
19272 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19275 tree qualifying_scope;
19276 tree unqualified_name;
19277 tree attrs;
19278 special_function_kind sfk;
19279 bool abstract_ok;
19280 bool pack_expansion_p = false;
19281 cp_token *declarator_id_start_token;
19283 /* Parse a declarator-id */
19284 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
19285 if (abstract_ok)
19287 cp_parser_parse_tentatively (parser);
19289 /* If we see an ellipsis, we should be looking at a
19290 parameter pack. */
19291 if (token->type == CPP_ELLIPSIS)
19293 /* Consume the `...' */
19294 cp_lexer_consume_token (parser->lexer);
19296 pack_expansion_p = true;
19300 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
19301 unqualified_name
19302 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
19303 qualifying_scope = parser->scope;
19304 if (abstract_ok)
19306 bool okay = false;
19308 if (!unqualified_name && pack_expansion_p)
19310 /* Check whether an error occurred. */
19311 okay = !cp_parser_error_occurred (parser);
19313 /* We already consumed the ellipsis to mark a
19314 parameter pack, but we have no way to report it,
19315 so abort the tentative parse. We will be exiting
19316 immediately anyway. */
19317 cp_parser_abort_tentative_parse (parser);
19319 else
19320 okay = cp_parser_parse_definitely (parser);
19322 if (!okay)
19323 unqualified_name = error_mark_node;
19324 else if (unqualified_name
19325 && (qualifying_scope
19326 || (!identifier_p (unqualified_name))))
19328 cp_parser_error (parser, "expected unqualified-id");
19329 unqualified_name = error_mark_node;
19333 if (!unqualified_name)
19334 return NULL;
19335 if (unqualified_name == error_mark_node)
19337 declarator = cp_error_declarator;
19338 pack_expansion_p = false;
19339 declarator->parameter_pack_p = false;
19340 break;
19343 attrs = cp_parser_std_attribute_spec_seq (parser);
19345 if (qualifying_scope && at_namespace_scope_p ()
19346 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
19348 /* In the declaration of a member of a template class
19349 outside of the class itself, the SCOPE will sometimes
19350 be a TYPENAME_TYPE. For example, given:
19352 template <typename T>
19353 int S<T>::R::i = 3;
19355 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
19356 this context, we must resolve S<T>::R to an ordinary
19357 type, rather than a typename type.
19359 The reason we normally avoid resolving TYPENAME_TYPEs
19360 is that a specialization of `S' might render
19361 `S<T>::R' not a type. However, if `S' is
19362 specialized, then this `i' will not be used, so there
19363 is no harm in resolving the types here. */
19364 tree type;
19366 /* Resolve the TYPENAME_TYPE. */
19367 type = resolve_typename_type (qualifying_scope,
19368 /*only_current_p=*/false);
19369 /* If that failed, the declarator is invalid. */
19370 if (TREE_CODE (type) == TYPENAME_TYPE)
19372 if (typedef_variant_p (type))
19373 error_at (declarator_id_start_token->location,
19374 "cannot define member of dependent typedef "
19375 "%qT", type);
19376 else
19377 error_at (declarator_id_start_token->location,
19378 "%<%T::%E%> is not a type",
19379 TYPE_CONTEXT (qualifying_scope),
19380 TYPE_IDENTIFIER (qualifying_scope));
19382 qualifying_scope = type;
19385 sfk = sfk_none;
19387 if (unqualified_name)
19389 tree class_type;
19391 if (qualifying_scope
19392 && CLASS_TYPE_P (qualifying_scope))
19393 class_type = qualifying_scope;
19394 else
19395 class_type = current_class_type;
19397 if (TREE_CODE (unqualified_name) == TYPE_DECL)
19399 tree name_type = TREE_TYPE (unqualified_name);
19400 if (class_type && same_type_p (name_type, class_type))
19402 if (qualifying_scope
19403 && CLASSTYPE_USE_TEMPLATE (name_type))
19405 error_at (declarator_id_start_token->location,
19406 "invalid use of constructor as a template");
19407 inform (declarator_id_start_token->location,
19408 "use %<%T::%D%> instead of %<%T::%D%> to "
19409 "name the constructor in a qualified name",
19410 class_type,
19411 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
19412 class_type, name_type);
19413 declarator = cp_error_declarator;
19414 break;
19416 else
19417 unqualified_name = constructor_name (class_type);
19419 else
19421 /* We do not attempt to print the declarator
19422 here because we do not have enough
19423 information about its original syntactic
19424 form. */
19425 cp_parser_error (parser, "invalid declarator");
19426 declarator = cp_error_declarator;
19427 break;
19431 if (class_type)
19433 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
19434 sfk = sfk_destructor;
19435 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
19436 sfk = sfk_conversion;
19437 else if (/* There's no way to declare a constructor
19438 for an unnamed type, even if the type
19439 got a name for linkage purposes. */
19440 !TYPE_WAS_UNNAMED (class_type)
19441 /* Handle correctly (c++/19200):
19443 struct S {
19444 struct T{};
19445 friend void S(T);
19448 and also:
19450 namespace N {
19451 void S();
19454 struct S {
19455 friend void N::S();
19456 }; */
19457 && !(friend_p
19458 && class_type != qualifying_scope)
19459 && constructor_name_p (unqualified_name,
19460 class_type))
19462 unqualified_name = constructor_name (class_type);
19463 sfk = sfk_constructor;
19465 else if (is_overloaded_fn (unqualified_name)
19466 && DECL_CONSTRUCTOR_P (get_first_fn
19467 (unqualified_name)))
19468 sfk = sfk_constructor;
19470 if (ctor_dtor_or_conv_p && sfk != sfk_none)
19471 *ctor_dtor_or_conv_p = -1;
19474 declarator = make_id_declarator (qualifying_scope,
19475 unqualified_name,
19476 sfk);
19477 declarator->std_attributes = attrs;
19478 declarator->id_loc = token->location;
19479 declarator->parameter_pack_p = pack_expansion_p;
19481 if (pack_expansion_p)
19482 maybe_warn_variadic_templates ();
19485 handle_declarator:;
19486 scope = get_scope_of_declarator (declarator);
19487 if (scope)
19489 /* Any names that appear after the declarator-id for a
19490 member are looked up in the containing scope. */
19491 if (at_function_scope_p ())
19493 /* But declarations with qualified-ids can't appear in a
19494 function. */
19495 cp_parser_error (parser, "qualified-id in declaration");
19496 declarator = cp_error_declarator;
19497 break;
19499 pushed_scope = push_scope (scope);
19501 parser->in_declarator_p = true;
19502 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
19503 || (declarator && declarator->kind == cdk_id))
19504 /* Default args are only allowed on function
19505 declarations. */
19506 parser->default_arg_ok_p = saved_default_arg_ok_p;
19507 else
19508 parser->default_arg_ok_p = false;
19510 first = false;
19512 /* We're done. */
19513 else
19514 break;
19517 /* For an abstract declarator, we might wind up with nothing at this
19518 point. That's an error; the declarator is not optional. */
19519 if (!declarator)
19520 cp_parser_error (parser, "expected declarator");
19522 /* If we entered a scope, we must exit it now. */
19523 if (pushed_scope)
19524 pop_scope (pushed_scope);
19526 parser->default_arg_ok_p = saved_default_arg_ok_p;
19527 parser->in_declarator_p = saved_in_declarator_p;
19529 return declarator;
19532 /* Parse a ptr-operator.
19534 ptr-operator:
19535 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19536 * cv-qualifier-seq [opt]
19538 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
19539 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19541 GNU Extension:
19543 ptr-operator:
19544 & cv-qualifier-seq [opt]
19546 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
19547 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
19548 an rvalue reference. In the case of a pointer-to-member, *TYPE is
19549 filled in with the TYPE containing the member. *CV_QUALS is
19550 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
19551 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
19552 Note that the tree codes returned by this function have nothing
19553 to do with the types of trees that will be eventually be created
19554 to represent the pointer or reference type being parsed. They are
19555 just constants with suggestive names. */
19556 static enum tree_code
19557 cp_parser_ptr_operator (cp_parser* parser,
19558 tree* type,
19559 cp_cv_quals *cv_quals,
19560 tree *attributes)
19562 enum tree_code code = ERROR_MARK;
19563 cp_token *token;
19564 tree attrs = NULL_TREE;
19566 /* Assume that it's not a pointer-to-member. */
19567 *type = NULL_TREE;
19568 /* And that there are no cv-qualifiers. */
19569 *cv_quals = TYPE_UNQUALIFIED;
19571 /* Peek at the next token. */
19572 token = cp_lexer_peek_token (parser->lexer);
19574 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
19575 if (token->type == CPP_MULT)
19576 code = INDIRECT_REF;
19577 else if (token->type == CPP_AND)
19578 code = ADDR_EXPR;
19579 else if ((cxx_dialect != cxx98) &&
19580 token->type == CPP_AND_AND) /* C++0x only */
19581 code = NON_LVALUE_EXPR;
19583 if (code != ERROR_MARK)
19585 /* Consume the `*', `&' or `&&'. */
19586 cp_lexer_consume_token (parser->lexer);
19588 /* A `*' can be followed by a cv-qualifier-seq, and so can a
19589 `&', if we are allowing GNU extensions. (The only qualifier
19590 that can legally appear after `&' is `restrict', but that is
19591 enforced during semantic analysis. */
19592 if (code == INDIRECT_REF
19593 || cp_parser_allow_gnu_extensions_p (parser))
19594 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19596 attrs = cp_parser_std_attribute_spec_seq (parser);
19597 if (attributes != NULL)
19598 *attributes = attrs;
19600 else
19602 /* Try the pointer-to-member case. */
19603 cp_parser_parse_tentatively (parser);
19604 /* Look for the optional `::' operator. */
19605 cp_parser_global_scope_opt (parser,
19606 /*current_scope_valid_p=*/false);
19607 /* Look for the nested-name specifier. */
19608 token = cp_lexer_peek_token (parser->lexer);
19609 cp_parser_nested_name_specifier (parser,
19610 /*typename_keyword_p=*/false,
19611 /*check_dependency_p=*/true,
19612 /*type_p=*/false,
19613 /*is_declaration=*/false);
19614 /* If we found it, and the next token is a `*', then we are
19615 indeed looking at a pointer-to-member operator. */
19616 if (!cp_parser_error_occurred (parser)
19617 && cp_parser_require (parser, CPP_MULT, RT_MULT))
19619 /* Indicate that the `*' operator was used. */
19620 code = INDIRECT_REF;
19622 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
19623 error_at (token->location, "%qD is a namespace", parser->scope);
19624 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
19625 error_at (token->location, "cannot form pointer to member of "
19626 "non-class %q#T", parser->scope);
19627 else
19629 /* The type of which the member is a member is given by the
19630 current SCOPE. */
19631 *type = parser->scope;
19632 /* The next name will not be qualified. */
19633 parser->scope = NULL_TREE;
19634 parser->qualifying_scope = NULL_TREE;
19635 parser->object_scope = NULL_TREE;
19636 /* Look for optional c++11 attributes. */
19637 attrs = cp_parser_std_attribute_spec_seq (parser);
19638 if (attributes != NULL)
19639 *attributes = attrs;
19640 /* Look for the optional cv-qualifier-seq. */
19641 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19644 /* If that didn't work we don't have a ptr-operator. */
19645 if (!cp_parser_parse_definitely (parser))
19646 cp_parser_error (parser, "expected ptr-operator");
19649 return code;
19652 /* Parse an (optional) cv-qualifier-seq.
19654 cv-qualifier-seq:
19655 cv-qualifier cv-qualifier-seq [opt]
19657 cv-qualifier:
19658 const
19659 volatile
19661 GNU Extension:
19663 cv-qualifier:
19664 __restrict__
19666 Returns a bitmask representing the cv-qualifiers. */
19668 static cp_cv_quals
19669 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
19671 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
19673 while (true)
19675 cp_token *token;
19676 cp_cv_quals cv_qualifier;
19678 /* Peek at the next token. */
19679 token = cp_lexer_peek_token (parser->lexer);
19680 /* See if it's a cv-qualifier. */
19681 switch (token->keyword)
19683 case RID_CONST:
19684 cv_qualifier = TYPE_QUAL_CONST;
19685 break;
19687 case RID_VOLATILE:
19688 cv_qualifier = TYPE_QUAL_VOLATILE;
19689 break;
19691 case RID_RESTRICT:
19692 cv_qualifier = TYPE_QUAL_RESTRICT;
19693 break;
19695 default:
19696 cv_qualifier = TYPE_UNQUALIFIED;
19697 break;
19700 if (!cv_qualifier)
19701 break;
19703 if (cv_quals & cv_qualifier)
19705 error_at (token->location, "duplicate cv-qualifier");
19706 cp_lexer_purge_token (parser->lexer);
19708 else
19710 cp_lexer_consume_token (parser->lexer);
19711 cv_quals |= cv_qualifier;
19715 return cv_quals;
19718 /* Parse an (optional) ref-qualifier
19720 ref-qualifier:
19724 Returns cp_ref_qualifier representing ref-qualifier. */
19726 static cp_ref_qualifier
19727 cp_parser_ref_qualifier_opt (cp_parser* parser)
19729 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
19731 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
19732 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
19733 return ref_qual;
19735 while (true)
19737 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
19738 cp_token *token = cp_lexer_peek_token (parser->lexer);
19740 switch (token->type)
19742 case CPP_AND:
19743 curr_ref_qual = REF_QUAL_LVALUE;
19744 break;
19746 case CPP_AND_AND:
19747 curr_ref_qual = REF_QUAL_RVALUE;
19748 break;
19750 default:
19751 curr_ref_qual = REF_QUAL_NONE;
19752 break;
19755 if (!curr_ref_qual)
19756 break;
19757 else if (ref_qual)
19759 error_at (token->location, "multiple ref-qualifiers");
19760 cp_lexer_purge_token (parser->lexer);
19762 else
19764 ref_qual = curr_ref_qual;
19765 cp_lexer_consume_token (parser->lexer);
19769 return ref_qual;
19772 /* Parse an optional tx-qualifier.
19774 tx-qualifier:
19775 transaction_safe
19776 transaction_safe_dynamic */
19778 static tree
19779 cp_parser_tx_qualifier_opt (cp_parser *parser)
19781 cp_token *token = cp_lexer_peek_token (parser->lexer);
19782 if (token->type == CPP_NAME)
19784 tree name = token->u.value;
19785 const char *p = IDENTIFIER_POINTER (name);
19786 const int len = strlen ("transaction_safe");
19787 if (!strncmp (p, "transaction_safe", len))
19789 p += len;
19790 if (*p == '\0'
19791 || !strcmp (p, "_dynamic"))
19793 cp_lexer_consume_token (parser->lexer);
19794 if (!flag_tm)
19796 error ("%E requires %<-fgnu-tm%>", name);
19797 return NULL_TREE;
19799 else
19800 return name;
19804 return NULL_TREE;
19807 /* Parse an (optional) virt-specifier-seq.
19809 virt-specifier-seq:
19810 virt-specifier virt-specifier-seq [opt]
19812 virt-specifier:
19813 override
19814 final
19816 Returns a bitmask representing the virt-specifiers. */
19818 static cp_virt_specifiers
19819 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
19821 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19823 while (true)
19825 cp_token *token;
19826 cp_virt_specifiers virt_specifier;
19828 /* Peek at the next token. */
19829 token = cp_lexer_peek_token (parser->lexer);
19830 /* See if it's a virt-specifier-qualifier. */
19831 if (token->type != CPP_NAME)
19832 break;
19833 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
19835 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19836 virt_specifier = VIRT_SPEC_OVERRIDE;
19838 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
19840 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19841 virt_specifier = VIRT_SPEC_FINAL;
19843 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
19845 virt_specifier = VIRT_SPEC_FINAL;
19847 else
19848 break;
19850 if (virt_specifiers & virt_specifier)
19852 error_at (token->location, "duplicate virt-specifier");
19853 cp_lexer_purge_token (parser->lexer);
19855 else
19857 cp_lexer_consume_token (parser->lexer);
19858 virt_specifiers |= virt_specifier;
19861 return virt_specifiers;
19864 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
19865 is in scope even though it isn't real. */
19867 void
19868 inject_this_parameter (tree ctype, cp_cv_quals quals)
19870 tree this_parm;
19872 if (current_class_ptr)
19874 /* We don't clear this between NSDMIs. Is it already what we want? */
19875 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
19876 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
19877 && cp_type_quals (type) == quals)
19878 return;
19881 this_parm = build_this_parm (ctype, quals);
19882 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
19883 current_class_ptr = NULL_TREE;
19884 current_class_ref
19885 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
19886 current_class_ptr = this_parm;
19889 /* Return true iff our current scope is a non-static data member
19890 initializer. */
19892 bool
19893 parsing_nsdmi (void)
19895 /* We recognize NSDMI context by the context-less 'this' pointer set up
19896 by the function above. */
19897 if (current_class_ptr
19898 && TREE_CODE (current_class_ptr) == PARM_DECL
19899 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
19900 return true;
19901 return false;
19904 /* Parse a late-specified return type, if any. This is not a separate
19905 non-terminal, but part of a function declarator, which looks like
19907 -> trailing-type-specifier-seq abstract-declarator(opt)
19909 Returns the type indicated by the type-id.
19911 In addition to this, parse any queued up #pragma omp declare simd
19912 clauses, Cilk Plus SIMD-enabled functions' vector attributes, and
19913 #pragma acc routine clauses.
19915 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
19916 function. */
19918 static tree
19919 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
19920 tree& requires_clause, cp_cv_quals quals)
19922 cp_token *token;
19923 tree type = NULL_TREE;
19924 bool declare_simd_p = (parser->omp_declare_simd
19925 && declarator
19926 && declarator->kind == cdk_id);
19928 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
19929 && declarator && declarator->kind == cdk_id);
19931 bool oacc_routine_p = (parser->oacc_routine
19932 && declarator
19933 && declarator->kind == cdk_id);
19935 /* Peek at the next token. */
19936 token = cp_lexer_peek_token (parser->lexer);
19937 /* A late-specified return type is indicated by an initial '->'. */
19938 if (token->type != CPP_DEREF
19939 && token->keyword != RID_REQUIRES
19940 && !(token->type == CPP_NAME
19941 && token->u.value == ridpointers[RID_REQUIRES])
19942 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
19943 return NULL_TREE;
19945 tree save_ccp = current_class_ptr;
19946 tree save_ccr = current_class_ref;
19947 if (quals >= 0)
19949 /* DR 1207: 'this' is in scope in the trailing return type. */
19950 inject_this_parameter (current_class_type, quals);
19953 if (token->type == CPP_DEREF)
19955 /* Consume the ->. */
19956 cp_lexer_consume_token (parser->lexer);
19958 type = cp_parser_trailing_type_id (parser);
19961 /* Function declarations may be followed by a trailing
19962 requires-clause. */
19963 requires_clause = cp_parser_requires_clause_opt (parser);
19965 if (cilk_simd_fn_vector_p)
19966 declarator->attributes
19967 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
19968 declarator->attributes);
19969 if (declare_simd_p)
19970 declarator->attributes
19971 = cp_parser_late_parsing_omp_declare_simd (parser,
19972 declarator->attributes);
19973 if (oacc_routine_p)
19974 declarator->attributes
19975 = cp_parser_late_parsing_oacc_routine (parser,
19976 declarator->attributes);
19978 if (quals >= 0)
19980 current_class_ptr = save_ccp;
19981 current_class_ref = save_ccr;
19984 return type;
19987 /* Parse a declarator-id.
19989 declarator-id:
19990 id-expression
19991 :: [opt] nested-name-specifier [opt] type-name
19993 In the `id-expression' case, the value returned is as for
19994 cp_parser_id_expression if the id-expression was an unqualified-id.
19995 If the id-expression was a qualified-id, then a SCOPE_REF is
19996 returned. The first operand is the scope (either a NAMESPACE_DECL
19997 or TREE_TYPE), but the second is still just a representation of an
19998 unqualified-id. */
20000 static tree
20001 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20003 tree id;
20004 /* The expression must be an id-expression. Assume that qualified
20005 names are the names of types so that:
20007 template <class T>
20008 int S<T>::R::i = 3;
20010 will work; we must treat `S<T>::R' as the name of a type.
20011 Similarly, assume that qualified names are templates, where
20012 required, so that:
20014 template <class T>
20015 int S<T>::R<T>::i = 3;
20017 will work, too. */
20018 id = cp_parser_id_expression (parser,
20019 /*template_keyword_p=*/false,
20020 /*check_dependency_p=*/false,
20021 /*template_p=*/NULL,
20022 /*declarator_p=*/true,
20023 optional_p);
20024 if (id && BASELINK_P (id))
20025 id = BASELINK_FUNCTIONS (id);
20026 return id;
20029 /* Parse a type-id.
20031 type-id:
20032 type-specifier-seq abstract-declarator [opt]
20034 Returns the TYPE specified. */
20036 static tree
20037 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20038 bool is_trailing_return)
20040 cp_decl_specifier_seq type_specifier_seq;
20041 cp_declarator *abstract_declarator;
20043 /* Parse the type-specifier-seq. */
20044 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20045 is_trailing_return,
20046 &type_specifier_seq);
20047 if (type_specifier_seq.type == error_mark_node)
20048 return error_mark_node;
20050 /* There might or might not be an abstract declarator. */
20051 cp_parser_parse_tentatively (parser);
20052 /* Look for the declarator. */
20053 abstract_declarator
20054 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20055 /*parenthesized_p=*/NULL,
20056 /*member_p=*/false,
20057 /*friend_p=*/false);
20058 /* Check to see if there really was a declarator. */
20059 if (!cp_parser_parse_definitely (parser))
20060 abstract_declarator = NULL;
20062 if (type_specifier_seq.type
20063 /* The concepts TS allows 'auto' as a type-id. */
20064 && (!flag_concepts || parser->in_type_id_in_expr_p)
20065 /* None of the valid uses of 'auto' in C++14 involve the type-id
20066 nonterminal, but it is valid in a trailing-return-type. */
20067 && !(cxx_dialect >= cxx14 && is_trailing_return)
20068 && type_uses_auto (type_specifier_seq.type))
20070 /* A type-id with type 'auto' is only ok if the abstract declarator
20071 is a function declarator with a late-specified return type.
20073 A type-id with 'auto' is also valid in a trailing-return-type
20074 in a compound-requirement. */
20075 if (abstract_declarator
20076 && abstract_declarator->kind == cdk_function
20077 && abstract_declarator->u.function.late_return_type)
20078 /* OK */;
20079 else if (parser->in_result_type_constraint_p)
20080 /* OK */;
20081 else
20083 error ("invalid use of %<auto%>");
20084 return error_mark_node;
20088 return groktypename (&type_specifier_seq, abstract_declarator,
20089 is_template_arg);
20092 static tree
20093 cp_parser_type_id (cp_parser *parser)
20095 return cp_parser_type_id_1 (parser, false, false);
20098 static tree
20099 cp_parser_template_type_arg (cp_parser *parser)
20101 tree r;
20102 const char *saved_message = parser->type_definition_forbidden_message;
20103 parser->type_definition_forbidden_message
20104 = G_("types may not be defined in template arguments");
20105 r = cp_parser_type_id_1 (parser, true, false);
20106 parser->type_definition_forbidden_message = saved_message;
20107 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
20109 error ("invalid use of %<auto%> in template argument");
20110 r = error_mark_node;
20112 return r;
20115 static tree
20116 cp_parser_trailing_type_id (cp_parser *parser)
20118 return cp_parser_type_id_1 (parser, false, true);
20121 /* Parse a type-specifier-seq.
20123 type-specifier-seq:
20124 type-specifier type-specifier-seq [opt]
20126 GNU extension:
20128 type-specifier-seq:
20129 attributes type-specifier-seq [opt]
20131 If IS_DECLARATION is true, we are at the start of a "condition" or
20132 exception-declaration, so we might be followed by a declarator-id.
20134 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20135 i.e. we've just seen "->".
20137 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20139 static void
20140 cp_parser_type_specifier_seq (cp_parser* parser,
20141 bool is_declaration,
20142 bool is_trailing_return,
20143 cp_decl_specifier_seq *type_specifier_seq)
20145 bool seen_type_specifier = false;
20146 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20147 cp_token *start_token = NULL;
20149 /* Clear the TYPE_SPECIFIER_SEQ. */
20150 clear_decl_specs (type_specifier_seq);
20152 /* In the context of a trailing return type, enum E { } is an
20153 elaborated-type-specifier followed by a function-body, not an
20154 enum-specifier. */
20155 if (is_trailing_return)
20156 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20158 /* Parse the type-specifiers and attributes. */
20159 while (true)
20161 tree type_specifier;
20162 bool is_cv_qualifier;
20164 /* Check for attributes first. */
20165 if (cp_next_tokens_can_be_attribute_p (parser))
20167 type_specifier_seq->attributes =
20168 chainon (type_specifier_seq->attributes,
20169 cp_parser_attributes_opt (parser));
20170 continue;
20173 /* record the token of the beginning of the type specifier seq,
20174 for error reporting purposes*/
20175 if (!start_token)
20176 start_token = cp_lexer_peek_token (parser->lexer);
20178 /* Look for the type-specifier. */
20179 type_specifier = cp_parser_type_specifier (parser,
20180 flags,
20181 type_specifier_seq,
20182 /*is_declaration=*/false,
20183 NULL,
20184 &is_cv_qualifier);
20185 if (!type_specifier)
20187 /* If the first type-specifier could not be found, this is not a
20188 type-specifier-seq at all. */
20189 if (!seen_type_specifier)
20191 /* Set in_declarator_p to avoid skipping to the semicolon. */
20192 int in_decl = parser->in_declarator_p;
20193 parser->in_declarator_p = true;
20195 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20196 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20197 cp_parser_error (parser, "expected type-specifier");
20199 parser->in_declarator_p = in_decl;
20201 type_specifier_seq->type = error_mark_node;
20202 return;
20204 /* If subsequent type-specifiers could not be found, the
20205 type-specifier-seq is complete. */
20206 break;
20209 seen_type_specifier = true;
20210 /* The standard says that a condition can be:
20212 type-specifier-seq declarator = assignment-expression
20214 However, given:
20216 struct S {};
20217 if (int S = ...)
20219 we should treat the "S" as a declarator, not as a
20220 type-specifier. The standard doesn't say that explicitly for
20221 type-specifier-seq, but it does say that for
20222 decl-specifier-seq in an ordinary declaration. Perhaps it
20223 would be clearer just to allow a decl-specifier-seq here, and
20224 then add a semantic restriction that if any decl-specifiers
20225 that are not type-specifiers appear, the program is invalid. */
20226 if (is_declaration && !is_cv_qualifier)
20227 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20231 /* Return whether the function currently being declared has an associated
20232 template parameter list. */
20234 static bool
20235 function_being_declared_is_template_p (cp_parser* parser)
20237 if (!current_template_parms || processing_template_parmlist)
20238 return false;
20240 if (parser->implicit_template_scope)
20241 return true;
20243 if (at_class_scope_p ()
20244 && TYPE_BEING_DEFINED (current_class_type))
20245 return parser->num_template_parameter_lists != 0;
20247 return ((int) parser->num_template_parameter_lists > template_class_depth
20248 (current_class_type));
20251 /* Parse a parameter-declaration-clause.
20253 parameter-declaration-clause:
20254 parameter-declaration-list [opt] ... [opt]
20255 parameter-declaration-list , ...
20257 Returns a representation for the parameter declarations. A return
20258 value of NULL indicates a parameter-declaration-clause consisting
20259 only of an ellipsis. */
20261 static tree
20262 cp_parser_parameter_declaration_clause (cp_parser* parser)
20264 tree parameters;
20265 cp_token *token;
20266 bool ellipsis_p;
20267 bool is_error;
20269 struct cleanup {
20270 cp_parser* parser;
20271 int auto_is_implicit_function_template_parm_p;
20272 ~cleanup() {
20273 parser->auto_is_implicit_function_template_parm_p
20274 = auto_is_implicit_function_template_parm_p;
20276 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
20278 (void) cleanup;
20280 if (!processing_specialization
20281 && !processing_template_parmlist
20282 && !processing_explicit_instantiation)
20283 if (!current_function_decl
20284 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
20285 parser->auto_is_implicit_function_template_parm_p = true;
20287 /* Peek at the next token. */
20288 token = cp_lexer_peek_token (parser->lexer);
20289 /* Check for trivial parameter-declaration-clauses. */
20290 if (token->type == CPP_ELLIPSIS)
20292 /* Consume the `...' token. */
20293 cp_lexer_consume_token (parser->lexer);
20294 return NULL_TREE;
20296 else if (token->type == CPP_CLOSE_PAREN)
20297 /* There are no parameters. */
20299 #ifndef NO_IMPLICIT_EXTERN_C
20300 if (in_system_header_at (input_location)
20301 && current_class_type == NULL
20302 && current_lang_name == lang_name_c)
20303 return NULL_TREE;
20304 else
20305 #endif
20306 return void_list_node;
20308 /* Check for `(void)', too, which is a special case. */
20309 else if (token->keyword == RID_VOID
20310 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20311 == CPP_CLOSE_PAREN))
20313 /* Consume the `void' token. */
20314 cp_lexer_consume_token (parser->lexer);
20315 /* There are no parameters. */
20316 return void_list_node;
20319 /* Parse the parameter-declaration-list. */
20320 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
20321 /* If a parse error occurred while parsing the
20322 parameter-declaration-list, then the entire
20323 parameter-declaration-clause is erroneous. */
20324 if (is_error)
20325 return NULL;
20327 /* Peek at the next token. */
20328 token = cp_lexer_peek_token (parser->lexer);
20329 /* If it's a `,', the clause should terminate with an ellipsis. */
20330 if (token->type == CPP_COMMA)
20332 /* Consume the `,'. */
20333 cp_lexer_consume_token (parser->lexer);
20334 /* Expect an ellipsis. */
20335 ellipsis_p
20336 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
20338 /* It might also be `...' if the optional trailing `,' was
20339 omitted. */
20340 else if (token->type == CPP_ELLIPSIS)
20342 /* Consume the `...' token. */
20343 cp_lexer_consume_token (parser->lexer);
20344 /* And remember that we saw it. */
20345 ellipsis_p = true;
20347 else
20348 ellipsis_p = false;
20350 /* Finish the parameter list. */
20351 if (!ellipsis_p)
20352 parameters = chainon (parameters, void_list_node);
20354 return parameters;
20357 /* Parse a parameter-declaration-list.
20359 parameter-declaration-list:
20360 parameter-declaration
20361 parameter-declaration-list , parameter-declaration
20363 Returns a representation of the parameter-declaration-list, as for
20364 cp_parser_parameter_declaration_clause. However, the
20365 `void_list_node' is never appended to the list. Upon return,
20366 *IS_ERROR will be true iff an error occurred. */
20368 static tree
20369 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
20371 tree parameters = NULL_TREE;
20372 tree *tail = &parameters;
20373 bool saved_in_unbraced_linkage_specification_p;
20374 int index = 0;
20376 /* Assume all will go well. */
20377 *is_error = false;
20378 /* The special considerations that apply to a function within an
20379 unbraced linkage specifications do not apply to the parameters
20380 to the function. */
20381 saved_in_unbraced_linkage_specification_p
20382 = parser->in_unbraced_linkage_specification_p;
20383 parser->in_unbraced_linkage_specification_p = false;
20385 /* Look for more parameters. */
20386 while (true)
20388 cp_parameter_declarator *parameter;
20389 tree decl = error_mark_node;
20390 bool parenthesized_p = false;
20391 int template_parm_idx = (function_being_declared_is_template_p (parser)?
20392 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
20393 (current_template_parms)) : 0);
20395 /* Parse the parameter. */
20396 parameter
20397 = cp_parser_parameter_declaration (parser,
20398 /*template_parm_p=*/false,
20399 &parenthesized_p);
20401 /* We don't know yet if the enclosing context is deprecated, so wait
20402 and warn in grokparms if appropriate. */
20403 deprecated_state = DEPRECATED_SUPPRESS;
20405 if (parameter)
20407 /* If a function parameter pack was specified and an implicit template
20408 parameter was introduced during cp_parser_parameter_declaration,
20409 change any implicit parameters introduced into packs. */
20410 if (parser->implicit_template_parms
20411 && parameter->declarator
20412 && parameter->declarator->parameter_pack_p)
20414 int latest_template_parm_idx = TREE_VEC_LENGTH
20415 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
20417 if (latest_template_parm_idx != template_parm_idx)
20418 parameter->decl_specifiers.type = convert_generic_types_to_packs
20419 (parameter->decl_specifiers.type,
20420 template_parm_idx, latest_template_parm_idx);
20423 decl = grokdeclarator (parameter->declarator,
20424 &parameter->decl_specifiers,
20425 PARM,
20426 parameter->default_argument != NULL_TREE,
20427 &parameter->decl_specifiers.attributes);
20430 deprecated_state = DEPRECATED_NORMAL;
20432 /* If a parse error occurred parsing the parameter declaration,
20433 then the entire parameter-declaration-list is erroneous. */
20434 if (decl == error_mark_node)
20436 *is_error = true;
20437 parameters = error_mark_node;
20438 break;
20441 if (parameter->decl_specifiers.attributes)
20442 cplus_decl_attributes (&decl,
20443 parameter->decl_specifiers.attributes,
20445 if (DECL_NAME (decl))
20446 decl = pushdecl (decl);
20448 if (decl != error_mark_node)
20450 retrofit_lang_decl (decl);
20451 DECL_PARM_INDEX (decl) = ++index;
20452 DECL_PARM_LEVEL (decl) = function_parm_depth ();
20455 /* Add the new parameter to the list. */
20456 *tail = build_tree_list (parameter->default_argument, decl);
20457 tail = &TREE_CHAIN (*tail);
20459 /* Peek at the next token. */
20460 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
20461 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
20462 /* These are for Objective-C++ */
20463 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20464 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20465 /* The parameter-declaration-list is complete. */
20466 break;
20467 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20469 cp_token *token;
20471 /* Peek at the next token. */
20472 token = cp_lexer_peek_nth_token (parser->lexer, 2);
20473 /* If it's an ellipsis, then the list is complete. */
20474 if (token->type == CPP_ELLIPSIS)
20475 break;
20476 /* Otherwise, there must be more parameters. Consume the
20477 `,'. */
20478 cp_lexer_consume_token (parser->lexer);
20479 /* When parsing something like:
20481 int i(float f, double d)
20483 we can tell after seeing the declaration for "f" that we
20484 are not looking at an initialization of a variable "i",
20485 but rather at the declaration of a function "i".
20487 Due to the fact that the parsing of template arguments
20488 (as specified to a template-id) requires backtracking we
20489 cannot use this technique when inside a template argument
20490 list. */
20491 if (!parser->in_template_argument_list_p
20492 && !parser->in_type_id_in_expr_p
20493 && cp_parser_uncommitted_to_tentative_parse_p (parser)
20494 /* However, a parameter-declaration of the form
20495 "float(f)" (which is a valid declaration of a
20496 parameter "f") can also be interpreted as an
20497 expression (the conversion of "f" to "float"). */
20498 && !parenthesized_p)
20499 cp_parser_commit_to_tentative_parse (parser);
20501 else
20503 cp_parser_error (parser, "expected %<,%> or %<...%>");
20504 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20505 cp_parser_skip_to_closing_parenthesis (parser,
20506 /*recovering=*/true,
20507 /*or_comma=*/false,
20508 /*consume_paren=*/false);
20509 break;
20513 parser->in_unbraced_linkage_specification_p
20514 = saved_in_unbraced_linkage_specification_p;
20516 /* Reset implicit_template_scope if we are about to leave the function
20517 parameter list that introduced it. Note that for out-of-line member
20518 definitions, there will be one or more class scopes before we get to
20519 the template parameter scope. */
20521 if (cp_binding_level *its = parser->implicit_template_scope)
20522 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
20524 while (maybe_its->kind == sk_class)
20525 maybe_its = maybe_its->level_chain;
20526 if (maybe_its == its)
20528 parser->implicit_template_parms = 0;
20529 parser->implicit_template_scope = 0;
20533 return parameters;
20536 /* Parse a parameter declaration.
20538 parameter-declaration:
20539 decl-specifier-seq ... [opt] declarator
20540 decl-specifier-seq declarator = assignment-expression
20541 decl-specifier-seq ... [opt] abstract-declarator [opt]
20542 decl-specifier-seq abstract-declarator [opt] = assignment-expression
20544 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
20545 declares a template parameter. (In that case, a non-nested `>'
20546 token encountered during the parsing of the assignment-expression
20547 is not interpreted as a greater-than operator.)
20549 Returns a representation of the parameter, or NULL if an error
20550 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
20551 true iff the declarator is of the form "(p)". */
20553 static cp_parameter_declarator *
20554 cp_parser_parameter_declaration (cp_parser *parser,
20555 bool template_parm_p,
20556 bool *parenthesized_p)
20558 int declares_class_or_enum;
20559 cp_decl_specifier_seq decl_specifiers;
20560 cp_declarator *declarator;
20561 tree default_argument;
20562 cp_token *token = NULL, *declarator_token_start = NULL;
20563 const char *saved_message;
20564 bool template_parameter_pack_p = false;
20566 /* In a template parameter, `>' is not an operator.
20568 [temp.param]
20570 When parsing a default template-argument for a non-type
20571 template-parameter, the first non-nested `>' is taken as the end
20572 of the template parameter-list rather than a greater-than
20573 operator. */
20575 /* Type definitions may not appear in parameter types. */
20576 saved_message = parser->type_definition_forbidden_message;
20577 parser->type_definition_forbidden_message
20578 = G_("types may not be defined in parameter types");
20580 /* Parse the declaration-specifiers. */
20581 cp_parser_decl_specifier_seq (parser,
20582 CP_PARSER_FLAGS_NONE,
20583 &decl_specifiers,
20584 &declares_class_or_enum);
20586 /* Complain about missing 'typename' or other invalid type names. */
20587 if (!decl_specifiers.any_type_specifiers_p
20588 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20589 decl_specifiers.type = error_mark_node;
20591 /* If an error occurred, there's no reason to attempt to parse the
20592 rest of the declaration. */
20593 if (cp_parser_error_occurred (parser))
20595 parser->type_definition_forbidden_message = saved_message;
20596 return NULL;
20599 /* Peek at the next token. */
20600 token = cp_lexer_peek_token (parser->lexer);
20602 /* If the next token is a `)', `,', `=', `>', or `...', then there
20603 is no declarator. However, when variadic templates are enabled,
20604 there may be a declarator following `...'. */
20605 if (token->type == CPP_CLOSE_PAREN
20606 || token->type == CPP_COMMA
20607 || token->type == CPP_EQ
20608 || token->type == CPP_GREATER)
20610 declarator = NULL;
20611 if (parenthesized_p)
20612 *parenthesized_p = false;
20614 /* Otherwise, there should be a declarator. */
20615 else
20617 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20618 parser->default_arg_ok_p = false;
20620 /* After seeing a decl-specifier-seq, if the next token is not a
20621 "(", there is no possibility that the code is a valid
20622 expression. Therefore, if parsing tentatively, we commit at
20623 this point. */
20624 if (!parser->in_template_argument_list_p
20625 /* In an expression context, having seen:
20627 (int((char ...
20629 we cannot be sure whether we are looking at a
20630 function-type (taking a "char" as a parameter) or a cast
20631 of some object of type "char" to "int". */
20632 && !parser->in_type_id_in_expr_p
20633 && cp_parser_uncommitted_to_tentative_parse_p (parser)
20634 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20635 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
20636 cp_parser_commit_to_tentative_parse (parser);
20637 /* Parse the declarator. */
20638 declarator_token_start = token;
20639 declarator = cp_parser_declarator (parser,
20640 CP_PARSER_DECLARATOR_EITHER,
20641 /*ctor_dtor_or_conv_p=*/NULL,
20642 parenthesized_p,
20643 /*member_p=*/false,
20644 /*friend_p=*/false);
20645 parser->default_arg_ok_p = saved_default_arg_ok_p;
20646 /* After the declarator, allow more attributes. */
20647 decl_specifiers.attributes
20648 = chainon (decl_specifiers.attributes,
20649 cp_parser_attributes_opt (parser));
20651 /* If the declarator is a template parameter pack, remember that and
20652 clear the flag in the declarator itself so we don't get errors
20653 from grokdeclarator. */
20654 if (template_parm_p && declarator && declarator->parameter_pack_p)
20656 declarator->parameter_pack_p = false;
20657 template_parameter_pack_p = true;
20661 /* If the next token is an ellipsis, and we have not seen a declarator
20662 name, and if either the type of the declarator contains parameter
20663 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
20664 for, eg, abbreviated integral type names), then we actually have a
20665 parameter pack expansion expression. Otherwise, leave the ellipsis
20666 for a C-style variadic function. */
20667 token = cp_lexer_peek_token (parser->lexer);
20668 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20670 tree type = decl_specifiers.type;
20672 if (type && DECL_P (type))
20673 type = TREE_TYPE (type);
20675 if (((type
20676 && TREE_CODE (type) != TYPE_PACK_EXPANSION
20677 && (template_parm_p || uses_parameter_packs (type)))
20678 || (!type && template_parm_p))
20679 && declarator_can_be_parameter_pack (declarator))
20681 /* Consume the `...'. */
20682 cp_lexer_consume_token (parser->lexer);
20683 maybe_warn_variadic_templates ();
20685 /* Build a pack expansion type */
20686 if (template_parm_p)
20687 template_parameter_pack_p = true;
20688 else if (declarator)
20689 declarator->parameter_pack_p = true;
20690 else
20691 decl_specifiers.type = make_pack_expansion (type);
20695 /* The restriction on defining new types applies only to the type
20696 of the parameter, not to the default argument. */
20697 parser->type_definition_forbidden_message = saved_message;
20699 /* If the next token is `=', then process a default argument. */
20700 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20702 tree type = decl_specifiers.type;
20703 token = cp_lexer_peek_token (parser->lexer);
20704 /* If we are defining a class, then the tokens that make up the
20705 default argument must be saved and processed later. */
20706 if (!template_parm_p && at_class_scope_p ()
20707 && TYPE_BEING_DEFINED (current_class_type)
20708 && !LAMBDA_TYPE_P (current_class_type))
20709 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
20711 // A constrained-type-specifier may declare a type template-parameter.
20712 else if (declares_constrained_type_template_parameter (type))
20713 default_argument
20714 = cp_parser_default_type_template_argument (parser);
20716 // A constrained-type-specifier may declare a template-template-parameter.
20717 else if (declares_constrained_template_template_parameter (type))
20718 default_argument
20719 = cp_parser_default_template_template_argument (parser);
20721 /* Outside of a class definition, we can just parse the
20722 assignment-expression. */
20723 else
20724 default_argument
20725 = cp_parser_default_argument (parser, template_parm_p);
20727 if (!parser->default_arg_ok_p)
20729 permerror (token->location,
20730 "default arguments are only "
20731 "permitted for function parameters");
20733 else if ((declarator && declarator->parameter_pack_p)
20734 || template_parameter_pack_p
20735 || (decl_specifiers.type
20736 && PACK_EXPANSION_P (decl_specifiers.type)))
20738 /* Find the name of the parameter pack. */
20739 cp_declarator *id_declarator = declarator;
20740 while (id_declarator && id_declarator->kind != cdk_id)
20741 id_declarator = id_declarator->declarator;
20743 if (id_declarator && id_declarator->kind == cdk_id)
20744 error_at (declarator_token_start->location,
20745 template_parm_p
20746 ? G_("template parameter pack %qD "
20747 "cannot have a default argument")
20748 : G_("parameter pack %qD cannot have "
20749 "a default argument"),
20750 id_declarator->u.id.unqualified_name);
20751 else
20752 error_at (declarator_token_start->location,
20753 template_parm_p
20754 ? G_("template parameter pack cannot have "
20755 "a default argument")
20756 : G_("parameter pack cannot have a "
20757 "default argument"));
20759 default_argument = NULL_TREE;
20762 else
20763 default_argument = NULL_TREE;
20765 return make_parameter_declarator (&decl_specifiers,
20766 declarator,
20767 default_argument,
20768 template_parameter_pack_p);
20771 /* Parse a default argument and return it.
20773 TEMPLATE_PARM_P is true if this is a default argument for a
20774 non-type template parameter. */
20775 static tree
20776 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
20778 tree default_argument = NULL_TREE;
20779 bool saved_greater_than_is_operator_p;
20780 bool saved_local_variables_forbidden_p;
20781 bool non_constant_p, is_direct_init;
20783 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
20784 set correctly. */
20785 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
20786 parser->greater_than_is_operator_p = !template_parm_p;
20787 /* Local variable names (and the `this' keyword) may not
20788 appear in a default argument. */
20789 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20790 parser->local_variables_forbidden_p = true;
20791 /* Parse the assignment-expression. */
20792 if (template_parm_p)
20793 push_deferring_access_checks (dk_no_deferred);
20794 tree saved_class_ptr = NULL_TREE;
20795 tree saved_class_ref = NULL_TREE;
20796 /* The "this" pointer is not valid in a default argument. */
20797 if (cfun)
20799 saved_class_ptr = current_class_ptr;
20800 cp_function_chain->x_current_class_ptr = NULL_TREE;
20801 saved_class_ref = current_class_ref;
20802 cp_function_chain->x_current_class_ref = NULL_TREE;
20804 default_argument
20805 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
20806 /* Restore the "this" pointer. */
20807 if (cfun)
20809 cp_function_chain->x_current_class_ptr = saved_class_ptr;
20810 cp_function_chain->x_current_class_ref = saved_class_ref;
20812 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
20813 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20814 if (template_parm_p)
20815 pop_deferring_access_checks ();
20816 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
20817 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20819 return default_argument;
20822 /* Parse a function-body.
20824 function-body:
20825 compound_statement */
20827 static void
20828 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
20830 cp_parser_compound_statement (parser, NULL, (in_function_try_block
20831 ? BCS_TRY_BLOCK : BCS_NORMAL),
20832 true);
20835 /* Parse a ctor-initializer-opt followed by a function-body. Return
20836 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
20837 is true we are parsing a function-try-block. */
20839 static bool
20840 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
20841 bool in_function_try_block)
20843 tree body, list;
20844 bool ctor_initializer_p;
20845 const bool check_body_p =
20846 DECL_CONSTRUCTOR_P (current_function_decl)
20847 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
20848 tree last = NULL;
20850 /* Begin the function body. */
20851 body = begin_function_body ();
20852 /* Parse the optional ctor-initializer. */
20853 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
20855 /* If we're parsing a constexpr constructor definition, we need
20856 to check that the constructor body is indeed empty. However,
20857 before we get to cp_parser_function_body lot of junk has been
20858 generated, so we can't just check that we have an empty block.
20859 Rather we take a snapshot of the outermost block, and check whether
20860 cp_parser_function_body changed its state. */
20861 if (check_body_p)
20863 list = cur_stmt_list;
20864 if (STATEMENT_LIST_TAIL (list))
20865 last = STATEMENT_LIST_TAIL (list)->stmt;
20867 /* Parse the function-body. */
20868 cp_parser_function_body (parser, in_function_try_block);
20869 if (check_body_p)
20870 check_constexpr_ctor_body (last, list, /*complain=*/true);
20871 /* Finish the function body. */
20872 finish_function_body (body);
20874 return ctor_initializer_p;
20877 /* Parse an initializer.
20879 initializer:
20880 = initializer-clause
20881 ( expression-list )
20883 Returns an expression representing the initializer. If no
20884 initializer is present, NULL_TREE is returned.
20886 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
20887 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
20888 set to TRUE if there is no initializer present. If there is an
20889 initializer, and it is not a constant-expression, *NON_CONSTANT_P
20890 is set to true; otherwise it is set to false. */
20892 static tree
20893 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
20894 bool* non_constant_p)
20896 cp_token *token;
20897 tree init;
20899 /* Peek at the next token. */
20900 token = cp_lexer_peek_token (parser->lexer);
20902 /* Let our caller know whether or not this initializer was
20903 parenthesized. */
20904 *is_direct_init = (token->type != CPP_EQ);
20905 /* Assume that the initializer is constant. */
20906 *non_constant_p = false;
20908 if (token->type == CPP_EQ)
20910 /* Consume the `='. */
20911 cp_lexer_consume_token (parser->lexer);
20912 /* Parse the initializer-clause. */
20913 init = cp_parser_initializer_clause (parser, non_constant_p);
20915 else if (token->type == CPP_OPEN_PAREN)
20917 vec<tree, va_gc> *vec;
20918 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20919 /*cast_p=*/false,
20920 /*allow_expansion_p=*/true,
20921 non_constant_p);
20922 if (vec == NULL)
20923 return error_mark_node;
20924 init = build_tree_list_vec (vec);
20925 release_tree_vector (vec);
20927 else if (token->type == CPP_OPEN_BRACE)
20929 cp_lexer_set_source_position (parser->lexer);
20930 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20931 init = cp_parser_braced_list (parser, non_constant_p);
20932 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
20934 else
20936 /* Anything else is an error. */
20937 cp_parser_error (parser, "expected initializer");
20938 init = error_mark_node;
20941 if (check_for_bare_parameter_packs (init))
20942 init = error_mark_node;
20944 return init;
20947 /* Parse an initializer-clause.
20949 initializer-clause:
20950 assignment-expression
20951 braced-init-list
20953 Returns an expression representing the initializer.
20955 If the `assignment-expression' production is used the value
20956 returned is simply a representation for the expression.
20958 Otherwise, calls cp_parser_braced_list. */
20960 static cp_expr
20961 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
20963 cp_expr initializer;
20965 /* Assume the expression is constant. */
20966 *non_constant_p = false;
20968 /* If it is not a `{', then we are looking at an
20969 assignment-expression. */
20970 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
20972 initializer
20973 = cp_parser_constant_expression (parser,
20974 /*allow_non_constant_p=*/true,
20975 non_constant_p);
20977 else
20978 initializer = cp_parser_braced_list (parser, non_constant_p);
20980 return initializer;
20983 /* Parse a brace-enclosed initializer list.
20985 braced-init-list:
20986 { initializer-list , [opt] }
20989 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
20990 the elements of the initializer-list (or NULL, if the last
20991 production is used). The TREE_TYPE for the CONSTRUCTOR will be
20992 NULL_TREE. There is no way to detect whether or not the optional
20993 trailing `,' was provided. NON_CONSTANT_P is as for
20994 cp_parser_initializer. */
20996 static cp_expr
20997 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
20999 tree initializer;
21000 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21002 /* Consume the `{' token. */
21003 cp_lexer_consume_token (parser->lexer);
21004 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21005 initializer = make_node (CONSTRUCTOR);
21006 /* If it's not a `}', then there is a non-trivial initializer. */
21007 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21009 /* Parse the initializer list. */
21010 CONSTRUCTOR_ELTS (initializer)
21011 = cp_parser_initializer_list (parser, non_constant_p);
21012 /* A trailing `,' token is allowed. */
21013 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21014 cp_lexer_consume_token (parser->lexer);
21016 else
21017 *non_constant_p = false;
21018 /* Now, there should be a trailing `}'. */
21019 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21020 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21021 TREE_TYPE (initializer) = init_list_type_node;
21023 cp_expr result (initializer);
21024 /* Build a location of the form:
21025 { ... }
21026 ^~~~~~~
21027 with caret==start at the open brace, finish at the close brace. */
21028 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21029 result.set_location (combined_loc);
21030 return result;
21033 /* Consume tokens up to, and including, the next non-nested closing `]'.
21034 Returns true iff we found a closing `]'. */
21036 static bool
21037 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21039 unsigned square_depth = 0;
21041 while (true)
21043 cp_token * token = cp_lexer_peek_token (parser->lexer);
21045 switch (token->type)
21047 case CPP_EOF:
21048 case CPP_PRAGMA_EOL:
21049 /* If we've run out of tokens, then there is no closing `]'. */
21050 return false;
21052 case CPP_OPEN_SQUARE:
21053 ++square_depth;
21054 break;
21056 case CPP_CLOSE_SQUARE:
21057 if (!square_depth--)
21059 cp_lexer_consume_token (parser->lexer);
21060 return true;
21062 break;
21064 default:
21065 break;
21068 /* Consume the token. */
21069 cp_lexer_consume_token (parser->lexer);
21073 /* Return true if we are looking at an array-designator, false otherwise. */
21075 static bool
21076 cp_parser_array_designator_p (cp_parser *parser)
21078 /* Consume the `['. */
21079 cp_lexer_consume_token (parser->lexer);
21081 cp_lexer_save_tokens (parser->lexer);
21083 /* Skip tokens until the next token is a closing square bracket.
21084 If we find the closing `]', and the next token is a `=', then
21085 we are looking at an array designator. */
21086 bool array_designator_p
21087 = (cp_parser_skip_to_closing_square_bracket (parser)
21088 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
21090 /* Roll back the tokens we skipped. */
21091 cp_lexer_rollback_tokens (parser->lexer);
21093 return array_designator_p;
21096 /* Parse an initializer-list.
21098 initializer-list:
21099 initializer-clause ... [opt]
21100 initializer-list , initializer-clause ... [opt]
21102 GNU Extension:
21104 initializer-list:
21105 designation initializer-clause ...[opt]
21106 initializer-list , designation initializer-clause ...[opt]
21108 designation:
21109 . identifier =
21110 identifier :
21111 [ constant-expression ] =
21113 Returns a vec of constructor_elt. The VALUE of each elt is an expression
21114 for the initializer. If the INDEX of the elt is non-NULL, it is the
21115 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21116 as for cp_parser_initializer. */
21118 static vec<constructor_elt, va_gc> *
21119 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
21121 vec<constructor_elt, va_gc> *v = NULL;
21123 /* Assume all of the expressions are constant. */
21124 *non_constant_p = false;
21126 /* Parse the rest of the list. */
21127 while (true)
21129 cp_token *token;
21130 tree designator;
21131 tree initializer;
21132 bool clause_non_constant_p;
21134 /* If the next token is an identifier and the following one is a
21135 colon, we are looking at the GNU designated-initializer
21136 syntax. */
21137 if (cp_parser_allow_gnu_extensions_p (parser)
21138 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21139 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
21141 /* Warn the user that they are using an extension. */
21142 pedwarn (input_location, OPT_Wpedantic,
21143 "ISO C++ does not allow designated initializers");
21144 /* Consume the identifier. */
21145 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21146 /* Consume the `:'. */
21147 cp_lexer_consume_token (parser->lexer);
21149 /* Also handle the C99 syntax, '. id ='. */
21150 else if (cp_parser_allow_gnu_extensions_p (parser)
21151 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21152 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21153 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
21155 /* Warn the user that they are using an extension. */
21156 pedwarn (input_location, OPT_Wpedantic,
21157 "ISO C++ does not allow C99 designated initializers");
21158 /* Consume the `.'. */
21159 cp_lexer_consume_token (parser->lexer);
21160 /* Consume the identifier. */
21161 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21162 /* Consume the `='. */
21163 cp_lexer_consume_token (parser->lexer);
21165 /* Also handle C99 array designators, '[ const ] ='. */
21166 else if (cp_parser_allow_gnu_extensions_p (parser)
21167 && !c_dialect_objc ()
21168 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21170 /* In C++11, [ could start a lambda-introducer. */
21171 bool non_const = false;
21173 cp_parser_parse_tentatively (parser);
21175 if (!cp_parser_array_designator_p (parser))
21177 cp_parser_simulate_error (parser);
21178 designator = NULL_TREE;
21180 else
21182 designator = cp_parser_constant_expression (parser, true,
21183 &non_const);
21184 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21185 cp_parser_require (parser, CPP_EQ, RT_EQ);
21188 if (!cp_parser_parse_definitely (parser))
21189 designator = NULL_TREE;
21190 else if (non_const)
21191 require_potential_rvalue_constant_expression (designator);
21193 else
21194 designator = NULL_TREE;
21196 /* Parse the initializer. */
21197 initializer = cp_parser_initializer_clause (parser,
21198 &clause_non_constant_p);
21199 /* If any clause is non-constant, so is the entire initializer. */
21200 if (clause_non_constant_p)
21201 *non_constant_p = true;
21203 /* If we have an ellipsis, this is an initializer pack
21204 expansion. */
21205 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21207 /* Consume the `...'. */
21208 cp_lexer_consume_token (parser->lexer);
21210 /* Turn the initializer into an initializer expansion. */
21211 initializer = make_pack_expansion (initializer);
21214 /* Add it to the vector. */
21215 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21217 /* If the next token is not a comma, we have reached the end of
21218 the list. */
21219 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21220 break;
21222 /* Peek at the next token. */
21223 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21224 /* If the next token is a `}', then we're still done. An
21225 initializer-clause can have a trailing `,' after the
21226 initializer-list and before the closing `}'. */
21227 if (token->type == CPP_CLOSE_BRACE)
21228 break;
21230 /* Consume the `,' token. */
21231 cp_lexer_consume_token (parser->lexer);
21234 return v;
21237 /* Classes [gram.class] */
21239 /* Parse a class-name.
21241 class-name:
21242 identifier
21243 template-id
21245 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21246 to indicate that names looked up in dependent types should be
21247 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21248 keyword has been used to indicate that the name that appears next
21249 is a template. TAG_TYPE indicates the explicit tag given before
21250 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21251 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21252 is the class being defined in a class-head. If ENUM_OK is TRUE,
21253 enum-names are also accepted.
21255 Returns the TYPE_DECL representing the class. */
21257 static tree
21258 cp_parser_class_name (cp_parser *parser,
21259 bool typename_keyword_p,
21260 bool template_keyword_p,
21261 enum tag_types tag_type,
21262 bool check_dependency_p,
21263 bool class_head_p,
21264 bool is_declaration,
21265 bool enum_ok)
21267 tree decl;
21268 tree scope;
21269 bool typename_p;
21270 cp_token *token;
21271 tree identifier = NULL_TREE;
21273 /* All class-names start with an identifier. */
21274 token = cp_lexer_peek_token (parser->lexer);
21275 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
21277 cp_parser_error (parser, "expected class-name");
21278 return error_mark_node;
21281 /* PARSER->SCOPE can be cleared when parsing the template-arguments
21282 to a template-id, so we save it here. */
21283 scope = parser->scope;
21284 if (scope == error_mark_node)
21285 return error_mark_node;
21287 /* Any name names a type if we're following the `typename' keyword
21288 in a qualified name where the enclosing scope is type-dependent. */
21289 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
21290 && dependent_type_p (scope));
21291 /* Handle the common case (an identifier, but not a template-id)
21292 efficiently. */
21293 if (token->type == CPP_NAME
21294 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
21296 cp_token *identifier_token;
21297 bool ambiguous_p;
21299 /* Look for the identifier. */
21300 identifier_token = cp_lexer_peek_token (parser->lexer);
21301 ambiguous_p = identifier_token->error_reported;
21302 identifier = cp_parser_identifier (parser);
21303 /* If the next token isn't an identifier, we are certainly not
21304 looking at a class-name. */
21305 if (identifier == error_mark_node)
21306 decl = error_mark_node;
21307 /* If we know this is a type-name, there's no need to look it
21308 up. */
21309 else if (typename_p)
21310 decl = identifier;
21311 else
21313 tree ambiguous_decls;
21314 /* If we already know that this lookup is ambiguous, then
21315 we've already issued an error message; there's no reason
21316 to check again. */
21317 if (ambiguous_p)
21319 cp_parser_simulate_error (parser);
21320 return error_mark_node;
21322 /* If the next token is a `::', then the name must be a type
21323 name.
21325 [basic.lookup.qual]
21327 During the lookup for a name preceding the :: scope
21328 resolution operator, object, function, and enumerator
21329 names are ignored. */
21330 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21331 tag_type = scope_type;
21332 /* Look up the name. */
21333 decl = cp_parser_lookup_name (parser, identifier,
21334 tag_type,
21335 /*is_template=*/false,
21336 /*is_namespace=*/false,
21337 check_dependency_p,
21338 &ambiguous_decls,
21339 identifier_token->location);
21340 if (ambiguous_decls)
21342 if (cp_parser_parsing_tentatively (parser))
21343 cp_parser_simulate_error (parser);
21344 return error_mark_node;
21348 else
21350 /* Try a template-id. */
21351 decl = cp_parser_template_id (parser, template_keyword_p,
21352 check_dependency_p,
21353 tag_type,
21354 is_declaration);
21355 if (decl == error_mark_node)
21356 return error_mark_node;
21359 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
21361 /* If this is a typename, create a TYPENAME_TYPE. */
21362 if (typename_p && decl != error_mark_node)
21364 decl = make_typename_type (scope, decl, typename_type,
21365 /*complain=*/tf_error);
21366 if (decl != error_mark_node)
21367 decl = TYPE_NAME (decl);
21370 decl = strip_using_decl (decl);
21372 /* Check to see that it is really the name of a class. */
21373 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21374 && identifier_p (TREE_OPERAND (decl, 0))
21375 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21376 /* Situations like this:
21378 template <typename T> struct A {
21379 typename T::template X<int>::I i;
21382 are problematic. Is `T::template X<int>' a class-name? The
21383 standard does not seem to be definitive, but there is no other
21384 valid interpretation of the following `::'. Therefore, those
21385 names are considered class-names. */
21387 decl = make_typename_type (scope, decl, tag_type, tf_error);
21388 if (decl != error_mark_node)
21389 decl = TYPE_NAME (decl);
21391 else if (TREE_CODE (decl) != TYPE_DECL
21392 || TREE_TYPE (decl) == error_mark_node
21393 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
21394 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
21395 /* In Objective-C 2.0, a classname followed by '.' starts a
21396 dot-syntax expression, and it's not a type-name. */
21397 || (c_dialect_objc ()
21398 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
21399 && objc_is_class_name (decl)))
21400 decl = error_mark_node;
21402 if (decl == error_mark_node)
21403 cp_parser_error (parser, "expected class-name");
21404 else if (identifier && !parser->scope)
21405 maybe_note_name_used_in_class (identifier, decl);
21407 return decl;
21410 /* Parse a class-specifier.
21412 class-specifier:
21413 class-head { member-specification [opt] }
21415 Returns the TREE_TYPE representing the class. */
21417 static tree
21418 cp_parser_class_specifier_1 (cp_parser* parser)
21420 tree type;
21421 tree attributes = NULL_TREE;
21422 bool nested_name_specifier_p;
21423 unsigned saved_num_template_parameter_lists;
21424 bool saved_in_function_body;
21425 unsigned char in_statement;
21426 bool in_switch_statement_p;
21427 bool saved_in_unbraced_linkage_specification_p;
21428 tree old_scope = NULL_TREE;
21429 tree scope = NULL_TREE;
21430 cp_token *closing_brace;
21432 push_deferring_access_checks (dk_no_deferred);
21434 /* Parse the class-head. */
21435 type = cp_parser_class_head (parser,
21436 &nested_name_specifier_p);
21437 /* If the class-head was a semantic disaster, skip the entire body
21438 of the class. */
21439 if (!type)
21441 cp_parser_skip_to_end_of_block_or_statement (parser);
21442 pop_deferring_access_checks ();
21443 return error_mark_node;
21446 /* Look for the `{'. */
21447 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
21449 pop_deferring_access_checks ();
21450 return error_mark_node;
21453 cp_ensure_no_omp_declare_simd (parser);
21454 cp_ensure_no_oacc_routine (parser);
21456 /* Issue an error message if type-definitions are forbidden here. */
21457 cp_parser_check_type_definition (parser);
21458 /* Remember that we are defining one more class. */
21459 ++parser->num_classes_being_defined;
21460 /* Inside the class, surrounding template-parameter-lists do not
21461 apply. */
21462 saved_num_template_parameter_lists
21463 = parser->num_template_parameter_lists;
21464 parser->num_template_parameter_lists = 0;
21465 /* We are not in a function body. */
21466 saved_in_function_body = parser->in_function_body;
21467 parser->in_function_body = false;
21468 /* Or in a loop. */
21469 in_statement = parser->in_statement;
21470 parser->in_statement = 0;
21471 /* Or in a switch. */
21472 in_switch_statement_p = parser->in_switch_statement_p;
21473 parser->in_switch_statement_p = false;
21474 /* We are not immediately inside an extern "lang" block. */
21475 saved_in_unbraced_linkage_specification_p
21476 = parser->in_unbraced_linkage_specification_p;
21477 parser->in_unbraced_linkage_specification_p = false;
21479 // Associate constraints with the type.
21480 if (flag_concepts)
21481 type = associate_classtype_constraints (type);
21483 /* Start the class. */
21484 if (nested_name_specifier_p)
21486 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
21487 old_scope = push_inner_scope (scope);
21489 type = begin_class_definition (type);
21491 if (type == error_mark_node)
21492 /* If the type is erroneous, skip the entire body of the class. */
21493 cp_parser_skip_to_closing_brace (parser);
21494 else
21495 /* Parse the member-specification. */
21496 cp_parser_member_specification_opt (parser);
21498 /* Look for the trailing `}'. */
21499 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21500 /* Look for trailing attributes to apply to this class. */
21501 if (cp_parser_allow_gnu_extensions_p (parser))
21502 attributes = cp_parser_gnu_attributes_opt (parser);
21503 if (type != error_mark_node)
21504 type = finish_struct (type, attributes);
21505 if (nested_name_specifier_p)
21506 pop_inner_scope (old_scope, scope);
21508 /* We've finished a type definition. Check for the common syntax
21509 error of forgetting a semicolon after the definition. We need to
21510 be careful, as we can't just check for not-a-semicolon and be done
21511 with it; the user might have typed:
21513 class X { } c = ...;
21514 class X { } *p = ...;
21516 and so forth. Instead, enumerate all the possible tokens that
21517 might follow this production; if we don't see one of them, then
21518 complain and silently insert the semicolon. */
21520 cp_token *token = cp_lexer_peek_token (parser->lexer);
21521 bool want_semicolon = true;
21523 if (cp_next_tokens_can_be_std_attribute_p (parser))
21524 /* Don't try to parse c++11 attributes here. As per the
21525 grammar, that should be a task for
21526 cp_parser_decl_specifier_seq. */
21527 want_semicolon = false;
21529 switch (token->type)
21531 case CPP_NAME:
21532 case CPP_SEMICOLON:
21533 case CPP_MULT:
21534 case CPP_AND:
21535 case CPP_OPEN_PAREN:
21536 case CPP_CLOSE_PAREN:
21537 case CPP_COMMA:
21538 want_semicolon = false;
21539 break;
21541 /* While it's legal for type qualifiers and storage class
21542 specifiers to follow type definitions in the grammar, only
21543 compiler testsuites contain code like that. Assume that if
21544 we see such code, then what we're really seeing is a case
21545 like:
21547 class X { }
21548 const <type> var = ...;
21552 class Y { }
21553 static <type> func (...) ...
21555 i.e. the qualifier or specifier applies to the next
21556 declaration. To do so, however, we need to look ahead one
21557 more token to see if *that* token is a type specifier.
21559 This code could be improved to handle:
21561 class Z { }
21562 static const <type> var = ...; */
21563 case CPP_KEYWORD:
21564 if (keyword_is_decl_specifier (token->keyword))
21566 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
21568 /* Handling user-defined types here would be nice, but very
21569 tricky. */
21570 want_semicolon
21571 = (lookahead->type == CPP_KEYWORD
21572 && keyword_begins_type_specifier (lookahead->keyword));
21574 break;
21575 default:
21576 break;
21579 /* If we don't have a type, then something is very wrong and we
21580 shouldn't try to do anything clever. Likewise for not seeing the
21581 closing brace. */
21582 if (closing_brace && TYPE_P (type) && want_semicolon)
21584 /* Locate the closing brace. */
21585 cp_token_position prev
21586 = cp_lexer_previous_token_position (parser->lexer);
21587 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
21588 location_t loc = prev_token->location;
21590 /* We want to suggest insertion of a ';' immediately *after* the
21591 closing brace, so, if we can, offset the location by 1 column. */
21592 location_t next_loc = loc;
21593 if (!linemap_location_from_macro_expansion_p (line_table, loc))
21594 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
21596 rich_location richloc (line_table, next_loc);
21597 richloc.add_fixit_insert (next_loc, ";");
21599 if (CLASSTYPE_DECLARED_CLASS (type))
21600 error_at_rich_loc (&richloc,
21601 "expected %<;%> after class definition");
21602 else if (TREE_CODE (type) == RECORD_TYPE)
21603 error_at_rich_loc (&richloc,
21604 "expected %<;%> after struct definition");
21605 else if (TREE_CODE (type) == UNION_TYPE)
21606 error_at_rich_loc (&richloc,
21607 "expected %<;%> after union definition");
21608 else
21609 gcc_unreachable ();
21611 /* Unget one token and smash it to look as though we encountered
21612 a semicolon in the input stream. */
21613 cp_lexer_set_token_position (parser->lexer, prev);
21614 token = cp_lexer_peek_token (parser->lexer);
21615 token->type = CPP_SEMICOLON;
21616 token->keyword = RID_MAX;
21620 /* If this class is not itself within the scope of another class,
21621 then we need to parse the bodies of all of the queued function
21622 definitions. Note that the queued functions defined in a class
21623 are not always processed immediately following the
21624 class-specifier for that class. Consider:
21626 struct A {
21627 struct B { void f() { sizeof (A); } };
21630 If `f' were processed before the processing of `A' were
21631 completed, there would be no way to compute the size of `A'.
21632 Note that the nesting we are interested in here is lexical --
21633 not the semantic nesting given by TYPE_CONTEXT. In particular,
21634 for:
21636 struct A { struct B; };
21637 struct A::B { void f() { } };
21639 there is no need to delay the parsing of `A::B::f'. */
21640 if (--parser->num_classes_being_defined == 0)
21642 tree decl;
21643 tree class_type = NULL_TREE;
21644 tree pushed_scope = NULL_TREE;
21645 unsigned ix;
21646 cp_default_arg_entry *e;
21647 tree save_ccp, save_ccr;
21649 /* In a first pass, parse default arguments to the functions.
21650 Then, in a second pass, parse the bodies of the functions.
21651 This two-phased approach handles cases like:
21653 struct S {
21654 void f() { g(); }
21655 void g(int i = 3);
21659 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
21661 decl = e->decl;
21662 /* If there are default arguments that have not yet been processed,
21663 take care of them now. */
21664 if (class_type != e->class_type)
21666 if (pushed_scope)
21667 pop_scope (pushed_scope);
21668 class_type = e->class_type;
21669 pushed_scope = push_scope (class_type);
21671 /* Make sure that any template parameters are in scope. */
21672 maybe_begin_member_template_processing (decl);
21673 /* Parse the default argument expressions. */
21674 cp_parser_late_parsing_default_args (parser, decl);
21675 /* Remove any template parameters from the symbol table. */
21676 maybe_end_member_template_processing ();
21678 vec_safe_truncate (unparsed_funs_with_default_args, 0);
21679 /* Now parse any NSDMIs. */
21680 save_ccp = current_class_ptr;
21681 save_ccr = current_class_ref;
21682 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
21684 if (class_type != DECL_CONTEXT (decl))
21686 if (pushed_scope)
21687 pop_scope (pushed_scope);
21688 class_type = DECL_CONTEXT (decl);
21689 pushed_scope = push_scope (class_type);
21691 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
21692 cp_parser_late_parsing_nsdmi (parser, decl);
21694 vec_safe_truncate (unparsed_nsdmis, 0);
21695 current_class_ptr = save_ccp;
21696 current_class_ref = save_ccr;
21697 if (pushed_scope)
21698 pop_scope (pushed_scope);
21700 /* Now do some post-NSDMI bookkeeping. */
21701 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
21702 after_nsdmi_defaulted_late_checks (class_type);
21703 vec_safe_truncate (unparsed_classes, 0);
21704 after_nsdmi_defaulted_late_checks (type);
21706 /* Now parse the body of the functions. */
21707 if (flag_openmp)
21709 /* OpenMP UDRs need to be parsed before all other functions. */
21710 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21711 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
21712 cp_parser_late_parsing_for_member (parser, decl);
21713 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21714 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
21715 cp_parser_late_parsing_for_member (parser, decl);
21717 else
21718 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21719 cp_parser_late_parsing_for_member (parser, decl);
21720 vec_safe_truncate (unparsed_funs_with_definitions, 0);
21722 else
21723 vec_safe_push (unparsed_classes, type);
21725 /* Put back any saved access checks. */
21726 pop_deferring_access_checks ();
21728 /* Restore saved state. */
21729 parser->in_switch_statement_p = in_switch_statement_p;
21730 parser->in_statement = in_statement;
21731 parser->in_function_body = saved_in_function_body;
21732 parser->num_template_parameter_lists
21733 = saved_num_template_parameter_lists;
21734 parser->in_unbraced_linkage_specification_p
21735 = saved_in_unbraced_linkage_specification_p;
21737 return type;
21740 static tree
21741 cp_parser_class_specifier (cp_parser* parser)
21743 tree ret;
21744 timevar_push (TV_PARSE_STRUCT);
21745 ret = cp_parser_class_specifier_1 (parser);
21746 timevar_pop (TV_PARSE_STRUCT);
21747 return ret;
21750 /* Parse a class-head.
21752 class-head:
21753 class-key identifier [opt] base-clause [opt]
21754 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
21755 class-key nested-name-specifier [opt] template-id
21756 base-clause [opt]
21758 class-virt-specifier:
21759 final
21761 GNU Extensions:
21762 class-key attributes identifier [opt] base-clause [opt]
21763 class-key attributes nested-name-specifier identifier base-clause [opt]
21764 class-key attributes nested-name-specifier [opt] template-id
21765 base-clause [opt]
21767 Upon return BASES is initialized to the list of base classes (or
21768 NULL, if there are none) in the same form returned by
21769 cp_parser_base_clause.
21771 Returns the TYPE of the indicated class. Sets
21772 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
21773 involving a nested-name-specifier was used, and FALSE otherwise.
21775 Returns error_mark_node if this is not a class-head.
21777 Returns NULL_TREE if the class-head is syntactically valid, but
21778 semantically invalid in a way that means we should skip the entire
21779 body of the class. */
21781 static tree
21782 cp_parser_class_head (cp_parser* parser,
21783 bool* nested_name_specifier_p)
21785 tree nested_name_specifier;
21786 enum tag_types class_key;
21787 tree id = NULL_TREE;
21788 tree type = NULL_TREE;
21789 tree attributes;
21790 tree bases;
21791 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21792 bool template_id_p = false;
21793 bool qualified_p = false;
21794 bool invalid_nested_name_p = false;
21795 bool invalid_explicit_specialization_p = false;
21796 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
21797 tree pushed_scope = NULL_TREE;
21798 unsigned num_templates;
21799 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
21800 /* Assume no nested-name-specifier will be present. */
21801 *nested_name_specifier_p = false;
21802 /* Assume no template parameter lists will be used in defining the
21803 type. */
21804 num_templates = 0;
21805 parser->colon_corrects_to_scope_p = false;
21807 /* Look for the class-key. */
21808 class_key = cp_parser_class_key (parser);
21809 if (class_key == none_type)
21810 return error_mark_node;
21812 location_t class_head_start_location = input_location;
21814 /* Parse the attributes. */
21815 attributes = cp_parser_attributes_opt (parser);
21817 /* If the next token is `::', that is invalid -- but sometimes
21818 people do try to write:
21820 struct ::S {};
21822 Handle this gracefully by accepting the extra qualifier, and then
21823 issuing an error about it later if this really is a
21824 class-head. If it turns out just to be an elaborated type
21825 specifier, remain silent. */
21826 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
21827 qualified_p = true;
21829 push_deferring_access_checks (dk_no_check);
21831 /* Determine the name of the class. Begin by looking for an
21832 optional nested-name-specifier. */
21833 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
21834 nested_name_specifier
21835 = cp_parser_nested_name_specifier_opt (parser,
21836 /*typename_keyword_p=*/false,
21837 /*check_dependency_p=*/false,
21838 /*type_p=*/true,
21839 /*is_declaration=*/false);
21840 /* If there was a nested-name-specifier, then there *must* be an
21841 identifier. */
21842 if (nested_name_specifier)
21844 type_start_token = cp_lexer_peek_token (parser->lexer);
21845 /* Although the grammar says `identifier', it really means
21846 `class-name' or `template-name'. You are only allowed to
21847 define a class that has already been declared with this
21848 syntax.
21850 The proposed resolution for Core Issue 180 says that wherever
21851 you see `class T::X' you should treat `X' as a type-name.
21853 It is OK to define an inaccessible class; for example:
21855 class A { class B; };
21856 class A::B {};
21858 We do not know if we will see a class-name, or a
21859 template-name. We look for a class-name first, in case the
21860 class-name is a template-id; if we looked for the
21861 template-name first we would stop after the template-name. */
21862 cp_parser_parse_tentatively (parser);
21863 type = cp_parser_class_name (parser,
21864 /*typename_keyword_p=*/false,
21865 /*template_keyword_p=*/false,
21866 class_type,
21867 /*check_dependency_p=*/false,
21868 /*class_head_p=*/true,
21869 /*is_declaration=*/false);
21870 /* If that didn't work, ignore the nested-name-specifier. */
21871 if (!cp_parser_parse_definitely (parser))
21873 invalid_nested_name_p = true;
21874 type_start_token = cp_lexer_peek_token (parser->lexer);
21875 id = cp_parser_identifier (parser);
21876 if (id == error_mark_node)
21877 id = NULL_TREE;
21879 /* If we could not find a corresponding TYPE, treat this
21880 declaration like an unqualified declaration. */
21881 if (type == error_mark_node)
21882 nested_name_specifier = NULL_TREE;
21883 /* Otherwise, count the number of templates used in TYPE and its
21884 containing scopes. */
21885 else
21887 tree scope;
21889 for (scope = TREE_TYPE (type);
21890 scope && TREE_CODE (scope) != NAMESPACE_DECL;
21891 scope = get_containing_scope (scope))
21892 if (TYPE_P (scope)
21893 && CLASS_TYPE_P (scope)
21894 && CLASSTYPE_TEMPLATE_INFO (scope)
21895 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
21896 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
21897 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
21898 ++num_templates;
21901 /* Otherwise, the identifier is optional. */
21902 else
21904 /* We don't know whether what comes next is a template-id,
21905 an identifier, or nothing at all. */
21906 cp_parser_parse_tentatively (parser);
21907 /* Check for a template-id. */
21908 type_start_token = cp_lexer_peek_token (parser->lexer);
21909 id = cp_parser_template_id (parser,
21910 /*template_keyword_p=*/false,
21911 /*check_dependency_p=*/true,
21912 class_key,
21913 /*is_declaration=*/true);
21914 /* If that didn't work, it could still be an identifier. */
21915 if (!cp_parser_parse_definitely (parser))
21917 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21919 type_start_token = cp_lexer_peek_token (parser->lexer);
21920 id = cp_parser_identifier (parser);
21922 else
21923 id = NULL_TREE;
21925 else
21927 template_id_p = true;
21928 ++num_templates;
21932 pop_deferring_access_checks ();
21934 if (id)
21936 cp_parser_check_for_invalid_template_id (parser, id,
21937 class_key,
21938 type_start_token->location);
21940 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
21942 /* If it's not a `:' or a `{' then we can't really be looking at a
21943 class-head, since a class-head only appears as part of a
21944 class-specifier. We have to detect this situation before calling
21945 xref_tag, since that has irreversible side-effects. */
21946 if (!cp_parser_next_token_starts_class_definition_p (parser))
21948 cp_parser_error (parser, "expected %<{%> or %<:%>");
21949 type = error_mark_node;
21950 goto out;
21953 /* At this point, we're going ahead with the class-specifier, even
21954 if some other problem occurs. */
21955 cp_parser_commit_to_tentative_parse (parser);
21956 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
21958 cp_parser_error (parser,
21959 "cannot specify %<override%> for a class");
21960 type = error_mark_node;
21961 goto out;
21963 /* Issue the error about the overly-qualified name now. */
21964 if (qualified_p)
21966 cp_parser_error (parser,
21967 "global qualification of class name is invalid");
21968 type = error_mark_node;
21969 goto out;
21971 else if (invalid_nested_name_p)
21973 cp_parser_error (parser,
21974 "qualified name does not name a class");
21975 type = error_mark_node;
21976 goto out;
21978 else if (nested_name_specifier)
21980 tree scope;
21982 /* Reject typedef-names in class heads. */
21983 if (!DECL_IMPLICIT_TYPEDEF_P (type))
21985 error_at (type_start_token->location,
21986 "invalid class name in declaration of %qD",
21987 type);
21988 type = NULL_TREE;
21989 goto done;
21992 /* Figure out in what scope the declaration is being placed. */
21993 scope = current_scope ();
21994 /* If that scope does not contain the scope in which the
21995 class was originally declared, the program is invalid. */
21996 if (scope && !is_ancestor (scope, nested_name_specifier))
21998 if (at_namespace_scope_p ())
21999 error_at (type_start_token->location,
22000 "declaration of %qD in namespace %qD which does not "
22001 "enclose %qD",
22002 type, scope, nested_name_specifier);
22003 else
22004 error_at (type_start_token->location,
22005 "declaration of %qD in %qD which does not enclose %qD",
22006 type, scope, nested_name_specifier);
22007 type = NULL_TREE;
22008 goto done;
22010 /* [dcl.meaning]
22012 A declarator-id shall not be qualified except for the
22013 definition of a ... nested class outside of its class
22014 ... [or] the definition or explicit instantiation of a
22015 class member of a namespace outside of its namespace. */
22016 if (scope == nested_name_specifier)
22018 permerror (nested_name_specifier_token_start->location,
22019 "extra qualification not allowed");
22020 nested_name_specifier = NULL_TREE;
22021 num_templates = 0;
22024 /* An explicit-specialization must be preceded by "template <>". If
22025 it is not, try to recover gracefully. */
22026 if (at_namespace_scope_p ()
22027 && parser->num_template_parameter_lists == 0
22028 && template_id_p)
22030 /* Build a location of this form:
22031 struct typename <ARGS>
22032 ^~~~~~~~~~~~~~~~~~~~~~
22033 with caret==start at the start token, and
22034 finishing at the end of the type. */
22035 location_t reported_loc
22036 = make_location (class_head_start_location,
22037 class_head_start_location,
22038 get_finish (type_start_token->location));
22039 rich_location richloc (line_table, reported_loc);
22040 richloc.add_fixit_insert (class_head_start_location, "template <> ");
22041 error_at_rich_loc
22042 (&richloc,
22043 "an explicit specialization must be preceded by %<template <>%>");
22044 invalid_explicit_specialization_p = true;
22045 /* Take the same action that would have been taken by
22046 cp_parser_explicit_specialization. */
22047 ++parser->num_template_parameter_lists;
22048 begin_specialization ();
22050 /* There must be no "return" statements between this point and the
22051 end of this function; set "type "to the correct return value and
22052 use "goto done;" to return. */
22053 /* Make sure that the right number of template parameters were
22054 present. */
22055 if (!cp_parser_check_template_parameters (parser, num_templates,
22056 type_start_token->location,
22057 /*declarator=*/NULL))
22059 /* If something went wrong, there is no point in even trying to
22060 process the class-definition. */
22061 type = NULL_TREE;
22062 goto done;
22065 /* Look up the type. */
22066 if (template_id_p)
22068 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
22069 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
22070 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
22072 error_at (type_start_token->location,
22073 "function template %qD redeclared as a class template", id);
22074 type = error_mark_node;
22076 else
22078 type = TREE_TYPE (id);
22079 type = maybe_process_partial_specialization (type);
22081 if (nested_name_specifier)
22082 pushed_scope = push_scope (nested_name_specifier);
22084 else if (nested_name_specifier)
22086 tree class_type;
22088 /* Given:
22090 template <typename T> struct S { struct T };
22091 template <typename T> struct S<T>::T { };
22093 we will get a TYPENAME_TYPE when processing the definition of
22094 `S::T'. We need to resolve it to the actual type before we
22095 try to define it. */
22096 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
22098 class_type = resolve_typename_type (TREE_TYPE (type),
22099 /*only_current_p=*/false);
22100 if (TREE_CODE (class_type) != TYPENAME_TYPE)
22101 type = TYPE_NAME (class_type);
22102 else
22104 cp_parser_error (parser, "could not resolve typename type");
22105 type = error_mark_node;
22109 if (maybe_process_partial_specialization (TREE_TYPE (type))
22110 == error_mark_node)
22112 type = NULL_TREE;
22113 goto done;
22116 class_type = current_class_type;
22117 /* Enter the scope indicated by the nested-name-specifier. */
22118 pushed_scope = push_scope (nested_name_specifier);
22119 /* Get the canonical version of this type. */
22120 type = TYPE_MAIN_DECL (TREE_TYPE (type));
22121 /* Call push_template_decl if it seems like we should be defining a
22122 template either from the template headers or the type we're
22123 defining, so that we diagnose both extra and missing headers. */
22124 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
22125 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
22126 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
22128 type = push_template_decl (type);
22129 if (type == error_mark_node)
22131 type = NULL_TREE;
22132 goto done;
22136 type = TREE_TYPE (type);
22137 *nested_name_specifier_p = true;
22139 else /* The name is not a nested name. */
22141 /* If the class was unnamed, create a dummy name. */
22142 if (!id)
22143 id = make_anon_name ();
22144 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
22145 parser->num_template_parameter_lists);
22148 /* Indicate whether this class was declared as a `class' or as a
22149 `struct'. */
22150 if (TREE_CODE (type) == RECORD_TYPE)
22151 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
22152 cp_parser_check_class_key (class_key, type);
22154 /* If this type was already complete, and we see another definition,
22155 that's an error. */
22156 if (type != error_mark_node && COMPLETE_TYPE_P (type))
22158 error_at (type_start_token->location, "redefinition of %q#T",
22159 type);
22160 inform (location_of (type), "previous definition of %q#T",
22161 type);
22162 type = NULL_TREE;
22163 goto done;
22165 else if (type == error_mark_node)
22166 type = NULL_TREE;
22168 if (type)
22170 /* Apply attributes now, before any use of the class as a template
22171 argument in its base list. */
22172 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
22173 fixup_attribute_variants (type);
22176 /* We will have entered the scope containing the class; the names of
22177 base classes should be looked up in that context. For example:
22179 struct A { struct B {}; struct C; };
22180 struct A::C : B {};
22182 is valid. */
22184 /* Get the list of base-classes, if there is one. */
22185 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22187 /* PR59482: enter the class scope so that base-specifiers are looked
22188 up correctly. */
22189 if (type)
22190 pushclass (type);
22191 bases = cp_parser_base_clause (parser);
22192 /* PR59482: get out of the previously pushed class scope so that the
22193 subsequent pops pop the right thing. */
22194 if (type)
22195 popclass ();
22197 else
22198 bases = NULL_TREE;
22200 /* If we're really defining a class, process the base classes.
22201 If they're invalid, fail. */
22202 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22203 xref_basetypes (type, bases);
22205 done:
22206 /* Leave the scope given by the nested-name-specifier. We will
22207 enter the class scope itself while processing the members. */
22208 if (pushed_scope)
22209 pop_scope (pushed_scope);
22211 if (invalid_explicit_specialization_p)
22213 end_specialization ();
22214 --parser->num_template_parameter_lists;
22217 if (type)
22218 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
22219 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
22220 CLASSTYPE_FINAL (type) = 1;
22221 out:
22222 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22223 return type;
22226 /* Parse a class-key.
22228 class-key:
22229 class
22230 struct
22231 union
22233 Returns the kind of class-key specified, or none_type to indicate
22234 error. */
22236 static enum tag_types
22237 cp_parser_class_key (cp_parser* parser)
22239 cp_token *token;
22240 enum tag_types tag_type;
22242 /* Look for the class-key. */
22243 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
22244 if (!token)
22245 return none_type;
22247 /* Check to see if the TOKEN is a class-key. */
22248 tag_type = cp_parser_token_is_class_key (token);
22249 if (!tag_type)
22250 cp_parser_error (parser, "expected class-key");
22251 return tag_type;
22254 /* Parse a type-parameter-key.
22256 type-parameter-key:
22257 class
22258 typename
22261 static void
22262 cp_parser_type_parameter_key (cp_parser* parser)
22264 /* Look for the type-parameter-key. */
22265 enum tag_types tag_type = none_type;
22266 cp_token *token = cp_lexer_peek_token (parser->lexer);
22267 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
22269 cp_lexer_consume_token (parser->lexer);
22270 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
22271 /* typename is not allowed in a template template parameter
22272 by the standard until C++1Z. */
22273 pedwarn (token->location, OPT_Wpedantic,
22274 "ISO C++ forbids typename key in template template parameter;"
22275 " use -std=c++1z or -std=gnu++1z");
22277 else
22278 cp_parser_error (parser, "expected %<class%> or %<typename%>");
22280 return;
22283 /* Parse an (optional) member-specification.
22285 member-specification:
22286 member-declaration member-specification [opt]
22287 access-specifier : member-specification [opt] */
22289 static void
22290 cp_parser_member_specification_opt (cp_parser* parser)
22292 while (true)
22294 cp_token *token;
22295 enum rid keyword;
22297 /* Peek at the next token. */
22298 token = cp_lexer_peek_token (parser->lexer);
22299 /* If it's a `}', or EOF then we've seen all the members. */
22300 if (token->type == CPP_CLOSE_BRACE
22301 || token->type == CPP_EOF
22302 || token->type == CPP_PRAGMA_EOL)
22303 break;
22305 /* See if this token is a keyword. */
22306 keyword = token->keyword;
22307 switch (keyword)
22309 case RID_PUBLIC:
22310 case RID_PROTECTED:
22311 case RID_PRIVATE:
22312 /* Consume the access-specifier. */
22313 cp_lexer_consume_token (parser->lexer);
22314 /* Remember which access-specifier is active. */
22315 current_access_specifier = token->u.value;
22316 /* Look for the `:'. */
22317 cp_parser_require (parser, CPP_COLON, RT_COLON);
22318 break;
22320 default:
22321 /* Accept #pragmas at class scope. */
22322 if (token->type == CPP_PRAGMA)
22324 cp_parser_pragma (parser, pragma_member, NULL);
22325 break;
22328 /* Otherwise, the next construction must be a
22329 member-declaration. */
22330 cp_parser_member_declaration (parser);
22335 /* Parse a member-declaration.
22337 member-declaration:
22338 decl-specifier-seq [opt] member-declarator-list [opt] ;
22339 function-definition ; [opt]
22340 :: [opt] nested-name-specifier template [opt] unqualified-id ;
22341 using-declaration
22342 template-declaration
22343 alias-declaration
22345 member-declarator-list:
22346 member-declarator
22347 member-declarator-list , member-declarator
22349 member-declarator:
22350 declarator pure-specifier [opt]
22351 declarator constant-initializer [opt]
22352 identifier [opt] : constant-expression
22354 GNU Extensions:
22356 member-declaration:
22357 __extension__ member-declaration
22359 member-declarator:
22360 declarator attributes [opt] pure-specifier [opt]
22361 declarator attributes [opt] constant-initializer [opt]
22362 identifier [opt] attributes [opt] : constant-expression
22364 C++0x Extensions:
22366 member-declaration:
22367 static_assert-declaration */
22369 static void
22370 cp_parser_member_declaration (cp_parser* parser)
22372 cp_decl_specifier_seq decl_specifiers;
22373 tree prefix_attributes;
22374 tree decl;
22375 int declares_class_or_enum;
22376 bool friend_p;
22377 cp_token *token = NULL;
22378 cp_token *decl_spec_token_start = NULL;
22379 cp_token *initializer_token_start = NULL;
22380 int saved_pedantic;
22381 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22383 /* Check for the `__extension__' keyword. */
22384 if (cp_parser_extension_opt (parser, &saved_pedantic))
22386 /* Recurse. */
22387 cp_parser_member_declaration (parser);
22388 /* Restore the old value of the PEDANTIC flag. */
22389 pedantic = saved_pedantic;
22391 return;
22394 /* Check for a template-declaration. */
22395 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22397 /* An explicit specialization here is an error condition, and we
22398 expect the specialization handler to detect and report this. */
22399 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
22400 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
22401 cp_parser_explicit_specialization (parser);
22402 else
22403 cp_parser_template_declaration (parser, /*member_p=*/true);
22405 return;
22407 /* Check for a template introduction. */
22408 else if (cp_parser_template_declaration_after_export (parser, true))
22409 return;
22411 /* Check for a using-declaration. */
22412 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
22414 if (cxx_dialect < cxx11)
22416 /* Parse the using-declaration. */
22417 cp_parser_using_declaration (parser,
22418 /*access_declaration_p=*/false);
22419 return;
22421 else
22423 tree decl;
22424 bool alias_decl_expected;
22425 cp_parser_parse_tentatively (parser);
22426 decl = cp_parser_alias_declaration (parser);
22427 /* Note that if we actually see the '=' token after the
22428 identifier, cp_parser_alias_declaration commits the
22429 tentative parse. In that case, we really expect an
22430 alias-declaration. Otherwise, we expect a using
22431 declaration. */
22432 alias_decl_expected =
22433 !cp_parser_uncommitted_to_tentative_parse_p (parser);
22434 cp_parser_parse_definitely (parser);
22436 if (alias_decl_expected)
22437 finish_member_declaration (decl);
22438 else
22439 cp_parser_using_declaration (parser,
22440 /*access_declaration_p=*/false);
22441 return;
22445 /* Check for @defs. */
22446 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
22448 tree ivar, member;
22449 tree ivar_chains = cp_parser_objc_defs_expression (parser);
22450 ivar = ivar_chains;
22451 while (ivar)
22453 member = ivar;
22454 ivar = TREE_CHAIN (member);
22455 TREE_CHAIN (member) = NULL_TREE;
22456 finish_member_declaration (member);
22458 return;
22461 /* If the next token is `static_assert' we have a static assertion. */
22462 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
22464 cp_parser_static_assert (parser, /*member_p=*/true);
22465 return;
22468 parser->colon_corrects_to_scope_p = false;
22470 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
22471 goto out;
22473 /* Parse the decl-specifier-seq. */
22474 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22475 cp_parser_decl_specifier_seq (parser,
22476 CP_PARSER_FLAGS_OPTIONAL,
22477 &decl_specifiers,
22478 &declares_class_or_enum);
22479 /* Check for an invalid type-name. */
22480 if (!decl_specifiers.any_type_specifiers_p
22481 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22482 goto out;
22483 /* If there is no declarator, then the decl-specifier-seq should
22484 specify a type. */
22485 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22487 /* If there was no decl-specifier-seq, and the next token is a
22488 `;', then we have something like:
22490 struct S { ; };
22492 [class.mem]
22494 Each member-declaration shall declare at least one member
22495 name of the class. */
22496 if (!decl_specifiers.any_specifiers_p)
22498 cp_token *token = cp_lexer_peek_token (parser->lexer);
22499 if (!in_system_header_at (token->location))
22500 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
22502 else
22504 tree type;
22506 /* See if this declaration is a friend. */
22507 friend_p = cp_parser_friend_p (&decl_specifiers);
22508 /* If there were decl-specifiers, check to see if there was
22509 a class-declaration. */
22510 type = check_tag_decl (&decl_specifiers,
22511 /*explicit_type_instantiation_p=*/false);
22512 /* Nested classes have already been added to the class, but
22513 a `friend' needs to be explicitly registered. */
22514 if (friend_p)
22516 /* If the `friend' keyword was present, the friend must
22517 be introduced with a class-key. */
22518 if (!declares_class_or_enum && cxx_dialect < cxx11)
22519 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
22520 "in C++03 a class-key must be used "
22521 "when declaring a friend");
22522 /* In this case:
22524 template <typename T> struct A {
22525 friend struct A<T>::B;
22528 A<T>::B will be represented by a TYPENAME_TYPE, and
22529 therefore not recognized by check_tag_decl. */
22530 if (!type)
22532 type = decl_specifiers.type;
22533 if (type && TREE_CODE (type) == TYPE_DECL)
22534 type = TREE_TYPE (type);
22536 if (!type || !TYPE_P (type))
22537 error_at (decl_spec_token_start->location,
22538 "friend declaration does not name a class or "
22539 "function");
22540 else
22541 make_friend_class (current_class_type, type,
22542 /*complain=*/true);
22544 /* If there is no TYPE, an error message will already have
22545 been issued. */
22546 else if (!type || type == error_mark_node)
22548 /* An anonymous aggregate has to be handled specially; such
22549 a declaration really declares a data member (with a
22550 particular type), as opposed to a nested class. */
22551 else if (ANON_AGGR_TYPE_P (type))
22553 /* C++11 9.5/6. */
22554 if (decl_specifiers.storage_class != sc_none)
22555 error_at (decl_spec_token_start->location,
22556 "a storage class on an anonymous aggregate "
22557 "in class scope is not allowed");
22559 /* Remove constructors and such from TYPE, now that we
22560 know it is an anonymous aggregate. */
22561 fixup_anonymous_aggr (type);
22562 /* And make the corresponding data member. */
22563 decl = build_decl (decl_spec_token_start->location,
22564 FIELD_DECL, NULL_TREE, type);
22565 /* Add it to the class. */
22566 finish_member_declaration (decl);
22568 else
22569 cp_parser_check_access_in_redeclaration
22570 (TYPE_NAME (type),
22571 decl_spec_token_start->location);
22574 else
22576 bool assume_semicolon = false;
22578 /* Clear attributes from the decl_specifiers but keep them
22579 around as prefix attributes that apply them to the entity
22580 being declared. */
22581 prefix_attributes = decl_specifiers.attributes;
22582 decl_specifiers.attributes = NULL_TREE;
22584 /* See if these declarations will be friends. */
22585 friend_p = cp_parser_friend_p (&decl_specifiers);
22587 /* Keep going until we hit the `;' at the end of the
22588 declaration. */
22589 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22591 tree attributes = NULL_TREE;
22592 tree first_attribute;
22594 /* Peek at the next token. */
22595 token = cp_lexer_peek_token (parser->lexer);
22597 /* Check for a bitfield declaration. */
22598 if (token->type == CPP_COLON
22599 || (token->type == CPP_NAME
22600 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
22601 == CPP_COLON))
22603 tree identifier;
22604 tree width;
22606 /* Get the name of the bitfield. Note that we cannot just
22607 check TOKEN here because it may have been invalidated by
22608 the call to cp_lexer_peek_nth_token above. */
22609 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
22610 identifier = cp_parser_identifier (parser);
22611 else
22612 identifier = NULL_TREE;
22614 /* Consume the `:' token. */
22615 cp_lexer_consume_token (parser->lexer);
22616 /* Get the width of the bitfield. */
22617 width
22618 = cp_parser_constant_expression (parser);
22620 /* Look for attributes that apply to the bitfield. */
22621 attributes = cp_parser_attributes_opt (parser);
22622 /* Remember which attributes are prefix attributes and
22623 which are not. */
22624 first_attribute = attributes;
22625 /* Combine the attributes. */
22626 attributes = chainon (prefix_attributes, attributes);
22628 /* Create the bitfield declaration. */
22629 decl = grokbitfield (identifier
22630 ? make_id_declarator (NULL_TREE,
22631 identifier,
22632 sfk_none)
22633 : NULL,
22634 &decl_specifiers,
22635 width,
22636 attributes);
22638 else
22640 cp_declarator *declarator;
22641 tree initializer;
22642 tree asm_specification;
22643 int ctor_dtor_or_conv_p;
22645 /* Parse the declarator. */
22646 declarator
22647 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22648 &ctor_dtor_or_conv_p,
22649 /*parenthesized_p=*/NULL,
22650 /*member_p=*/true,
22651 friend_p);
22653 /* If something went wrong parsing the declarator, make sure
22654 that we at least consume some tokens. */
22655 if (declarator == cp_error_declarator)
22657 /* Skip to the end of the statement. */
22658 cp_parser_skip_to_end_of_statement (parser);
22659 /* If the next token is not a semicolon, that is
22660 probably because we just skipped over the body of
22661 a function. So, we consume a semicolon if
22662 present, but do not issue an error message if it
22663 is not present. */
22664 if (cp_lexer_next_token_is (parser->lexer,
22665 CPP_SEMICOLON))
22666 cp_lexer_consume_token (parser->lexer);
22667 goto out;
22670 if (declares_class_or_enum & 2)
22671 cp_parser_check_for_definition_in_return_type
22672 (declarator, decl_specifiers.type,
22673 decl_specifiers.locations[ds_type_spec]);
22675 /* Look for an asm-specification. */
22676 asm_specification = cp_parser_asm_specification_opt (parser);
22677 /* Look for attributes that apply to the declaration. */
22678 attributes = cp_parser_attributes_opt (parser);
22679 /* Remember which attributes are prefix attributes and
22680 which are not. */
22681 first_attribute = attributes;
22682 /* Combine the attributes. */
22683 attributes = chainon (prefix_attributes, attributes);
22685 /* If it's an `=', then we have a constant-initializer or a
22686 pure-specifier. It is not correct to parse the
22687 initializer before registering the member declaration
22688 since the member declaration should be in scope while
22689 its initializer is processed. However, the rest of the
22690 front end does not yet provide an interface that allows
22691 us to handle this correctly. */
22692 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22694 /* In [class.mem]:
22696 A pure-specifier shall be used only in the declaration of
22697 a virtual function.
22699 A member-declarator can contain a constant-initializer
22700 only if it declares a static member of integral or
22701 enumeration type.
22703 Therefore, if the DECLARATOR is for a function, we look
22704 for a pure-specifier; otherwise, we look for a
22705 constant-initializer. When we call `grokfield', it will
22706 perform more stringent semantics checks. */
22707 initializer_token_start = cp_lexer_peek_token (parser->lexer);
22708 if (function_declarator_p (declarator)
22709 || (decl_specifiers.type
22710 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
22711 && declarator->kind == cdk_id
22712 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
22713 == FUNCTION_TYPE)))
22714 initializer = cp_parser_pure_specifier (parser);
22715 else if (decl_specifiers.storage_class != sc_static)
22716 initializer = cp_parser_save_nsdmi (parser);
22717 else if (cxx_dialect >= cxx11)
22719 bool nonconst;
22720 /* Don't require a constant rvalue in C++11, since we
22721 might want a reference constant. We'll enforce
22722 constancy later. */
22723 cp_lexer_consume_token (parser->lexer);
22724 /* Parse the initializer. */
22725 initializer = cp_parser_initializer_clause (parser,
22726 &nonconst);
22728 else
22729 /* Parse the initializer. */
22730 initializer = cp_parser_constant_initializer (parser);
22732 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
22733 && !function_declarator_p (declarator))
22735 bool x;
22736 if (decl_specifiers.storage_class != sc_static)
22737 initializer = cp_parser_save_nsdmi (parser);
22738 else
22739 initializer = cp_parser_initializer (parser, &x, &x);
22741 /* Otherwise, there is no initializer. */
22742 else
22743 initializer = NULL_TREE;
22745 /* See if we are probably looking at a function
22746 definition. We are certainly not looking at a
22747 member-declarator. Calling `grokfield' has
22748 side-effects, so we must not do it unless we are sure
22749 that we are looking at a member-declarator. */
22750 if (cp_parser_token_starts_function_definition_p
22751 (cp_lexer_peek_token (parser->lexer)))
22753 /* The grammar does not allow a pure-specifier to be
22754 used when a member function is defined. (It is
22755 possible that this fact is an oversight in the
22756 standard, since a pure function may be defined
22757 outside of the class-specifier. */
22758 if (initializer && initializer_token_start)
22759 error_at (initializer_token_start->location,
22760 "pure-specifier on function-definition");
22761 decl = cp_parser_save_member_function_body (parser,
22762 &decl_specifiers,
22763 declarator,
22764 attributes);
22765 if (parser->fully_implicit_function_template_p)
22766 decl = finish_fully_implicit_template (parser, decl);
22767 /* If the member was not a friend, declare it here. */
22768 if (!friend_p)
22769 finish_member_declaration (decl);
22770 /* Peek at the next token. */
22771 token = cp_lexer_peek_token (parser->lexer);
22772 /* If the next token is a semicolon, consume it. */
22773 if (token->type == CPP_SEMICOLON)
22774 cp_lexer_consume_token (parser->lexer);
22775 goto out;
22777 else
22778 if (declarator->kind == cdk_function)
22779 declarator->id_loc = token->location;
22780 /* Create the declaration. */
22781 decl = grokfield (declarator, &decl_specifiers,
22782 initializer, /*init_const_expr_p=*/true,
22783 asm_specification, attributes);
22784 if (parser->fully_implicit_function_template_p)
22786 if (friend_p)
22787 finish_fully_implicit_template (parser, 0);
22788 else
22789 decl = finish_fully_implicit_template (parser, decl);
22793 cp_finalize_omp_declare_simd (parser, decl);
22794 cp_finalize_oacc_routine (parser, decl, false);
22796 /* Reset PREFIX_ATTRIBUTES. */
22797 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22798 attributes = TREE_CHAIN (attributes);
22799 if (attributes)
22800 TREE_CHAIN (attributes) = NULL_TREE;
22802 /* If there is any qualification still in effect, clear it
22803 now; we will be starting fresh with the next declarator. */
22804 parser->scope = NULL_TREE;
22805 parser->qualifying_scope = NULL_TREE;
22806 parser->object_scope = NULL_TREE;
22807 /* If it's a `,', then there are more declarators. */
22808 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22810 cp_lexer_consume_token (parser->lexer);
22811 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22813 cp_token *token = cp_lexer_previous_token (parser->lexer);
22814 error_at (token->location,
22815 "stray %<,%> at end of member declaration");
22818 /* If the next token isn't a `;', then we have a parse error. */
22819 else if (cp_lexer_next_token_is_not (parser->lexer,
22820 CPP_SEMICOLON))
22822 /* The next token might be a ways away from where the
22823 actual semicolon is missing. Find the previous token
22824 and use that for our error position. */
22825 cp_token *token = cp_lexer_previous_token (parser->lexer);
22826 error_at (token->location,
22827 "expected %<;%> at end of member declaration");
22829 /* Assume that the user meant to provide a semicolon. If
22830 we were to cp_parser_skip_to_end_of_statement, we might
22831 skip to a semicolon inside a member function definition
22832 and issue nonsensical error messages. */
22833 assume_semicolon = true;
22836 if (decl)
22838 /* Add DECL to the list of members. */
22839 if (!friend_p
22840 /* Explicitly include, eg, NSDMIs, for better error
22841 recovery (c++/58650). */
22842 || !DECL_DECLARES_FUNCTION_P (decl))
22843 finish_member_declaration (decl);
22845 if (TREE_CODE (decl) == FUNCTION_DECL)
22846 cp_parser_save_default_args (parser, decl);
22847 else if (TREE_CODE (decl) == FIELD_DECL
22848 && !DECL_C_BIT_FIELD (decl)
22849 && DECL_INITIAL (decl))
22850 /* Add DECL to the queue of NSDMI to be parsed later. */
22851 vec_safe_push (unparsed_nsdmis, decl);
22854 if (assume_semicolon)
22855 goto out;
22859 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22860 out:
22861 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22864 /* Parse a pure-specifier.
22866 pure-specifier:
22869 Returns INTEGER_ZERO_NODE if a pure specifier is found.
22870 Otherwise, ERROR_MARK_NODE is returned. */
22872 static tree
22873 cp_parser_pure_specifier (cp_parser* parser)
22875 cp_token *token;
22877 /* Look for the `=' token. */
22878 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22879 return error_mark_node;
22880 /* Look for the `0' token. */
22881 token = cp_lexer_peek_token (parser->lexer);
22883 if (token->type == CPP_EOF
22884 || token->type == CPP_PRAGMA_EOL)
22885 return error_mark_node;
22887 cp_lexer_consume_token (parser->lexer);
22889 /* Accept = default or = delete in c++0x mode. */
22890 if (token->keyword == RID_DEFAULT
22891 || token->keyword == RID_DELETE)
22893 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
22894 return token->u.value;
22897 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
22898 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
22900 cp_parser_error (parser,
22901 "invalid pure specifier (only %<= 0%> is allowed)");
22902 cp_parser_skip_to_end_of_statement (parser);
22903 return error_mark_node;
22905 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
22907 error_at (token->location, "templates may not be %<virtual%>");
22908 return error_mark_node;
22911 return integer_zero_node;
22914 /* Parse a constant-initializer.
22916 constant-initializer:
22917 = constant-expression
22919 Returns a representation of the constant-expression. */
22921 static tree
22922 cp_parser_constant_initializer (cp_parser* parser)
22924 /* Look for the `=' token. */
22925 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22926 return error_mark_node;
22928 /* It is invalid to write:
22930 struct S { static const int i = { 7 }; };
22933 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22935 cp_parser_error (parser,
22936 "a brace-enclosed initializer is not allowed here");
22937 /* Consume the opening brace. */
22938 cp_lexer_consume_token (parser->lexer);
22939 /* Skip the initializer. */
22940 cp_parser_skip_to_closing_brace (parser);
22941 /* Look for the trailing `}'. */
22942 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22944 return error_mark_node;
22947 return cp_parser_constant_expression (parser);
22950 /* Derived classes [gram.class.derived] */
22952 /* Parse a base-clause.
22954 base-clause:
22955 : base-specifier-list
22957 base-specifier-list:
22958 base-specifier ... [opt]
22959 base-specifier-list , base-specifier ... [opt]
22961 Returns a TREE_LIST representing the base-classes, in the order in
22962 which they were declared. The representation of each node is as
22963 described by cp_parser_base_specifier.
22965 In the case that no bases are specified, this function will return
22966 NULL_TREE, not ERROR_MARK_NODE. */
22968 static tree
22969 cp_parser_base_clause (cp_parser* parser)
22971 tree bases = NULL_TREE;
22973 /* Look for the `:' that begins the list. */
22974 cp_parser_require (parser, CPP_COLON, RT_COLON);
22976 /* Scan the base-specifier-list. */
22977 while (true)
22979 cp_token *token;
22980 tree base;
22981 bool pack_expansion_p = false;
22983 /* Look for the base-specifier. */
22984 base = cp_parser_base_specifier (parser);
22985 /* Look for the (optional) ellipsis. */
22986 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22988 /* Consume the `...'. */
22989 cp_lexer_consume_token (parser->lexer);
22991 pack_expansion_p = true;
22994 /* Add BASE to the front of the list. */
22995 if (base && base != error_mark_node)
22997 if (pack_expansion_p)
22998 /* Make this a pack expansion type. */
22999 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
23001 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
23003 TREE_CHAIN (base) = bases;
23004 bases = base;
23007 /* Peek at the next token. */
23008 token = cp_lexer_peek_token (parser->lexer);
23009 /* If it's not a comma, then the list is complete. */
23010 if (token->type != CPP_COMMA)
23011 break;
23012 /* Consume the `,'. */
23013 cp_lexer_consume_token (parser->lexer);
23016 /* PARSER->SCOPE may still be non-NULL at this point, if the last
23017 base class had a qualified name. However, the next name that
23018 appears is certainly not qualified. */
23019 parser->scope = NULL_TREE;
23020 parser->qualifying_scope = NULL_TREE;
23021 parser->object_scope = NULL_TREE;
23023 return nreverse (bases);
23026 /* Parse a base-specifier.
23028 base-specifier:
23029 :: [opt] nested-name-specifier [opt] class-name
23030 virtual access-specifier [opt] :: [opt] nested-name-specifier
23031 [opt] class-name
23032 access-specifier virtual [opt] :: [opt] nested-name-specifier
23033 [opt] class-name
23035 Returns a TREE_LIST. The TREE_PURPOSE will be one of
23036 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
23037 indicate the specifiers provided. The TREE_VALUE will be a TYPE
23038 (or the ERROR_MARK_NODE) indicating the type that was specified. */
23040 static tree
23041 cp_parser_base_specifier (cp_parser* parser)
23043 cp_token *token;
23044 bool done = false;
23045 bool virtual_p = false;
23046 bool duplicate_virtual_error_issued_p = false;
23047 bool duplicate_access_error_issued_p = false;
23048 bool class_scope_p, template_p;
23049 tree access = access_default_node;
23050 tree type;
23052 /* Process the optional `virtual' and `access-specifier'. */
23053 while (!done)
23055 /* Peek at the next token. */
23056 token = cp_lexer_peek_token (parser->lexer);
23057 /* Process `virtual'. */
23058 switch (token->keyword)
23060 case RID_VIRTUAL:
23061 /* If `virtual' appears more than once, issue an error. */
23062 if (virtual_p && !duplicate_virtual_error_issued_p)
23064 cp_parser_error (parser,
23065 "%<virtual%> specified more than once in base-specified");
23066 duplicate_virtual_error_issued_p = true;
23069 virtual_p = true;
23071 /* Consume the `virtual' token. */
23072 cp_lexer_consume_token (parser->lexer);
23074 break;
23076 case RID_PUBLIC:
23077 case RID_PROTECTED:
23078 case RID_PRIVATE:
23079 /* If more than one access specifier appears, issue an
23080 error. */
23081 if (access != access_default_node
23082 && !duplicate_access_error_issued_p)
23084 cp_parser_error (parser,
23085 "more than one access specifier in base-specified");
23086 duplicate_access_error_issued_p = true;
23089 access = ridpointers[(int) token->keyword];
23091 /* Consume the access-specifier. */
23092 cp_lexer_consume_token (parser->lexer);
23094 break;
23096 default:
23097 done = true;
23098 break;
23101 /* It is not uncommon to see programs mechanically, erroneously, use
23102 the 'typename' keyword to denote (dependent) qualified types
23103 as base classes. */
23104 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
23106 token = cp_lexer_peek_token (parser->lexer);
23107 if (!processing_template_decl)
23108 error_at (token->location,
23109 "keyword %<typename%> not allowed outside of templates");
23110 else
23111 error_at (token->location,
23112 "keyword %<typename%> not allowed in this context "
23113 "(the base class is implicitly a type)");
23114 cp_lexer_consume_token (parser->lexer);
23117 /* Look for the optional `::' operator. */
23118 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
23119 /* Look for the nested-name-specifier. The simplest way to
23120 implement:
23122 [temp.res]
23124 The keyword `typename' is not permitted in a base-specifier or
23125 mem-initializer; in these contexts a qualified name that
23126 depends on a template-parameter is implicitly assumed to be a
23127 type name.
23129 is to pretend that we have seen the `typename' keyword at this
23130 point. */
23131 cp_parser_nested_name_specifier_opt (parser,
23132 /*typename_keyword_p=*/true,
23133 /*check_dependency_p=*/true,
23134 typename_type,
23135 /*is_declaration=*/true);
23136 /* If the base class is given by a qualified name, assume that names
23137 we see are type names or templates, as appropriate. */
23138 class_scope_p = (parser->scope && TYPE_P (parser->scope));
23139 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
23141 if (!parser->scope
23142 && cp_lexer_next_token_is_decltype (parser->lexer))
23143 /* DR 950 allows decltype as a base-specifier. */
23144 type = cp_parser_decltype (parser);
23145 else
23147 /* Otherwise, look for the class-name. */
23148 type = cp_parser_class_name (parser,
23149 class_scope_p,
23150 template_p,
23151 typename_type,
23152 /*check_dependency_p=*/true,
23153 /*class_head_p=*/false,
23154 /*is_declaration=*/true);
23155 type = TREE_TYPE (type);
23158 if (type == error_mark_node)
23159 return error_mark_node;
23161 return finish_base_specifier (type, access, virtual_p);
23164 /* Exception handling [gram.exception] */
23166 /* Parse an (optional) noexcept-specification.
23168 noexcept-specification:
23169 noexcept ( constant-expression ) [opt]
23171 If no noexcept-specification is present, returns NULL_TREE.
23172 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23173 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23174 there are no parentheses. CONSUMED_EXPR will be set accordingly.
23175 Otherwise, returns a noexcept specification unless RETURN_COND is true,
23176 in which case a boolean condition is returned instead. */
23178 static tree
23179 cp_parser_noexcept_specification_opt (cp_parser* parser,
23180 bool require_constexpr,
23181 bool* consumed_expr,
23182 bool return_cond)
23184 cp_token *token;
23185 const char *saved_message;
23187 /* Peek at the next token. */
23188 token = cp_lexer_peek_token (parser->lexer);
23190 /* Is it a noexcept-specification? */
23191 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
23193 tree expr;
23194 cp_lexer_consume_token (parser->lexer);
23196 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
23198 cp_lexer_consume_token (parser->lexer);
23200 if (require_constexpr)
23202 /* Types may not be defined in an exception-specification. */
23203 saved_message = parser->type_definition_forbidden_message;
23204 parser->type_definition_forbidden_message
23205 = G_("types may not be defined in an exception-specification");
23207 expr = cp_parser_constant_expression (parser);
23209 /* Restore the saved message. */
23210 parser->type_definition_forbidden_message = saved_message;
23212 else
23214 expr = cp_parser_expression (parser);
23215 *consumed_expr = true;
23218 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23220 else
23222 expr = boolean_true_node;
23223 if (!require_constexpr)
23224 *consumed_expr = false;
23227 /* We cannot build a noexcept-spec right away because this will check
23228 that expr is a constexpr. */
23229 if (!return_cond)
23230 return build_noexcept_spec (expr, tf_warning_or_error);
23231 else
23232 return expr;
23234 else
23235 return NULL_TREE;
23238 /* Parse an (optional) exception-specification.
23240 exception-specification:
23241 throw ( type-id-list [opt] )
23243 Returns a TREE_LIST representing the exception-specification. The
23244 TREE_VALUE of each node is a type. */
23246 static tree
23247 cp_parser_exception_specification_opt (cp_parser* parser)
23249 cp_token *token;
23250 tree type_id_list;
23251 const char *saved_message;
23253 /* Peek at the next token. */
23254 token = cp_lexer_peek_token (parser->lexer);
23256 /* Is it a noexcept-specification? */
23257 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
23258 false);
23259 if (type_id_list != NULL_TREE)
23260 return type_id_list;
23262 /* If it's not `throw', then there's no exception-specification. */
23263 if (!cp_parser_is_keyword (token, RID_THROW))
23264 return NULL_TREE;
23266 #if 0
23267 /* Enable this once a lot of code has transitioned to noexcept? */
23268 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
23269 warning (OPT_Wdeprecated, "dynamic exception specifications are "
23270 "deprecated in C++0x; use %<noexcept%> instead");
23271 #endif
23273 /* Consume the `throw'. */
23274 cp_lexer_consume_token (parser->lexer);
23276 /* Look for the `('. */
23277 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23279 /* Peek at the next token. */
23280 token = cp_lexer_peek_token (parser->lexer);
23281 /* If it's not a `)', then there is a type-id-list. */
23282 if (token->type != CPP_CLOSE_PAREN)
23284 /* Types may not be defined in an exception-specification. */
23285 saved_message = parser->type_definition_forbidden_message;
23286 parser->type_definition_forbidden_message
23287 = G_("types may not be defined in an exception-specification");
23288 /* Parse the type-id-list. */
23289 type_id_list = cp_parser_type_id_list (parser);
23290 /* Restore the saved message. */
23291 parser->type_definition_forbidden_message = saved_message;
23293 else
23294 type_id_list = empty_except_spec;
23296 /* Look for the `)'. */
23297 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23299 return type_id_list;
23302 /* Parse an (optional) type-id-list.
23304 type-id-list:
23305 type-id ... [opt]
23306 type-id-list , type-id ... [opt]
23308 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
23309 in the order that the types were presented. */
23311 static tree
23312 cp_parser_type_id_list (cp_parser* parser)
23314 tree types = NULL_TREE;
23316 while (true)
23318 cp_token *token;
23319 tree type;
23321 token = cp_lexer_peek_token (parser->lexer);
23323 /* Get the next type-id. */
23324 type = cp_parser_type_id (parser);
23325 /* Check for invalid 'auto'. */
23326 if (flag_concepts && type_uses_auto (type))
23328 error_at (token->location,
23329 "invalid use of %<auto%> in exception-specification");
23330 type = error_mark_node;
23332 /* Parse the optional ellipsis. */
23333 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23335 /* Consume the `...'. */
23336 cp_lexer_consume_token (parser->lexer);
23338 /* Turn the type into a pack expansion expression. */
23339 type = make_pack_expansion (type);
23341 /* Add it to the list. */
23342 types = add_exception_specifier (types, type, /*complain=*/1);
23343 /* Peek at the next token. */
23344 token = cp_lexer_peek_token (parser->lexer);
23345 /* If it is not a `,', we are done. */
23346 if (token->type != CPP_COMMA)
23347 break;
23348 /* Consume the `,'. */
23349 cp_lexer_consume_token (parser->lexer);
23352 return nreverse (types);
23355 /* Parse a try-block.
23357 try-block:
23358 try compound-statement handler-seq */
23360 static tree
23361 cp_parser_try_block (cp_parser* parser)
23363 tree try_block;
23365 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
23366 if (parser->in_function_body
23367 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
23368 error ("%<try%> in %<constexpr%> function");
23370 try_block = begin_try_block ();
23371 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
23372 finish_try_block (try_block);
23373 cp_parser_handler_seq (parser);
23374 finish_handler_sequence (try_block);
23376 return try_block;
23379 /* Parse a function-try-block.
23381 function-try-block:
23382 try ctor-initializer [opt] function-body handler-seq */
23384 static bool
23385 cp_parser_function_try_block (cp_parser* parser)
23387 tree compound_stmt;
23388 tree try_block;
23389 bool ctor_initializer_p;
23391 /* Look for the `try' keyword. */
23392 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
23393 return false;
23394 /* Let the rest of the front end know where we are. */
23395 try_block = begin_function_try_block (&compound_stmt);
23396 /* Parse the function-body. */
23397 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23398 (parser, /*in_function_try_block=*/true);
23399 /* We're done with the `try' part. */
23400 finish_function_try_block (try_block);
23401 /* Parse the handlers. */
23402 cp_parser_handler_seq (parser);
23403 /* We're done with the handlers. */
23404 finish_function_handler_sequence (try_block, compound_stmt);
23406 return ctor_initializer_p;
23409 /* Parse a handler-seq.
23411 handler-seq:
23412 handler handler-seq [opt] */
23414 static void
23415 cp_parser_handler_seq (cp_parser* parser)
23417 while (true)
23419 cp_token *token;
23421 /* Parse the handler. */
23422 cp_parser_handler (parser);
23423 /* Peek at the next token. */
23424 token = cp_lexer_peek_token (parser->lexer);
23425 /* If it's not `catch' then there are no more handlers. */
23426 if (!cp_parser_is_keyword (token, RID_CATCH))
23427 break;
23431 /* Parse a handler.
23433 handler:
23434 catch ( exception-declaration ) compound-statement */
23436 static void
23437 cp_parser_handler (cp_parser* parser)
23439 tree handler;
23440 tree declaration;
23442 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
23443 handler = begin_handler ();
23444 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23445 declaration = cp_parser_exception_declaration (parser);
23446 finish_handler_parms (declaration, handler);
23447 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23448 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
23449 finish_handler (handler);
23452 /* Parse an exception-declaration.
23454 exception-declaration:
23455 type-specifier-seq declarator
23456 type-specifier-seq abstract-declarator
23457 type-specifier-seq
23460 Returns a VAR_DECL for the declaration, or NULL_TREE if the
23461 ellipsis variant is used. */
23463 static tree
23464 cp_parser_exception_declaration (cp_parser* parser)
23466 cp_decl_specifier_seq type_specifiers;
23467 cp_declarator *declarator;
23468 const char *saved_message;
23470 /* If it's an ellipsis, it's easy to handle. */
23471 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23473 /* Consume the `...' token. */
23474 cp_lexer_consume_token (parser->lexer);
23475 return NULL_TREE;
23478 /* Types may not be defined in exception-declarations. */
23479 saved_message = parser->type_definition_forbidden_message;
23480 parser->type_definition_forbidden_message
23481 = G_("types may not be defined in exception-declarations");
23483 /* Parse the type-specifier-seq. */
23484 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
23485 /*is_trailing_return=*/false,
23486 &type_specifiers);
23487 /* If it's a `)', then there is no declarator. */
23488 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23489 declarator = NULL;
23490 else
23491 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
23492 /*ctor_dtor_or_conv_p=*/NULL,
23493 /*parenthesized_p=*/NULL,
23494 /*member_p=*/false,
23495 /*friend_p=*/false);
23497 /* Restore the saved message. */
23498 parser->type_definition_forbidden_message = saved_message;
23500 if (!type_specifiers.any_specifiers_p)
23501 return error_mark_node;
23503 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
23506 /* Parse a throw-expression.
23508 throw-expression:
23509 throw assignment-expression [opt]
23511 Returns a THROW_EXPR representing the throw-expression. */
23513 static tree
23514 cp_parser_throw_expression (cp_parser* parser)
23516 tree expression;
23517 cp_token* token;
23519 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
23520 token = cp_lexer_peek_token (parser->lexer);
23521 /* Figure out whether or not there is an assignment-expression
23522 following the "throw" keyword. */
23523 if (token->type == CPP_COMMA
23524 || token->type == CPP_SEMICOLON
23525 || token->type == CPP_CLOSE_PAREN
23526 || token->type == CPP_CLOSE_SQUARE
23527 || token->type == CPP_CLOSE_BRACE
23528 || token->type == CPP_COLON)
23529 expression = NULL_TREE;
23530 else
23531 expression = cp_parser_assignment_expression (parser);
23533 return build_throw (expression);
23536 /* GNU Extensions */
23538 /* Parse an (optional) asm-specification.
23540 asm-specification:
23541 asm ( string-literal )
23543 If the asm-specification is present, returns a STRING_CST
23544 corresponding to the string-literal. Otherwise, returns
23545 NULL_TREE. */
23547 static tree
23548 cp_parser_asm_specification_opt (cp_parser* parser)
23550 cp_token *token;
23551 tree asm_specification;
23553 /* Peek at the next token. */
23554 token = cp_lexer_peek_token (parser->lexer);
23555 /* If the next token isn't the `asm' keyword, then there's no
23556 asm-specification. */
23557 if (!cp_parser_is_keyword (token, RID_ASM))
23558 return NULL_TREE;
23560 /* Consume the `asm' token. */
23561 cp_lexer_consume_token (parser->lexer);
23562 /* Look for the `('. */
23563 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23565 /* Look for the string-literal. */
23566 asm_specification = cp_parser_string_literal (parser, false, false);
23568 /* Look for the `)'. */
23569 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23571 return asm_specification;
23574 /* Parse an asm-operand-list.
23576 asm-operand-list:
23577 asm-operand
23578 asm-operand-list , asm-operand
23580 asm-operand:
23581 string-literal ( expression )
23582 [ string-literal ] string-literal ( expression )
23584 Returns a TREE_LIST representing the operands. The TREE_VALUE of
23585 each node is the expression. The TREE_PURPOSE is itself a
23586 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
23587 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
23588 is a STRING_CST for the string literal before the parenthesis. Returns
23589 ERROR_MARK_NODE if any of the operands are invalid. */
23591 static tree
23592 cp_parser_asm_operand_list (cp_parser* parser)
23594 tree asm_operands = NULL_TREE;
23595 bool invalid_operands = false;
23597 while (true)
23599 tree string_literal;
23600 tree expression;
23601 tree name;
23603 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
23605 /* Consume the `[' token. */
23606 cp_lexer_consume_token (parser->lexer);
23607 /* Read the operand name. */
23608 name = cp_parser_identifier (parser);
23609 if (name != error_mark_node)
23610 name = build_string (IDENTIFIER_LENGTH (name),
23611 IDENTIFIER_POINTER (name));
23612 /* Look for the closing `]'. */
23613 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23615 else
23616 name = NULL_TREE;
23617 /* Look for the string-literal. */
23618 string_literal = cp_parser_string_literal (parser, false, false);
23620 /* Look for the `('. */
23621 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23622 /* Parse the expression. */
23623 expression = cp_parser_expression (parser);
23624 /* Look for the `)'. */
23625 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23627 if (name == error_mark_node
23628 || string_literal == error_mark_node
23629 || expression == error_mark_node)
23630 invalid_operands = true;
23632 /* Add this operand to the list. */
23633 asm_operands = tree_cons (build_tree_list (name, string_literal),
23634 expression,
23635 asm_operands);
23636 /* If the next token is not a `,', there are no more
23637 operands. */
23638 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23639 break;
23640 /* Consume the `,'. */
23641 cp_lexer_consume_token (parser->lexer);
23644 return invalid_operands ? error_mark_node : nreverse (asm_operands);
23647 /* Parse an asm-clobber-list.
23649 asm-clobber-list:
23650 string-literal
23651 asm-clobber-list , string-literal
23653 Returns a TREE_LIST, indicating the clobbers in the order that they
23654 appeared. The TREE_VALUE of each node is a STRING_CST. */
23656 static tree
23657 cp_parser_asm_clobber_list (cp_parser* parser)
23659 tree clobbers = NULL_TREE;
23661 while (true)
23663 tree string_literal;
23665 /* Look for the string literal. */
23666 string_literal = cp_parser_string_literal (parser, false, false);
23667 /* Add it to the list. */
23668 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
23669 /* If the next token is not a `,', then the list is
23670 complete. */
23671 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23672 break;
23673 /* Consume the `,' token. */
23674 cp_lexer_consume_token (parser->lexer);
23677 return clobbers;
23680 /* Parse an asm-label-list.
23682 asm-label-list:
23683 identifier
23684 asm-label-list , identifier
23686 Returns a TREE_LIST, indicating the labels in the order that they
23687 appeared. The TREE_VALUE of each node is a label. */
23689 static tree
23690 cp_parser_asm_label_list (cp_parser* parser)
23692 tree labels = NULL_TREE;
23694 while (true)
23696 tree identifier, label, name;
23698 /* Look for the identifier. */
23699 identifier = cp_parser_identifier (parser);
23700 if (!error_operand_p (identifier))
23702 label = lookup_label (identifier);
23703 if (TREE_CODE (label) == LABEL_DECL)
23705 TREE_USED (label) = 1;
23706 check_goto (label);
23707 name = build_string (IDENTIFIER_LENGTH (identifier),
23708 IDENTIFIER_POINTER (identifier));
23709 labels = tree_cons (name, label, labels);
23712 /* If the next token is not a `,', then the list is
23713 complete. */
23714 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23715 break;
23716 /* Consume the `,' token. */
23717 cp_lexer_consume_token (parser->lexer);
23720 return nreverse (labels);
23723 /* Return TRUE iff the next tokens in the stream are possibly the
23724 beginning of a GNU extension attribute. */
23726 static bool
23727 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
23729 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
23732 /* Return TRUE iff the next tokens in the stream are possibly the
23733 beginning of a standard C++-11 attribute specifier. */
23735 static bool
23736 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
23738 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
23741 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23742 beginning of a standard C++-11 attribute specifier. */
23744 static bool
23745 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
23747 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23749 return (cxx_dialect >= cxx11
23750 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
23751 || (token->type == CPP_OPEN_SQUARE
23752 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
23753 && token->type == CPP_OPEN_SQUARE)));
23756 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23757 beginning of a GNU extension attribute. */
23759 static bool
23760 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
23762 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23764 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
23767 /* Return true iff the next tokens can be the beginning of either a
23768 GNU attribute list, or a standard C++11 attribute sequence. */
23770 static bool
23771 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
23773 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
23774 || cp_next_tokens_can_be_std_attribute_p (parser));
23777 /* Return true iff the next Nth tokens can be the beginning of either
23778 a GNU attribute list, or a standard C++11 attribute sequence. */
23780 static bool
23781 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
23783 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
23784 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
23787 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
23788 of GNU attributes, or return NULL. */
23790 static tree
23791 cp_parser_attributes_opt (cp_parser *parser)
23793 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
23794 return cp_parser_gnu_attributes_opt (parser);
23795 return cp_parser_std_attribute_spec_seq (parser);
23798 #define CILK_SIMD_FN_CLAUSE_MASK \
23799 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
23800 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
23801 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
23802 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
23803 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
23805 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
23806 vector [(<clauses>)] */
23808 static void
23809 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
23811 bool first_p = parser->cilk_simd_fn_info == NULL;
23812 cp_token *token = v_token;
23813 if (first_p)
23815 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
23816 parser->cilk_simd_fn_info->error_seen = false;
23817 parser->cilk_simd_fn_info->fndecl_seen = false;
23818 parser->cilk_simd_fn_info->tokens = vNULL;
23819 parser->cilk_simd_fn_info->clauses = NULL_TREE;
23821 int paren_scope = 0;
23822 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23824 cp_lexer_consume_token (parser->lexer);
23825 v_token = cp_lexer_peek_token (parser->lexer);
23826 paren_scope++;
23828 while (paren_scope > 0)
23830 token = cp_lexer_peek_token (parser->lexer);
23831 if (token->type == CPP_OPEN_PAREN)
23832 paren_scope++;
23833 else if (token->type == CPP_CLOSE_PAREN)
23834 paren_scope--;
23835 /* Do not push the last ')' */
23836 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
23837 cp_lexer_consume_token (parser->lexer);
23840 token->type = CPP_PRAGMA_EOL;
23841 parser->lexer->next_token = token;
23842 cp_lexer_consume_token (parser->lexer);
23844 struct cp_token_cache *cp
23845 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
23846 parser->cilk_simd_fn_info->tokens.safe_push (cp);
23849 /* Parse an (optional) series of attributes.
23851 attributes:
23852 attributes attribute
23854 attribute:
23855 __attribute__ (( attribute-list [opt] ))
23857 The return value is as for cp_parser_gnu_attribute_list. */
23859 static tree
23860 cp_parser_gnu_attributes_opt (cp_parser* parser)
23862 tree attributes = NULL_TREE;
23864 while (true)
23866 cp_token *token;
23867 tree attribute_list;
23868 bool ok = true;
23870 /* Peek at the next token. */
23871 token = cp_lexer_peek_token (parser->lexer);
23872 /* If it's not `__attribute__', then we're done. */
23873 if (token->keyword != RID_ATTRIBUTE)
23874 break;
23876 /* Consume the `__attribute__' keyword. */
23877 cp_lexer_consume_token (parser->lexer);
23878 /* Look for the two `(' tokens. */
23879 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23880 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23882 /* Peek at the next token. */
23883 token = cp_lexer_peek_token (parser->lexer);
23884 if (token->type != CPP_CLOSE_PAREN)
23885 /* Parse the attribute-list. */
23886 attribute_list = cp_parser_gnu_attribute_list (parser);
23887 else
23888 /* If the next token is a `)', then there is no attribute
23889 list. */
23890 attribute_list = NULL;
23892 /* Look for the two `)' tokens. */
23893 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23894 ok = false;
23895 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23896 ok = false;
23897 if (!ok)
23898 cp_parser_skip_to_end_of_statement (parser);
23900 /* Add these new attributes to the list. */
23901 attributes = chainon (attributes, attribute_list);
23904 return attributes;
23907 /* Parse a GNU attribute-list.
23909 attribute-list:
23910 attribute
23911 attribute-list , attribute
23913 attribute:
23914 identifier
23915 identifier ( identifier )
23916 identifier ( identifier , expression-list )
23917 identifier ( expression-list )
23919 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
23920 to an attribute. The TREE_PURPOSE of each node is the identifier
23921 indicating which attribute is in use. The TREE_VALUE represents
23922 the arguments, if any. */
23924 static tree
23925 cp_parser_gnu_attribute_list (cp_parser* parser)
23927 tree attribute_list = NULL_TREE;
23928 bool save_translate_strings_p = parser->translate_strings_p;
23930 parser->translate_strings_p = false;
23931 while (true)
23933 cp_token *token;
23934 tree identifier;
23935 tree attribute;
23937 /* Look for the identifier. We also allow keywords here; for
23938 example `__attribute__ ((const))' is legal. */
23939 token = cp_lexer_peek_token (parser->lexer);
23940 if (token->type == CPP_NAME
23941 || token->type == CPP_KEYWORD)
23943 tree arguments = NULL_TREE;
23945 /* Consume the token, but save it since we need it for the
23946 SIMD enabled function parsing. */
23947 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
23949 /* Save away the identifier that indicates which attribute
23950 this is. */
23951 identifier = (token->type == CPP_KEYWORD)
23952 /* For keywords, use the canonical spelling, not the
23953 parsed identifier. */
23954 ? ridpointers[(int) token->keyword]
23955 : id_token->u.value;
23957 attribute = build_tree_list (identifier, NULL_TREE);
23959 /* Peek at the next token. */
23960 token = cp_lexer_peek_token (parser->lexer);
23961 /* If it's an `(', then parse the attribute arguments. */
23962 if (token->type == CPP_OPEN_PAREN)
23964 vec<tree, va_gc> *vec;
23965 int attr_flag = (attribute_takes_identifier_p (identifier)
23966 ? id_attr : normal_attr);
23967 if (is_cilkplus_vector_p (identifier))
23969 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23970 continue;
23972 else
23973 vec = cp_parser_parenthesized_expression_list
23974 (parser, attr_flag, /*cast_p=*/false,
23975 /*allow_expansion_p=*/false,
23976 /*non_constant_p=*/NULL);
23977 if (vec == NULL)
23978 arguments = error_mark_node;
23979 else
23981 arguments = build_tree_list_vec (vec);
23982 release_tree_vector (vec);
23984 /* Save the arguments away. */
23985 TREE_VALUE (attribute) = arguments;
23987 else if (is_cilkplus_vector_p (identifier))
23989 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23990 continue;
23993 if (arguments != error_mark_node)
23995 /* Add this attribute to the list. */
23996 TREE_CHAIN (attribute) = attribute_list;
23997 attribute_list = attribute;
24000 token = cp_lexer_peek_token (parser->lexer);
24002 /* Now, look for more attributes. If the next token isn't a
24003 `,', we're done. */
24004 if (token->type != CPP_COMMA)
24005 break;
24007 /* Consume the comma and keep going. */
24008 cp_lexer_consume_token (parser->lexer);
24010 parser->translate_strings_p = save_translate_strings_p;
24012 /* We built up the list in reverse order. */
24013 return nreverse (attribute_list);
24016 /* Parse a standard C++11 attribute.
24018 The returned representation is a TREE_LIST which TREE_PURPOSE is
24019 the scoped name of the attribute, and the TREE_VALUE is its
24020 arguments list.
24022 Note that the scoped name of the attribute is itself a TREE_LIST
24023 which TREE_PURPOSE is the namespace of the attribute, and
24024 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
24025 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
24026 and which TREE_PURPOSE is directly the attribute name.
24028 Clients of the attribute code should use get_attribute_namespace
24029 and get_attribute_name to get the actual namespace and name of
24030 attributes, regardless of their being GNU or C++11 attributes.
24032 attribute:
24033 attribute-token attribute-argument-clause [opt]
24035 attribute-token:
24036 identifier
24037 attribute-scoped-token
24039 attribute-scoped-token:
24040 attribute-namespace :: identifier
24042 attribute-namespace:
24043 identifier
24045 attribute-argument-clause:
24046 ( balanced-token-seq )
24048 balanced-token-seq:
24049 balanced-token [opt]
24050 balanced-token-seq balanced-token
24052 balanced-token:
24053 ( balanced-token-seq )
24054 [ balanced-token-seq ]
24055 { balanced-token-seq }. */
24057 static tree
24058 cp_parser_std_attribute (cp_parser *parser)
24060 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
24061 cp_token *token;
24063 /* First, parse name of the attribute, a.k.a attribute-token. */
24065 token = cp_lexer_peek_token (parser->lexer);
24066 if (token->type == CPP_NAME)
24067 attr_id = token->u.value;
24068 else if (token->type == CPP_KEYWORD)
24069 attr_id = ridpointers[(int) token->keyword];
24070 else if (token->flags & NAMED_OP)
24071 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24073 if (attr_id == NULL_TREE)
24074 return NULL_TREE;
24076 cp_lexer_consume_token (parser->lexer);
24078 token = cp_lexer_peek_token (parser->lexer);
24079 if (token->type == CPP_SCOPE)
24081 /* We are seeing a scoped attribute token. */
24083 cp_lexer_consume_token (parser->lexer);
24084 attr_ns = attr_id;
24086 token = cp_lexer_consume_token (parser->lexer);
24087 if (token->type == CPP_NAME)
24088 attr_id = token->u.value;
24089 else if (token->type == CPP_KEYWORD)
24090 attr_id = ridpointers[(int) token->keyword];
24091 else
24093 error_at (token->location,
24094 "expected an identifier for the attribute name");
24095 return error_mark_node;
24097 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24098 NULL_TREE);
24099 token = cp_lexer_peek_token (parser->lexer);
24101 else
24103 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
24104 NULL_TREE);
24105 /* C++11 noreturn attribute is equivalent to GNU's. */
24106 if (is_attribute_p ("noreturn", attr_id))
24107 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24108 /* C++14 deprecated attribute is equivalent to GNU's. */
24109 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
24111 if (cxx_dialect == cxx11)
24112 pedwarn (token->location, OPT_Wpedantic,
24113 "%<deprecated%> is a C++14 feature;"
24114 " use %<gnu::deprecated%>");
24115 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24117 /* Transactional Memory TS optimize_for_synchronized attribute is
24118 equivalent to GNU transaction_callable. */
24119 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
24120 TREE_PURPOSE (attribute)
24121 = get_identifier ("transaction_callable");
24122 /* Transactional Memory attributes are GNU attributes. */
24123 else if (tm_attr_to_mask (attr_id))
24124 TREE_PURPOSE (attribute) = attr_id;
24127 /* Now parse the optional argument clause of the attribute. */
24129 if (token->type != CPP_OPEN_PAREN)
24130 return attribute;
24133 vec<tree, va_gc> *vec;
24134 int attr_flag = normal_attr;
24136 if (attr_ns == get_identifier ("gnu")
24137 && attribute_takes_identifier_p (attr_id))
24138 /* A GNU attribute that takes an identifier in parameter. */
24139 attr_flag = id_attr;
24141 vec = cp_parser_parenthesized_expression_list
24142 (parser, attr_flag, /*cast_p=*/false,
24143 /*allow_expansion_p=*/true,
24144 /*non_constant_p=*/NULL);
24145 if (vec == NULL)
24146 arguments = error_mark_node;
24147 else
24149 arguments = build_tree_list_vec (vec);
24150 release_tree_vector (vec);
24153 if (arguments == error_mark_node)
24154 attribute = error_mark_node;
24155 else
24156 TREE_VALUE (attribute) = arguments;
24159 return attribute;
24162 /* Check that the attribute ATTRIBUTE appears at most once in the
24163 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
24164 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
24165 isn't implemented yet in GCC. */
24167 static void
24168 cp_parser_check_std_attribute (tree attributes, tree attribute)
24170 if (attributes)
24172 tree name = get_attribute_name (attribute);
24173 if (is_attribute_p ("noreturn", name)
24174 && lookup_attribute ("noreturn", attributes))
24175 error ("attribute noreturn can appear at most once "
24176 "in an attribute-list");
24177 else if (is_attribute_p ("deprecated", name)
24178 && lookup_attribute ("deprecated", attributes))
24179 error ("attribute deprecated can appear at most once "
24180 "in an attribute-list");
24184 /* Parse a list of standard C++-11 attributes.
24186 attribute-list:
24187 attribute [opt]
24188 attribute-list , attribute[opt]
24189 attribute ...
24190 attribute-list , attribute ...
24193 static tree
24194 cp_parser_std_attribute_list (cp_parser *parser)
24196 tree attributes = NULL_TREE, attribute = NULL_TREE;
24197 cp_token *token = NULL;
24199 while (true)
24201 attribute = cp_parser_std_attribute (parser);
24202 if (attribute == error_mark_node)
24203 break;
24204 if (attribute != NULL_TREE)
24206 cp_parser_check_std_attribute (attributes, attribute);
24207 TREE_CHAIN (attribute) = attributes;
24208 attributes = attribute;
24210 token = cp_lexer_peek_token (parser->lexer);
24211 if (token->type == CPP_ELLIPSIS)
24213 cp_lexer_consume_token (parser->lexer);
24214 TREE_VALUE (attribute)
24215 = make_pack_expansion (TREE_VALUE (attribute));
24216 token = cp_lexer_peek_token (parser->lexer);
24218 if (token->type != CPP_COMMA)
24219 break;
24220 cp_lexer_consume_token (parser->lexer);
24222 attributes = nreverse (attributes);
24223 return attributes;
24226 /* Parse a standard C++-11 attribute specifier.
24228 attribute-specifier:
24229 [ [ attribute-list ] ]
24230 alignment-specifier
24232 alignment-specifier:
24233 alignas ( type-id ... [opt] )
24234 alignas ( alignment-expression ... [opt] ). */
24236 static tree
24237 cp_parser_std_attribute_spec (cp_parser *parser)
24239 tree attributes = NULL_TREE;
24240 cp_token *token = cp_lexer_peek_token (parser->lexer);
24242 if (token->type == CPP_OPEN_SQUARE
24243 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
24245 cp_lexer_consume_token (parser->lexer);
24246 cp_lexer_consume_token (parser->lexer);
24248 attributes = cp_parser_std_attribute_list (parser);
24250 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
24251 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24252 cp_parser_skip_to_end_of_statement (parser);
24253 else
24254 /* Warn about parsing c++11 attribute in non-c++1 mode, only
24255 when we are sure that we have actually parsed them. */
24256 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24258 else
24260 tree alignas_expr;
24262 /* Look for an alignment-specifier. */
24264 token = cp_lexer_peek_token (parser->lexer);
24266 if (token->type != CPP_KEYWORD
24267 || token->keyword != RID_ALIGNAS)
24268 return NULL_TREE;
24270 cp_lexer_consume_token (parser->lexer);
24271 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24273 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
24275 cp_parser_error (parser, "expected %<(%>");
24276 return error_mark_node;
24279 cp_parser_parse_tentatively (parser);
24280 alignas_expr = cp_parser_type_id (parser);
24282 if (!cp_parser_parse_definitely (parser))
24284 gcc_assert (alignas_expr == error_mark_node
24285 || alignas_expr == NULL_TREE);
24287 alignas_expr =
24288 cp_parser_assignment_expression (parser);
24289 if (alignas_expr == error_mark_node)
24290 cp_parser_skip_to_end_of_statement (parser);
24291 if (alignas_expr == NULL_TREE
24292 || alignas_expr == error_mark_node)
24293 return alignas_expr;
24296 alignas_expr = cxx_alignas_expr (alignas_expr);
24297 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
24299 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24301 cp_lexer_consume_token (parser->lexer);
24302 alignas_expr = make_pack_expansion (alignas_expr);
24305 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
24307 cp_parser_error (parser, "expected %<)%>");
24308 return error_mark_node;
24311 /* Build the C++-11 representation of an 'aligned'
24312 attribute. */
24313 attributes =
24314 build_tree_list (build_tree_list (get_identifier ("gnu"),
24315 get_identifier ("aligned")),
24316 alignas_expr);
24319 return attributes;
24322 /* Parse a standard C++-11 attribute-specifier-seq.
24324 attribute-specifier-seq:
24325 attribute-specifier-seq [opt] attribute-specifier
24328 static tree
24329 cp_parser_std_attribute_spec_seq (cp_parser *parser)
24331 tree attr_specs = NULL_TREE;
24332 tree attr_last = NULL_TREE;
24334 while (true)
24336 tree attr_spec = cp_parser_std_attribute_spec (parser);
24337 if (attr_spec == NULL_TREE)
24338 break;
24339 if (attr_spec == error_mark_node)
24340 return error_mark_node;
24342 if (attr_last)
24343 TREE_CHAIN (attr_last) = attr_spec;
24344 else
24345 attr_specs = attr_last = attr_spec;
24346 attr_last = tree_last (attr_last);
24349 return attr_specs;
24352 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
24353 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
24354 current value of the PEDANTIC flag, regardless of whether or not
24355 the `__extension__' keyword is present. The caller is responsible
24356 for restoring the value of the PEDANTIC flag. */
24358 static bool
24359 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
24361 /* Save the old value of the PEDANTIC flag. */
24362 *saved_pedantic = pedantic;
24364 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
24366 /* Consume the `__extension__' token. */
24367 cp_lexer_consume_token (parser->lexer);
24368 /* We're not being pedantic while the `__extension__' keyword is
24369 in effect. */
24370 pedantic = 0;
24372 return true;
24375 return false;
24378 /* Parse a label declaration.
24380 label-declaration:
24381 __label__ label-declarator-seq ;
24383 label-declarator-seq:
24384 identifier , label-declarator-seq
24385 identifier */
24387 static void
24388 cp_parser_label_declaration (cp_parser* parser)
24390 /* Look for the `__label__' keyword. */
24391 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
24393 while (true)
24395 tree identifier;
24397 /* Look for an identifier. */
24398 identifier = cp_parser_identifier (parser);
24399 /* If we failed, stop. */
24400 if (identifier == error_mark_node)
24401 break;
24402 /* Declare it as a label. */
24403 finish_label_decl (identifier);
24404 /* If the next token is a `;', stop. */
24405 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24406 break;
24407 /* Look for the `,' separating the label declarations. */
24408 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
24411 /* Look for the final `;'. */
24412 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24415 // -------------------------------------------------------------------------- //
24416 // Requires Clause
24418 // Parse a requires clause.
24420 // requires-clause:
24421 // 'requires' logical-or-expression
24423 // The required logical-or-expression must be a constant expression. Note
24424 // that we don't check that the expression is constepxr here. We defer until
24425 // we analyze constraints and then, we only check atomic constraints.
24426 static tree
24427 cp_parser_requires_clause (cp_parser *parser)
24429 // Parse the requires clause so that it is not automatically folded.
24430 ++processing_template_decl;
24431 tree expr = cp_parser_binary_expression (parser, false, false,
24432 PREC_NOT_OPERATOR, NULL);
24433 if (check_for_bare_parameter_packs (expr))
24434 expr = error_mark_node;
24435 --processing_template_decl;
24436 return expr;
24439 // Optionally parse a requires clause:
24440 static tree
24441 cp_parser_requires_clause_opt (cp_parser *parser)
24443 cp_token *tok = cp_lexer_peek_token (parser->lexer);
24444 if (tok->keyword != RID_REQUIRES)
24446 if (!flag_concepts && tok->type == CPP_NAME
24447 && tok->u.value == ridpointers[RID_REQUIRES])
24449 error_at (cp_lexer_peek_token (parser->lexer)->location,
24450 "%<requires%> only available with -fconcepts");
24451 /* Parse and discard the requires-clause. */
24452 cp_lexer_consume_token (parser->lexer);
24453 cp_parser_requires_clause (parser);
24455 return NULL_TREE;
24457 cp_lexer_consume_token (parser->lexer);
24458 return cp_parser_requires_clause (parser);
24462 /*---------------------------------------------------------------------------
24463 Requires expressions
24464 ---------------------------------------------------------------------------*/
24466 /* Parse a requires expression
24468 requirement-expression:
24469 'requires' requirement-parameter-list [opt] requirement-body */
24470 static tree
24471 cp_parser_requires_expression (cp_parser *parser)
24473 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
24474 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
24476 /* A requires-expression shall appear only within a concept
24477 definition or a requires-clause.
24479 TODO: Implement this diagnostic correctly. */
24480 if (!processing_template_decl)
24482 error_at (loc, "a requires expression cannot appear outside a template");
24483 cp_parser_skip_to_end_of_statement (parser);
24484 return error_mark_node;
24487 tree parms, reqs;
24489 /* Local parameters are delared as variables within the scope
24490 of the expression. They are not visible past the end of
24491 the expression. Expressions within the requires-expression
24492 are unevaluated. */
24493 struct scope_sentinel
24495 scope_sentinel ()
24497 ++cp_unevaluated_operand;
24498 begin_scope (sk_block, NULL_TREE);
24501 ~scope_sentinel ()
24503 pop_bindings_and_leave_scope ();
24504 --cp_unevaluated_operand;
24506 } s;
24508 /* Parse the optional parameter list. */
24509 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24511 parms = cp_parser_requirement_parameter_list (parser);
24512 if (parms == error_mark_node)
24513 return error_mark_node;
24515 else
24516 parms = NULL_TREE;
24518 /* Parse the requirement body. */
24519 reqs = cp_parser_requirement_body (parser);
24520 if (reqs == error_mark_node)
24521 return error_mark_node;
24524 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
24525 the parm chain. */
24526 grokparms (parms, &parms);
24527 return finish_requires_expr (parms, reqs);
24530 /* Parse a parameterized requirement.
24532 requirement-parameter-list:
24533 '(' parameter-declaration-clause ')' */
24534 static tree
24535 cp_parser_requirement_parameter_list (cp_parser *parser)
24537 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24538 return error_mark_node;
24540 tree parms = cp_parser_parameter_declaration_clause (parser);
24542 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24543 return error_mark_node;
24545 return parms;
24548 /* Parse the body of a requirement.
24550 requirement-body:
24551 '{' requirement-list '}' */
24552 static tree
24553 cp_parser_requirement_body (cp_parser *parser)
24555 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24556 return error_mark_node;
24558 tree reqs = cp_parser_requirement_list (parser);
24560 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24561 return error_mark_node;
24563 return reqs;
24566 /* Parse a list of requirements.
24568 requirement-list:
24569 requirement
24570 requirement-list ';' requirement[opt] */
24571 static tree
24572 cp_parser_requirement_list (cp_parser *parser)
24574 tree result = NULL_TREE;
24575 while (true)
24577 tree req = cp_parser_requirement (parser);
24578 if (req == error_mark_node)
24579 return error_mark_node;
24581 result = tree_cons (NULL_TREE, req, result);
24583 /* If we see a semi-colon, consume it. */
24584 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24585 cp_lexer_consume_token (parser->lexer);
24587 /* Stop processing at the end of the list. */
24588 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24589 break;
24592 /* Reverse the order of requirements so they are analyzed in
24593 declaration order. */
24594 return nreverse (result);
24597 /* Parse a syntactic requirement or type requirement.
24599 requirement:
24600 simple-requirement
24601 compound-requirement
24602 type-requirement
24603 nested-requirement */
24604 static tree
24605 cp_parser_requirement (cp_parser *parser)
24607 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24608 return cp_parser_compound_requirement (parser);
24609 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24610 return cp_parser_type_requirement (parser);
24611 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
24612 return cp_parser_nested_requirement (parser);
24613 else
24614 return cp_parser_simple_requirement (parser);
24617 /* Parse a simple requirement.
24619 simple-requirement:
24620 expression ';' */
24621 static tree
24622 cp_parser_simple_requirement (cp_parser *parser)
24624 tree expr = cp_parser_expression (parser, NULL, false, false);
24625 if (!expr || expr == error_mark_node)
24626 return error_mark_node;
24628 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24629 return error_mark_node;
24631 return finish_simple_requirement (expr);
24634 /* Parse a type requirement
24636 type-requirement
24637 nested-name-specifier [opt] required-type-name ';'
24639 required-type-name:
24640 type-name
24641 'template' [opt] simple-template-id */
24642 static tree
24643 cp_parser_type_requirement (cp_parser *parser)
24645 cp_lexer_consume_token (parser->lexer);
24647 // Save the scope before parsing name specifiers.
24648 tree saved_scope = parser->scope;
24649 tree saved_object_scope = parser->object_scope;
24650 tree saved_qualifying_scope = parser->qualifying_scope;
24651 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
24652 cp_parser_nested_name_specifier_opt (parser,
24653 /*typename_keyword_p=*/true,
24654 /*check_dependency_p=*/false,
24655 /*type_p=*/true,
24656 /*is_declaration=*/false);
24658 tree type;
24659 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24661 cp_lexer_consume_token (parser->lexer);
24662 type = cp_parser_template_id (parser,
24663 /*template_keyword_p=*/true,
24664 /*check_dependency=*/false,
24665 /*tag_type=*/none_type,
24666 /*is_declaration=*/false);
24667 type = make_typename_type (parser->scope, type, typename_type,
24668 /*complain=*/tf_error);
24670 else
24671 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
24673 if (TREE_CODE (type) == TYPE_DECL)
24674 type = TREE_TYPE (type);
24676 parser->scope = saved_scope;
24677 parser->object_scope = saved_object_scope;
24678 parser->qualifying_scope = saved_qualifying_scope;
24680 if (type == error_mark_node)
24681 cp_parser_skip_to_end_of_statement (parser);
24683 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24684 return error_mark_node;
24685 if (type == error_mark_node)
24686 return error_mark_node;
24688 return finish_type_requirement (type);
24691 /* Parse a compound requirement
24693 compound-requirement:
24694 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
24695 static tree
24696 cp_parser_compound_requirement (cp_parser *parser)
24698 /* Parse an expression enclosed in '{ }'s. */
24699 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24700 return error_mark_node;
24702 tree expr = cp_parser_expression (parser, NULL, false, false);
24703 if (!expr || expr == error_mark_node)
24704 return error_mark_node;
24706 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24707 return error_mark_node;
24709 /* Parse the optional noexcept. */
24710 bool noexcept_p = false;
24711 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
24713 cp_lexer_consume_token (parser->lexer);
24714 noexcept_p = true;
24717 /* Parse the optional trailing return type. */
24718 tree type = NULL_TREE;
24719 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
24721 cp_lexer_consume_token (parser->lexer);
24722 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
24723 parser->in_result_type_constraint_p = true;
24724 type = cp_parser_trailing_type_id (parser);
24725 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
24726 if (type == error_mark_node)
24727 return error_mark_node;
24730 return finish_compound_requirement (expr, type, noexcept_p);
24733 /* Parse a nested requirement. This is the same as a requires clause.
24735 nested-requirement:
24736 requires-clause */
24737 static tree
24738 cp_parser_nested_requirement (cp_parser *parser)
24740 cp_lexer_consume_token (parser->lexer);
24741 tree req = cp_parser_requires_clause (parser);
24742 if (req == error_mark_node)
24743 return error_mark_node;
24744 return finish_nested_requirement (req);
24747 /* Support Functions */
24749 /* Return the appropriate prefer_type argument for lookup_name_real based on
24750 tag_type and template_mem_access. */
24752 static inline int
24753 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
24755 /* DR 141: When looking in the current enclosing context for a template-name
24756 after -> or ., only consider class templates. */
24757 if (template_mem_access)
24758 return 2;
24759 switch (tag_type)
24761 case none_type: return 0; // No preference.
24762 case scope_type: return 1; // Type or namespace.
24763 default: return 2; // Type only.
24767 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
24768 NAME should have one of the representations used for an
24769 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
24770 is returned. If PARSER->SCOPE is a dependent type, then a
24771 SCOPE_REF is returned.
24773 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
24774 returned; the name was already resolved when the TEMPLATE_ID_EXPR
24775 was formed. Abstractly, such entities should not be passed to this
24776 function, because they do not need to be looked up, but it is
24777 simpler to check for this special case here, rather than at the
24778 call-sites.
24780 In cases not explicitly covered above, this function returns a
24781 DECL, OVERLOAD, or baselink representing the result of the lookup.
24782 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
24783 is returned.
24785 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
24786 (e.g., "struct") that was used. In that case bindings that do not
24787 refer to types are ignored.
24789 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
24790 ignored.
24792 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
24793 are ignored.
24795 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
24796 types.
24798 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
24799 TREE_LIST of candidates if name-lookup results in an ambiguity, and
24800 NULL_TREE otherwise. */
24802 static cp_expr
24803 cp_parser_lookup_name (cp_parser *parser, tree name,
24804 enum tag_types tag_type,
24805 bool is_template,
24806 bool is_namespace,
24807 bool check_dependency,
24808 tree *ambiguous_decls,
24809 location_t name_location)
24811 tree decl;
24812 tree object_type = parser->context->object_type;
24814 /* Assume that the lookup will be unambiguous. */
24815 if (ambiguous_decls)
24816 *ambiguous_decls = NULL_TREE;
24818 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
24819 no longer valid. Note that if we are parsing tentatively, and
24820 the parse fails, OBJECT_TYPE will be automatically restored. */
24821 parser->context->object_type = NULL_TREE;
24823 if (name == error_mark_node)
24824 return error_mark_node;
24826 /* A template-id has already been resolved; there is no lookup to
24827 do. */
24828 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
24829 return name;
24830 if (BASELINK_P (name))
24832 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
24833 == TEMPLATE_ID_EXPR);
24834 return name;
24837 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
24838 it should already have been checked to make sure that the name
24839 used matches the type being destroyed. */
24840 if (TREE_CODE (name) == BIT_NOT_EXPR)
24842 tree type;
24844 /* Figure out to which type this destructor applies. */
24845 if (parser->scope)
24846 type = parser->scope;
24847 else if (object_type)
24848 type = object_type;
24849 else
24850 type = current_class_type;
24851 /* If that's not a class type, there is no destructor. */
24852 if (!type || !CLASS_TYPE_P (type))
24853 return error_mark_node;
24854 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
24855 lazily_declare_fn (sfk_destructor, type);
24856 if (!CLASSTYPE_DESTRUCTORS (type))
24857 return error_mark_node;
24858 /* If it was a class type, return the destructor. */
24859 return CLASSTYPE_DESTRUCTORS (type);
24862 /* By this point, the NAME should be an ordinary identifier. If
24863 the id-expression was a qualified name, the qualifying scope is
24864 stored in PARSER->SCOPE at this point. */
24865 gcc_assert (identifier_p (name));
24867 /* Perform the lookup. */
24868 if (parser->scope)
24870 bool dependent_p;
24872 if (parser->scope == error_mark_node)
24873 return error_mark_node;
24875 /* If the SCOPE is dependent, the lookup must be deferred until
24876 the template is instantiated -- unless we are explicitly
24877 looking up names in uninstantiated templates. Even then, we
24878 cannot look up the name if the scope is not a class type; it
24879 might, for example, be a template type parameter. */
24880 dependent_p = (TYPE_P (parser->scope)
24881 && dependent_scope_p (parser->scope));
24882 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
24883 && dependent_p)
24884 /* Defer lookup. */
24885 decl = error_mark_node;
24886 else
24888 tree pushed_scope = NULL_TREE;
24890 /* If PARSER->SCOPE is a dependent type, then it must be a
24891 class type, and we must not be checking dependencies;
24892 otherwise, we would have processed this lookup above. So
24893 that PARSER->SCOPE is not considered a dependent base by
24894 lookup_member, we must enter the scope here. */
24895 if (dependent_p)
24896 pushed_scope = push_scope (parser->scope);
24898 /* If the PARSER->SCOPE is a template specialization, it
24899 may be instantiated during name lookup. In that case,
24900 errors may be issued. Even if we rollback the current
24901 tentative parse, those errors are valid. */
24902 decl = lookup_qualified_name (parser->scope, name,
24903 prefer_type_arg (tag_type),
24904 /*complain=*/true);
24906 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
24907 lookup result and the nested-name-specifier nominates a class C:
24908 * if the name specified after the nested-name-specifier, when
24909 looked up in C, is the injected-class-name of C (Clause 9), or
24910 * if the name specified after the nested-name-specifier is the
24911 same as the identifier or the simple-template-id's template-
24912 name in the last component of the nested-name-specifier,
24913 the name is instead considered to name the constructor of
24914 class C. [ Note: for example, the constructor is not an
24915 acceptable lookup result in an elaborated-type-specifier so
24916 the constructor would not be used in place of the
24917 injected-class-name. --end note ] Such a constructor name
24918 shall be used only in the declarator-id of a declaration that
24919 names a constructor or in a using-declaration. */
24920 if (tag_type == none_type
24921 && DECL_SELF_REFERENCE_P (decl)
24922 && same_type_p (DECL_CONTEXT (decl), parser->scope))
24923 decl = lookup_qualified_name (parser->scope, ctor_identifier,
24924 prefer_type_arg (tag_type),
24925 /*complain=*/true);
24927 /* If we have a single function from a using decl, pull it out. */
24928 if (TREE_CODE (decl) == OVERLOAD
24929 && !really_overloaded_fn (decl))
24930 decl = OVL_FUNCTION (decl);
24932 if (pushed_scope)
24933 pop_scope (pushed_scope);
24936 /* If the scope is a dependent type and either we deferred lookup or
24937 we did lookup but didn't find the name, rememeber the name. */
24938 if (decl == error_mark_node && TYPE_P (parser->scope)
24939 && dependent_type_p (parser->scope))
24941 if (tag_type)
24943 tree type;
24945 /* The resolution to Core Issue 180 says that `struct
24946 A::B' should be considered a type-name, even if `A'
24947 is dependent. */
24948 type = make_typename_type (parser->scope, name, tag_type,
24949 /*complain=*/tf_error);
24950 if (type != error_mark_node)
24951 decl = TYPE_NAME (type);
24953 else if (is_template
24954 && (cp_parser_next_token_ends_template_argument_p (parser)
24955 || cp_lexer_next_token_is (parser->lexer,
24956 CPP_CLOSE_PAREN)))
24957 decl = make_unbound_class_template (parser->scope,
24958 name, NULL_TREE,
24959 /*complain=*/tf_error);
24960 else
24961 decl = build_qualified_name (/*type=*/NULL_TREE,
24962 parser->scope, name,
24963 is_template);
24965 parser->qualifying_scope = parser->scope;
24966 parser->object_scope = NULL_TREE;
24968 else if (object_type)
24970 /* Look up the name in the scope of the OBJECT_TYPE, unless the
24971 OBJECT_TYPE is not a class. */
24972 if (CLASS_TYPE_P (object_type))
24973 /* If the OBJECT_TYPE is a template specialization, it may
24974 be instantiated during name lookup. In that case, errors
24975 may be issued. Even if we rollback the current tentative
24976 parse, those errors are valid. */
24977 decl = lookup_member (object_type,
24978 name,
24979 /*protect=*/0,
24980 prefer_type_arg (tag_type),
24981 tf_warning_or_error);
24982 else
24983 decl = NULL_TREE;
24985 if (!decl)
24986 /* Look it up in the enclosing context. DR 141: When looking for a
24987 template-name after -> or ., only consider class templates. */
24988 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
24989 /*nonclass=*/0,
24990 /*block_p=*/true, is_namespace, 0);
24991 if (object_type == unknown_type_node)
24992 /* The object is type-dependent, so we can't look anything up; we used
24993 this to get the DR 141 behavior. */
24994 object_type = NULL_TREE;
24995 parser->object_scope = object_type;
24996 parser->qualifying_scope = NULL_TREE;
24998 else
25000 decl = lookup_name_real (name, prefer_type_arg (tag_type),
25001 /*nonclass=*/0,
25002 /*block_p=*/true, is_namespace, 0);
25003 parser->qualifying_scope = NULL_TREE;
25004 parser->object_scope = NULL_TREE;
25007 /* If the lookup failed, let our caller know. */
25008 if (!decl || decl == error_mark_node)
25009 return error_mark_node;
25011 /* Pull out the template from an injected-class-name (or multiple). */
25012 if (is_template)
25013 decl = maybe_get_template_decl_from_type_decl (decl);
25015 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
25016 if (TREE_CODE (decl) == TREE_LIST)
25018 if (ambiguous_decls)
25019 *ambiguous_decls = decl;
25020 /* The error message we have to print is too complicated for
25021 cp_parser_error, so we incorporate its actions directly. */
25022 if (!cp_parser_simulate_error (parser))
25024 error_at (name_location, "reference to %qD is ambiguous",
25025 name);
25026 print_candidates (decl);
25028 return error_mark_node;
25031 gcc_assert (DECL_P (decl)
25032 || TREE_CODE (decl) == OVERLOAD
25033 || TREE_CODE (decl) == SCOPE_REF
25034 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
25035 || BASELINK_P (decl));
25037 /* If we have resolved the name of a member declaration, check to
25038 see if the declaration is accessible. When the name resolves to
25039 set of overloaded functions, accessibility is checked when
25040 overload resolution is done.
25042 During an explicit instantiation, access is not checked at all,
25043 as per [temp.explicit]. */
25044 if (DECL_P (decl))
25045 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
25047 maybe_record_typedef_use (decl);
25049 return cp_expr (decl, name_location);
25052 /* Like cp_parser_lookup_name, but for use in the typical case where
25053 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
25054 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
25056 static tree
25057 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
25059 return cp_parser_lookup_name (parser, name,
25060 none_type,
25061 /*is_template=*/false,
25062 /*is_namespace=*/false,
25063 /*check_dependency=*/true,
25064 /*ambiguous_decls=*/NULL,
25065 location);
25068 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
25069 the current context, return the TYPE_DECL. If TAG_NAME_P is
25070 true, the DECL indicates the class being defined in a class-head,
25071 or declared in an elaborated-type-specifier.
25073 Otherwise, return DECL. */
25075 static tree
25076 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
25078 /* If the TEMPLATE_DECL is being declared as part of a class-head,
25079 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
25081 struct A {
25082 template <typename T> struct B;
25085 template <typename T> struct A::B {};
25087 Similarly, in an elaborated-type-specifier:
25089 namespace N { struct X{}; }
25091 struct A {
25092 template <typename T> friend struct N::X;
25095 However, if the DECL refers to a class type, and we are in
25096 the scope of the class, then the name lookup automatically
25097 finds the TYPE_DECL created by build_self_reference rather
25098 than a TEMPLATE_DECL. For example, in:
25100 template <class T> struct S {
25101 S s;
25104 there is no need to handle such case. */
25106 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
25107 return DECL_TEMPLATE_RESULT (decl);
25109 return decl;
25112 /* If too many, or too few, template-parameter lists apply to the
25113 declarator, issue an error message. Returns TRUE if all went well,
25114 and FALSE otherwise. */
25116 static bool
25117 cp_parser_check_declarator_template_parameters (cp_parser* parser,
25118 cp_declarator *declarator,
25119 location_t declarator_location)
25121 switch (declarator->kind)
25123 case cdk_id:
25125 unsigned num_templates = 0;
25126 tree scope = declarator->u.id.qualifying_scope;
25128 if (scope)
25129 num_templates = num_template_headers_for_class (scope);
25130 else if (TREE_CODE (declarator->u.id.unqualified_name)
25131 == TEMPLATE_ID_EXPR)
25132 /* If the DECLARATOR has the form `X<y>' then it uses one
25133 additional level of template parameters. */
25134 ++num_templates;
25136 return cp_parser_check_template_parameters
25137 (parser, num_templates, declarator_location, declarator);
25140 case cdk_function:
25141 case cdk_array:
25142 case cdk_pointer:
25143 case cdk_reference:
25144 case cdk_ptrmem:
25145 return (cp_parser_check_declarator_template_parameters
25146 (parser, declarator->declarator, declarator_location));
25148 case cdk_error:
25149 return true;
25151 default:
25152 gcc_unreachable ();
25154 return false;
25157 /* NUM_TEMPLATES were used in the current declaration. If that is
25158 invalid, return FALSE and issue an error messages. Otherwise,
25159 return TRUE. If DECLARATOR is non-NULL, then we are checking a
25160 declarator and we can print more accurate diagnostics. */
25162 static bool
25163 cp_parser_check_template_parameters (cp_parser* parser,
25164 unsigned num_templates,
25165 location_t location,
25166 cp_declarator *declarator)
25168 /* If there are the same number of template classes and parameter
25169 lists, that's OK. */
25170 if (parser->num_template_parameter_lists == num_templates)
25171 return true;
25172 /* If there are more, but only one more, then we are referring to a
25173 member template. That's OK too. */
25174 if (parser->num_template_parameter_lists == num_templates + 1)
25175 return true;
25176 /* If there are more template classes than parameter lists, we have
25177 something like:
25179 template <class T> void S<T>::R<T>::f (); */
25180 if (parser->num_template_parameter_lists < num_templates)
25182 if (declarator && !current_function_decl)
25183 error_at (location, "specializing member %<%T::%E%> "
25184 "requires %<template<>%> syntax",
25185 declarator->u.id.qualifying_scope,
25186 declarator->u.id.unqualified_name);
25187 else if (declarator)
25188 error_at (location, "invalid declaration of %<%T::%E%>",
25189 declarator->u.id.qualifying_scope,
25190 declarator->u.id.unqualified_name);
25191 else
25192 error_at (location, "too few template-parameter-lists");
25193 return false;
25195 /* Otherwise, there are too many template parameter lists. We have
25196 something like:
25198 template <class T> template <class U> void S::f(); */
25199 error_at (location, "too many template-parameter-lists");
25200 return false;
25203 /* Parse an optional `::' token indicating that the following name is
25204 from the global namespace. If so, PARSER->SCOPE is set to the
25205 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
25206 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
25207 Returns the new value of PARSER->SCOPE, if the `::' token is
25208 present, and NULL_TREE otherwise. */
25210 static tree
25211 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
25213 cp_token *token;
25215 /* Peek at the next token. */
25216 token = cp_lexer_peek_token (parser->lexer);
25217 /* If we're looking at a `::' token then we're starting from the
25218 global namespace, not our current location. */
25219 if (token->type == CPP_SCOPE)
25221 /* Consume the `::' token. */
25222 cp_lexer_consume_token (parser->lexer);
25223 /* Set the SCOPE so that we know where to start the lookup. */
25224 parser->scope = global_namespace;
25225 parser->qualifying_scope = global_namespace;
25226 parser->object_scope = NULL_TREE;
25228 return parser->scope;
25230 else if (!current_scope_valid_p)
25232 parser->scope = NULL_TREE;
25233 parser->qualifying_scope = NULL_TREE;
25234 parser->object_scope = NULL_TREE;
25237 return NULL_TREE;
25240 /* Returns TRUE if the upcoming token sequence is the start of a
25241 constructor declarator. If FRIEND_P is true, the declarator is
25242 preceded by the `friend' specifier. */
25244 static bool
25245 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
25247 bool constructor_p;
25248 bool outside_class_specifier_p;
25249 tree nested_name_specifier;
25250 cp_token *next_token;
25252 /* The common case is that this is not a constructor declarator, so
25253 try to avoid doing lots of work if at all possible. It's not
25254 valid declare a constructor at function scope. */
25255 if (parser->in_function_body)
25256 return false;
25257 /* And only certain tokens can begin a constructor declarator. */
25258 next_token = cp_lexer_peek_token (parser->lexer);
25259 if (next_token->type != CPP_NAME
25260 && next_token->type != CPP_SCOPE
25261 && next_token->type != CPP_NESTED_NAME_SPECIFIER
25262 && next_token->type != CPP_TEMPLATE_ID)
25263 return false;
25265 /* Parse tentatively; we are going to roll back all of the tokens
25266 consumed here. */
25267 cp_parser_parse_tentatively (parser);
25268 /* Assume that we are looking at a constructor declarator. */
25269 constructor_p = true;
25271 /* Look for the optional `::' operator. */
25272 cp_parser_global_scope_opt (parser,
25273 /*current_scope_valid_p=*/false);
25274 /* Look for the nested-name-specifier. */
25275 nested_name_specifier
25276 = (cp_parser_nested_name_specifier_opt (parser,
25277 /*typename_keyword_p=*/false,
25278 /*check_dependency_p=*/false,
25279 /*type_p=*/false,
25280 /*is_declaration=*/false));
25282 outside_class_specifier_p = (!at_class_scope_p ()
25283 || !TYPE_BEING_DEFINED (current_class_type)
25284 || friend_p);
25286 /* Outside of a class-specifier, there must be a
25287 nested-name-specifier. */
25288 if (!nested_name_specifier && outside_class_specifier_p)
25289 constructor_p = false;
25290 else if (nested_name_specifier == error_mark_node)
25291 constructor_p = false;
25293 /* If we have a class scope, this is easy; DR 147 says that S::S always
25294 names the constructor, and no other qualified name could. */
25295 if (constructor_p && nested_name_specifier
25296 && CLASS_TYPE_P (nested_name_specifier))
25298 tree id = cp_parser_unqualified_id (parser,
25299 /*template_keyword_p=*/false,
25300 /*check_dependency_p=*/false,
25301 /*declarator_p=*/true,
25302 /*optional_p=*/false);
25303 if (is_overloaded_fn (id))
25304 id = DECL_NAME (get_first_fn (id));
25305 if (!constructor_name_p (id, nested_name_specifier))
25306 constructor_p = false;
25308 /* If we still think that this might be a constructor-declarator,
25309 look for a class-name. */
25310 else if (constructor_p)
25312 /* If we have:
25314 template <typename T> struct S {
25315 S();
25318 we must recognize that the nested `S' names a class. */
25319 tree type_decl;
25320 type_decl = cp_parser_class_name (parser,
25321 /*typename_keyword_p=*/false,
25322 /*template_keyword_p=*/false,
25323 none_type,
25324 /*check_dependency_p=*/false,
25325 /*class_head_p=*/false,
25326 /*is_declaration=*/false);
25327 /* If there was no class-name, then this is not a constructor.
25328 Otherwise, if we are in a class-specifier and we aren't
25329 handling a friend declaration, check that its type matches
25330 current_class_type (c++/38313). Note: error_mark_node
25331 is left alone for error recovery purposes. */
25332 constructor_p = (!cp_parser_error_occurred (parser)
25333 && (outside_class_specifier_p
25334 || type_decl == error_mark_node
25335 || same_type_p (current_class_type,
25336 TREE_TYPE (type_decl))));
25338 /* If we're still considering a constructor, we have to see a `(',
25339 to begin the parameter-declaration-clause, followed by either a
25340 `)', an `...', or a decl-specifier. We need to check for a
25341 type-specifier to avoid being fooled into thinking that:
25343 S (f) (int);
25345 is a constructor. (It is actually a function named `f' that
25346 takes one parameter (of type `int') and returns a value of type
25347 `S'. */
25348 if (constructor_p
25349 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25350 constructor_p = false;
25352 if (constructor_p
25353 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
25354 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
25355 /* A parameter declaration begins with a decl-specifier,
25356 which is either the "attribute" keyword, a storage class
25357 specifier, or (usually) a type-specifier. */
25358 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
25360 tree type;
25361 tree pushed_scope = NULL_TREE;
25362 unsigned saved_num_template_parameter_lists;
25364 /* Names appearing in the type-specifier should be looked up
25365 in the scope of the class. */
25366 if (current_class_type)
25367 type = NULL_TREE;
25368 else
25370 type = TREE_TYPE (type_decl);
25371 if (TREE_CODE (type) == TYPENAME_TYPE)
25373 type = resolve_typename_type (type,
25374 /*only_current_p=*/false);
25375 if (TREE_CODE (type) == TYPENAME_TYPE)
25377 cp_parser_abort_tentative_parse (parser);
25378 return false;
25381 pushed_scope = push_scope (type);
25384 /* Inside the constructor parameter list, surrounding
25385 template-parameter-lists do not apply. */
25386 saved_num_template_parameter_lists
25387 = parser->num_template_parameter_lists;
25388 parser->num_template_parameter_lists = 0;
25390 /* Look for the type-specifier. */
25391 cp_parser_type_specifier (parser,
25392 CP_PARSER_FLAGS_NONE,
25393 /*decl_specs=*/NULL,
25394 /*is_declarator=*/true,
25395 /*declares_class_or_enum=*/NULL,
25396 /*is_cv_qualifier=*/NULL);
25398 parser->num_template_parameter_lists
25399 = saved_num_template_parameter_lists;
25401 /* Leave the scope of the class. */
25402 if (pushed_scope)
25403 pop_scope (pushed_scope);
25405 constructor_p = !cp_parser_error_occurred (parser);
25409 /* We did not really want to consume any tokens. */
25410 cp_parser_abort_tentative_parse (parser);
25412 return constructor_p;
25415 /* Parse the definition of the function given by the DECL_SPECIFIERS,
25416 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
25417 they must be performed once we are in the scope of the function.
25419 Returns the function defined. */
25421 static tree
25422 cp_parser_function_definition_from_specifiers_and_declarator
25423 (cp_parser* parser,
25424 cp_decl_specifier_seq *decl_specifiers,
25425 tree attributes,
25426 const cp_declarator *declarator)
25428 tree fn;
25429 bool success_p;
25431 /* Begin the function-definition. */
25432 success_p = start_function (decl_specifiers, declarator, attributes);
25434 /* The things we're about to see are not directly qualified by any
25435 template headers we've seen thus far. */
25436 reset_specialization ();
25438 /* If there were names looked up in the decl-specifier-seq that we
25439 did not check, check them now. We must wait until we are in the
25440 scope of the function to perform the checks, since the function
25441 might be a friend. */
25442 perform_deferred_access_checks (tf_warning_or_error);
25444 if (success_p)
25446 cp_finalize_omp_declare_simd (parser, current_function_decl);
25447 parser->omp_declare_simd = NULL;
25448 cp_finalize_oacc_routine (parser, current_function_decl, true);
25449 parser->oacc_routine = NULL;
25452 if (!success_p)
25454 /* Skip the entire function. */
25455 cp_parser_skip_to_end_of_block_or_statement (parser);
25456 fn = error_mark_node;
25458 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
25460 /* Seen already, skip it. An error message has already been output. */
25461 cp_parser_skip_to_end_of_block_or_statement (parser);
25462 fn = current_function_decl;
25463 current_function_decl = NULL_TREE;
25464 /* If this is a function from a class, pop the nested class. */
25465 if (current_class_name)
25466 pop_nested_class ();
25468 else
25470 timevar_id_t tv;
25471 if (DECL_DECLARED_INLINE_P (current_function_decl))
25472 tv = TV_PARSE_INLINE;
25473 else
25474 tv = TV_PARSE_FUNC;
25475 timevar_push (tv);
25476 fn = cp_parser_function_definition_after_declarator (parser,
25477 /*inline_p=*/false);
25478 timevar_pop (tv);
25481 return fn;
25484 /* Parse the part of a function-definition that follows the
25485 declarator. INLINE_P is TRUE iff this function is an inline
25486 function defined within a class-specifier.
25488 Returns the function defined. */
25490 static tree
25491 cp_parser_function_definition_after_declarator (cp_parser* parser,
25492 bool inline_p)
25494 tree fn;
25495 bool ctor_initializer_p = false;
25496 bool saved_in_unbraced_linkage_specification_p;
25497 bool saved_in_function_body;
25498 unsigned saved_num_template_parameter_lists;
25499 cp_token *token;
25500 bool fully_implicit_function_template_p
25501 = parser->fully_implicit_function_template_p;
25502 parser->fully_implicit_function_template_p = false;
25503 tree implicit_template_parms
25504 = parser->implicit_template_parms;
25505 parser->implicit_template_parms = 0;
25506 cp_binding_level* implicit_template_scope
25507 = parser->implicit_template_scope;
25508 parser->implicit_template_scope = 0;
25510 saved_in_function_body = parser->in_function_body;
25511 parser->in_function_body = true;
25512 /* If the next token is `return', then the code may be trying to
25513 make use of the "named return value" extension that G++ used to
25514 support. */
25515 token = cp_lexer_peek_token (parser->lexer);
25516 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
25518 /* Consume the `return' keyword. */
25519 cp_lexer_consume_token (parser->lexer);
25520 /* Look for the identifier that indicates what value is to be
25521 returned. */
25522 cp_parser_identifier (parser);
25523 /* Issue an error message. */
25524 error_at (token->location,
25525 "named return values are no longer supported");
25526 /* Skip tokens until we reach the start of the function body. */
25527 while (true)
25529 cp_token *token = cp_lexer_peek_token (parser->lexer);
25530 if (token->type == CPP_OPEN_BRACE
25531 || token->type == CPP_EOF
25532 || token->type == CPP_PRAGMA_EOL)
25533 break;
25534 cp_lexer_consume_token (parser->lexer);
25537 /* The `extern' in `extern "C" void f () { ... }' does not apply to
25538 anything declared inside `f'. */
25539 saved_in_unbraced_linkage_specification_p
25540 = parser->in_unbraced_linkage_specification_p;
25541 parser->in_unbraced_linkage_specification_p = false;
25542 /* Inside the function, surrounding template-parameter-lists do not
25543 apply. */
25544 saved_num_template_parameter_lists
25545 = parser->num_template_parameter_lists;
25546 parser->num_template_parameter_lists = 0;
25548 start_lambda_scope (current_function_decl);
25550 /* If the next token is `try', `__transaction_atomic', or
25551 `__transaction_relaxed`, then we are looking at either function-try-block
25552 or function-transaction-block. Note that all of these include the
25553 function-body. */
25554 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
25555 ctor_initializer_p = cp_parser_function_transaction (parser,
25556 RID_TRANSACTION_ATOMIC);
25557 else if (cp_lexer_next_token_is_keyword (parser->lexer,
25558 RID_TRANSACTION_RELAXED))
25559 ctor_initializer_p = cp_parser_function_transaction (parser,
25560 RID_TRANSACTION_RELAXED);
25561 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
25562 ctor_initializer_p = cp_parser_function_try_block (parser);
25563 else
25564 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
25565 (parser, /*in_function_try_block=*/false);
25567 finish_lambda_scope ();
25569 /* Finish the function. */
25570 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
25571 (inline_p ? 2 : 0));
25572 /* Generate code for it, if necessary. */
25573 expand_or_defer_fn (fn);
25574 /* Restore the saved values. */
25575 parser->in_unbraced_linkage_specification_p
25576 = saved_in_unbraced_linkage_specification_p;
25577 parser->num_template_parameter_lists
25578 = saved_num_template_parameter_lists;
25579 parser->in_function_body = saved_in_function_body;
25581 parser->fully_implicit_function_template_p
25582 = fully_implicit_function_template_p;
25583 parser->implicit_template_parms
25584 = implicit_template_parms;
25585 parser->implicit_template_scope
25586 = implicit_template_scope;
25588 if (parser->fully_implicit_function_template_p)
25589 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
25591 return fn;
25594 /* Parse a template-declaration body (following argument list). */
25596 static void
25597 cp_parser_template_declaration_after_parameters (cp_parser* parser,
25598 tree parameter_list,
25599 bool member_p)
25601 tree decl = NULL_TREE;
25602 bool friend_p = false;
25604 /* We just processed one more parameter list. */
25605 ++parser->num_template_parameter_lists;
25607 /* Get the deferred access checks from the parameter list. These
25608 will be checked once we know what is being declared, as for a
25609 member template the checks must be performed in the scope of the
25610 class containing the member. */
25611 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
25613 /* Tentatively parse for a new template parameter list, which can either be
25614 the template keyword or a template introduction. */
25615 if (cp_parser_template_declaration_after_export (parser, member_p))
25616 /* OK */;
25617 else if (cxx_dialect >= cxx11
25618 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25619 decl = cp_parser_alias_declaration (parser);
25620 else
25622 /* There are no access checks when parsing a template, as we do not
25623 know if a specialization will be a friend. */
25624 push_deferring_access_checks (dk_no_check);
25625 cp_token *token = cp_lexer_peek_token (parser->lexer);
25626 decl = cp_parser_single_declaration (parser,
25627 checks,
25628 member_p,
25629 /*explicit_specialization_p=*/false,
25630 &friend_p);
25631 pop_deferring_access_checks ();
25633 /* If this is a member template declaration, let the front
25634 end know. */
25635 if (member_p && !friend_p && decl)
25637 if (TREE_CODE (decl) == TYPE_DECL)
25638 cp_parser_check_access_in_redeclaration (decl, token->location);
25640 decl = finish_member_template_decl (decl);
25642 else if (friend_p && decl
25643 && DECL_DECLARES_TYPE_P (decl))
25644 make_friend_class (current_class_type, TREE_TYPE (decl),
25645 /*complain=*/true);
25647 /* We are done with the current parameter list. */
25648 --parser->num_template_parameter_lists;
25650 pop_deferring_access_checks ();
25652 /* Finish up. */
25653 finish_template_decl (parameter_list);
25655 /* Check the template arguments for a literal operator template. */
25656 if (decl
25657 && DECL_DECLARES_FUNCTION_P (decl)
25658 && UDLIT_OPER_P (DECL_NAME (decl)))
25660 bool ok = true;
25661 if (parameter_list == NULL_TREE)
25662 ok = false;
25663 else
25665 int num_parms = TREE_VEC_LENGTH (parameter_list);
25666 if (num_parms == 1)
25668 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
25669 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25670 if (TREE_TYPE (parm) != char_type_node
25671 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25672 ok = false;
25674 else if (num_parms == 2 && cxx_dialect >= cxx14)
25676 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
25677 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
25678 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
25679 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25680 if (TREE_TYPE (parm) != TREE_TYPE (type)
25681 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25682 ok = false;
25684 else
25685 ok = false;
25687 if (!ok)
25689 if (cxx_dialect >= cxx14)
25690 error ("literal operator template %qD has invalid parameter list."
25691 " Expected non-type template argument pack <char...>"
25692 " or <typename CharT, CharT...>",
25693 decl);
25694 else
25695 error ("literal operator template %qD has invalid parameter list."
25696 " Expected non-type template argument pack <char...>",
25697 decl);
25701 /* Register member declarations. */
25702 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
25703 finish_member_declaration (decl);
25704 /* If DECL is a function template, we must return to parse it later.
25705 (Even though there is no definition, there might be default
25706 arguments that need handling.) */
25707 if (member_p && decl
25708 && DECL_DECLARES_FUNCTION_P (decl))
25709 vec_safe_push (unparsed_funs_with_definitions, decl);
25712 /* Parse a template introduction header for a template-declaration. Returns
25713 false if tentative parse fails. */
25715 static bool
25716 cp_parser_template_introduction (cp_parser* parser, bool member_p)
25718 cp_parser_parse_tentatively (parser);
25720 tree saved_scope = parser->scope;
25721 tree saved_object_scope = parser->object_scope;
25722 tree saved_qualifying_scope = parser->qualifying_scope;
25724 /* Look for the optional `::' operator. */
25725 cp_parser_global_scope_opt (parser,
25726 /*current_scope_valid_p=*/false);
25727 /* Look for the nested-name-specifier. */
25728 cp_parser_nested_name_specifier_opt (parser,
25729 /*typename_keyword_p=*/false,
25730 /*check_dependency_p=*/true,
25731 /*type_p=*/false,
25732 /*is_declaration=*/false);
25734 cp_token *token = cp_lexer_peek_token (parser->lexer);
25735 tree concept_name = cp_parser_identifier (parser);
25737 /* Look up the concept for which we will be matching
25738 template parameters. */
25739 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
25740 token->location);
25741 parser->scope = saved_scope;
25742 parser->object_scope = saved_object_scope;
25743 parser->qualifying_scope = saved_qualifying_scope;
25745 if (concept_name == error_mark_node)
25746 cp_parser_simulate_error (parser);
25748 /* Look for opening brace for introduction. */
25749 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
25751 if (!cp_parser_parse_definitely (parser))
25752 return false;
25754 push_deferring_access_checks (dk_deferred);
25756 /* Build vector of placeholder parameters and grab
25757 matching identifiers. */
25758 tree introduction_list = cp_parser_introduction_list (parser);
25760 /* The introduction-list shall not be empty. */
25761 int nargs = TREE_VEC_LENGTH (introduction_list);
25762 if (nargs == 0)
25764 error ("empty introduction-list");
25765 return true;
25768 /* Look for closing brace for introduction. */
25769 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25770 return true;
25772 if (tmpl_decl == error_mark_node)
25774 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
25775 token->location);
25776 return true;
25779 /* Build and associate the constraint. */
25780 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
25781 if (parms && parms != error_mark_node)
25783 cp_parser_template_declaration_after_parameters (parser, parms,
25784 member_p);
25785 return true;
25788 error_at (token->location, "no matching concept for template-introduction");
25789 return true;
25792 /* Parse a normal template-declaration following the template keyword. */
25794 static void
25795 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
25797 tree parameter_list;
25798 bool need_lang_pop;
25799 location_t location = input_location;
25801 /* Look for the `<' token. */
25802 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
25803 return;
25804 if (at_class_scope_p () && current_function_decl)
25806 /* 14.5.2.2 [temp.mem]
25808 A local class shall not have member templates. */
25809 error_at (location,
25810 "invalid declaration of member template in local class");
25811 cp_parser_skip_to_end_of_block_or_statement (parser);
25812 return;
25814 /* [temp]
25816 A template ... shall not have C linkage. */
25817 if (current_lang_name == lang_name_c)
25819 error_at (location, "template with C linkage");
25820 /* Give it C++ linkage to avoid confusing other parts of the
25821 front end. */
25822 push_lang_context (lang_name_cplusplus);
25823 need_lang_pop = true;
25825 else
25826 need_lang_pop = false;
25828 /* We cannot perform access checks on the template parameter
25829 declarations until we know what is being declared, just as we
25830 cannot check the decl-specifier list. */
25831 push_deferring_access_checks (dk_deferred);
25833 /* If the next token is `>', then we have an invalid
25834 specialization. Rather than complain about an invalid template
25835 parameter, issue an error message here. */
25836 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
25838 cp_parser_error (parser, "invalid explicit specialization");
25839 begin_specialization ();
25840 parameter_list = NULL_TREE;
25842 else
25844 /* Parse the template parameters. */
25845 parameter_list = cp_parser_template_parameter_list (parser);
25848 /* Look for the `>'. */
25849 cp_parser_skip_to_end_of_template_parameter_list (parser);
25851 /* Manage template requirements */
25852 if (flag_concepts)
25854 tree reqs = get_shorthand_constraints (current_template_parms);
25855 if (tree r = cp_parser_requires_clause_opt (parser))
25856 reqs = conjoin_constraints (reqs, normalize_expression (r));
25857 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
25860 cp_parser_template_declaration_after_parameters (parser, parameter_list,
25861 member_p);
25863 /* For the erroneous case of a template with C linkage, we pushed an
25864 implicit C++ linkage scope; exit that scope now. */
25865 if (need_lang_pop)
25866 pop_lang_context ();
25869 /* Parse a template-declaration, assuming that the `export' (and
25870 `extern') keywords, if present, has already been scanned. MEMBER_P
25871 is as for cp_parser_template_declaration. */
25873 static bool
25874 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
25876 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25878 cp_lexer_consume_token (parser->lexer);
25879 cp_parser_explicit_template_declaration (parser, member_p);
25880 return true;
25882 else if (flag_concepts)
25883 return cp_parser_template_introduction (parser, member_p);
25885 return false;
25888 /* Perform the deferred access checks from a template-parameter-list.
25889 CHECKS is a TREE_LIST of access checks, as returned by
25890 get_deferred_access_checks. */
25892 static void
25893 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
25895 ++processing_template_parmlist;
25896 perform_access_checks (checks, tf_warning_or_error);
25897 --processing_template_parmlist;
25900 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
25901 `function-definition' sequence that follows a template header.
25902 If MEMBER_P is true, this declaration appears in a class scope.
25904 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
25905 *FRIEND_P is set to TRUE iff the declaration is a friend. */
25907 static tree
25908 cp_parser_single_declaration (cp_parser* parser,
25909 vec<deferred_access_check, va_gc> *checks,
25910 bool member_p,
25911 bool explicit_specialization_p,
25912 bool* friend_p)
25914 int declares_class_or_enum;
25915 tree decl = NULL_TREE;
25916 cp_decl_specifier_seq decl_specifiers;
25917 bool function_definition_p = false;
25918 cp_token *decl_spec_token_start;
25920 /* This function is only used when processing a template
25921 declaration. */
25922 gcc_assert (innermost_scope_kind () == sk_template_parms
25923 || innermost_scope_kind () == sk_template_spec);
25925 /* Defer access checks until we know what is being declared. */
25926 push_deferring_access_checks (dk_deferred);
25928 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
25929 alternative. */
25930 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25931 cp_parser_decl_specifier_seq (parser,
25932 CP_PARSER_FLAGS_OPTIONAL,
25933 &decl_specifiers,
25934 &declares_class_or_enum);
25935 if (friend_p)
25936 *friend_p = cp_parser_friend_p (&decl_specifiers);
25938 /* There are no template typedefs. */
25939 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
25941 error_at (decl_spec_token_start->location,
25942 "template declaration of %<typedef%>");
25943 decl = error_mark_node;
25946 /* Gather up the access checks that occurred the
25947 decl-specifier-seq. */
25948 stop_deferring_access_checks ();
25950 /* Check for the declaration of a template class. */
25951 if (declares_class_or_enum)
25953 if (cp_parser_declares_only_class_p (parser)
25954 || (declares_class_or_enum & 2))
25956 // If this is a declaration, but not a definition, associate
25957 // any constraints with the type declaration. Constraints
25958 // are associated with definitions in cp_parser_class_specifier.
25959 if (declares_class_or_enum == 1)
25960 associate_classtype_constraints (decl_specifiers.type);
25962 decl = shadow_tag (&decl_specifiers);
25964 /* In this case:
25966 struct C {
25967 friend template <typename T> struct A<T>::B;
25970 A<T>::B will be represented by a TYPENAME_TYPE, and
25971 therefore not recognized by shadow_tag. */
25972 if (friend_p && *friend_p
25973 && !decl
25974 && decl_specifiers.type
25975 && TYPE_P (decl_specifiers.type))
25976 decl = decl_specifiers.type;
25978 if (decl && decl != error_mark_node)
25979 decl = TYPE_NAME (decl);
25980 else
25981 decl = error_mark_node;
25983 /* Perform access checks for template parameters. */
25984 cp_parser_perform_template_parameter_access_checks (checks);
25986 /* Give a helpful diagnostic for
25987 template <class T> struct A { } a;
25988 if we aren't already recovering from an error. */
25989 if (!cp_parser_declares_only_class_p (parser)
25990 && !seen_error ())
25992 error_at (cp_lexer_peek_token (parser->lexer)->location,
25993 "a class template declaration must not declare "
25994 "anything else");
25995 cp_parser_skip_to_end_of_block_or_statement (parser);
25996 goto out;
26001 /* Complain about missing 'typename' or other invalid type names. */
26002 if (!decl_specifiers.any_type_specifiers_p
26003 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
26005 /* cp_parser_parse_and_diagnose_invalid_type_name calls
26006 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
26007 the rest of this declaration. */
26008 decl = error_mark_node;
26009 goto out;
26012 /* If it's not a template class, try for a template function. If
26013 the next token is a `;', then this declaration does not declare
26014 anything. But, if there were errors in the decl-specifiers, then
26015 the error might well have come from an attempted class-specifier.
26016 In that case, there's no need to warn about a missing declarator. */
26017 if (!decl
26018 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
26019 || decl_specifiers.type != error_mark_node))
26021 decl = cp_parser_init_declarator (parser,
26022 &decl_specifiers,
26023 checks,
26024 /*function_definition_allowed_p=*/true,
26025 member_p,
26026 declares_class_or_enum,
26027 &function_definition_p,
26028 NULL, NULL, NULL);
26030 /* 7.1.1-1 [dcl.stc]
26032 A storage-class-specifier shall not be specified in an explicit
26033 specialization... */
26034 if (decl
26035 && explicit_specialization_p
26036 && decl_specifiers.storage_class != sc_none)
26038 error_at (decl_spec_token_start->location,
26039 "explicit template specialization cannot have a storage class");
26040 decl = error_mark_node;
26043 if (decl && VAR_P (decl))
26044 check_template_variable (decl);
26047 /* Look for a trailing `;' after the declaration. */
26048 if (!function_definition_p
26049 && (decl == error_mark_node
26050 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
26051 cp_parser_skip_to_end_of_block_or_statement (parser);
26053 out:
26054 pop_deferring_access_checks ();
26056 /* Clear any current qualification; whatever comes next is the start
26057 of something new. */
26058 parser->scope = NULL_TREE;
26059 parser->qualifying_scope = NULL_TREE;
26060 parser->object_scope = NULL_TREE;
26062 return decl;
26065 /* Parse a cast-expression that is not the operand of a unary "&". */
26067 static cp_expr
26068 cp_parser_simple_cast_expression (cp_parser *parser)
26070 return cp_parser_cast_expression (parser, /*address_p=*/false,
26071 /*cast_p=*/false, /*decltype*/false, NULL);
26074 /* Parse a functional cast to TYPE. Returns an expression
26075 representing the cast. */
26077 static cp_expr
26078 cp_parser_functional_cast (cp_parser* parser, tree type)
26080 vec<tree, va_gc> *vec;
26081 tree expression_list;
26082 cp_expr cast;
26083 bool nonconst_p;
26085 location_t start_loc = input_location;
26087 if (!type)
26088 type = error_mark_node;
26090 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26092 cp_lexer_set_source_position (parser->lexer);
26093 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26094 expression_list = cp_parser_braced_list (parser, &nonconst_p);
26095 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
26096 if (TREE_CODE (type) == TYPE_DECL)
26097 type = TREE_TYPE (type);
26099 cast = finish_compound_literal (type, expression_list,
26100 tf_warning_or_error);
26101 /* Create a location of the form:
26102 type_name{i, f}
26103 ^~~~~~~~~~~~~~~
26104 with caret == start at the start of the type name,
26105 finishing at the closing brace. */
26106 location_t finish_loc
26107 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26108 location_t combined_loc = make_location (start_loc, start_loc,
26109 finish_loc);
26110 cast.set_location (combined_loc);
26111 return cast;
26115 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
26116 /*cast_p=*/true,
26117 /*allow_expansion_p=*/true,
26118 /*non_constant_p=*/NULL);
26119 if (vec == NULL)
26120 expression_list = error_mark_node;
26121 else
26123 expression_list = build_tree_list_vec (vec);
26124 release_tree_vector (vec);
26127 cast = build_functional_cast (type, expression_list,
26128 tf_warning_or_error);
26129 /* [expr.const]/1: In an integral constant expression "only type
26130 conversions to integral or enumeration type can be used". */
26131 if (TREE_CODE (type) == TYPE_DECL)
26132 type = TREE_TYPE (type);
26133 if (cast != error_mark_node
26134 && !cast_valid_in_integral_constant_expression_p (type)
26135 && cp_parser_non_integral_constant_expression (parser,
26136 NIC_CONSTRUCTOR))
26137 return error_mark_node;
26139 /* Create a location of the form:
26140 float(i)
26141 ^~~~~~~~
26142 with caret == start at the start of the type name,
26143 finishing at the closing paren. */
26144 location_t finish_loc
26145 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26146 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
26147 cast.set_location (combined_loc);
26148 return cast;
26151 /* Save the tokens that make up the body of a member function defined
26152 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
26153 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
26154 specifiers applied to the declaration. Returns the FUNCTION_DECL
26155 for the member function. */
26157 static tree
26158 cp_parser_save_member_function_body (cp_parser* parser,
26159 cp_decl_specifier_seq *decl_specifiers,
26160 cp_declarator *declarator,
26161 tree attributes)
26163 cp_token *first;
26164 cp_token *last;
26165 tree fn;
26166 bool function_try_block = false;
26168 /* Create the FUNCTION_DECL. */
26169 fn = grokmethod (decl_specifiers, declarator, attributes);
26170 cp_finalize_omp_declare_simd (parser, fn);
26171 cp_finalize_oacc_routine (parser, fn, true);
26172 /* If something went badly wrong, bail out now. */
26173 if (fn == error_mark_node)
26175 /* If there's a function-body, skip it. */
26176 if (cp_parser_token_starts_function_definition_p
26177 (cp_lexer_peek_token (parser->lexer)))
26178 cp_parser_skip_to_end_of_block_or_statement (parser);
26179 return error_mark_node;
26182 /* Remember it, if there default args to post process. */
26183 cp_parser_save_default_args (parser, fn);
26185 /* Save away the tokens that make up the body of the
26186 function. */
26187 first = parser->lexer->next_token;
26189 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
26190 cp_lexer_consume_token (parser->lexer);
26191 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26192 RID_TRANSACTION_ATOMIC))
26194 cp_lexer_consume_token (parser->lexer);
26195 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
26196 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
26197 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
26198 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
26199 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
26200 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
26201 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
26203 cp_lexer_consume_token (parser->lexer);
26204 cp_lexer_consume_token (parser->lexer);
26205 cp_lexer_consume_token (parser->lexer);
26206 cp_lexer_consume_token (parser->lexer);
26207 cp_lexer_consume_token (parser->lexer);
26209 else
26210 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
26211 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
26213 cp_lexer_consume_token (parser->lexer);
26214 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26215 break;
26219 /* Handle function try blocks. */
26220 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26222 cp_lexer_consume_token (parser->lexer);
26223 function_try_block = true;
26225 /* We can have braced-init-list mem-initializers before the fn body. */
26226 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26228 cp_lexer_consume_token (parser->lexer);
26229 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
26231 /* cache_group will stop after an un-nested { } pair, too. */
26232 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26233 break;
26235 /* variadic mem-inits have ... after the ')'. */
26236 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26237 cp_lexer_consume_token (parser->lexer);
26240 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26241 /* Handle function try blocks. */
26242 if (function_try_block)
26243 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
26244 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26245 last = parser->lexer->next_token;
26247 /* Save away the inline definition; we will process it when the
26248 class is complete. */
26249 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
26250 DECL_PENDING_INLINE_P (fn) = 1;
26252 /* We need to know that this was defined in the class, so that
26253 friend templates are handled correctly. */
26254 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
26256 /* Add FN to the queue of functions to be parsed later. */
26257 vec_safe_push (unparsed_funs_with_definitions, fn);
26259 return fn;
26262 /* Save the tokens that make up the in-class initializer for a non-static
26263 data member. Returns a DEFAULT_ARG. */
26265 static tree
26266 cp_parser_save_nsdmi (cp_parser* parser)
26268 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
26271 /* Parse a template-argument-list, as well as the trailing ">" (but
26272 not the opening "<"). See cp_parser_template_argument_list for the
26273 return value. */
26275 static tree
26276 cp_parser_enclosed_template_argument_list (cp_parser* parser)
26278 tree arguments;
26279 tree saved_scope;
26280 tree saved_qualifying_scope;
26281 tree saved_object_scope;
26282 bool saved_greater_than_is_operator_p;
26283 int saved_unevaluated_operand;
26284 int saved_inhibit_evaluation_warnings;
26286 /* [temp.names]
26288 When parsing a template-id, the first non-nested `>' is taken as
26289 the end of the template-argument-list rather than a greater-than
26290 operator. */
26291 saved_greater_than_is_operator_p
26292 = parser->greater_than_is_operator_p;
26293 parser->greater_than_is_operator_p = false;
26294 /* Parsing the argument list may modify SCOPE, so we save it
26295 here. */
26296 saved_scope = parser->scope;
26297 saved_qualifying_scope = parser->qualifying_scope;
26298 saved_object_scope = parser->object_scope;
26299 /* We need to evaluate the template arguments, even though this
26300 template-id may be nested within a "sizeof". */
26301 saved_unevaluated_operand = cp_unevaluated_operand;
26302 cp_unevaluated_operand = 0;
26303 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
26304 c_inhibit_evaluation_warnings = 0;
26305 /* Parse the template-argument-list itself. */
26306 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
26307 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26308 arguments = NULL_TREE;
26309 else
26310 arguments = cp_parser_template_argument_list (parser);
26311 /* Look for the `>' that ends the template-argument-list. If we find
26312 a '>>' instead, it's probably just a typo. */
26313 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26315 if (cxx_dialect != cxx98)
26317 /* In C++0x, a `>>' in a template argument list or cast
26318 expression is considered to be two separate `>'
26319 tokens. So, change the current token to a `>', but don't
26320 consume it: it will be consumed later when the outer
26321 template argument list (or cast expression) is parsed.
26322 Note that this replacement of `>' for `>>' is necessary
26323 even if we are parsing tentatively: in the tentative
26324 case, after calling
26325 cp_parser_enclosed_template_argument_list we will always
26326 throw away all of the template arguments and the first
26327 closing `>', either because the template argument list
26328 was erroneous or because we are replacing those tokens
26329 with a CPP_TEMPLATE_ID token. The second `>' (which will
26330 not have been thrown away) is needed either to close an
26331 outer template argument list or to complete a new-style
26332 cast. */
26333 cp_token *token = cp_lexer_peek_token (parser->lexer);
26334 token->type = CPP_GREATER;
26336 else if (!saved_greater_than_is_operator_p)
26338 /* If we're in a nested template argument list, the '>>' has
26339 to be a typo for '> >'. We emit the error message, but we
26340 continue parsing and we push a '>' as next token, so that
26341 the argument list will be parsed correctly. Note that the
26342 global source location is still on the token before the
26343 '>>', so we need to say explicitly where we want it. */
26344 cp_token *token = cp_lexer_peek_token (parser->lexer);
26345 gcc_rich_location richloc (token->location);
26346 richloc.add_fixit_replace ("> >");
26347 error_at_rich_loc (&richloc, "%<>>%> should be %<> >%> "
26348 "within a nested template argument list");
26350 token->type = CPP_GREATER;
26352 else
26354 /* If this is not a nested template argument list, the '>>'
26355 is a typo for '>'. Emit an error message and continue.
26356 Same deal about the token location, but here we can get it
26357 right by consuming the '>>' before issuing the diagnostic. */
26358 cp_token *token = cp_lexer_consume_token (parser->lexer);
26359 error_at (token->location,
26360 "spurious %<>>%>, use %<>%> to terminate "
26361 "a template argument list");
26364 else
26365 cp_parser_skip_to_end_of_template_parameter_list (parser);
26366 /* The `>' token might be a greater-than operator again now. */
26367 parser->greater_than_is_operator_p
26368 = saved_greater_than_is_operator_p;
26369 /* Restore the SAVED_SCOPE. */
26370 parser->scope = saved_scope;
26371 parser->qualifying_scope = saved_qualifying_scope;
26372 parser->object_scope = saved_object_scope;
26373 cp_unevaluated_operand = saved_unevaluated_operand;
26374 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
26376 return arguments;
26379 /* MEMBER_FUNCTION is a member function, or a friend. If default
26380 arguments, or the body of the function have not yet been parsed,
26381 parse them now. */
26383 static void
26384 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
26386 timevar_push (TV_PARSE_INMETH);
26387 /* If this member is a template, get the underlying
26388 FUNCTION_DECL. */
26389 if (DECL_FUNCTION_TEMPLATE_P (member_function))
26390 member_function = DECL_TEMPLATE_RESULT (member_function);
26392 /* There should not be any class definitions in progress at this
26393 point; the bodies of members are only parsed outside of all class
26394 definitions. */
26395 gcc_assert (parser->num_classes_being_defined == 0);
26396 /* While we're parsing the member functions we might encounter more
26397 classes. We want to handle them right away, but we don't want
26398 them getting mixed up with functions that are currently in the
26399 queue. */
26400 push_unparsed_function_queues (parser);
26402 /* Make sure that any template parameters are in scope. */
26403 maybe_begin_member_template_processing (member_function);
26405 /* If the body of the function has not yet been parsed, parse it
26406 now. */
26407 if (DECL_PENDING_INLINE_P (member_function))
26409 tree function_scope;
26410 cp_token_cache *tokens;
26412 /* The function is no longer pending; we are processing it. */
26413 tokens = DECL_PENDING_INLINE_INFO (member_function);
26414 DECL_PENDING_INLINE_INFO (member_function) = NULL;
26415 DECL_PENDING_INLINE_P (member_function) = 0;
26417 /* If this is a local class, enter the scope of the containing
26418 function. */
26419 function_scope = current_function_decl;
26420 if (function_scope)
26421 push_function_context ();
26423 /* Push the body of the function onto the lexer stack. */
26424 cp_parser_push_lexer_for_tokens (parser, tokens);
26426 /* Let the front end know that we going to be defining this
26427 function. */
26428 start_preparsed_function (member_function, NULL_TREE,
26429 SF_PRE_PARSED | SF_INCLASS_INLINE);
26431 /* Don't do access checking if it is a templated function. */
26432 if (processing_template_decl)
26433 push_deferring_access_checks (dk_no_check);
26435 /* #pragma omp declare reduction needs special parsing. */
26436 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
26438 parser->lexer->in_pragma = true;
26439 cp_parser_omp_declare_reduction_exprs (member_function, parser);
26440 finish_function (/*inline*/2);
26441 cp_check_omp_declare_reduction (member_function);
26443 else
26444 /* Now, parse the body of the function. */
26445 cp_parser_function_definition_after_declarator (parser,
26446 /*inline_p=*/true);
26448 if (processing_template_decl)
26449 pop_deferring_access_checks ();
26451 /* Leave the scope of the containing function. */
26452 if (function_scope)
26453 pop_function_context ();
26454 cp_parser_pop_lexer (parser);
26457 /* Remove any template parameters from the symbol table. */
26458 maybe_end_member_template_processing ();
26460 /* Restore the queue. */
26461 pop_unparsed_function_queues (parser);
26462 timevar_pop (TV_PARSE_INMETH);
26465 /* If DECL contains any default args, remember it on the unparsed
26466 functions queue. */
26468 static void
26469 cp_parser_save_default_args (cp_parser* parser, tree decl)
26471 tree probe;
26473 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
26474 probe;
26475 probe = TREE_CHAIN (probe))
26476 if (TREE_PURPOSE (probe))
26478 cp_default_arg_entry entry = {current_class_type, decl};
26479 vec_safe_push (unparsed_funs_with_default_args, entry);
26480 break;
26484 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
26485 which is either a FIELD_DECL or PARM_DECL. Parse it and return
26486 the result. For a PARM_DECL, PARMTYPE is the corresponding type
26487 from the parameter-type-list. */
26489 static tree
26490 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
26491 tree default_arg, tree parmtype)
26493 cp_token_cache *tokens;
26494 tree parsed_arg;
26495 bool dummy;
26497 if (default_arg == error_mark_node)
26498 return error_mark_node;
26500 /* Push the saved tokens for the default argument onto the parser's
26501 lexer stack. */
26502 tokens = DEFARG_TOKENS (default_arg);
26503 cp_parser_push_lexer_for_tokens (parser, tokens);
26505 start_lambda_scope (decl);
26507 /* Parse the default argument. */
26508 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
26509 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
26510 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26512 finish_lambda_scope ();
26514 if (parsed_arg == error_mark_node)
26515 cp_parser_skip_to_end_of_statement (parser);
26517 if (!processing_template_decl)
26519 /* In a non-template class, check conversions now. In a template,
26520 we'll wait and instantiate these as needed. */
26521 if (TREE_CODE (decl) == PARM_DECL)
26522 parsed_arg = check_default_argument (parmtype, parsed_arg,
26523 tf_warning_or_error);
26524 else
26525 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
26528 /* If the token stream has not been completely used up, then
26529 there was extra junk after the end of the default
26530 argument. */
26531 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26533 if (TREE_CODE (decl) == PARM_DECL)
26534 cp_parser_error (parser, "expected %<,%>");
26535 else
26536 cp_parser_error (parser, "expected %<;%>");
26539 /* Revert to the main lexer. */
26540 cp_parser_pop_lexer (parser);
26542 return parsed_arg;
26545 /* FIELD is a non-static data member with an initializer which we saved for
26546 later; parse it now. */
26548 static void
26549 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
26551 tree def;
26553 maybe_begin_member_template_processing (field);
26555 push_unparsed_function_queues (parser);
26556 def = cp_parser_late_parse_one_default_arg (parser, field,
26557 DECL_INITIAL (field),
26558 NULL_TREE);
26559 pop_unparsed_function_queues (parser);
26561 maybe_end_member_template_processing ();
26563 DECL_INITIAL (field) = def;
26566 /* FN is a FUNCTION_DECL which may contains a parameter with an
26567 unparsed DEFAULT_ARG. Parse the default args now. This function
26568 assumes that the current scope is the scope in which the default
26569 argument should be processed. */
26571 static void
26572 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
26574 bool saved_local_variables_forbidden_p;
26575 tree parm, parmdecl;
26577 /* While we're parsing the default args, we might (due to the
26578 statement expression extension) encounter more classes. We want
26579 to handle them right away, but we don't want them getting mixed
26580 up with default args that are currently in the queue. */
26581 push_unparsed_function_queues (parser);
26583 /* Local variable names (and the `this' keyword) may not appear
26584 in a default argument. */
26585 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
26586 parser->local_variables_forbidden_p = true;
26588 push_defarg_context (fn);
26590 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
26591 parmdecl = DECL_ARGUMENTS (fn);
26592 parm && parm != void_list_node;
26593 parm = TREE_CHAIN (parm),
26594 parmdecl = DECL_CHAIN (parmdecl))
26596 tree default_arg = TREE_PURPOSE (parm);
26597 tree parsed_arg;
26598 vec<tree, va_gc> *insts;
26599 tree copy;
26600 unsigned ix;
26602 if (!default_arg)
26603 continue;
26605 if (TREE_CODE (default_arg) != DEFAULT_ARG)
26606 /* This can happen for a friend declaration for a function
26607 already declared with default arguments. */
26608 continue;
26610 parsed_arg
26611 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
26612 default_arg,
26613 TREE_VALUE (parm));
26614 if (parsed_arg == error_mark_node)
26616 continue;
26619 TREE_PURPOSE (parm) = parsed_arg;
26621 /* Update any instantiations we've already created. */
26622 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
26623 vec_safe_iterate (insts, ix, &copy); ix++)
26624 TREE_PURPOSE (copy) = parsed_arg;
26627 pop_defarg_context ();
26629 /* Make sure no default arg is missing. */
26630 check_default_args (fn);
26632 /* Restore the state of local_variables_forbidden_p. */
26633 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
26635 /* Restore the queue. */
26636 pop_unparsed_function_queues (parser);
26639 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
26641 sizeof ... ( identifier )
26643 where the 'sizeof' token has already been consumed. */
26645 static tree
26646 cp_parser_sizeof_pack (cp_parser *parser)
26648 /* Consume the `...'. */
26649 cp_lexer_consume_token (parser->lexer);
26650 maybe_warn_variadic_templates ();
26652 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
26653 if (paren)
26654 cp_lexer_consume_token (parser->lexer);
26655 else
26656 permerror (cp_lexer_peek_token (parser->lexer)->location,
26657 "%<sizeof...%> argument must be surrounded by parentheses");
26659 cp_token *token = cp_lexer_peek_token (parser->lexer);
26660 tree name = cp_parser_identifier (parser);
26661 if (name == error_mark_node)
26662 return error_mark_node;
26663 /* The name is not qualified. */
26664 parser->scope = NULL_TREE;
26665 parser->qualifying_scope = NULL_TREE;
26666 parser->object_scope = NULL_TREE;
26667 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
26668 if (expr == error_mark_node)
26669 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
26670 token->location);
26671 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
26672 expr = TREE_TYPE (expr);
26673 else if (TREE_CODE (expr) == CONST_DECL)
26674 expr = DECL_INITIAL (expr);
26675 expr = make_pack_expansion (expr);
26676 PACK_EXPANSION_SIZEOF_P (expr) = true;
26678 if (paren)
26679 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26681 return expr;
26684 /* Parse the operand of `sizeof' (or a similar operator). Returns
26685 either a TYPE or an expression, depending on the form of the
26686 input. The KEYWORD indicates which kind of expression we have
26687 encountered. */
26689 static tree
26690 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
26692 tree expr = NULL_TREE;
26693 const char *saved_message;
26694 char *tmp;
26695 bool saved_integral_constant_expression_p;
26696 bool saved_non_integral_constant_expression_p;
26698 /* If it's a `...', then we are computing the length of a parameter
26699 pack. */
26700 if (keyword == RID_SIZEOF
26701 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26702 return cp_parser_sizeof_pack (parser);
26704 /* Types cannot be defined in a `sizeof' expression. Save away the
26705 old message. */
26706 saved_message = parser->type_definition_forbidden_message;
26707 /* And create the new one. */
26708 tmp = concat ("types may not be defined in %<",
26709 IDENTIFIER_POINTER (ridpointers[keyword]),
26710 "%> expressions", NULL);
26711 parser->type_definition_forbidden_message = tmp;
26713 /* The restrictions on constant-expressions do not apply inside
26714 sizeof expressions. */
26715 saved_integral_constant_expression_p
26716 = parser->integral_constant_expression_p;
26717 saved_non_integral_constant_expression_p
26718 = parser->non_integral_constant_expression_p;
26719 parser->integral_constant_expression_p = false;
26721 /* Do not actually evaluate the expression. */
26722 ++cp_unevaluated_operand;
26723 ++c_inhibit_evaluation_warnings;
26724 /* If it's a `(', then we might be looking at the type-id
26725 construction. */
26726 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26728 tree type = NULL_TREE;
26730 /* We can't be sure yet whether we're looking at a type-id or an
26731 expression. */
26732 cp_parser_parse_tentatively (parser);
26733 /* Note: as a GNU Extension, compound literals are considered
26734 postfix-expressions as they are in C99, so they are valid
26735 arguments to sizeof. See comment in cp_parser_cast_expression
26736 for details. */
26737 if (cp_parser_compound_literal_p (parser))
26738 cp_parser_simulate_error (parser);
26739 else
26741 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
26742 parser->in_type_id_in_expr_p = true;
26743 /* Look for the type-id. */
26744 type = cp_parser_type_id (parser);
26745 /* Look for the closing `)'. */
26746 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26747 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
26750 /* If all went well, then we're done. */
26751 if (cp_parser_parse_definitely (parser))
26753 cp_decl_specifier_seq decl_specs;
26755 /* Build a trivial decl-specifier-seq. */
26756 clear_decl_specs (&decl_specs);
26757 decl_specs.type = type;
26759 /* Call grokdeclarator to figure out what type this is. */
26760 expr = grokdeclarator (NULL,
26761 &decl_specs,
26762 TYPENAME,
26763 /*initialized=*/0,
26764 /*attrlist=*/NULL);
26768 /* If the type-id production did not work out, then we must be
26769 looking at the unary-expression production. */
26770 if (!expr)
26771 expr = cp_parser_unary_expression (parser);
26773 /* Go back to evaluating expressions. */
26774 --cp_unevaluated_operand;
26775 --c_inhibit_evaluation_warnings;
26777 /* Free the message we created. */
26778 free (tmp);
26779 /* And restore the old one. */
26780 parser->type_definition_forbidden_message = saved_message;
26781 parser->integral_constant_expression_p
26782 = saved_integral_constant_expression_p;
26783 parser->non_integral_constant_expression_p
26784 = saved_non_integral_constant_expression_p;
26786 return expr;
26789 /* If the current declaration has no declarator, return true. */
26791 static bool
26792 cp_parser_declares_only_class_p (cp_parser *parser)
26794 /* If the next token is a `;' or a `,' then there is no
26795 declarator. */
26796 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26797 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
26800 /* Update the DECL_SPECS to reflect the storage class indicated by
26801 KEYWORD. */
26803 static void
26804 cp_parser_set_storage_class (cp_parser *parser,
26805 cp_decl_specifier_seq *decl_specs,
26806 enum rid keyword,
26807 cp_token *token)
26809 cp_storage_class storage_class;
26811 if (parser->in_unbraced_linkage_specification_p)
26813 error_at (token->location, "invalid use of %qD in linkage specification",
26814 ridpointers[keyword]);
26815 return;
26817 else if (decl_specs->storage_class != sc_none)
26819 decl_specs->conflicting_specifiers_p = true;
26820 return;
26823 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
26824 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
26825 && decl_specs->gnu_thread_keyword_p)
26827 pedwarn (decl_specs->locations[ds_thread], 0,
26828 "%<__thread%> before %qD", ridpointers[keyword]);
26831 switch (keyword)
26833 case RID_AUTO:
26834 storage_class = sc_auto;
26835 break;
26836 case RID_REGISTER:
26837 storage_class = sc_register;
26838 break;
26839 case RID_STATIC:
26840 storage_class = sc_static;
26841 break;
26842 case RID_EXTERN:
26843 storage_class = sc_extern;
26844 break;
26845 case RID_MUTABLE:
26846 storage_class = sc_mutable;
26847 break;
26848 default:
26849 gcc_unreachable ();
26851 decl_specs->storage_class = storage_class;
26852 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
26854 /* A storage class specifier cannot be applied alongside a typedef
26855 specifier. If there is a typedef specifier present then set
26856 conflicting_specifiers_p which will trigger an error later
26857 on in grokdeclarator. */
26858 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
26859 decl_specs->conflicting_specifiers_p = true;
26862 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
26863 is true, the type is a class or enum definition. */
26865 static void
26866 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
26867 tree type_spec,
26868 cp_token *token,
26869 bool type_definition_p)
26871 decl_specs->any_specifiers_p = true;
26873 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
26874 (with, for example, in "typedef int wchar_t;") we remember that
26875 this is what happened. In system headers, we ignore these
26876 declarations so that G++ can work with system headers that are not
26877 C++-safe. */
26878 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
26879 && !type_definition_p
26880 && (type_spec == boolean_type_node
26881 || type_spec == char16_type_node
26882 || type_spec == char32_type_node
26883 || type_spec == wchar_type_node)
26884 && (decl_specs->type
26885 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
26886 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
26887 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
26888 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
26890 decl_specs->redefined_builtin_type = type_spec;
26891 set_and_check_decl_spec_loc (decl_specs,
26892 ds_redefined_builtin_type_spec,
26893 token);
26894 if (!decl_specs->type)
26896 decl_specs->type = type_spec;
26897 decl_specs->type_definition_p = false;
26898 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
26901 else if (decl_specs->type)
26902 decl_specs->multiple_types_p = true;
26903 else
26905 decl_specs->type = type_spec;
26906 decl_specs->type_definition_p = type_definition_p;
26907 decl_specs->redefined_builtin_type = NULL_TREE;
26908 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
26912 /* True iff TOKEN is the GNU keyword __thread. */
26914 static bool
26915 token_is__thread (cp_token *token)
26917 gcc_assert (token->keyword == RID_THREAD);
26918 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
26921 /* Set the location for a declarator specifier and check if it is
26922 duplicated.
26924 DECL_SPECS is the sequence of declarator specifiers onto which to
26925 set the location.
26927 DS is the single declarator specifier to set which location is to
26928 be set onto the existing sequence of declarators.
26930 LOCATION is the location for the declarator specifier to
26931 consider. */
26933 static void
26934 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
26935 cp_decl_spec ds, cp_token *token)
26937 gcc_assert (ds < ds_last);
26939 if (decl_specs == NULL)
26940 return;
26942 source_location location = token->location;
26944 if (decl_specs->locations[ds] == 0)
26946 decl_specs->locations[ds] = location;
26947 if (ds == ds_thread)
26948 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
26950 else
26952 if (ds == ds_long)
26954 if (decl_specs->locations[ds_long_long] != 0)
26955 error_at (location,
26956 "%<long long long%> is too long for GCC");
26957 else
26959 decl_specs->locations[ds_long_long] = location;
26960 pedwarn_cxx98 (location,
26961 OPT_Wlong_long,
26962 "ISO C++ 1998 does not support %<long long%>");
26965 else if (ds == ds_thread)
26967 bool gnu = token_is__thread (token);
26968 if (gnu != decl_specs->gnu_thread_keyword_p)
26969 error_at (location,
26970 "both %<__thread%> and %<thread_local%> specified");
26971 else
26972 error_at (location, "duplicate %qD", token->u.value);
26974 else
26976 static const char *const decl_spec_names[] = {
26977 "signed",
26978 "unsigned",
26979 "short",
26980 "long",
26981 "const",
26982 "volatile",
26983 "restrict",
26984 "inline",
26985 "virtual",
26986 "explicit",
26987 "friend",
26988 "typedef",
26989 "using",
26990 "constexpr",
26991 "__complex"
26993 error_at (location,
26994 "duplicate %qs", decl_spec_names[ds]);
26999 /* Return true iff the declarator specifier DS is present in the
27000 sequence of declarator specifiers DECL_SPECS. */
27002 bool
27003 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
27004 cp_decl_spec ds)
27006 gcc_assert (ds < ds_last);
27008 if (decl_specs == NULL)
27009 return false;
27011 return decl_specs->locations[ds] != 0;
27014 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
27015 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
27017 static bool
27018 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
27020 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
27023 /* Issue an error message indicating that TOKEN_DESC was expected.
27024 If KEYWORD is true, it indicated this function is called by
27025 cp_parser_require_keword and the required token can only be
27026 a indicated keyword. */
27028 static void
27029 cp_parser_required_error (cp_parser *parser,
27030 required_token token_desc,
27031 bool keyword)
27033 switch (token_desc)
27035 case RT_NEW:
27036 cp_parser_error (parser, "expected %<new%>");
27037 return;
27038 case RT_DELETE:
27039 cp_parser_error (parser, "expected %<delete%>");
27040 return;
27041 case RT_RETURN:
27042 cp_parser_error (parser, "expected %<return%>");
27043 return;
27044 case RT_WHILE:
27045 cp_parser_error (parser, "expected %<while%>");
27046 return;
27047 case RT_EXTERN:
27048 cp_parser_error (parser, "expected %<extern%>");
27049 return;
27050 case RT_STATIC_ASSERT:
27051 cp_parser_error (parser, "expected %<static_assert%>");
27052 return;
27053 case RT_DECLTYPE:
27054 cp_parser_error (parser, "expected %<decltype%>");
27055 return;
27056 case RT_OPERATOR:
27057 cp_parser_error (parser, "expected %<operator%>");
27058 return;
27059 case RT_CLASS:
27060 cp_parser_error (parser, "expected %<class%>");
27061 return;
27062 case RT_TEMPLATE:
27063 cp_parser_error (parser, "expected %<template%>");
27064 return;
27065 case RT_NAMESPACE:
27066 cp_parser_error (parser, "expected %<namespace%>");
27067 return;
27068 case RT_USING:
27069 cp_parser_error (parser, "expected %<using%>");
27070 return;
27071 case RT_ASM:
27072 cp_parser_error (parser, "expected %<asm%>");
27073 return;
27074 case RT_TRY:
27075 cp_parser_error (parser, "expected %<try%>");
27076 return;
27077 case RT_CATCH:
27078 cp_parser_error (parser, "expected %<catch%>");
27079 return;
27080 case RT_THROW:
27081 cp_parser_error (parser, "expected %<throw%>");
27082 return;
27083 case RT_LABEL:
27084 cp_parser_error (parser, "expected %<__label__%>");
27085 return;
27086 case RT_AT_TRY:
27087 cp_parser_error (parser, "expected %<@try%>");
27088 return;
27089 case RT_AT_SYNCHRONIZED:
27090 cp_parser_error (parser, "expected %<@synchronized%>");
27091 return;
27092 case RT_AT_THROW:
27093 cp_parser_error (parser, "expected %<@throw%>");
27094 return;
27095 case RT_TRANSACTION_ATOMIC:
27096 cp_parser_error (parser, "expected %<__transaction_atomic%>");
27097 return;
27098 case RT_TRANSACTION_RELAXED:
27099 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
27100 return;
27101 default:
27102 break;
27104 if (!keyword)
27106 switch (token_desc)
27108 case RT_SEMICOLON:
27109 cp_parser_error (parser, "expected %<;%>");
27110 return;
27111 case RT_OPEN_PAREN:
27112 cp_parser_error (parser, "expected %<(%>");
27113 return;
27114 case RT_CLOSE_BRACE:
27115 cp_parser_error (parser, "expected %<}%>");
27116 return;
27117 case RT_OPEN_BRACE:
27118 cp_parser_error (parser, "expected %<{%>");
27119 return;
27120 case RT_CLOSE_SQUARE:
27121 cp_parser_error (parser, "expected %<]%>");
27122 return;
27123 case RT_OPEN_SQUARE:
27124 cp_parser_error (parser, "expected %<[%>");
27125 return;
27126 case RT_COMMA:
27127 cp_parser_error (parser, "expected %<,%>");
27128 return;
27129 case RT_SCOPE:
27130 cp_parser_error (parser, "expected %<::%>");
27131 return;
27132 case RT_LESS:
27133 cp_parser_error (parser, "expected %<<%>");
27134 return;
27135 case RT_GREATER:
27136 cp_parser_error (parser, "expected %<>%>");
27137 return;
27138 case RT_EQ:
27139 cp_parser_error (parser, "expected %<=%>");
27140 return;
27141 case RT_ELLIPSIS:
27142 cp_parser_error (parser, "expected %<...%>");
27143 return;
27144 case RT_MULT:
27145 cp_parser_error (parser, "expected %<*%>");
27146 return;
27147 case RT_COMPL:
27148 cp_parser_error (parser, "expected %<~%>");
27149 return;
27150 case RT_COLON:
27151 cp_parser_error (parser, "expected %<:%>");
27152 return;
27153 case RT_COLON_SCOPE:
27154 cp_parser_error (parser, "expected %<:%> or %<::%>");
27155 return;
27156 case RT_CLOSE_PAREN:
27157 cp_parser_error (parser, "expected %<)%>");
27158 return;
27159 case RT_COMMA_CLOSE_PAREN:
27160 cp_parser_error (parser, "expected %<,%> or %<)%>");
27161 return;
27162 case RT_PRAGMA_EOL:
27163 cp_parser_error (parser, "expected end of line");
27164 return;
27165 case RT_NAME:
27166 cp_parser_error (parser, "expected identifier");
27167 return;
27168 case RT_SELECT:
27169 cp_parser_error (parser, "expected selection-statement");
27170 return;
27171 case RT_INTERATION:
27172 cp_parser_error (parser, "expected iteration-statement");
27173 return;
27174 case RT_JUMP:
27175 cp_parser_error (parser, "expected jump-statement");
27176 return;
27177 case RT_CLASS_KEY:
27178 cp_parser_error (parser, "expected class-key");
27179 return;
27180 case RT_CLASS_TYPENAME_TEMPLATE:
27181 cp_parser_error (parser,
27182 "expected %<class%>, %<typename%>, or %<template%>");
27183 return;
27184 default:
27185 gcc_unreachable ();
27188 else
27189 gcc_unreachable ();
27194 /* If the next token is of the indicated TYPE, consume it. Otherwise,
27195 issue an error message indicating that TOKEN_DESC was expected.
27197 Returns the token consumed, if the token had the appropriate type.
27198 Otherwise, returns NULL. */
27200 static cp_token *
27201 cp_parser_require (cp_parser* parser,
27202 enum cpp_ttype type,
27203 required_token token_desc)
27205 if (cp_lexer_next_token_is (parser->lexer, type))
27206 return cp_lexer_consume_token (parser->lexer);
27207 else
27209 /* Output the MESSAGE -- unless we're parsing tentatively. */
27210 if (!cp_parser_simulate_error (parser))
27211 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
27212 return NULL;
27216 /* An error message is produced if the next token is not '>'.
27217 All further tokens are skipped until the desired token is
27218 found or '{', '}', ';' or an unbalanced ')' or ']'. */
27220 static void
27221 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
27223 /* Current level of '< ... >'. */
27224 unsigned level = 0;
27225 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
27226 unsigned nesting_depth = 0;
27228 /* Are we ready, yet? If not, issue error message. */
27229 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
27230 return;
27232 /* Skip tokens until the desired token is found. */
27233 while (true)
27235 /* Peek at the next token. */
27236 switch (cp_lexer_peek_token (parser->lexer)->type)
27238 case CPP_LESS:
27239 if (!nesting_depth)
27240 ++level;
27241 break;
27243 case CPP_RSHIFT:
27244 if (cxx_dialect == cxx98)
27245 /* C++0x views the `>>' operator as two `>' tokens, but
27246 C++98 does not. */
27247 break;
27248 else if (!nesting_depth && level-- == 0)
27250 /* We've hit a `>>' where the first `>' closes the
27251 template argument list, and the second `>' is
27252 spurious. Just consume the `>>' and stop; we've
27253 already produced at least one error. */
27254 cp_lexer_consume_token (parser->lexer);
27255 return;
27257 /* Fall through for C++0x, so we handle the second `>' in
27258 the `>>'. */
27260 case CPP_GREATER:
27261 if (!nesting_depth && level-- == 0)
27263 /* We've reached the token we want, consume it and stop. */
27264 cp_lexer_consume_token (parser->lexer);
27265 return;
27267 break;
27269 case CPP_OPEN_PAREN:
27270 case CPP_OPEN_SQUARE:
27271 ++nesting_depth;
27272 break;
27274 case CPP_CLOSE_PAREN:
27275 case CPP_CLOSE_SQUARE:
27276 if (nesting_depth-- == 0)
27277 return;
27278 break;
27280 case CPP_EOF:
27281 case CPP_PRAGMA_EOL:
27282 case CPP_SEMICOLON:
27283 case CPP_OPEN_BRACE:
27284 case CPP_CLOSE_BRACE:
27285 /* The '>' was probably forgotten, don't look further. */
27286 return;
27288 default:
27289 break;
27292 /* Consume this token. */
27293 cp_lexer_consume_token (parser->lexer);
27297 /* If the next token is the indicated keyword, consume it. Otherwise,
27298 issue an error message indicating that TOKEN_DESC was expected.
27300 Returns the token consumed, if the token had the appropriate type.
27301 Otherwise, returns NULL. */
27303 static cp_token *
27304 cp_parser_require_keyword (cp_parser* parser,
27305 enum rid keyword,
27306 required_token token_desc)
27308 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
27310 if (token && token->keyword != keyword)
27312 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
27313 return NULL;
27316 return token;
27319 /* Returns TRUE iff TOKEN is a token that can begin the body of a
27320 function-definition. */
27322 static bool
27323 cp_parser_token_starts_function_definition_p (cp_token* token)
27325 return (/* An ordinary function-body begins with an `{'. */
27326 token->type == CPP_OPEN_BRACE
27327 /* A ctor-initializer begins with a `:'. */
27328 || token->type == CPP_COLON
27329 /* A function-try-block begins with `try'. */
27330 || token->keyword == RID_TRY
27331 /* A function-transaction-block begins with `__transaction_atomic'
27332 or `__transaction_relaxed'. */
27333 || token->keyword == RID_TRANSACTION_ATOMIC
27334 || token->keyword == RID_TRANSACTION_RELAXED
27335 /* The named return value extension begins with `return'. */
27336 || token->keyword == RID_RETURN);
27339 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
27340 definition. */
27342 static bool
27343 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
27345 cp_token *token;
27347 token = cp_lexer_peek_token (parser->lexer);
27348 return (token->type == CPP_OPEN_BRACE
27349 || (token->type == CPP_COLON
27350 && !parser->colon_doesnt_start_class_def_p));
27353 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
27354 C++0x) ending a template-argument. */
27356 static bool
27357 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
27359 cp_token *token;
27361 token = cp_lexer_peek_token (parser->lexer);
27362 return (token->type == CPP_COMMA
27363 || token->type == CPP_GREATER
27364 || token->type == CPP_ELLIPSIS
27365 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
27368 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
27369 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
27371 static bool
27372 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
27373 size_t n)
27375 cp_token *token;
27377 token = cp_lexer_peek_nth_token (parser->lexer, n);
27378 if (token->type == CPP_LESS)
27379 return true;
27380 /* Check for the sequence `<::' in the original code. It would be lexed as
27381 `[:', where `[' is a digraph, and there is no whitespace before
27382 `:'. */
27383 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
27385 cp_token *token2;
27386 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
27387 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
27388 return true;
27390 return false;
27393 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
27394 or none_type otherwise. */
27396 static enum tag_types
27397 cp_parser_token_is_class_key (cp_token* token)
27399 switch (token->keyword)
27401 case RID_CLASS:
27402 return class_type;
27403 case RID_STRUCT:
27404 return record_type;
27405 case RID_UNION:
27406 return union_type;
27408 default:
27409 return none_type;
27413 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
27414 or none_type otherwise or if the token is null. */
27416 static enum tag_types
27417 cp_parser_token_is_type_parameter_key (cp_token* token)
27419 if (!token)
27420 return none_type;
27422 switch (token->keyword)
27424 case RID_CLASS:
27425 return class_type;
27426 case RID_TYPENAME:
27427 return typename_type;
27429 default:
27430 return none_type;
27434 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
27436 static void
27437 cp_parser_check_class_key (enum tag_types class_key, tree type)
27439 if (type == error_mark_node)
27440 return;
27441 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
27443 if (permerror (input_location, "%qs tag used in naming %q#T",
27444 class_key == union_type ? "union"
27445 : class_key == record_type ? "struct" : "class",
27446 type))
27447 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
27448 "%q#T was previously declared here", type);
27452 /* Issue an error message if DECL is redeclared with different
27453 access than its original declaration [class.access.spec/3].
27454 This applies to nested classes, nested class templates and
27455 enumerations [class.mem/1]. */
27457 static void
27458 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
27460 if (!decl
27461 || (!CLASS_TYPE_P (TREE_TYPE (decl))
27462 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
27463 return;
27465 if ((TREE_PRIVATE (decl)
27466 != (current_access_specifier == access_private_node))
27467 || (TREE_PROTECTED (decl)
27468 != (current_access_specifier == access_protected_node)))
27469 error_at (location, "%qD redeclared with different access", decl);
27472 /* Look for the `template' keyword, as a syntactic disambiguator.
27473 Return TRUE iff it is present, in which case it will be
27474 consumed. */
27476 static bool
27477 cp_parser_optional_template_keyword (cp_parser *parser)
27479 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27481 /* In C++98 the `template' keyword can only be used within templates;
27482 outside templates the parser can always figure out what is a
27483 template and what is not. In C++11, per the resolution of DR 468,
27484 `template' is allowed in cases where it is not strictly necessary. */
27485 if (!processing_template_decl
27486 && pedantic && cxx_dialect == cxx98)
27488 cp_token *token = cp_lexer_peek_token (parser->lexer);
27489 pedwarn (token->location, OPT_Wpedantic,
27490 "in C++98 %<template%> (as a disambiguator) is only "
27491 "allowed within templates");
27492 /* If this part of the token stream is rescanned, the same
27493 error message would be generated. So, we purge the token
27494 from the stream. */
27495 cp_lexer_purge_token (parser->lexer);
27496 return false;
27498 else
27500 /* Consume the `template' keyword. */
27501 cp_lexer_consume_token (parser->lexer);
27502 return true;
27505 return false;
27508 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
27509 set PARSER->SCOPE, and perform other related actions. */
27511 static void
27512 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
27514 struct tree_check *check_value;
27516 /* Get the stored value. */
27517 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
27518 /* Set the scope from the stored value. */
27519 parser->scope = saved_checks_value (check_value);
27520 parser->qualifying_scope = check_value->qualifying_scope;
27521 parser->object_scope = NULL_TREE;
27524 /* Consume tokens up through a non-nested END token. Returns TRUE if we
27525 encounter the end of a block before what we were looking for. */
27527 static bool
27528 cp_parser_cache_group (cp_parser *parser,
27529 enum cpp_ttype end,
27530 unsigned depth)
27532 while (true)
27534 cp_token *token = cp_lexer_peek_token (parser->lexer);
27536 /* Abort a parenthesized expression if we encounter a semicolon. */
27537 if ((end == CPP_CLOSE_PAREN || depth == 0)
27538 && token->type == CPP_SEMICOLON)
27539 return true;
27540 /* If we've reached the end of the file, stop. */
27541 if (token->type == CPP_EOF
27542 || (end != CPP_PRAGMA_EOL
27543 && token->type == CPP_PRAGMA_EOL))
27544 return true;
27545 if (token->type == CPP_CLOSE_BRACE && depth == 0)
27546 /* We've hit the end of an enclosing block, so there's been some
27547 kind of syntax error. */
27548 return true;
27550 /* Consume the token. */
27551 cp_lexer_consume_token (parser->lexer);
27552 /* See if it starts a new group. */
27553 if (token->type == CPP_OPEN_BRACE)
27555 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
27556 /* In theory this should probably check end == '}', but
27557 cp_parser_save_member_function_body needs it to exit
27558 after either '}' or ')' when called with ')'. */
27559 if (depth == 0)
27560 return false;
27562 else if (token->type == CPP_OPEN_PAREN)
27564 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
27565 if (depth == 0 && end == CPP_CLOSE_PAREN)
27566 return false;
27568 else if (token->type == CPP_PRAGMA)
27569 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
27570 else if (token->type == end)
27571 return false;
27575 /* Like above, for caching a default argument or NSDMI. Both of these are
27576 terminated by a non-nested comma, but it can be unclear whether or not a
27577 comma is nested in a template argument list unless we do more parsing.
27578 In order to handle this ambiguity, when we encounter a ',' after a '<'
27579 we try to parse what follows as a parameter-declaration-list (in the
27580 case of a default argument) or a member-declarator (in the case of an
27581 NSDMI). If that succeeds, then we stop caching. */
27583 static tree
27584 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
27586 unsigned depth = 0;
27587 int maybe_template_id = 0;
27588 cp_token *first_token;
27589 cp_token *token;
27590 tree default_argument;
27592 /* Add tokens until we have processed the entire default
27593 argument. We add the range [first_token, token). */
27594 first_token = cp_lexer_peek_token (parser->lexer);
27595 if (first_token->type == CPP_OPEN_BRACE)
27597 /* For list-initialization, this is straightforward. */
27598 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27599 token = cp_lexer_peek_token (parser->lexer);
27601 else while (true)
27603 bool done = false;
27605 /* Peek at the next token. */
27606 token = cp_lexer_peek_token (parser->lexer);
27607 /* What we do depends on what token we have. */
27608 switch (token->type)
27610 /* In valid code, a default argument must be
27611 immediately followed by a `,' `)', or `...'. */
27612 case CPP_COMMA:
27613 if (depth == 0 && maybe_template_id)
27615 /* If we've seen a '<', we might be in a
27616 template-argument-list. Until Core issue 325 is
27617 resolved, we don't know how this situation ought
27618 to be handled, so try to DTRT. We check whether
27619 what comes after the comma is a valid parameter
27620 declaration list. If it is, then the comma ends
27621 the default argument; otherwise the default
27622 argument continues. */
27623 bool error = false;
27624 cp_token *peek;
27626 /* Set ITALP so cp_parser_parameter_declaration_list
27627 doesn't decide to commit to this parse. */
27628 bool saved_italp = parser->in_template_argument_list_p;
27629 parser->in_template_argument_list_p = true;
27631 cp_parser_parse_tentatively (parser);
27633 if (nsdmi)
27635 /* Parse declarators until we reach a non-comma or
27636 somthing that cannot be an initializer.
27637 Just checking whether we're looking at a single
27638 declarator is insufficient. Consider:
27639 int var = tuple<T,U>::x;
27640 The template parameter 'U' looks exactly like a
27641 declarator. */
27644 int ctor_dtor_or_conv_p;
27645 cp_lexer_consume_token (parser->lexer);
27646 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27647 &ctor_dtor_or_conv_p,
27648 /*parenthesized_p=*/NULL,
27649 /*member_p=*/true,
27650 /*friend_p=*/false);
27651 peek = cp_lexer_peek_token (parser->lexer);
27652 if (cp_parser_error_occurred (parser))
27653 break;
27655 while (peek->type == CPP_COMMA);
27656 /* If we met an '=' or ';' then the original comma
27657 was the end of the NSDMI. Otherwise assume
27658 we're still in the NSDMI. */
27659 error = (peek->type != CPP_EQ
27660 && peek->type != CPP_SEMICOLON);
27662 else
27664 cp_lexer_consume_token (parser->lexer);
27665 begin_scope (sk_function_parms, NULL_TREE);
27666 cp_parser_parameter_declaration_list (parser, &error);
27667 pop_bindings_and_leave_scope ();
27669 if (!cp_parser_error_occurred (parser) && !error)
27670 done = true;
27671 cp_parser_abort_tentative_parse (parser);
27673 parser->in_template_argument_list_p = saved_italp;
27674 break;
27676 /* XXX Really fallthru? */
27677 /* FALLTHRU */
27678 case CPP_CLOSE_PAREN:
27679 case CPP_ELLIPSIS:
27680 /* If we run into a non-nested `;', `}', or `]',
27681 then the code is invalid -- but the default
27682 argument is certainly over. */
27683 case CPP_SEMICOLON:
27684 case CPP_CLOSE_BRACE:
27685 case CPP_CLOSE_SQUARE:
27686 if (depth == 0
27687 /* Handle correctly int n = sizeof ... ( p ); */
27688 && token->type != CPP_ELLIPSIS)
27689 done = true;
27690 /* Update DEPTH, if necessary. */
27691 else if (token->type == CPP_CLOSE_PAREN
27692 || token->type == CPP_CLOSE_BRACE
27693 || token->type == CPP_CLOSE_SQUARE)
27694 --depth;
27695 break;
27697 case CPP_OPEN_PAREN:
27698 case CPP_OPEN_SQUARE:
27699 case CPP_OPEN_BRACE:
27700 ++depth;
27701 break;
27703 case CPP_LESS:
27704 if (depth == 0)
27705 /* This might be the comparison operator, or it might
27706 start a template argument list. */
27707 ++maybe_template_id;
27708 break;
27710 case CPP_RSHIFT:
27711 if (cxx_dialect == cxx98)
27712 break;
27713 /* Fall through for C++0x, which treats the `>>'
27714 operator like two `>' tokens in certain
27715 cases. */
27717 case CPP_GREATER:
27718 if (depth == 0)
27720 /* This might be an operator, or it might close a
27721 template argument list. But if a previous '<'
27722 started a template argument list, this will have
27723 closed it, so we can't be in one anymore. */
27724 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
27725 if (maybe_template_id < 0)
27726 maybe_template_id = 0;
27728 break;
27730 /* If we run out of tokens, issue an error message. */
27731 case CPP_EOF:
27732 case CPP_PRAGMA_EOL:
27733 error_at (token->location, "file ends in default argument");
27734 return error_mark_node;
27736 case CPP_NAME:
27737 case CPP_SCOPE:
27738 /* In these cases, we should look for template-ids.
27739 For example, if the default argument is
27740 `X<int, double>()', we need to do name lookup to
27741 figure out whether or not `X' is a template; if
27742 so, the `,' does not end the default argument.
27744 That is not yet done. */
27745 break;
27747 default:
27748 break;
27751 /* If we've reached the end, stop. */
27752 if (done)
27753 break;
27755 /* Add the token to the token block. */
27756 token = cp_lexer_consume_token (parser->lexer);
27759 /* Create a DEFAULT_ARG to represent the unparsed default
27760 argument. */
27761 default_argument = make_node (DEFAULT_ARG);
27762 DEFARG_TOKENS (default_argument)
27763 = cp_token_cache_new (first_token, token);
27764 DEFARG_INSTANTIATIONS (default_argument) = NULL;
27766 return default_argument;
27769 /* Begin parsing tentatively. We always save tokens while parsing
27770 tentatively so that if the tentative parsing fails we can restore the
27771 tokens. */
27773 static void
27774 cp_parser_parse_tentatively (cp_parser* parser)
27776 /* Enter a new parsing context. */
27777 parser->context = cp_parser_context_new (parser->context);
27778 /* Begin saving tokens. */
27779 cp_lexer_save_tokens (parser->lexer);
27780 /* In order to avoid repetitive access control error messages,
27781 access checks are queued up until we are no longer parsing
27782 tentatively. */
27783 push_deferring_access_checks (dk_deferred);
27786 /* Commit to the currently active tentative parse. */
27788 static void
27789 cp_parser_commit_to_tentative_parse (cp_parser* parser)
27791 cp_parser_context *context;
27792 cp_lexer *lexer;
27794 /* Mark all of the levels as committed. */
27795 lexer = parser->lexer;
27796 for (context = parser->context; context->next; context = context->next)
27798 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27799 break;
27800 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27801 while (!cp_lexer_saving_tokens (lexer))
27802 lexer = lexer->next;
27803 cp_lexer_commit_tokens (lexer);
27807 /* Commit to the topmost currently active tentative parse.
27809 Note that this function shouldn't be called when there are
27810 irreversible side-effects while in a tentative state. For
27811 example, we shouldn't create a permanent entry in the symbol
27812 table, or issue an error message that might not apply if the
27813 tentative parse is aborted. */
27815 static void
27816 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
27818 cp_parser_context *context = parser->context;
27819 cp_lexer *lexer = parser->lexer;
27821 if (context)
27823 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27824 return;
27825 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27827 while (!cp_lexer_saving_tokens (lexer))
27828 lexer = lexer->next;
27829 cp_lexer_commit_tokens (lexer);
27833 /* Abort the currently active tentative parse. All consumed tokens
27834 will be rolled back, and no diagnostics will be issued. */
27836 static void
27837 cp_parser_abort_tentative_parse (cp_parser* parser)
27839 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
27840 || errorcount > 0);
27841 cp_parser_simulate_error (parser);
27842 /* Now, pretend that we want to see if the construct was
27843 successfully parsed. */
27844 cp_parser_parse_definitely (parser);
27847 /* Stop parsing tentatively. If a parse error has occurred, restore the
27848 token stream. Otherwise, commit to the tokens we have consumed.
27849 Returns true if no error occurred; false otherwise. */
27851 static bool
27852 cp_parser_parse_definitely (cp_parser* parser)
27854 bool error_occurred;
27855 cp_parser_context *context;
27857 /* Remember whether or not an error occurred, since we are about to
27858 destroy that information. */
27859 error_occurred = cp_parser_error_occurred (parser);
27860 /* Remove the topmost context from the stack. */
27861 context = parser->context;
27862 parser->context = context->next;
27863 /* If no parse errors occurred, commit to the tentative parse. */
27864 if (!error_occurred)
27866 /* Commit to the tokens read tentatively, unless that was
27867 already done. */
27868 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
27869 cp_lexer_commit_tokens (parser->lexer);
27871 pop_to_parent_deferring_access_checks ();
27873 /* Otherwise, if errors occurred, roll back our state so that things
27874 are just as they were before we began the tentative parse. */
27875 else
27877 cp_lexer_rollback_tokens (parser->lexer);
27878 pop_deferring_access_checks ();
27880 /* Add the context to the front of the free list. */
27881 context->next = cp_parser_context_free_list;
27882 cp_parser_context_free_list = context;
27884 return !error_occurred;
27887 /* Returns true if we are parsing tentatively and are not committed to
27888 this tentative parse. */
27890 static bool
27891 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
27893 return (cp_parser_parsing_tentatively (parser)
27894 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
27897 /* Returns nonzero iff an error has occurred during the most recent
27898 tentative parse. */
27900 static bool
27901 cp_parser_error_occurred (cp_parser* parser)
27903 return (cp_parser_parsing_tentatively (parser)
27904 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
27907 /* Returns nonzero if GNU extensions are allowed. */
27909 static bool
27910 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
27912 return parser->allow_gnu_extensions_p;
27915 /* Objective-C++ Productions */
27918 /* Parse an Objective-C expression, which feeds into a primary-expression
27919 above.
27921 objc-expression:
27922 objc-message-expression
27923 objc-string-literal
27924 objc-encode-expression
27925 objc-protocol-expression
27926 objc-selector-expression
27928 Returns a tree representation of the expression. */
27930 static cp_expr
27931 cp_parser_objc_expression (cp_parser* parser)
27933 /* Try to figure out what kind of declaration is present. */
27934 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27936 switch (kwd->type)
27938 case CPP_OPEN_SQUARE:
27939 return cp_parser_objc_message_expression (parser);
27941 case CPP_OBJC_STRING:
27942 kwd = cp_lexer_consume_token (parser->lexer);
27943 return objc_build_string_object (kwd->u.value);
27945 case CPP_KEYWORD:
27946 switch (kwd->keyword)
27948 case RID_AT_ENCODE:
27949 return cp_parser_objc_encode_expression (parser);
27951 case RID_AT_PROTOCOL:
27952 return cp_parser_objc_protocol_expression (parser);
27954 case RID_AT_SELECTOR:
27955 return cp_parser_objc_selector_expression (parser);
27957 default:
27958 break;
27960 default:
27961 error_at (kwd->location,
27962 "misplaced %<@%D%> Objective-C++ construct",
27963 kwd->u.value);
27964 cp_parser_skip_to_end_of_block_or_statement (parser);
27967 return error_mark_node;
27970 /* Parse an Objective-C message expression.
27972 objc-message-expression:
27973 [ objc-message-receiver objc-message-args ]
27975 Returns a representation of an Objective-C message. */
27977 static tree
27978 cp_parser_objc_message_expression (cp_parser* parser)
27980 tree receiver, messageargs;
27982 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27983 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
27984 receiver = cp_parser_objc_message_receiver (parser);
27985 messageargs = cp_parser_objc_message_args (parser);
27986 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
27987 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27989 tree result = objc_build_message_expr (receiver, messageargs);
27991 /* Construct a location e.g.
27992 [self func1:5]
27993 ^~~~~~~~~~~~~~
27994 ranging from the '[' to the ']', with the caret at the start. */
27995 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
27996 protected_set_expr_location (result, combined_loc);
27998 return result;
28001 /* Parse an objc-message-receiver.
28003 objc-message-receiver:
28004 expression
28005 simple-type-specifier
28007 Returns a representation of the type or expression. */
28009 static tree
28010 cp_parser_objc_message_receiver (cp_parser* parser)
28012 tree rcv;
28014 /* An Objective-C message receiver may be either (1) a type
28015 or (2) an expression. */
28016 cp_parser_parse_tentatively (parser);
28017 rcv = cp_parser_expression (parser);
28019 /* If that worked out, fine. */
28020 if (cp_parser_parse_definitely (parser))
28021 return rcv;
28023 cp_parser_parse_tentatively (parser);
28024 rcv = cp_parser_simple_type_specifier (parser,
28025 /*decl_specs=*/NULL,
28026 CP_PARSER_FLAGS_NONE);
28028 if (cp_parser_parse_definitely (parser))
28029 return objc_get_class_reference (rcv);
28031 cp_parser_error (parser, "objective-c++ message receiver expected");
28032 return error_mark_node;
28035 /* Parse the arguments and selectors comprising an Objective-C message.
28037 objc-message-args:
28038 objc-selector
28039 objc-selector-args
28040 objc-selector-args , objc-comma-args
28042 objc-selector-args:
28043 objc-selector [opt] : assignment-expression
28044 objc-selector-args objc-selector [opt] : assignment-expression
28046 objc-comma-args:
28047 assignment-expression
28048 objc-comma-args , assignment-expression
28050 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
28051 selector arguments and TREE_VALUE containing a list of comma
28052 arguments. */
28054 static tree
28055 cp_parser_objc_message_args (cp_parser* parser)
28057 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
28058 bool maybe_unary_selector_p = true;
28059 cp_token *token = cp_lexer_peek_token (parser->lexer);
28061 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28063 tree selector = NULL_TREE, arg;
28065 if (token->type != CPP_COLON)
28066 selector = cp_parser_objc_selector (parser);
28068 /* Detect if we have a unary selector. */
28069 if (maybe_unary_selector_p
28070 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28071 return build_tree_list (selector, NULL_TREE);
28073 maybe_unary_selector_p = false;
28074 cp_parser_require (parser, CPP_COLON, RT_COLON);
28075 arg = cp_parser_assignment_expression (parser);
28077 sel_args
28078 = chainon (sel_args,
28079 build_tree_list (selector, arg));
28081 token = cp_lexer_peek_token (parser->lexer);
28084 /* Handle non-selector arguments, if any. */
28085 while (token->type == CPP_COMMA)
28087 tree arg;
28089 cp_lexer_consume_token (parser->lexer);
28090 arg = cp_parser_assignment_expression (parser);
28092 addl_args
28093 = chainon (addl_args,
28094 build_tree_list (NULL_TREE, arg));
28096 token = cp_lexer_peek_token (parser->lexer);
28099 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
28101 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
28102 return build_tree_list (error_mark_node, error_mark_node);
28105 return build_tree_list (sel_args, addl_args);
28108 /* Parse an Objective-C encode expression.
28110 objc-encode-expression:
28111 @encode objc-typename
28113 Returns an encoded representation of the type argument. */
28115 static cp_expr
28116 cp_parser_objc_encode_expression (cp_parser* parser)
28118 tree type;
28119 cp_token *token;
28120 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28122 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
28123 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28124 token = cp_lexer_peek_token (parser->lexer);
28125 type = complete_type (cp_parser_type_id (parser));
28126 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28128 if (!type)
28130 error_at (token->location,
28131 "%<@encode%> must specify a type as an argument");
28132 return error_mark_node;
28135 /* This happens if we find @encode(T) (where T is a template
28136 typename or something dependent on a template typename) when
28137 parsing a template. In that case, we can't compile it
28138 immediately, but we rather create an AT_ENCODE_EXPR which will
28139 need to be instantiated when the template is used.
28141 if (dependent_type_p (type))
28143 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
28144 TREE_READONLY (value) = 1;
28145 return value;
28149 /* Build a location of the form:
28150 @encode(int)
28151 ^~~~~~~~~~~~
28152 with caret==start at the @ token, finishing at the close paren. */
28153 location_t combined_loc
28154 = make_location (start_loc, start_loc,
28155 cp_lexer_previous_token (parser->lexer)->location);
28157 return cp_expr (objc_build_encode_expr (type), combined_loc);
28160 /* Parse an Objective-C @defs expression. */
28162 static tree
28163 cp_parser_objc_defs_expression (cp_parser *parser)
28165 tree name;
28167 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
28168 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28169 name = cp_parser_identifier (parser);
28170 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28172 return objc_get_class_ivars (name);
28175 /* Parse an Objective-C protocol expression.
28177 objc-protocol-expression:
28178 @protocol ( identifier )
28180 Returns a representation of the protocol expression. */
28182 static tree
28183 cp_parser_objc_protocol_expression (cp_parser* parser)
28185 tree proto;
28186 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28188 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28189 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28190 proto = cp_parser_identifier (parser);
28191 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28193 /* Build a location of the form:
28194 @protocol(prot)
28195 ^~~~~~~~~~~~~~~
28196 with caret==start at the @ token, finishing at the close paren. */
28197 location_t combined_loc
28198 = make_location (start_loc, start_loc,
28199 cp_lexer_previous_token (parser->lexer)->location);
28200 tree result = objc_build_protocol_expr (proto);
28201 protected_set_expr_location (result, combined_loc);
28202 return result;
28205 /* Parse an Objective-C selector expression.
28207 objc-selector-expression:
28208 @selector ( objc-method-signature )
28210 objc-method-signature:
28211 objc-selector
28212 objc-selector-seq
28214 objc-selector-seq:
28215 objc-selector :
28216 objc-selector-seq objc-selector :
28218 Returns a representation of the method selector. */
28220 static tree
28221 cp_parser_objc_selector_expression (cp_parser* parser)
28223 tree sel_seq = NULL_TREE;
28224 bool maybe_unary_selector_p = true;
28225 cp_token *token;
28226 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28228 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
28229 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28230 token = cp_lexer_peek_token (parser->lexer);
28232 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
28233 || token->type == CPP_SCOPE)
28235 tree selector = NULL_TREE;
28237 if (token->type != CPP_COLON
28238 || token->type == CPP_SCOPE)
28239 selector = cp_parser_objc_selector (parser);
28241 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
28242 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
28244 /* Detect if we have a unary selector. */
28245 if (maybe_unary_selector_p)
28247 sel_seq = selector;
28248 goto finish_selector;
28250 else
28252 cp_parser_error (parser, "expected %<:%>");
28255 maybe_unary_selector_p = false;
28256 token = cp_lexer_consume_token (parser->lexer);
28258 if (token->type == CPP_SCOPE)
28260 sel_seq
28261 = chainon (sel_seq,
28262 build_tree_list (selector, NULL_TREE));
28263 sel_seq
28264 = chainon (sel_seq,
28265 build_tree_list (NULL_TREE, NULL_TREE));
28267 else
28268 sel_seq
28269 = chainon (sel_seq,
28270 build_tree_list (selector, NULL_TREE));
28272 token = cp_lexer_peek_token (parser->lexer);
28275 finish_selector:
28276 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28279 /* Build a location of the form:
28280 @selector(func)
28281 ^~~~~~~~~~~~~~~
28282 with caret==start at the @ token, finishing at the close paren. */
28283 location_t combined_loc
28284 = make_location (loc, loc,
28285 cp_lexer_previous_token (parser->lexer)->location);
28286 tree result = objc_build_selector_expr (combined_loc, sel_seq);
28287 /* TODO: objc_build_selector_expr doesn't always honor the location. */
28288 protected_set_expr_location (result, combined_loc);
28289 return result;
28292 /* Parse a list of identifiers.
28294 objc-identifier-list:
28295 identifier
28296 objc-identifier-list , identifier
28298 Returns a TREE_LIST of identifier nodes. */
28300 static tree
28301 cp_parser_objc_identifier_list (cp_parser* parser)
28303 tree identifier;
28304 tree list;
28305 cp_token *sep;
28307 identifier = cp_parser_identifier (parser);
28308 if (identifier == error_mark_node)
28309 return error_mark_node;
28311 list = build_tree_list (NULL_TREE, identifier);
28312 sep = cp_lexer_peek_token (parser->lexer);
28314 while (sep->type == CPP_COMMA)
28316 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28317 identifier = cp_parser_identifier (parser);
28318 if (identifier == error_mark_node)
28319 return list;
28321 list = chainon (list, build_tree_list (NULL_TREE,
28322 identifier));
28323 sep = cp_lexer_peek_token (parser->lexer);
28326 return list;
28329 /* Parse an Objective-C alias declaration.
28331 objc-alias-declaration:
28332 @compatibility_alias identifier identifier ;
28334 This function registers the alias mapping with the Objective-C front end.
28335 It returns nothing. */
28337 static void
28338 cp_parser_objc_alias_declaration (cp_parser* parser)
28340 tree alias, orig;
28342 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
28343 alias = cp_parser_identifier (parser);
28344 orig = cp_parser_identifier (parser);
28345 objc_declare_alias (alias, orig);
28346 cp_parser_consume_semicolon_at_end_of_statement (parser);
28349 /* Parse an Objective-C class forward-declaration.
28351 objc-class-declaration:
28352 @class objc-identifier-list ;
28354 The function registers the forward declarations with the Objective-C
28355 front end. It returns nothing. */
28357 static void
28358 cp_parser_objc_class_declaration (cp_parser* parser)
28360 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
28361 while (true)
28363 tree id;
28365 id = cp_parser_identifier (parser);
28366 if (id == error_mark_node)
28367 break;
28369 objc_declare_class (id);
28371 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28372 cp_lexer_consume_token (parser->lexer);
28373 else
28374 break;
28376 cp_parser_consume_semicolon_at_end_of_statement (parser);
28379 /* Parse a list of Objective-C protocol references.
28381 objc-protocol-refs-opt:
28382 objc-protocol-refs [opt]
28384 objc-protocol-refs:
28385 < objc-identifier-list >
28387 Returns a TREE_LIST of identifiers, if any. */
28389 static tree
28390 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
28392 tree protorefs = NULL_TREE;
28394 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
28396 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
28397 protorefs = cp_parser_objc_identifier_list (parser);
28398 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
28401 return protorefs;
28404 /* Parse a Objective-C visibility specification. */
28406 static void
28407 cp_parser_objc_visibility_spec (cp_parser* parser)
28409 cp_token *vis = cp_lexer_peek_token (parser->lexer);
28411 switch (vis->keyword)
28413 case RID_AT_PRIVATE:
28414 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
28415 break;
28416 case RID_AT_PROTECTED:
28417 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
28418 break;
28419 case RID_AT_PUBLIC:
28420 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
28421 break;
28422 case RID_AT_PACKAGE:
28423 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
28424 break;
28425 default:
28426 return;
28429 /* Eat '@private'/'@protected'/'@public'. */
28430 cp_lexer_consume_token (parser->lexer);
28433 /* Parse an Objective-C method type. Return 'true' if it is a class
28434 (+) method, and 'false' if it is an instance (-) method. */
28436 static inline bool
28437 cp_parser_objc_method_type (cp_parser* parser)
28439 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
28440 return true;
28441 else
28442 return false;
28445 /* Parse an Objective-C protocol qualifier. */
28447 static tree
28448 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
28450 tree quals = NULL_TREE, node;
28451 cp_token *token = cp_lexer_peek_token (parser->lexer);
28453 node = token->u.value;
28455 while (node && identifier_p (node)
28456 && (node == ridpointers [(int) RID_IN]
28457 || node == ridpointers [(int) RID_OUT]
28458 || node == ridpointers [(int) RID_INOUT]
28459 || node == ridpointers [(int) RID_BYCOPY]
28460 || node == ridpointers [(int) RID_BYREF]
28461 || node == ridpointers [(int) RID_ONEWAY]))
28463 quals = tree_cons (NULL_TREE, node, quals);
28464 cp_lexer_consume_token (parser->lexer);
28465 token = cp_lexer_peek_token (parser->lexer);
28466 node = token->u.value;
28469 return quals;
28472 /* Parse an Objective-C typename. */
28474 static tree
28475 cp_parser_objc_typename (cp_parser* parser)
28477 tree type_name = NULL_TREE;
28479 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28481 tree proto_quals, cp_type = NULL_TREE;
28483 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
28484 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
28486 /* An ObjC type name may consist of just protocol qualifiers, in which
28487 case the type shall default to 'id'. */
28488 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
28490 cp_type = cp_parser_type_id (parser);
28492 /* If the type could not be parsed, an error has already
28493 been produced. For error recovery, behave as if it had
28494 not been specified, which will use the default type
28495 'id'. */
28496 if (cp_type == error_mark_node)
28498 cp_type = NULL_TREE;
28499 /* We need to skip to the closing parenthesis as
28500 cp_parser_type_id() does not seem to do it for
28501 us. */
28502 cp_parser_skip_to_closing_parenthesis (parser,
28503 /*recovering=*/true,
28504 /*or_comma=*/false,
28505 /*consume_paren=*/false);
28509 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28510 type_name = build_tree_list (proto_quals, cp_type);
28513 return type_name;
28516 /* Check to see if TYPE refers to an Objective-C selector name. */
28518 static bool
28519 cp_parser_objc_selector_p (enum cpp_ttype type)
28521 return (type == CPP_NAME || type == CPP_KEYWORD
28522 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
28523 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
28524 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
28525 || type == CPP_XOR || type == CPP_XOR_EQ);
28528 /* Parse an Objective-C selector. */
28530 static tree
28531 cp_parser_objc_selector (cp_parser* parser)
28533 cp_token *token = cp_lexer_consume_token (parser->lexer);
28535 if (!cp_parser_objc_selector_p (token->type))
28537 error_at (token->location, "invalid Objective-C++ selector name");
28538 return error_mark_node;
28541 /* C++ operator names are allowed to appear in ObjC selectors. */
28542 switch (token->type)
28544 case CPP_AND_AND: return get_identifier ("and");
28545 case CPP_AND_EQ: return get_identifier ("and_eq");
28546 case CPP_AND: return get_identifier ("bitand");
28547 case CPP_OR: return get_identifier ("bitor");
28548 case CPP_COMPL: return get_identifier ("compl");
28549 case CPP_NOT: return get_identifier ("not");
28550 case CPP_NOT_EQ: return get_identifier ("not_eq");
28551 case CPP_OR_OR: return get_identifier ("or");
28552 case CPP_OR_EQ: return get_identifier ("or_eq");
28553 case CPP_XOR: return get_identifier ("xor");
28554 case CPP_XOR_EQ: return get_identifier ("xor_eq");
28555 default: return token->u.value;
28559 /* Parse an Objective-C params list. */
28561 static tree
28562 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
28564 tree params = NULL_TREE;
28565 bool maybe_unary_selector_p = true;
28566 cp_token *token = cp_lexer_peek_token (parser->lexer);
28568 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28570 tree selector = NULL_TREE, type_name, identifier;
28571 tree parm_attr = NULL_TREE;
28573 if (token->keyword == RID_ATTRIBUTE)
28574 break;
28576 if (token->type != CPP_COLON)
28577 selector = cp_parser_objc_selector (parser);
28579 /* Detect if we have a unary selector. */
28580 if (maybe_unary_selector_p
28581 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28583 params = selector; /* Might be followed by attributes. */
28584 break;
28587 maybe_unary_selector_p = false;
28588 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28590 /* Something went quite wrong. There should be a colon
28591 here, but there is not. Stop parsing parameters. */
28592 break;
28594 type_name = cp_parser_objc_typename (parser);
28595 /* New ObjC allows attributes on parameters too. */
28596 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
28597 parm_attr = cp_parser_attributes_opt (parser);
28598 identifier = cp_parser_identifier (parser);
28600 params
28601 = chainon (params,
28602 objc_build_keyword_decl (selector,
28603 type_name,
28604 identifier,
28605 parm_attr));
28607 token = cp_lexer_peek_token (parser->lexer);
28610 if (params == NULL_TREE)
28612 cp_parser_error (parser, "objective-c++ method declaration is expected");
28613 return error_mark_node;
28616 /* We allow tail attributes for the method. */
28617 if (token->keyword == RID_ATTRIBUTE)
28619 *attributes = cp_parser_attributes_opt (parser);
28620 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28621 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28622 return params;
28623 cp_parser_error (parser,
28624 "method attributes must be specified at the end");
28625 return error_mark_node;
28628 if (params == NULL_TREE)
28630 cp_parser_error (parser, "objective-c++ method declaration is expected");
28631 return error_mark_node;
28633 return params;
28636 /* Parse the non-keyword Objective-C params. */
28638 static tree
28639 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
28640 tree* attributes)
28642 tree params = make_node (TREE_LIST);
28643 cp_token *token = cp_lexer_peek_token (parser->lexer);
28644 *ellipsisp = false; /* Initially, assume no ellipsis. */
28646 while (token->type == CPP_COMMA)
28648 cp_parameter_declarator *parmdecl;
28649 tree parm;
28651 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28652 token = cp_lexer_peek_token (parser->lexer);
28654 if (token->type == CPP_ELLIPSIS)
28656 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
28657 *ellipsisp = true;
28658 token = cp_lexer_peek_token (parser->lexer);
28659 break;
28662 /* TODO: parse attributes for tail parameters. */
28663 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
28664 parm = grokdeclarator (parmdecl->declarator,
28665 &parmdecl->decl_specifiers,
28666 PARM, /*initialized=*/0,
28667 /*attrlist=*/NULL);
28669 chainon (params, build_tree_list (NULL_TREE, parm));
28670 token = cp_lexer_peek_token (parser->lexer);
28673 /* We allow tail attributes for the method. */
28674 if (token->keyword == RID_ATTRIBUTE)
28676 if (*attributes == NULL_TREE)
28678 *attributes = cp_parser_attributes_opt (parser);
28679 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28680 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28681 return params;
28683 else
28684 /* We have an error, but parse the attributes, so that we can
28685 carry on. */
28686 *attributes = cp_parser_attributes_opt (parser);
28688 cp_parser_error (parser,
28689 "method attributes must be specified at the end");
28690 return error_mark_node;
28693 return params;
28696 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
28698 static void
28699 cp_parser_objc_interstitial_code (cp_parser* parser)
28701 cp_token *token = cp_lexer_peek_token (parser->lexer);
28703 /* If the next token is `extern' and the following token is a string
28704 literal, then we have a linkage specification. */
28705 if (token->keyword == RID_EXTERN
28706 && cp_parser_is_pure_string_literal
28707 (cp_lexer_peek_nth_token (parser->lexer, 2)))
28708 cp_parser_linkage_specification (parser);
28709 /* Handle #pragma, if any. */
28710 else if (token->type == CPP_PRAGMA)
28711 cp_parser_pragma (parser, pragma_objc_icode, NULL);
28712 /* Allow stray semicolons. */
28713 else if (token->type == CPP_SEMICOLON)
28714 cp_lexer_consume_token (parser->lexer);
28715 /* Mark methods as optional or required, when building protocols. */
28716 else if (token->keyword == RID_AT_OPTIONAL)
28718 cp_lexer_consume_token (parser->lexer);
28719 objc_set_method_opt (true);
28721 else if (token->keyword == RID_AT_REQUIRED)
28723 cp_lexer_consume_token (parser->lexer);
28724 objc_set_method_opt (false);
28726 else if (token->keyword == RID_NAMESPACE)
28727 cp_parser_namespace_definition (parser);
28728 /* Other stray characters must generate errors. */
28729 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
28731 cp_lexer_consume_token (parser->lexer);
28732 error ("stray %qs between Objective-C++ methods",
28733 token->type == CPP_OPEN_BRACE ? "{" : "}");
28735 /* Finally, try to parse a block-declaration, or a function-definition. */
28736 else
28737 cp_parser_block_declaration (parser, /*statement_p=*/false);
28740 /* Parse a method signature. */
28742 static tree
28743 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
28745 tree rettype, kwdparms, optparms;
28746 bool ellipsis = false;
28747 bool is_class_method;
28749 is_class_method = cp_parser_objc_method_type (parser);
28750 rettype = cp_parser_objc_typename (parser);
28751 *attributes = NULL_TREE;
28752 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
28753 if (kwdparms == error_mark_node)
28754 return error_mark_node;
28755 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
28756 if (optparms == error_mark_node)
28757 return error_mark_node;
28759 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
28762 static bool
28763 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
28765 tree tattr;
28766 cp_lexer_save_tokens (parser->lexer);
28767 tattr = cp_parser_attributes_opt (parser);
28768 gcc_assert (tattr) ;
28770 /* If the attributes are followed by a method introducer, this is not allowed.
28771 Dump the attributes and flag the situation. */
28772 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
28773 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
28774 return true;
28776 /* Otherwise, the attributes introduce some interstitial code, possibly so
28777 rewind to allow that check. */
28778 cp_lexer_rollback_tokens (parser->lexer);
28779 return false;
28782 /* Parse an Objective-C method prototype list. */
28784 static void
28785 cp_parser_objc_method_prototype_list (cp_parser* parser)
28787 cp_token *token = cp_lexer_peek_token (parser->lexer);
28789 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28791 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28793 tree attributes, sig;
28794 bool is_class_method;
28795 if (token->type == CPP_PLUS)
28796 is_class_method = true;
28797 else
28798 is_class_method = false;
28799 sig = cp_parser_objc_method_signature (parser, &attributes);
28800 if (sig == error_mark_node)
28802 cp_parser_skip_to_end_of_block_or_statement (parser);
28803 token = cp_lexer_peek_token (parser->lexer);
28804 continue;
28806 objc_add_method_declaration (is_class_method, sig, attributes);
28807 cp_parser_consume_semicolon_at_end_of_statement (parser);
28809 else if (token->keyword == RID_AT_PROPERTY)
28810 cp_parser_objc_at_property_declaration (parser);
28811 else if (token->keyword == RID_ATTRIBUTE
28812 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28813 warning_at (cp_lexer_peek_token (parser->lexer)->location,
28814 OPT_Wattributes,
28815 "prefix attributes are ignored for methods");
28816 else
28817 /* Allow for interspersed non-ObjC++ code. */
28818 cp_parser_objc_interstitial_code (parser);
28820 token = cp_lexer_peek_token (parser->lexer);
28823 if (token->type != CPP_EOF)
28824 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28825 else
28826 cp_parser_error (parser, "expected %<@end%>");
28828 objc_finish_interface ();
28831 /* Parse an Objective-C method definition list. */
28833 static void
28834 cp_parser_objc_method_definition_list (cp_parser* parser)
28836 cp_token *token = cp_lexer_peek_token (parser->lexer);
28838 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28840 tree meth;
28842 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28844 cp_token *ptk;
28845 tree sig, attribute;
28846 bool is_class_method;
28847 if (token->type == CPP_PLUS)
28848 is_class_method = true;
28849 else
28850 is_class_method = false;
28851 push_deferring_access_checks (dk_deferred);
28852 sig = cp_parser_objc_method_signature (parser, &attribute);
28853 if (sig == error_mark_node)
28855 cp_parser_skip_to_end_of_block_or_statement (parser);
28856 token = cp_lexer_peek_token (parser->lexer);
28857 continue;
28859 objc_start_method_definition (is_class_method, sig, attribute,
28860 NULL_TREE);
28862 /* For historical reasons, we accept an optional semicolon. */
28863 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28864 cp_lexer_consume_token (parser->lexer);
28866 ptk = cp_lexer_peek_token (parser->lexer);
28867 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
28868 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
28870 perform_deferred_access_checks (tf_warning_or_error);
28871 stop_deferring_access_checks ();
28872 meth = cp_parser_function_definition_after_declarator (parser,
28873 false);
28874 pop_deferring_access_checks ();
28875 objc_finish_method_definition (meth);
28878 /* The following case will be removed once @synthesize is
28879 completely implemented. */
28880 else if (token->keyword == RID_AT_PROPERTY)
28881 cp_parser_objc_at_property_declaration (parser);
28882 else if (token->keyword == RID_AT_SYNTHESIZE)
28883 cp_parser_objc_at_synthesize_declaration (parser);
28884 else if (token->keyword == RID_AT_DYNAMIC)
28885 cp_parser_objc_at_dynamic_declaration (parser);
28886 else if (token->keyword == RID_ATTRIBUTE
28887 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28888 warning_at (token->location, OPT_Wattributes,
28889 "prefix attributes are ignored for methods");
28890 else
28891 /* Allow for interspersed non-ObjC++ code. */
28892 cp_parser_objc_interstitial_code (parser);
28894 token = cp_lexer_peek_token (parser->lexer);
28897 if (token->type != CPP_EOF)
28898 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28899 else
28900 cp_parser_error (parser, "expected %<@end%>");
28902 objc_finish_implementation ();
28905 /* Parse Objective-C ivars. */
28907 static void
28908 cp_parser_objc_class_ivars (cp_parser* parser)
28910 cp_token *token = cp_lexer_peek_token (parser->lexer);
28912 if (token->type != CPP_OPEN_BRACE)
28913 return; /* No ivars specified. */
28915 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
28916 token = cp_lexer_peek_token (parser->lexer);
28918 while (token->type != CPP_CLOSE_BRACE
28919 && token->keyword != RID_AT_END && token->type != CPP_EOF)
28921 cp_decl_specifier_seq declspecs;
28922 int decl_class_or_enum_p;
28923 tree prefix_attributes;
28925 cp_parser_objc_visibility_spec (parser);
28927 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28928 break;
28930 cp_parser_decl_specifier_seq (parser,
28931 CP_PARSER_FLAGS_OPTIONAL,
28932 &declspecs,
28933 &decl_class_or_enum_p);
28935 /* auto, register, static, extern, mutable. */
28936 if (declspecs.storage_class != sc_none)
28938 cp_parser_error (parser, "invalid type for instance variable");
28939 declspecs.storage_class = sc_none;
28942 /* thread_local. */
28943 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
28945 cp_parser_error (parser, "invalid type for instance variable");
28946 declspecs.locations[ds_thread] = 0;
28949 /* typedef. */
28950 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
28952 cp_parser_error (parser, "invalid type for instance variable");
28953 declspecs.locations[ds_typedef] = 0;
28956 prefix_attributes = declspecs.attributes;
28957 declspecs.attributes = NULL_TREE;
28959 /* Keep going until we hit the `;' at the end of the
28960 declaration. */
28961 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28963 tree width = NULL_TREE, attributes, first_attribute, decl;
28964 cp_declarator *declarator = NULL;
28965 int ctor_dtor_or_conv_p;
28967 /* Check for a (possibly unnamed) bitfield declaration. */
28968 token = cp_lexer_peek_token (parser->lexer);
28969 if (token->type == CPP_COLON)
28970 goto eat_colon;
28972 if (token->type == CPP_NAME
28973 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
28974 == CPP_COLON))
28976 /* Get the name of the bitfield. */
28977 declarator = make_id_declarator (NULL_TREE,
28978 cp_parser_identifier (parser),
28979 sfk_none);
28981 eat_colon:
28982 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
28983 /* Get the width of the bitfield. */
28984 width
28985 = cp_parser_constant_expression (parser);
28987 else
28989 /* Parse the declarator. */
28990 declarator
28991 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28992 &ctor_dtor_or_conv_p,
28993 /*parenthesized_p=*/NULL,
28994 /*member_p=*/false,
28995 /*friend_p=*/false);
28998 /* Look for attributes that apply to the ivar. */
28999 attributes = cp_parser_attributes_opt (parser);
29000 /* Remember which attributes are prefix attributes and
29001 which are not. */
29002 first_attribute = attributes;
29003 /* Combine the attributes. */
29004 attributes = chainon (prefix_attributes, attributes);
29006 if (width)
29007 /* Create the bitfield declaration. */
29008 decl = grokbitfield (declarator, &declspecs,
29009 width,
29010 attributes);
29011 else
29012 decl = grokfield (declarator, &declspecs,
29013 NULL_TREE, /*init_const_expr_p=*/false,
29014 NULL_TREE, attributes);
29016 /* Add the instance variable. */
29017 if (decl != error_mark_node && decl != NULL_TREE)
29018 objc_add_instance_variable (decl);
29020 /* Reset PREFIX_ATTRIBUTES. */
29021 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29022 attributes = TREE_CHAIN (attributes);
29023 if (attributes)
29024 TREE_CHAIN (attributes) = NULL_TREE;
29026 token = cp_lexer_peek_token (parser->lexer);
29028 if (token->type == CPP_COMMA)
29030 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29031 continue;
29033 break;
29036 cp_parser_consume_semicolon_at_end_of_statement (parser);
29037 token = cp_lexer_peek_token (parser->lexer);
29040 if (token->keyword == RID_AT_END)
29041 cp_parser_error (parser, "expected %<}%>");
29043 /* Do not consume the RID_AT_END, so it will be read again as terminating
29044 the @interface of @implementation. */
29045 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
29046 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
29048 /* For historical reasons, we accept an optional semicolon. */
29049 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29050 cp_lexer_consume_token (parser->lexer);
29053 /* Parse an Objective-C protocol declaration. */
29055 static void
29056 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
29058 tree proto, protorefs;
29059 cp_token *tok;
29061 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29062 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
29064 tok = cp_lexer_peek_token (parser->lexer);
29065 error_at (tok->location, "identifier expected after %<@protocol%>");
29066 cp_parser_consume_semicolon_at_end_of_statement (parser);
29067 return;
29070 /* See if we have a forward declaration or a definition. */
29071 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
29073 /* Try a forward declaration first. */
29074 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
29076 while (true)
29078 tree id;
29080 id = cp_parser_identifier (parser);
29081 if (id == error_mark_node)
29082 break;
29084 objc_declare_protocol (id, attributes);
29086 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29087 cp_lexer_consume_token (parser->lexer);
29088 else
29089 break;
29091 cp_parser_consume_semicolon_at_end_of_statement (parser);
29094 /* Ok, we got a full-fledged definition (or at least should). */
29095 else
29097 proto = cp_parser_identifier (parser);
29098 protorefs = cp_parser_objc_protocol_refs_opt (parser);
29099 objc_start_protocol (proto, protorefs, attributes);
29100 cp_parser_objc_method_prototype_list (parser);
29104 /* Parse an Objective-C superclass or category. */
29106 static void
29107 cp_parser_objc_superclass_or_category (cp_parser *parser,
29108 bool iface_p,
29109 tree *super,
29110 tree *categ, bool *is_class_extension)
29112 cp_token *next = cp_lexer_peek_token (parser->lexer);
29114 *super = *categ = NULL_TREE;
29115 *is_class_extension = false;
29116 if (next->type == CPP_COLON)
29118 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29119 *super = cp_parser_identifier (parser);
29121 else if (next->type == CPP_OPEN_PAREN)
29123 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29125 /* If there is no category name, and this is an @interface, we
29126 have a class extension. */
29127 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29129 *categ = NULL_TREE;
29130 *is_class_extension = true;
29132 else
29133 *categ = cp_parser_identifier (parser);
29135 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29139 /* Parse an Objective-C class interface. */
29141 static void
29142 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
29144 tree name, super, categ, protos;
29145 bool is_class_extension;
29147 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
29148 name = cp_parser_identifier (parser);
29149 if (name == error_mark_node)
29151 /* It's hard to recover because even if valid @interface stuff
29152 is to follow, we can't compile it (or validate it) if we
29153 don't even know which class it refers to. Let's assume this
29154 was a stray '@interface' token in the stream and skip it.
29156 return;
29158 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
29159 &is_class_extension);
29160 protos = cp_parser_objc_protocol_refs_opt (parser);
29162 /* We have either a class or a category on our hands. */
29163 if (categ || is_class_extension)
29164 objc_start_category_interface (name, categ, protos, attributes);
29165 else
29167 objc_start_class_interface (name, super, protos, attributes);
29168 /* Handle instance variable declarations, if any. */
29169 cp_parser_objc_class_ivars (parser);
29170 objc_continue_interface ();
29173 cp_parser_objc_method_prototype_list (parser);
29176 /* Parse an Objective-C class implementation. */
29178 static void
29179 cp_parser_objc_class_implementation (cp_parser* parser)
29181 tree name, super, categ;
29182 bool is_class_extension;
29184 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
29185 name = cp_parser_identifier (parser);
29186 if (name == error_mark_node)
29188 /* It's hard to recover because even if valid @implementation
29189 stuff is to follow, we can't compile it (or validate it) if
29190 we don't even know which class it refers to. Let's assume
29191 this was a stray '@implementation' token in the stream and
29192 skip it.
29194 return;
29196 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
29197 &is_class_extension);
29199 /* We have either a class or a category on our hands. */
29200 if (categ)
29201 objc_start_category_implementation (name, categ);
29202 else
29204 objc_start_class_implementation (name, super);
29205 /* Handle instance variable declarations, if any. */
29206 cp_parser_objc_class_ivars (parser);
29207 objc_continue_implementation ();
29210 cp_parser_objc_method_definition_list (parser);
29213 /* Consume the @end token and finish off the implementation. */
29215 static void
29216 cp_parser_objc_end_implementation (cp_parser* parser)
29218 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29219 objc_finish_implementation ();
29222 /* Parse an Objective-C declaration. */
29224 static void
29225 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
29227 /* Try to figure out what kind of declaration is present. */
29228 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29230 if (attributes)
29231 switch (kwd->keyword)
29233 case RID_AT_ALIAS:
29234 case RID_AT_CLASS:
29235 case RID_AT_END:
29236 error_at (kwd->location, "attributes may not be specified before"
29237 " the %<@%D%> Objective-C++ keyword",
29238 kwd->u.value);
29239 attributes = NULL;
29240 break;
29241 case RID_AT_IMPLEMENTATION:
29242 warning_at (kwd->location, OPT_Wattributes,
29243 "prefix attributes are ignored before %<@%D%>",
29244 kwd->u.value);
29245 attributes = NULL;
29246 default:
29247 break;
29250 switch (kwd->keyword)
29252 case RID_AT_ALIAS:
29253 cp_parser_objc_alias_declaration (parser);
29254 break;
29255 case RID_AT_CLASS:
29256 cp_parser_objc_class_declaration (parser);
29257 break;
29258 case RID_AT_PROTOCOL:
29259 cp_parser_objc_protocol_declaration (parser, attributes);
29260 break;
29261 case RID_AT_INTERFACE:
29262 cp_parser_objc_class_interface (parser, attributes);
29263 break;
29264 case RID_AT_IMPLEMENTATION:
29265 cp_parser_objc_class_implementation (parser);
29266 break;
29267 case RID_AT_END:
29268 cp_parser_objc_end_implementation (parser);
29269 break;
29270 default:
29271 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29272 kwd->u.value);
29273 cp_parser_skip_to_end_of_block_or_statement (parser);
29277 /* Parse an Objective-C try-catch-finally statement.
29279 objc-try-catch-finally-stmt:
29280 @try compound-statement objc-catch-clause-seq [opt]
29281 objc-finally-clause [opt]
29283 objc-catch-clause-seq:
29284 objc-catch-clause objc-catch-clause-seq [opt]
29286 objc-catch-clause:
29287 @catch ( objc-exception-declaration ) compound-statement
29289 objc-finally-clause:
29290 @finally compound-statement
29292 objc-exception-declaration:
29293 parameter-declaration
29294 '...'
29296 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
29298 Returns NULL_TREE.
29300 PS: This function is identical to c_parser_objc_try_catch_finally_statement
29301 for C. Keep them in sync. */
29303 static tree
29304 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
29306 location_t location;
29307 tree stmt;
29309 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
29310 location = cp_lexer_peek_token (parser->lexer)->location;
29311 objc_maybe_warn_exceptions (location);
29312 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
29313 node, lest it get absorbed into the surrounding block. */
29314 stmt = push_stmt_list ();
29315 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29316 objc_begin_try_stmt (location, pop_stmt_list (stmt));
29318 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
29320 cp_parameter_declarator *parm;
29321 tree parameter_declaration = error_mark_node;
29322 bool seen_open_paren = false;
29324 cp_lexer_consume_token (parser->lexer);
29325 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29326 seen_open_paren = true;
29327 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29329 /* We have "@catch (...)" (where the '...' are literally
29330 what is in the code). Skip the '...'.
29331 parameter_declaration is set to NULL_TREE, and
29332 objc_being_catch_clauses() knows that that means
29333 '...'. */
29334 cp_lexer_consume_token (parser->lexer);
29335 parameter_declaration = NULL_TREE;
29337 else
29339 /* We have "@catch (NSException *exception)" or something
29340 like that. Parse the parameter declaration. */
29341 parm = cp_parser_parameter_declaration (parser, false, NULL);
29342 if (parm == NULL)
29343 parameter_declaration = error_mark_node;
29344 else
29345 parameter_declaration = grokdeclarator (parm->declarator,
29346 &parm->decl_specifiers,
29347 PARM, /*initialized=*/0,
29348 /*attrlist=*/NULL);
29350 if (seen_open_paren)
29351 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29352 else
29354 /* If there was no open parenthesis, we are recovering from
29355 an error, and we are trying to figure out what mistake
29356 the user has made. */
29358 /* If there is an immediate closing parenthesis, the user
29359 probably forgot the opening one (ie, they typed "@catch
29360 NSException *e)". Parse the closing parenthesis and keep
29361 going. */
29362 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29363 cp_lexer_consume_token (parser->lexer);
29365 /* If these is no immediate closing parenthesis, the user
29366 probably doesn't know that parenthesis are required at
29367 all (ie, they typed "@catch NSException *e"). So, just
29368 forget about the closing parenthesis and keep going. */
29370 objc_begin_catch_clause (parameter_declaration);
29371 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29372 objc_finish_catch_clause ();
29374 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
29376 cp_lexer_consume_token (parser->lexer);
29377 location = cp_lexer_peek_token (parser->lexer)->location;
29378 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
29379 node, lest it get absorbed into the surrounding block. */
29380 stmt = push_stmt_list ();
29381 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29382 objc_build_finally_clause (location, pop_stmt_list (stmt));
29385 return objc_finish_try_stmt ();
29388 /* Parse an Objective-C synchronized statement.
29390 objc-synchronized-stmt:
29391 @synchronized ( expression ) compound-statement
29393 Returns NULL_TREE. */
29395 static tree
29396 cp_parser_objc_synchronized_statement (cp_parser *parser)
29398 location_t location;
29399 tree lock, stmt;
29401 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
29403 location = cp_lexer_peek_token (parser->lexer)->location;
29404 objc_maybe_warn_exceptions (location);
29405 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29406 lock = cp_parser_expression (parser);
29407 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29409 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
29410 node, lest it get absorbed into the surrounding block. */
29411 stmt = push_stmt_list ();
29412 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29414 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
29417 /* Parse an Objective-C throw statement.
29419 objc-throw-stmt:
29420 @throw assignment-expression [opt] ;
29422 Returns a constructed '@throw' statement. */
29424 static tree
29425 cp_parser_objc_throw_statement (cp_parser *parser)
29427 tree expr = NULL_TREE;
29428 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29430 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
29432 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29433 expr = cp_parser_expression (parser);
29435 cp_parser_consume_semicolon_at_end_of_statement (parser);
29437 return objc_build_throw_stmt (loc, expr);
29440 /* Parse an Objective-C statement. */
29442 static tree
29443 cp_parser_objc_statement (cp_parser * parser)
29445 /* Try to figure out what kind of declaration is present. */
29446 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29448 switch (kwd->keyword)
29450 case RID_AT_TRY:
29451 return cp_parser_objc_try_catch_finally_statement (parser);
29452 case RID_AT_SYNCHRONIZED:
29453 return cp_parser_objc_synchronized_statement (parser);
29454 case RID_AT_THROW:
29455 return cp_parser_objc_throw_statement (parser);
29456 default:
29457 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29458 kwd->u.value);
29459 cp_parser_skip_to_end_of_block_or_statement (parser);
29462 return error_mark_node;
29465 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
29466 look ahead to see if an objc keyword follows the attributes. This
29467 is to detect the use of prefix attributes on ObjC @interface and
29468 @protocol. */
29470 static bool
29471 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
29473 cp_lexer_save_tokens (parser->lexer);
29474 *attrib = cp_parser_attributes_opt (parser);
29475 gcc_assert (*attrib);
29476 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
29478 cp_lexer_commit_tokens (parser->lexer);
29479 return true;
29481 cp_lexer_rollback_tokens (parser->lexer);
29482 return false;
29485 /* This routine is a minimal replacement for
29486 c_parser_struct_declaration () used when parsing the list of
29487 types/names or ObjC++ properties. For example, when parsing the
29488 code
29490 @property (readonly) int a, b, c;
29492 this function is responsible for parsing "int a, int b, int c" and
29493 returning the declarations as CHAIN of DECLs.
29495 TODO: Share this code with cp_parser_objc_class_ivars. It's very
29496 similar parsing. */
29497 static tree
29498 cp_parser_objc_struct_declaration (cp_parser *parser)
29500 tree decls = NULL_TREE;
29501 cp_decl_specifier_seq declspecs;
29502 int decl_class_or_enum_p;
29503 tree prefix_attributes;
29505 cp_parser_decl_specifier_seq (parser,
29506 CP_PARSER_FLAGS_NONE,
29507 &declspecs,
29508 &decl_class_or_enum_p);
29510 if (declspecs.type == error_mark_node)
29511 return error_mark_node;
29513 /* auto, register, static, extern, mutable. */
29514 if (declspecs.storage_class != sc_none)
29516 cp_parser_error (parser, "invalid type for property");
29517 declspecs.storage_class = sc_none;
29520 /* thread_local. */
29521 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29523 cp_parser_error (parser, "invalid type for property");
29524 declspecs.locations[ds_thread] = 0;
29527 /* typedef. */
29528 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29530 cp_parser_error (parser, "invalid type for property");
29531 declspecs.locations[ds_typedef] = 0;
29534 prefix_attributes = declspecs.attributes;
29535 declspecs.attributes = NULL_TREE;
29537 /* Keep going until we hit the `;' at the end of the declaration. */
29538 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29540 tree attributes, first_attribute, decl;
29541 cp_declarator *declarator;
29542 cp_token *token;
29544 /* Parse the declarator. */
29545 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29546 NULL, NULL, false, false);
29548 /* Look for attributes that apply to the ivar. */
29549 attributes = cp_parser_attributes_opt (parser);
29550 /* Remember which attributes are prefix attributes and
29551 which are not. */
29552 first_attribute = attributes;
29553 /* Combine the attributes. */
29554 attributes = chainon (prefix_attributes, attributes);
29556 decl = grokfield (declarator, &declspecs,
29557 NULL_TREE, /*init_const_expr_p=*/false,
29558 NULL_TREE, attributes);
29560 if (decl == error_mark_node || decl == NULL_TREE)
29561 return error_mark_node;
29563 /* Reset PREFIX_ATTRIBUTES. */
29564 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29565 attributes = TREE_CHAIN (attributes);
29566 if (attributes)
29567 TREE_CHAIN (attributes) = NULL_TREE;
29569 DECL_CHAIN (decl) = decls;
29570 decls = decl;
29572 token = cp_lexer_peek_token (parser->lexer);
29573 if (token->type == CPP_COMMA)
29575 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29576 continue;
29578 else
29579 break;
29581 return decls;
29584 /* Parse an Objective-C @property declaration. The syntax is:
29586 objc-property-declaration:
29587 '@property' objc-property-attributes[opt] struct-declaration ;
29589 objc-property-attributes:
29590 '(' objc-property-attribute-list ')'
29592 objc-property-attribute-list:
29593 objc-property-attribute
29594 objc-property-attribute-list, objc-property-attribute
29596 objc-property-attribute
29597 'getter' = identifier
29598 'setter' = identifier
29599 'readonly'
29600 'readwrite'
29601 'assign'
29602 'retain'
29603 'copy'
29604 'nonatomic'
29606 For example:
29607 @property NSString *name;
29608 @property (readonly) id object;
29609 @property (retain, nonatomic, getter=getTheName) id name;
29610 @property int a, b, c;
29612 PS: This function is identical to
29613 c_parser_objc_at_property_declaration for C. Keep them in sync. */
29614 static void
29615 cp_parser_objc_at_property_declaration (cp_parser *parser)
29617 /* The following variables hold the attributes of the properties as
29618 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
29619 seen. When we see an attribute, we set them to 'true' (if they
29620 are boolean properties) or to the identifier (if they have an
29621 argument, ie, for getter and setter). Note that here we only
29622 parse the list of attributes, check the syntax and accumulate the
29623 attributes that we find. objc_add_property_declaration() will
29624 then process the information. */
29625 bool property_assign = false;
29626 bool property_copy = false;
29627 tree property_getter_ident = NULL_TREE;
29628 bool property_nonatomic = false;
29629 bool property_readonly = false;
29630 bool property_readwrite = false;
29631 bool property_retain = false;
29632 tree property_setter_ident = NULL_TREE;
29634 /* 'properties' is the list of properties that we read. Usually a
29635 single one, but maybe more (eg, in "@property int a, b, c;" there
29636 are three). */
29637 tree properties;
29638 location_t loc;
29640 loc = cp_lexer_peek_token (parser->lexer)->location;
29642 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
29644 /* Parse the optional attribute list... */
29645 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29647 /* Eat the '('. */
29648 cp_lexer_consume_token (parser->lexer);
29650 while (true)
29652 bool syntax_error = false;
29653 cp_token *token = cp_lexer_peek_token (parser->lexer);
29654 enum rid keyword;
29656 if (token->type != CPP_NAME)
29658 cp_parser_error (parser, "expected identifier");
29659 break;
29661 keyword = C_RID_CODE (token->u.value);
29662 cp_lexer_consume_token (parser->lexer);
29663 switch (keyword)
29665 case RID_ASSIGN: property_assign = true; break;
29666 case RID_COPY: property_copy = true; break;
29667 case RID_NONATOMIC: property_nonatomic = true; break;
29668 case RID_READONLY: property_readonly = true; break;
29669 case RID_READWRITE: property_readwrite = true; break;
29670 case RID_RETAIN: property_retain = true; break;
29672 case RID_GETTER:
29673 case RID_SETTER:
29674 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29676 if (keyword == RID_GETTER)
29677 cp_parser_error (parser,
29678 "missing %<=%> (after %<getter%> attribute)");
29679 else
29680 cp_parser_error (parser,
29681 "missing %<=%> (after %<setter%> attribute)");
29682 syntax_error = true;
29683 break;
29685 cp_lexer_consume_token (parser->lexer); /* eat the = */
29686 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
29688 cp_parser_error (parser, "expected identifier");
29689 syntax_error = true;
29690 break;
29692 if (keyword == RID_SETTER)
29694 if (property_setter_ident != NULL_TREE)
29696 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
29697 cp_lexer_consume_token (parser->lexer);
29699 else
29700 property_setter_ident = cp_parser_objc_selector (parser);
29701 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29702 cp_parser_error (parser, "setter name must terminate with %<:%>");
29703 else
29704 cp_lexer_consume_token (parser->lexer);
29706 else
29708 if (property_getter_ident != NULL_TREE)
29710 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
29711 cp_lexer_consume_token (parser->lexer);
29713 else
29714 property_getter_ident = cp_parser_objc_selector (parser);
29716 break;
29717 default:
29718 cp_parser_error (parser, "unknown property attribute");
29719 syntax_error = true;
29720 break;
29723 if (syntax_error)
29724 break;
29726 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29727 cp_lexer_consume_token (parser->lexer);
29728 else
29729 break;
29732 /* FIXME: "@property (setter, assign);" will generate a spurious
29733 "error: expected ‘)’ before ‘,’ token". This is because
29734 cp_parser_require, unlike the C counterpart, will produce an
29735 error even if we are in error recovery. */
29736 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29738 cp_parser_skip_to_closing_parenthesis (parser,
29739 /*recovering=*/true,
29740 /*or_comma=*/false,
29741 /*consume_paren=*/true);
29745 /* ... and the property declaration(s). */
29746 properties = cp_parser_objc_struct_declaration (parser);
29748 if (properties == error_mark_node)
29750 cp_parser_skip_to_end_of_statement (parser);
29751 /* If the next token is now a `;', consume it. */
29752 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29753 cp_lexer_consume_token (parser->lexer);
29754 return;
29757 if (properties == NULL_TREE)
29758 cp_parser_error (parser, "expected identifier");
29759 else
29761 /* Comma-separated properties are chained together in
29762 reverse order; add them one by one. */
29763 properties = nreverse (properties);
29765 for (; properties; properties = TREE_CHAIN (properties))
29766 objc_add_property_declaration (loc, copy_node (properties),
29767 property_readonly, property_readwrite,
29768 property_assign, property_retain,
29769 property_copy, property_nonatomic,
29770 property_getter_ident, property_setter_ident);
29773 cp_parser_consume_semicolon_at_end_of_statement (parser);
29776 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
29778 objc-synthesize-declaration:
29779 @synthesize objc-synthesize-identifier-list ;
29781 objc-synthesize-identifier-list:
29782 objc-synthesize-identifier
29783 objc-synthesize-identifier-list, objc-synthesize-identifier
29785 objc-synthesize-identifier
29786 identifier
29787 identifier = identifier
29789 For example:
29790 @synthesize MyProperty;
29791 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
29793 PS: This function is identical to c_parser_objc_at_synthesize_declaration
29794 for C. Keep them in sync.
29796 static void
29797 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
29799 tree list = NULL_TREE;
29800 location_t loc;
29801 loc = cp_lexer_peek_token (parser->lexer)->location;
29803 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
29804 while (true)
29806 tree property, ivar;
29807 property = cp_parser_identifier (parser);
29808 if (property == error_mark_node)
29810 cp_parser_consume_semicolon_at_end_of_statement (parser);
29811 return;
29813 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
29815 cp_lexer_consume_token (parser->lexer);
29816 ivar = cp_parser_identifier (parser);
29817 if (ivar == error_mark_node)
29819 cp_parser_consume_semicolon_at_end_of_statement (parser);
29820 return;
29823 else
29824 ivar = NULL_TREE;
29825 list = chainon (list, build_tree_list (ivar, property));
29826 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29827 cp_lexer_consume_token (parser->lexer);
29828 else
29829 break;
29831 cp_parser_consume_semicolon_at_end_of_statement (parser);
29832 objc_add_synthesize_declaration (loc, list);
29835 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
29837 objc-dynamic-declaration:
29838 @dynamic identifier-list ;
29840 For example:
29841 @dynamic MyProperty;
29842 @dynamic MyProperty, AnotherProperty;
29844 PS: This function is identical to c_parser_objc_at_dynamic_declaration
29845 for C. Keep them in sync.
29847 static void
29848 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
29850 tree list = NULL_TREE;
29851 location_t loc;
29852 loc = cp_lexer_peek_token (parser->lexer)->location;
29854 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
29855 while (true)
29857 tree property;
29858 property = cp_parser_identifier (parser);
29859 if (property == error_mark_node)
29861 cp_parser_consume_semicolon_at_end_of_statement (parser);
29862 return;
29864 list = chainon (list, build_tree_list (NULL, property));
29865 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29866 cp_lexer_consume_token (parser->lexer);
29867 else
29868 break;
29870 cp_parser_consume_semicolon_at_end_of_statement (parser);
29871 objc_add_dynamic_declaration (loc, list);
29875 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
29877 /* Returns name of the next clause.
29878 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
29879 the token is not consumed. Otherwise appropriate pragma_omp_clause is
29880 returned and the token is consumed. */
29882 static pragma_omp_clause
29883 cp_parser_omp_clause_name (cp_parser *parser)
29885 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
29887 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
29888 result = PRAGMA_OACC_CLAUSE_AUTO;
29889 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
29890 result = PRAGMA_OMP_CLAUSE_IF;
29891 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
29892 result = PRAGMA_OMP_CLAUSE_DEFAULT;
29893 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
29894 result = PRAGMA_OACC_CLAUSE_DELETE;
29895 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
29896 result = PRAGMA_OMP_CLAUSE_PRIVATE;
29897 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29898 result = PRAGMA_OMP_CLAUSE_FOR;
29899 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29901 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29902 const char *p = IDENTIFIER_POINTER (id);
29904 switch (p[0])
29906 case 'a':
29907 if (!strcmp ("aligned", p))
29908 result = PRAGMA_OMP_CLAUSE_ALIGNED;
29909 else if (!strcmp ("async", p))
29910 result = PRAGMA_OACC_CLAUSE_ASYNC;
29911 break;
29912 case 'c':
29913 if (!strcmp ("collapse", p))
29914 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
29915 else if (!strcmp ("copy", p))
29916 result = PRAGMA_OACC_CLAUSE_COPY;
29917 else if (!strcmp ("copyin", p))
29918 result = PRAGMA_OMP_CLAUSE_COPYIN;
29919 else if (!strcmp ("copyout", p))
29920 result = PRAGMA_OACC_CLAUSE_COPYOUT;
29921 else if (!strcmp ("copyprivate", p))
29922 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
29923 else if (!strcmp ("create", p))
29924 result = PRAGMA_OACC_CLAUSE_CREATE;
29925 break;
29926 case 'd':
29927 if (!strcmp ("defaultmap", p))
29928 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
29929 else if (!strcmp ("depend", p))
29930 result = PRAGMA_OMP_CLAUSE_DEPEND;
29931 else if (!strcmp ("device", p))
29932 result = PRAGMA_OMP_CLAUSE_DEVICE;
29933 else if (!strcmp ("deviceptr", p))
29934 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
29935 else if (!strcmp ("device_resident", p))
29936 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
29937 else if (!strcmp ("dist_schedule", p))
29938 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
29939 break;
29940 case 'f':
29941 if (!strcmp ("final", p))
29942 result = PRAGMA_OMP_CLAUSE_FINAL;
29943 else if (!strcmp ("firstprivate", p))
29944 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
29945 else if (!strcmp ("from", p))
29946 result = PRAGMA_OMP_CLAUSE_FROM;
29947 break;
29948 case 'g':
29949 if (!strcmp ("gang", p))
29950 result = PRAGMA_OACC_CLAUSE_GANG;
29951 else if (!strcmp ("grainsize", p))
29952 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
29953 break;
29954 case 'h':
29955 if (!strcmp ("hint", p))
29956 result = PRAGMA_OMP_CLAUSE_HINT;
29957 else if (!strcmp ("host", p))
29958 result = PRAGMA_OACC_CLAUSE_HOST;
29959 break;
29960 case 'i':
29961 if (!strcmp ("inbranch", p))
29962 result = PRAGMA_OMP_CLAUSE_INBRANCH;
29963 else if (!strcmp ("independent", p))
29964 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
29965 else if (!strcmp ("is_device_ptr", p))
29966 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
29967 break;
29968 case 'l':
29969 if (!strcmp ("lastprivate", p))
29970 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
29971 else if (!strcmp ("linear", p))
29972 result = PRAGMA_OMP_CLAUSE_LINEAR;
29973 else if (!strcmp ("link", p))
29974 result = PRAGMA_OMP_CLAUSE_LINK;
29975 break;
29976 case 'm':
29977 if (!strcmp ("map", p))
29978 result = PRAGMA_OMP_CLAUSE_MAP;
29979 else if (!strcmp ("mergeable", p))
29980 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
29981 else if (flag_cilkplus && !strcmp ("mask", p))
29982 result = PRAGMA_CILK_CLAUSE_MASK;
29983 break;
29984 case 'n':
29985 if (!strcmp ("nogroup", p))
29986 result = PRAGMA_OMP_CLAUSE_NOGROUP;
29987 else if (!strcmp ("notinbranch", p))
29988 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
29989 else if (!strcmp ("nowait", p))
29990 result = PRAGMA_OMP_CLAUSE_NOWAIT;
29991 else if (flag_cilkplus && !strcmp ("nomask", p))
29992 result = PRAGMA_CILK_CLAUSE_NOMASK;
29993 else if (!strcmp ("num_gangs", p))
29994 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
29995 else if (!strcmp ("num_tasks", p))
29996 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
29997 else if (!strcmp ("num_teams", p))
29998 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
29999 else if (!strcmp ("num_threads", p))
30000 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
30001 else if (!strcmp ("num_workers", p))
30002 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
30003 break;
30004 case 'o':
30005 if (!strcmp ("ordered", p))
30006 result = PRAGMA_OMP_CLAUSE_ORDERED;
30007 break;
30008 case 'p':
30009 if (!strcmp ("parallel", p))
30010 result = PRAGMA_OMP_CLAUSE_PARALLEL;
30011 else if (!strcmp ("present", p))
30012 result = PRAGMA_OACC_CLAUSE_PRESENT;
30013 else if (!strcmp ("present_or_copy", p)
30014 || !strcmp ("pcopy", p))
30015 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
30016 else if (!strcmp ("present_or_copyin", p)
30017 || !strcmp ("pcopyin", p))
30018 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
30019 else if (!strcmp ("present_or_copyout", p)
30020 || !strcmp ("pcopyout", p))
30021 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
30022 else if (!strcmp ("present_or_create", p)
30023 || !strcmp ("pcreate", p))
30024 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
30025 else if (!strcmp ("priority", p))
30026 result = PRAGMA_OMP_CLAUSE_PRIORITY;
30027 else if (!strcmp ("proc_bind", p))
30028 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
30029 break;
30030 case 'r':
30031 if (!strcmp ("reduction", p))
30032 result = PRAGMA_OMP_CLAUSE_REDUCTION;
30033 break;
30034 case 's':
30035 if (!strcmp ("safelen", p))
30036 result = PRAGMA_OMP_CLAUSE_SAFELEN;
30037 else if (!strcmp ("schedule", p))
30038 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
30039 else if (!strcmp ("sections", p))
30040 result = PRAGMA_OMP_CLAUSE_SECTIONS;
30041 else if (!strcmp ("self", p))
30042 result = PRAGMA_OACC_CLAUSE_SELF;
30043 else if (!strcmp ("seq", p))
30044 result = PRAGMA_OACC_CLAUSE_SEQ;
30045 else if (!strcmp ("shared", p))
30046 result = PRAGMA_OMP_CLAUSE_SHARED;
30047 else if (!strcmp ("simd", p))
30048 result = PRAGMA_OMP_CLAUSE_SIMD;
30049 else if (!strcmp ("simdlen", p))
30050 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
30051 break;
30052 case 't':
30053 if (!strcmp ("taskgroup", p))
30054 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
30055 else if (!strcmp ("thread_limit", p))
30056 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
30057 else if (!strcmp ("threads", p))
30058 result = PRAGMA_OMP_CLAUSE_THREADS;
30059 else if (!strcmp ("tile", p))
30060 result = PRAGMA_OACC_CLAUSE_TILE;
30061 else if (!strcmp ("to", p))
30062 result = PRAGMA_OMP_CLAUSE_TO;
30063 break;
30064 case 'u':
30065 if (!strcmp ("uniform", p))
30066 result = PRAGMA_OMP_CLAUSE_UNIFORM;
30067 else if (!strcmp ("untied", p))
30068 result = PRAGMA_OMP_CLAUSE_UNTIED;
30069 else if (!strcmp ("use_device", p))
30070 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
30071 else if (!strcmp ("use_device_ptr", p))
30072 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
30073 break;
30074 case 'v':
30075 if (!strcmp ("vector", p))
30076 result = PRAGMA_OACC_CLAUSE_VECTOR;
30077 else if (!strcmp ("vector_length", p))
30078 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
30079 else if (flag_cilkplus && !strcmp ("vectorlength", p))
30080 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
30081 break;
30082 case 'w':
30083 if (!strcmp ("wait", p))
30084 result = PRAGMA_OACC_CLAUSE_WAIT;
30085 else if (!strcmp ("worker", p))
30086 result = PRAGMA_OACC_CLAUSE_WORKER;
30087 break;
30091 if (result != PRAGMA_OMP_CLAUSE_NONE)
30092 cp_lexer_consume_token (parser->lexer);
30094 return result;
30097 /* Validate that a clause of the given type does not already exist. */
30099 static void
30100 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
30101 const char *name, location_t location)
30103 tree c;
30105 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30106 if (OMP_CLAUSE_CODE (c) == code)
30108 error_at (location, "too many %qs clauses", name);
30109 break;
30113 /* OpenMP 2.5:
30114 variable-list:
30115 identifier
30116 variable-list , identifier
30118 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
30119 colon). An opening parenthesis will have been consumed by the caller.
30121 If KIND is nonzero, create the appropriate node and install the decl
30122 in OMP_CLAUSE_DECL and add the node to the head of the list.
30124 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
30125 return the list created.
30127 COLON can be NULL if only closing parenthesis should end the list,
30128 or pointer to bool which will receive false if the list is terminated
30129 by closing parenthesis or true if the list is terminated by colon. */
30131 static tree
30132 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
30133 tree list, bool *colon)
30135 cp_token *token;
30136 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30137 if (colon)
30139 parser->colon_corrects_to_scope_p = false;
30140 *colon = false;
30142 while (1)
30144 tree name, decl;
30146 token = cp_lexer_peek_token (parser->lexer);
30147 if (kind != 0
30148 && current_class_ptr
30149 && cp_parser_is_keyword (token, RID_THIS))
30151 decl = finish_this_expr ();
30152 if (TREE_CODE (decl) == NON_LVALUE_EXPR
30153 || CONVERT_EXPR_P (decl))
30154 decl = TREE_OPERAND (decl, 0);
30155 cp_lexer_consume_token (parser->lexer);
30157 else
30159 name = cp_parser_id_expression (parser, /*template_p=*/false,
30160 /*check_dependency_p=*/true,
30161 /*template_p=*/NULL,
30162 /*declarator_p=*/false,
30163 /*optional_p=*/false);
30164 if (name == error_mark_node)
30165 goto skip_comma;
30167 decl = cp_parser_lookup_name_simple (parser, name, token->location);
30168 if (decl == error_mark_node)
30169 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
30170 token->location);
30172 if (decl == error_mark_node)
30174 else if (kind != 0)
30176 switch (kind)
30178 case OMP_CLAUSE__CACHE_:
30179 /* The OpenACC cache directive explicitly only allows "array
30180 elements or subarrays". */
30181 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
30183 error_at (token->location, "expected %<[%>");
30184 decl = error_mark_node;
30185 break;
30187 /* FALLTHROUGH. */
30188 case OMP_CLAUSE_MAP:
30189 case OMP_CLAUSE_FROM:
30190 case OMP_CLAUSE_TO:
30191 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
30193 location_t loc
30194 = cp_lexer_peek_token (parser->lexer)->location;
30195 cp_id_kind idk = CP_ID_KIND_NONE;
30196 cp_lexer_consume_token (parser->lexer);
30197 decl = convert_from_reference (decl);
30198 decl
30199 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
30200 decl, false,
30201 &idk, loc);
30203 /* FALLTHROUGH. */
30204 case OMP_CLAUSE_DEPEND:
30205 case OMP_CLAUSE_REDUCTION:
30206 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
30208 tree low_bound = NULL_TREE, length = NULL_TREE;
30210 parser->colon_corrects_to_scope_p = false;
30211 cp_lexer_consume_token (parser->lexer);
30212 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30213 low_bound = cp_parser_expression (parser);
30214 if (!colon)
30215 parser->colon_corrects_to_scope_p
30216 = saved_colon_corrects_to_scope_p;
30217 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
30218 length = integer_one_node;
30219 else
30221 /* Look for `:'. */
30222 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30223 goto skip_comma;
30224 if (!cp_lexer_next_token_is (parser->lexer,
30225 CPP_CLOSE_SQUARE))
30226 length = cp_parser_expression (parser);
30228 /* Look for the closing `]'. */
30229 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
30230 RT_CLOSE_SQUARE))
30231 goto skip_comma;
30233 decl = tree_cons (low_bound, length, decl);
30235 break;
30236 default:
30237 break;
30240 tree u = build_omp_clause (token->location, kind);
30241 OMP_CLAUSE_DECL (u) = decl;
30242 OMP_CLAUSE_CHAIN (u) = list;
30243 list = u;
30245 else
30246 list = tree_cons (decl, NULL_TREE, list);
30248 get_comma:
30249 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
30250 break;
30251 cp_lexer_consume_token (parser->lexer);
30254 if (colon)
30255 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30257 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30259 *colon = true;
30260 cp_parser_require (parser, CPP_COLON, RT_COLON);
30261 return list;
30264 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30266 int ending;
30268 /* Try to resync to an unnested comma. Copied from
30269 cp_parser_parenthesized_expression_list. */
30270 skip_comma:
30271 if (colon)
30272 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30273 ending = cp_parser_skip_to_closing_parenthesis (parser,
30274 /*recovering=*/true,
30275 /*or_comma=*/true,
30276 /*consume_paren=*/true);
30277 if (ending < 0)
30278 goto get_comma;
30281 return list;
30284 /* Similarly, but expect leading and trailing parenthesis. This is a very
30285 common case for omp clauses. */
30287 static tree
30288 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
30290 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30291 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
30292 return list;
30295 /* OpenACC 2.0:
30296 copy ( variable-list )
30297 copyin ( variable-list )
30298 copyout ( variable-list )
30299 create ( variable-list )
30300 delete ( variable-list )
30301 present ( variable-list )
30302 present_or_copy ( variable-list )
30303 pcopy ( variable-list )
30304 present_or_copyin ( variable-list )
30305 pcopyin ( variable-list )
30306 present_or_copyout ( variable-list )
30307 pcopyout ( variable-list )
30308 present_or_create ( variable-list )
30309 pcreate ( variable-list ) */
30311 static tree
30312 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
30313 tree list)
30315 enum gomp_map_kind kind;
30316 switch (c_kind)
30318 case PRAGMA_OACC_CLAUSE_COPY:
30319 kind = GOMP_MAP_FORCE_TOFROM;
30320 break;
30321 case PRAGMA_OACC_CLAUSE_COPYIN:
30322 kind = GOMP_MAP_FORCE_TO;
30323 break;
30324 case PRAGMA_OACC_CLAUSE_COPYOUT:
30325 kind = GOMP_MAP_FORCE_FROM;
30326 break;
30327 case PRAGMA_OACC_CLAUSE_CREATE:
30328 kind = GOMP_MAP_FORCE_ALLOC;
30329 break;
30330 case PRAGMA_OACC_CLAUSE_DELETE:
30331 kind = GOMP_MAP_DELETE;
30332 break;
30333 case PRAGMA_OACC_CLAUSE_DEVICE:
30334 kind = GOMP_MAP_FORCE_TO;
30335 break;
30336 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
30337 kind = GOMP_MAP_DEVICE_RESIDENT;
30338 break;
30339 case PRAGMA_OACC_CLAUSE_HOST:
30340 case PRAGMA_OACC_CLAUSE_SELF:
30341 kind = GOMP_MAP_FORCE_FROM;
30342 break;
30343 case PRAGMA_OACC_CLAUSE_LINK:
30344 kind = GOMP_MAP_LINK;
30345 break;
30346 case PRAGMA_OACC_CLAUSE_PRESENT:
30347 kind = GOMP_MAP_FORCE_PRESENT;
30348 break;
30349 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
30350 kind = GOMP_MAP_TOFROM;
30351 break;
30352 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
30353 kind = GOMP_MAP_TO;
30354 break;
30355 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
30356 kind = GOMP_MAP_FROM;
30357 break;
30358 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
30359 kind = GOMP_MAP_ALLOC;
30360 break;
30361 default:
30362 gcc_unreachable ();
30364 tree nl, c;
30365 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
30367 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
30368 OMP_CLAUSE_SET_MAP_KIND (c, kind);
30370 return nl;
30373 /* OpenACC 2.0:
30374 deviceptr ( variable-list ) */
30376 static tree
30377 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
30379 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30380 tree vars, t;
30382 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
30383 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
30384 variable-list must only allow for pointer variables. */
30385 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30386 for (t = vars; t; t = TREE_CHAIN (t))
30388 tree v = TREE_PURPOSE (t);
30389 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
30390 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
30391 OMP_CLAUSE_DECL (u) = v;
30392 OMP_CLAUSE_CHAIN (u) = list;
30393 list = u;
30396 return list;
30399 /* OpenACC 2.0:
30400 auto
30401 independent
30402 nohost
30403 seq */
30405 static tree
30406 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
30407 enum omp_clause_code code,
30408 tree list, location_t location)
30410 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
30411 tree c = build_omp_clause (location, code);
30412 OMP_CLAUSE_CHAIN (c) = list;
30413 return c;
30416 /* OpenACC:
30417 num_gangs ( expression )
30418 num_workers ( expression )
30419 vector_length ( expression ) */
30421 static tree
30422 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
30423 const char *str, tree list)
30425 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30427 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30428 return list;
30430 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
30432 if (t == error_mark_node
30433 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30435 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30436 /*or_comma=*/false,
30437 /*consume_paren=*/true);
30438 return list;
30441 check_no_duplicate_clause (list, code, str, loc);
30443 tree c = build_omp_clause (loc, code);
30444 OMP_CLAUSE_OPERAND (c, 0) = t;
30445 OMP_CLAUSE_CHAIN (c) = list;
30446 return c;
30449 /* OpenACC:
30451 gang [( gang-arg-list )]
30452 worker [( [num:] int-expr )]
30453 vector [( [length:] int-expr )]
30455 where gang-arg is one of:
30457 [num:] int-expr
30458 static: size-expr
30460 and size-expr may be:
30463 int-expr
30466 static tree
30467 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
30468 const char *str, tree list)
30470 const char *id = "num";
30471 cp_lexer *lexer = parser->lexer;
30472 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
30473 location_t loc = cp_lexer_peek_token (lexer)->location;
30475 if (kind == OMP_CLAUSE_VECTOR)
30476 id = "length";
30478 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
30480 cp_lexer_consume_token (lexer);
30484 cp_token *next = cp_lexer_peek_token (lexer);
30485 int idx = 0;
30487 /* Gang static argument. */
30488 if (kind == OMP_CLAUSE_GANG
30489 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
30491 cp_lexer_consume_token (lexer);
30493 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30494 goto cleanup_error;
30496 idx = 1;
30497 if (ops[idx] != NULL)
30499 cp_parser_error (parser, "too many %<static%> arguments");
30500 goto cleanup_error;
30503 /* Check for the '*' argument. */
30504 if (cp_lexer_next_token_is (lexer, CPP_MULT)
30505 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30506 || cp_lexer_nth_token_is (parser->lexer, 2,
30507 CPP_CLOSE_PAREN)))
30509 cp_lexer_consume_token (lexer);
30510 ops[idx] = integer_minus_one_node;
30512 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
30514 cp_lexer_consume_token (lexer);
30515 continue;
30517 else break;
30520 /* Worker num: argument and vector length: arguments. */
30521 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
30522 && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
30523 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
30525 cp_lexer_consume_token (lexer); /* id */
30526 cp_lexer_consume_token (lexer); /* ':' */
30529 /* Now collect the actual argument. */
30530 if (ops[idx] != NULL_TREE)
30532 cp_parser_error (parser, "unexpected argument");
30533 goto cleanup_error;
30536 tree expr = cp_parser_assignment_expression (parser, NULL, false,
30537 false);
30538 if (expr == error_mark_node)
30539 goto cleanup_error;
30541 mark_exp_read (expr);
30542 ops[idx] = expr;
30544 if (kind == OMP_CLAUSE_GANG
30545 && cp_lexer_next_token_is (lexer, CPP_COMMA))
30547 cp_lexer_consume_token (lexer);
30548 continue;
30550 break;
30552 while (1);
30554 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30555 goto cleanup_error;
30558 check_no_duplicate_clause (list, kind, str, loc);
30560 c = build_omp_clause (loc, kind);
30562 if (ops[1])
30563 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
30565 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
30566 OMP_CLAUSE_CHAIN (c) = list;
30568 return c;
30570 cleanup_error:
30571 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
30572 return list;
30575 /* OpenACC 2.0:
30576 tile ( size-expr-list ) */
30578 static tree
30579 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
30581 tree c, expr = error_mark_node;
30582 tree tile = NULL_TREE;
30584 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
30586 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30587 return list;
30591 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
30592 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30593 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
30595 cp_lexer_consume_token (parser->lexer);
30596 expr = integer_minus_one_node;
30598 else
30599 expr = cp_parser_assignment_expression (parser, NULL, false, false);
30601 if (expr == error_mark_node)
30602 return list;
30604 tile = tree_cons (NULL_TREE, expr, tile);
30606 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30607 cp_lexer_consume_token (parser->lexer);
30609 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
30611 /* Consume the trailing ')'. */
30612 cp_lexer_consume_token (parser->lexer);
30614 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
30615 tile = nreverse (tile);
30616 OMP_CLAUSE_TILE_LIST (c) = tile;
30617 OMP_CLAUSE_CHAIN (c) = list;
30618 return c;
30621 /* OpenACC 2.0
30622 Parse wait clause or directive parameters. */
30624 static tree
30625 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
30627 vec<tree, va_gc> *args;
30628 tree t, args_tree;
30630 args = cp_parser_parenthesized_expression_list (parser, non_attr,
30631 /*cast_p=*/false,
30632 /*allow_expansion_p=*/true,
30633 /*non_constant_p=*/NULL);
30635 if (args == NULL || args->length () == 0)
30637 cp_parser_error (parser, "expected integer expression before ')'");
30638 if (args != NULL)
30639 release_tree_vector (args);
30640 return list;
30643 args_tree = build_tree_list_vec (args);
30645 release_tree_vector (args);
30647 for (t = args_tree; t; t = TREE_CHAIN (t))
30649 tree targ = TREE_VALUE (t);
30651 if (targ != error_mark_node)
30653 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
30654 error ("%<wait%> expression must be integral");
30655 else
30657 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
30659 mark_rvalue_use (targ);
30660 OMP_CLAUSE_DECL (c) = targ;
30661 OMP_CLAUSE_CHAIN (c) = list;
30662 list = c;
30667 return list;
30670 /* OpenACC:
30671 wait ( int-expr-list ) */
30673 static tree
30674 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
30676 location_t location = cp_lexer_peek_token (parser->lexer)->location;
30678 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
30679 return list;
30681 list = cp_parser_oacc_wait_list (parser, location, list);
30683 return list;
30686 /* OpenMP 3.0:
30687 collapse ( constant-expression ) */
30689 static tree
30690 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
30692 tree c, num;
30693 location_t loc;
30694 HOST_WIDE_INT n;
30696 loc = cp_lexer_peek_token (parser->lexer)->location;
30697 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30698 return list;
30700 num = cp_parser_constant_expression (parser);
30702 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30703 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30704 /*or_comma=*/false,
30705 /*consume_paren=*/true);
30707 if (num == error_mark_node)
30708 return list;
30709 num = fold_non_dependent_expr (num);
30710 if (!tree_fits_shwi_p (num)
30711 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
30712 || (n = tree_to_shwi (num)) <= 0
30713 || (int) n != n)
30715 error_at (loc, "collapse argument needs positive constant integer expression");
30716 return list;
30719 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
30720 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
30721 OMP_CLAUSE_CHAIN (c) = list;
30722 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
30724 return c;
30727 /* OpenMP 2.5:
30728 default ( shared | none )
30730 OpenACC 2.0
30731 default (none) */
30733 static tree
30734 cp_parser_omp_clause_default (cp_parser *parser, tree list,
30735 location_t location, bool is_oacc)
30737 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
30738 tree c;
30740 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30741 return list;
30742 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30744 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30745 const char *p = IDENTIFIER_POINTER (id);
30747 switch (p[0])
30749 case 'n':
30750 if (strcmp ("none", p) != 0)
30751 goto invalid_kind;
30752 kind = OMP_CLAUSE_DEFAULT_NONE;
30753 break;
30755 case 's':
30756 if (strcmp ("shared", p) != 0 || is_oacc)
30757 goto invalid_kind;
30758 kind = OMP_CLAUSE_DEFAULT_SHARED;
30759 break;
30761 default:
30762 goto invalid_kind;
30765 cp_lexer_consume_token (parser->lexer);
30767 else
30769 invalid_kind:
30770 if (is_oacc)
30771 cp_parser_error (parser, "expected %<none%>");
30772 else
30773 cp_parser_error (parser, "expected %<none%> or %<shared%>");
30776 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30777 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30778 /*or_comma=*/false,
30779 /*consume_paren=*/true);
30781 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
30782 return list;
30784 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
30785 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
30786 OMP_CLAUSE_CHAIN (c) = list;
30787 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
30789 return c;
30792 /* OpenMP 3.1:
30793 final ( expression ) */
30795 static tree
30796 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
30798 tree t, c;
30800 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30801 return list;
30803 t = cp_parser_condition (parser);
30805 if (t == error_mark_node
30806 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30807 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30808 /*or_comma=*/false,
30809 /*consume_paren=*/true);
30811 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
30813 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
30814 OMP_CLAUSE_FINAL_EXPR (c) = t;
30815 OMP_CLAUSE_CHAIN (c) = list;
30817 return c;
30820 /* OpenMP 2.5:
30821 if ( expression )
30823 OpenMP 4.5:
30824 if ( directive-name-modifier : expression )
30826 directive-name-modifier:
30827 parallel | task | taskloop | target data | target | target update
30828 | target enter data | target exit data */
30830 static tree
30831 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
30832 bool is_omp)
30834 tree t, c;
30835 enum tree_code if_modifier = ERROR_MARK;
30837 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30838 return list;
30840 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30842 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30843 const char *p = IDENTIFIER_POINTER (id);
30844 int n = 2;
30846 if (strcmp ("parallel", p) == 0)
30847 if_modifier = OMP_PARALLEL;
30848 else if (strcmp ("task", p) == 0)
30849 if_modifier = OMP_TASK;
30850 else if (strcmp ("taskloop", p) == 0)
30851 if_modifier = OMP_TASKLOOP;
30852 else if (strcmp ("target", p) == 0)
30854 if_modifier = OMP_TARGET;
30855 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
30857 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
30858 p = IDENTIFIER_POINTER (id);
30859 if (strcmp ("data", p) == 0)
30860 if_modifier = OMP_TARGET_DATA;
30861 else if (strcmp ("update", p) == 0)
30862 if_modifier = OMP_TARGET_UPDATE;
30863 else if (strcmp ("enter", p) == 0)
30864 if_modifier = OMP_TARGET_ENTER_DATA;
30865 else if (strcmp ("exit", p) == 0)
30866 if_modifier = OMP_TARGET_EXIT_DATA;
30867 if (if_modifier != OMP_TARGET)
30868 n = 3;
30869 else
30871 location_t loc
30872 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
30873 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
30874 "or %<exit%>");
30875 if_modifier = ERROR_MARK;
30877 if (if_modifier == OMP_TARGET_ENTER_DATA
30878 || if_modifier == OMP_TARGET_EXIT_DATA)
30880 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
30882 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
30883 p = IDENTIFIER_POINTER (id);
30884 if (strcmp ("data", p) == 0)
30885 n = 4;
30887 if (n != 4)
30889 location_t loc
30890 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
30891 error_at (loc, "expected %<data%>");
30892 if_modifier = ERROR_MARK;
30897 if (if_modifier != ERROR_MARK)
30899 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
30901 while (n-- > 0)
30902 cp_lexer_consume_token (parser->lexer);
30904 else
30906 if (n > 2)
30908 location_t loc
30909 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
30910 error_at (loc, "expected %<:%>");
30912 if_modifier = ERROR_MARK;
30917 t = cp_parser_condition (parser);
30919 if (t == error_mark_node
30920 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30921 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30922 /*or_comma=*/false,
30923 /*consume_paren=*/true);
30925 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
30926 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
30928 if (if_modifier != ERROR_MARK
30929 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30931 const char *p = NULL;
30932 switch (if_modifier)
30934 case OMP_PARALLEL: p = "parallel"; break;
30935 case OMP_TASK: p = "task"; break;
30936 case OMP_TASKLOOP: p = "taskloop"; break;
30937 case OMP_TARGET_DATA: p = "target data"; break;
30938 case OMP_TARGET: p = "target"; break;
30939 case OMP_TARGET_UPDATE: p = "target update"; break;
30940 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
30941 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
30942 default: gcc_unreachable ();
30944 error_at (location, "too many %<if%> clauses with %qs modifier",
30946 return list;
30948 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30950 if (!is_omp)
30951 error_at (location, "too many %<if%> clauses");
30952 else
30953 error_at (location, "too many %<if%> clauses without modifier");
30954 return list;
30956 else if (if_modifier == ERROR_MARK
30957 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
30959 error_at (location, "if any %<if%> clause has modifier, then all "
30960 "%<if%> clauses have to use modifier");
30961 return list;
30965 c = build_omp_clause (location, OMP_CLAUSE_IF);
30966 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
30967 OMP_CLAUSE_IF_EXPR (c) = t;
30968 OMP_CLAUSE_CHAIN (c) = list;
30970 return c;
30973 /* OpenMP 3.1:
30974 mergeable */
30976 static tree
30977 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
30978 tree list, location_t location)
30980 tree c;
30982 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
30983 location);
30985 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
30986 OMP_CLAUSE_CHAIN (c) = list;
30987 return c;
30990 /* OpenMP 2.5:
30991 nowait */
30993 static tree
30994 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
30995 tree list, location_t location)
30997 tree c;
30999 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
31001 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
31002 OMP_CLAUSE_CHAIN (c) = list;
31003 return c;
31006 /* OpenMP 2.5:
31007 num_threads ( expression ) */
31009 static tree
31010 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
31011 location_t location)
31013 tree t, c;
31015 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31016 return list;
31018 t = cp_parser_expression (parser);
31020 if (t == error_mark_node
31021 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31022 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31023 /*or_comma=*/false,
31024 /*consume_paren=*/true);
31026 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
31027 "num_threads", location);
31029 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
31030 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
31031 OMP_CLAUSE_CHAIN (c) = list;
31033 return c;
31036 /* OpenMP 4.5:
31037 num_tasks ( expression ) */
31039 static tree
31040 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
31041 location_t location)
31043 tree t, c;
31045 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31046 return list;
31048 t = cp_parser_expression (parser);
31050 if (t == error_mark_node
31051 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31052 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31053 /*or_comma=*/false,
31054 /*consume_paren=*/true);
31056 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
31057 "num_tasks", location);
31059 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
31060 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
31061 OMP_CLAUSE_CHAIN (c) = list;
31063 return c;
31066 /* OpenMP 4.5:
31067 grainsize ( expression ) */
31069 static tree
31070 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
31071 location_t location)
31073 tree t, c;
31075 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31076 return list;
31078 t = cp_parser_expression (parser);
31080 if (t == error_mark_node
31081 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31082 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31083 /*or_comma=*/false,
31084 /*consume_paren=*/true);
31086 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
31087 "grainsize", location);
31089 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
31090 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
31091 OMP_CLAUSE_CHAIN (c) = list;
31093 return c;
31096 /* OpenMP 4.5:
31097 priority ( expression ) */
31099 static tree
31100 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
31101 location_t location)
31103 tree t, c;
31105 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31106 return list;
31108 t = cp_parser_expression (parser);
31110 if (t == error_mark_node
31111 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31112 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31113 /*or_comma=*/false,
31114 /*consume_paren=*/true);
31116 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
31117 "priority", location);
31119 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
31120 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
31121 OMP_CLAUSE_CHAIN (c) = list;
31123 return c;
31126 /* OpenMP 4.5:
31127 hint ( expression ) */
31129 static tree
31130 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
31131 location_t location)
31133 tree t, c;
31135 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31136 return list;
31138 t = cp_parser_expression (parser);
31140 if (t == error_mark_node
31141 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31142 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31143 /*or_comma=*/false,
31144 /*consume_paren=*/true);
31146 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
31148 c = build_omp_clause (location, OMP_CLAUSE_HINT);
31149 OMP_CLAUSE_HINT_EXPR (c) = t;
31150 OMP_CLAUSE_CHAIN (c) = list;
31152 return c;
31155 /* OpenMP 4.5:
31156 defaultmap ( tofrom : scalar ) */
31158 static tree
31159 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
31160 location_t location)
31162 tree c, id;
31163 const char *p;
31165 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31166 return list;
31168 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31170 cp_parser_error (parser, "expected %<tofrom%>");
31171 goto out_err;
31173 id = cp_lexer_peek_token (parser->lexer)->u.value;
31174 p = IDENTIFIER_POINTER (id);
31175 if (strcmp (p, "tofrom") != 0)
31177 cp_parser_error (parser, "expected %<tofrom%>");
31178 goto out_err;
31180 cp_lexer_consume_token (parser->lexer);
31181 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31182 goto out_err;
31184 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31186 cp_parser_error (parser, "expected %<scalar%>");
31187 goto out_err;
31189 id = cp_lexer_peek_token (parser->lexer)->u.value;
31190 p = IDENTIFIER_POINTER (id);
31191 if (strcmp (p, "scalar") != 0)
31193 cp_parser_error (parser, "expected %<scalar%>");
31194 goto out_err;
31196 cp_lexer_consume_token (parser->lexer);
31197 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31198 goto out_err;
31200 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
31201 location);
31203 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
31204 OMP_CLAUSE_CHAIN (c) = list;
31205 return c;
31207 out_err:
31208 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31209 /*or_comma=*/false,
31210 /*consume_paren=*/true);
31211 return list;
31214 /* OpenMP 2.5:
31215 ordered
31217 OpenMP 4.5:
31218 ordered ( constant-expression ) */
31220 static tree
31221 cp_parser_omp_clause_ordered (cp_parser *parser,
31222 tree list, location_t location)
31224 tree c, num = NULL_TREE;
31225 HOST_WIDE_INT n;
31227 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
31228 "ordered", location);
31230 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31232 cp_lexer_consume_token (parser->lexer);
31234 num = cp_parser_constant_expression (parser);
31236 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31237 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31238 /*or_comma=*/false,
31239 /*consume_paren=*/true);
31241 if (num == error_mark_node)
31242 return list;
31243 num = fold_non_dependent_expr (num);
31244 if (!tree_fits_shwi_p (num)
31245 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31246 || (n = tree_to_shwi (num)) <= 0
31247 || (int) n != n)
31249 error_at (location,
31250 "ordered argument needs positive constant integer "
31251 "expression");
31252 return list;
31256 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
31257 OMP_CLAUSE_ORDERED_EXPR (c) = num;
31258 OMP_CLAUSE_CHAIN (c) = list;
31259 return c;
31262 /* OpenMP 2.5:
31263 reduction ( reduction-operator : variable-list )
31265 reduction-operator:
31266 One of: + * - & ^ | && ||
31268 OpenMP 3.1:
31270 reduction-operator:
31271 One of: + * - & ^ | && || min max
31273 OpenMP 4.0:
31275 reduction-operator:
31276 One of: + * - & ^ | && ||
31277 id-expression */
31279 static tree
31280 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
31282 enum tree_code code = ERROR_MARK;
31283 tree nlist, c, id = NULL_TREE;
31285 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31286 return list;
31288 switch (cp_lexer_peek_token (parser->lexer)->type)
31290 case CPP_PLUS: code = PLUS_EXPR; break;
31291 case CPP_MULT: code = MULT_EXPR; break;
31292 case CPP_MINUS: code = MINUS_EXPR; break;
31293 case CPP_AND: code = BIT_AND_EXPR; break;
31294 case CPP_XOR: code = BIT_XOR_EXPR; break;
31295 case CPP_OR: code = BIT_IOR_EXPR; break;
31296 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
31297 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
31298 default: break;
31301 if (code != ERROR_MARK)
31302 cp_lexer_consume_token (parser->lexer);
31303 else
31305 bool saved_colon_corrects_to_scope_p;
31306 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31307 parser->colon_corrects_to_scope_p = false;
31308 id = cp_parser_id_expression (parser, /*template_p=*/false,
31309 /*check_dependency_p=*/true,
31310 /*template_p=*/NULL,
31311 /*declarator_p=*/false,
31312 /*optional_p=*/false);
31313 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31314 if (identifier_p (id))
31316 const char *p = IDENTIFIER_POINTER (id);
31318 if (strcmp (p, "min") == 0)
31319 code = MIN_EXPR;
31320 else if (strcmp (p, "max") == 0)
31321 code = MAX_EXPR;
31322 else if (id == ansi_opname (PLUS_EXPR))
31323 code = PLUS_EXPR;
31324 else if (id == ansi_opname (MULT_EXPR))
31325 code = MULT_EXPR;
31326 else if (id == ansi_opname (MINUS_EXPR))
31327 code = MINUS_EXPR;
31328 else if (id == ansi_opname (BIT_AND_EXPR))
31329 code = BIT_AND_EXPR;
31330 else if (id == ansi_opname (BIT_IOR_EXPR))
31331 code = BIT_IOR_EXPR;
31332 else if (id == ansi_opname (BIT_XOR_EXPR))
31333 code = BIT_XOR_EXPR;
31334 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
31335 code = TRUTH_ANDIF_EXPR;
31336 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
31337 code = TRUTH_ORIF_EXPR;
31338 id = omp_reduction_id (code, id, NULL_TREE);
31339 tree scope = parser->scope;
31340 if (scope)
31341 id = build_qualified_name (NULL_TREE, scope, id, false);
31342 parser->scope = NULL_TREE;
31343 parser->qualifying_scope = NULL_TREE;
31344 parser->object_scope = NULL_TREE;
31346 else
31348 error ("invalid reduction-identifier");
31349 resync_fail:
31350 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31351 /*or_comma=*/false,
31352 /*consume_paren=*/true);
31353 return list;
31357 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31358 goto resync_fail;
31360 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
31361 NULL);
31362 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31364 OMP_CLAUSE_REDUCTION_CODE (c) = code;
31365 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
31368 return nlist;
31371 /* OpenMP 2.5:
31372 schedule ( schedule-kind )
31373 schedule ( schedule-kind , expression )
31375 schedule-kind:
31376 static | dynamic | guided | runtime | auto
31378 OpenMP 4.5:
31379 schedule ( schedule-modifier : schedule-kind )
31380 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
31382 schedule-modifier:
31383 simd
31384 monotonic
31385 nonmonotonic */
31387 static tree
31388 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
31390 tree c, t;
31391 int modifiers = 0, nmodifiers = 0;
31393 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31394 return list;
31396 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
31398 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31400 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31401 const char *p = IDENTIFIER_POINTER (id);
31402 if (strcmp ("simd", p) == 0)
31403 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
31404 else if (strcmp ("monotonic", p) == 0)
31405 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
31406 else if (strcmp ("nonmonotonic", p) == 0)
31407 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
31408 else
31409 break;
31410 cp_lexer_consume_token (parser->lexer);
31411 if (nmodifiers++ == 0
31412 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31413 cp_lexer_consume_token (parser->lexer);
31414 else
31416 cp_parser_require (parser, CPP_COLON, RT_COLON);
31417 break;
31421 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31423 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31424 const char *p = IDENTIFIER_POINTER (id);
31426 switch (p[0])
31428 case 'd':
31429 if (strcmp ("dynamic", p) != 0)
31430 goto invalid_kind;
31431 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
31432 break;
31434 case 'g':
31435 if (strcmp ("guided", p) != 0)
31436 goto invalid_kind;
31437 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
31438 break;
31440 case 'r':
31441 if (strcmp ("runtime", p) != 0)
31442 goto invalid_kind;
31443 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
31444 break;
31446 default:
31447 goto invalid_kind;
31450 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
31451 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
31452 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31453 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
31454 else
31455 goto invalid_kind;
31456 cp_lexer_consume_token (parser->lexer);
31458 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
31459 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31460 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
31461 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31463 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
31464 "specified");
31465 modifiers = 0;
31468 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31470 cp_token *token;
31471 cp_lexer_consume_token (parser->lexer);
31473 token = cp_lexer_peek_token (parser->lexer);
31474 t = cp_parser_assignment_expression (parser);
31476 if (t == error_mark_node)
31477 goto resync_fail;
31478 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
31479 error_at (token->location, "schedule %<runtime%> does not take "
31480 "a %<chunk_size%> parameter");
31481 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
31482 error_at (token->location, "schedule %<auto%> does not take "
31483 "a %<chunk_size%> parameter");
31484 else
31485 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
31487 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31488 goto resync_fail;
31490 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31491 goto resync_fail;
31493 OMP_CLAUSE_SCHEDULE_KIND (c)
31494 = (enum omp_clause_schedule_kind)
31495 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
31497 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
31498 OMP_CLAUSE_CHAIN (c) = list;
31499 return c;
31501 invalid_kind:
31502 cp_parser_error (parser, "invalid schedule kind");
31503 resync_fail:
31504 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31505 /*or_comma=*/false,
31506 /*consume_paren=*/true);
31507 return list;
31510 /* OpenMP 3.0:
31511 untied */
31513 static tree
31514 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
31515 tree list, location_t location)
31517 tree c;
31519 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
31521 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
31522 OMP_CLAUSE_CHAIN (c) = list;
31523 return c;
31526 /* OpenMP 4.0:
31527 inbranch
31528 notinbranch */
31530 static tree
31531 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
31532 tree list, location_t location)
31534 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31535 tree c = build_omp_clause (location, code);
31536 OMP_CLAUSE_CHAIN (c) = list;
31537 return c;
31540 /* OpenMP 4.0:
31541 parallel
31543 sections
31544 taskgroup */
31546 static tree
31547 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
31548 enum omp_clause_code code,
31549 tree list, location_t location)
31551 tree c = build_omp_clause (location, code);
31552 OMP_CLAUSE_CHAIN (c) = list;
31553 return c;
31556 /* OpenMP 4.5:
31557 nogroup */
31559 static tree
31560 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
31561 tree list, location_t location)
31563 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
31564 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
31565 OMP_CLAUSE_CHAIN (c) = list;
31566 return c;
31569 /* OpenMP 4.5:
31570 simd
31571 threads */
31573 static tree
31574 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
31575 enum omp_clause_code code,
31576 tree list, location_t location)
31578 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31579 tree c = build_omp_clause (location, code);
31580 OMP_CLAUSE_CHAIN (c) = list;
31581 return c;
31584 /* OpenMP 4.0:
31585 num_teams ( expression ) */
31587 static tree
31588 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
31589 location_t location)
31591 tree t, c;
31593 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31594 return list;
31596 t = cp_parser_expression (parser);
31598 if (t == error_mark_node
31599 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31600 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31601 /*or_comma=*/false,
31602 /*consume_paren=*/true);
31604 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
31605 "num_teams", location);
31607 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
31608 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
31609 OMP_CLAUSE_CHAIN (c) = list;
31611 return c;
31614 /* OpenMP 4.0:
31615 thread_limit ( expression ) */
31617 static tree
31618 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
31619 location_t location)
31621 tree t, c;
31623 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31624 return list;
31626 t = cp_parser_expression (parser);
31628 if (t == error_mark_node
31629 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31630 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31631 /*or_comma=*/false,
31632 /*consume_paren=*/true);
31634 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
31635 "thread_limit", location);
31637 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
31638 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
31639 OMP_CLAUSE_CHAIN (c) = list;
31641 return c;
31644 /* OpenMP 4.0:
31645 aligned ( variable-list )
31646 aligned ( variable-list : constant-expression ) */
31648 static tree
31649 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
31651 tree nlist, c, alignment = NULL_TREE;
31652 bool colon;
31654 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31655 return list;
31657 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
31658 &colon);
31660 if (colon)
31662 alignment = cp_parser_constant_expression (parser);
31664 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31665 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31666 /*or_comma=*/false,
31667 /*consume_paren=*/true);
31669 if (alignment == error_mark_node)
31670 alignment = NULL_TREE;
31673 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31674 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
31676 return nlist;
31679 /* OpenMP 4.0:
31680 linear ( variable-list )
31681 linear ( variable-list : expression )
31683 OpenMP 4.5:
31684 linear ( modifier ( variable-list ) )
31685 linear ( modifier ( variable-list ) : expression ) */
31687 static tree
31688 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
31689 bool is_cilk_simd_fn, bool declare_simd)
31691 tree nlist, c, step = integer_one_node;
31692 bool colon;
31693 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
31695 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31696 return list;
31698 if (!is_cilk_simd_fn
31699 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31701 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31702 const char *p = IDENTIFIER_POINTER (id);
31704 if (strcmp ("ref", p) == 0)
31705 kind = OMP_CLAUSE_LINEAR_REF;
31706 else if (strcmp ("val", p) == 0)
31707 kind = OMP_CLAUSE_LINEAR_VAL;
31708 else if (strcmp ("uval", p) == 0)
31709 kind = OMP_CLAUSE_LINEAR_UVAL;
31710 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
31711 cp_lexer_consume_token (parser->lexer);
31712 else
31713 kind = OMP_CLAUSE_LINEAR_DEFAULT;
31716 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
31717 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
31718 &colon);
31719 else
31721 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
31722 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
31723 if (colon)
31724 cp_parser_require (parser, CPP_COLON, RT_COLON);
31725 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31726 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31727 /*or_comma=*/false,
31728 /*consume_paren=*/true);
31731 if (colon)
31733 step = NULL_TREE;
31734 if (declare_simd
31735 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31736 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
31738 cp_token *token = cp_lexer_peek_token (parser->lexer);
31739 cp_parser_parse_tentatively (parser);
31740 step = cp_parser_id_expression (parser, /*template_p=*/false,
31741 /*check_dependency_p=*/true,
31742 /*template_p=*/NULL,
31743 /*declarator_p=*/false,
31744 /*optional_p=*/false);
31745 if (step != error_mark_node)
31746 step = cp_parser_lookup_name_simple (parser, step, token->location);
31747 if (step == error_mark_node)
31749 step = NULL_TREE;
31750 cp_parser_abort_tentative_parse (parser);
31752 else if (!cp_parser_parse_definitely (parser))
31753 step = NULL_TREE;
31755 if (!step)
31756 step = cp_parser_expression (parser);
31758 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
31760 sorry ("using parameters for %<linear%> step is not supported yet");
31761 step = integer_one_node;
31763 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31764 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31765 /*or_comma=*/false,
31766 /*consume_paren=*/true);
31768 if (step == error_mark_node)
31769 return list;
31772 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31774 OMP_CLAUSE_LINEAR_STEP (c) = step;
31775 OMP_CLAUSE_LINEAR_KIND (c) = kind;
31778 return nlist;
31781 /* OpenMP 4.0:
31782 safelen ( constant-expression ) */
31784 static tree
31785 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
31786 location_t location)
31788 tree t, c;
31790 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31791 return list;
31793 t = cp_parser_constant_expression (parser);
31795 if (t == error_mark_node
31796 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31797 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31798 /*or_comma=*/false,
31799 /*consume_paren=*/true);
31801 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
31803 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
31804 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
31805 OMP_CLAUSE_CHAIN (c) = list;
31807 return c;
31810 /* OpenMP 4.0:
31811 simdlen ( constant-expression ) */
31813 static tree
31814 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
31815 location_t location)
31817 tree t, c;
31819 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31820 return list;
31822 t = cp_parser_constant_expression (parser);
31824 if (t == error_mark_node
31825 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31826 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31827 /*or_comma=*/false,
31828 /*consume_paren=*/true);
31830 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
31832 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
31833 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
31834 OMP_CLAUSE_CHAIN (c) = list;
31836 return c;
31839 /* OpenMP 4.5:
31840 vec:
31841 identifier [+/- integer]
31842 vec , identifier [+/- integer]
31845 static tree
31846 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
31847 tree list)
31849 tree vec = NULL;
31851 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31853 cp_parser_error (parser, "expected identifier");
31854 return list;
31857 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31859 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
31860 tree t, identifier = cp_parser_identifier (parser);
31861 tree addend = NULL;
31863 if (identifier == error_mark_node)
31864 t = error_mark_node;
31865 else
31867 t = cp_parser_lookup_name_simple
31868 (parser, identifier,
31869 cp_lexer_peek_token (parser->lexer)->location);
31870 if (t == error_mark_node)
31871 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
31872 id_loc);
31875 bool neg = false;
31876 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
31877 neg = true;
31878 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
31880 addend = integer_zero_node;
31881 goto add_to_vector;
31883 cp_lexer_consume_token (parser->lexer);
31885 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
31887 cp_parser_error (parser, "expected integer");
31888 return list;
31891 addend = cp_lexer_peek_token (parser->lexer)->u.value;
31892 if (TREE_CODE (addend) != INTEGER_CST)
31894 cp_parser_error (parser, "expected integer");
31895 return list;
31897 cp_lexer_consume_token (parser->lexer);
31899 add_to_vector:
31900 if (t != error_mark_node)
31902 vec = tree_cons (addend, t, vec);
31903 if (neg)
31904 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
31907 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31908 break;
31910 cp_lexer_consume_token (parser->lexer);
31913 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
31915 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
31916 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
31917 OMP_CLAUSE_DECL (u) = nreverse (vec);
31918 OMP_CLAUSE_CHAIN (u) = list;
31919 return u;
31921 return list;
31924 /* OpenMP 4.0:
31925 depend ( depend-kind : variable-list )
31927 depend-kind:
31928 in | out | inout
31930 OpenMP 4.5:
31931 depend ( source )
31933 depend ( sink : vec ) */
31935 static tree
31936 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
31938 tree nlist, c;
31939 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
31941 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31942 return list;
31944 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31946 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31947 const char *p = IDENTIFIER_POINTER (id);
31949 if (strcmp ("in", p) == 0)
31950 kind = OMP_CLAUSE_DEPEND_IN;
31951 else if (strcmp ("inout", p) == 0)
31952 kind = OMP_CLAUSE_DEPEND_INOUT;
31953 else if (strcmp ("out", p) == 0)
31954 kind = OMP_CLAUSE_DEPEND_OUT;
31955 else if (strcmp ("source", p) == 0)
31956 kind = OMP_CLAUSE_DEPEND_SOURCE;
31957 else if (strcmp ("sink", p) == 0)
31958 kind = OMP_CLAUSE_DEPEND_SINK;
31959 else
31960 goto invalid_kind;
31962 else
31963 goto invalid_kind;
31965 cp_lexer_consume_token (parser->lexer);
31967 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
31969 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
31970 OMP_CLAUSE_DEPEND_KIND (c) = kind;
31971 OMP_CLAUSE_DECL (c) = NULL_TREE;
31972 OMP_CLAUSE_CHAIN (c) = list;
31973 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31974 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31975 /*or_comma=*/false,
31976 /*consume_paren=*/true);
31977 return c;
31980 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31981 goto resync_fail;
31983 if (kind == OMP_CLAUSE_DEPEND_SINK)
31984 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
31985 else
31987 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
31988 list, NULL);
31990 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31991 OMP_CLAUSE_DEPEND_KIND (c) = kind;
31993 return nlist;
31995 invalid_kind:
31996 cp_parser_error (parser, "invalid depend kind");
31997 resync_fail:
31998 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31999 /*or_comma=*/false,
32000 /*consume_paren=*/true);
32001 return list;
32004 /* OpenMP 4.0:
32005 map ( map-kind : variable-list )
32006 map ( variable-list )
32008 map-kind:
32009 alloc | to | from | tofrom
32011 OpenMP 4.5:
32012 map-kind:
32013 alloc | to | from | tofrom | release | delete
32015 map ( always [,] map-kind: variable-list ) */
32017 static tree
32018 cp_parser_omp_clause_map (cp_parser *parser, tree list)
32020 tree nlist, c;
32021 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
32022 bool always = false;
32024 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32025 return list;
32027 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32029 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32030 const char *p = IDENTIFIER_POINTER (id);
32032 if (strcmp ("always", p) == 0)
32034 int nth = 2;
32035 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
32036 nth++;
32037 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
32038 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
32039 == RID_DELETE))
32040 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
32041 == CPP_COLON))
32043 always = true;
32044 cp_lexer_consume_token (parser->lexer);
32045 if (nth == 3)
32046 cp_lexer_consume_token (parser->lexer);
32051 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32052 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32054 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32055 const char *p = IDENTIFIER_POINTER (id);
32057 if (strcmp ("alloc", p) == 0)
32058 kind = GOMP_MAP_ALLOC;
32059 else if (strcmp ("to", p) == 0)
32060 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
32061 else if (strcmp ("from", p) == 0)
32062 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
32063 else if (strcmp ("tofrom", p) == 0)
32064 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
32065 else if (strcmp ("release", p) == 0)
32066 kind = GOMP_MAP_RELEASE;
32067 else
32069 cp_parser_error (parser, "invalid map kind");
32070 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32071 /*or_comma=*/false,
32072 /*consume_paren=*/true);
32073 return list;
32075 cp_lexer_consume_token (parser->lexer);
32076 cp_lexer_consume_token (parser->lexer);
32078 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
32079 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32081 kind = GOMP_MAP_DELETE;
32082 cp_lexer_consume_token (parser->lexer);
32083 cp_lexer_consume_token (parser->lexer);
32086 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
32087 NULL);
32089 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32090 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32092 return nlist;
32095 /* OpenMP 4.0:
32096 device ( expression ) */
32098 static tree
32099 cp_parser_omp_clause_device (cp_parser *parser, tree list,
32100 location_t location)
32102 tree t, c;
32104 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32105 return list;
32107 t = cp_parser_expression (parser);
32109 if (t == error_mark_node
32110 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32111 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32112 /*or_comma=*/false,
32113 /*consume_paren=*/true);
32115 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
32116 "device", location);
32118 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
32119 OMP_CLAUSE_DEVICE_ID (c) = t;
32120 OMP_CLAUSE_CHAIN (c) = list;
32122 return c;
32125 /* OpenMP 4.0:
32126 dist_schedule ( static )
32127 dist_schedule ( static , expression ) */
32129 static tree
32130 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
32131 location_t location)
32133 tree c, t;
32135 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32136 return list;
32138 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
32140 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32141 goto invalid_kind;
32142 cp_lexer_consume_token (parser->lexer);
32144 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32146 cp_lexer_consume_token (parser->lexer);
32148 t = cp_parser_assignment_expression (parser);
32150 if (t == error_mark_node)
32151 goto resync_fail;
32152 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
32154 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32155 goto resync_fail;
32157 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32158 goto resync_fail;
32160 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
32161 location);
32162 OMP_CLAUSE_CHAIN (c) = list;
32163 return c;
32165 invalid_kind:
32166 cp_parser_error (parser, "invalid dist_schedule kind");
32167 resync_fail:
32168 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32169 /*or_comma=*/false,
32170 /*consume_paren=*/true);
32171 return list;
32174 /* OpenMP 4.0:
32175 proc_bind ( proc-bind-kind )
32177 proc-bind-kind:
32178 master | close | spread */
32180 static tree
32181 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
32182 location_t location)
32184 tree c;
32185 enum omp_clause_proc_bind_kind kind;
32187 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32188 return list;
32190 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32192 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32193 const char *p = IDENTIFIER_POINTER (id);
32195 if (strcmp ("master", p) == 0)
32196 kind = OMP_CLAUSE_PROC_BIND_MASTER;
32197 else if (strcmp ("close", p) == 0)
32198 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
32199 else if (strcmp ("spread", p) == 0)
32200 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
32201 else
32202 goto invalid_kind;
32204 else
32205 goto invalid_kind;
32207 cp_lexer_consume_token (parser->lexer);
32208 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32209 goto resync_fail;
32211 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
32212 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
32213 location);
32214 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
32215 OMP_CLAUSE_CHAIN (c) = list;
32216 return c;
32218 invalid_kind:
32219 cp_parser_error (parser, "invalid depend kind");
32220 resync_fail:
32221 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32222 /*or_comma=*/false,
32223 /*consume_paren=*/true);
32224 return list;
32227 /* OpenACC:
32228 async [( int-expr )] */
32230 static tree
32231 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
32233 tree c, t;
32234 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32236 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
32238 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32240 cp_lexer_consume_token (parser->lexer);
32242 t = cp_parser_expression (parser);
32243 if (t == error_mark_node
32244 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32245 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32246 /*or_comma=*/false,
32247 /*consume_paren=*/true);
32250 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
32252 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
32253 OMP_CLAUSE_ASYNC_EXPR (c) = t;
32254 OMP_CLAUSE_CHAIN (c) = list;
32255 list = c;
32257 return list;
32260 /* Parse all OpenACC clauses. The set clauses allowed by the directive
32261 is a bitmask in MASK. Return the list of clauses found. */
32263 static tree
32264 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
32265 const char *where, cp_token *pragma_tok,
32266 bool finish_p = true)
32268 tree clauses = NULL;
32269 bool first = true;
32271 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32273 location_t here;
32274 pragma_omp_clause c_kind;
32275 omp_clause_code code;
32276 const char *c_name;
32277 tree prev = clauses;
32279 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32280 cp_lexer_consume_token (parser->lexer);
32282 here = cp_lexer_peek_token (parser->lexer)->location;
32283 c_kind = cp_parser_omp_clause_name (parser);
32285 switch (c_kind)
32287 case PRAGMA_OACC_CLAUSE_ASYNC:
32288 clauses = cp_parser_oacc_clause_async (parser, clauses);
32289 c_name = "async";
32290 break;
32291 case PRAGMA_OACC_CLAUSE_AUTO:
32292 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
32293 clauses, here);
32294 c_name = "auto";
32295 break;
32296 case PRAGMA_OACC_CLAUSE_COLLAPSE:
32297 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
32298 c_name = "collapse";
32299 break;
32300 case PRAGMA_OACC_CLAUSE_COPY:
32301 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32302 c_name = "copy";
32303 break;
32304 case PRAGMA_OACC_CLAUSE_COPYIN:
32305 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32306 c_name = "copyin";
32307 break;
32308 case PRAGMA_OACC_CLAUSE_COPYOUT:
32309 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32310 c_name = "copyout";
32311 break;
32312 case PRAGMA_OACC_CLAUSE_CREATE:
32313 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32314 c_name = "create";
32315 break;
32316 case PRAGMA_OACC_CLAUSE_DELETE:
32317 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32318 c_name = "delete";
32319 break;
32320 case PRAGMA_OMP_CLAUSE_DEFAULT:
32321 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
32322 c_name = "default";
32323 break;
32324 case PRAGMA_OACC_CLAUSE_DEVICE:
32325 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32326 c_name = "device";
32327 break;
32328 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
32329 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
32330 c_name = "deviceptr";
32331 break;
32332 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32333 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32334 c_name = "device_resident";
32335 break;
32336 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
32337 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32338 clauses);
32339 c_name = "firstprivate";
32340 break;
32341 case PRAGMA_OACC_CLAUSE_GANG:
32342 c_name = "gang";
32343 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
32344 c_name, clauses);
32345 break;
32346 case PRAGMA_OACC_CLAUSE_HOST:
32347 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32348 c_name = "host";
32349 break;
32350 case PRAGMA_OACC_CLAUSE_IF:
32351 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
32352 c_name = "if";
32353 break;
32354 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
32355 clauses = cp_parser_oacc_simple_clause (parser,
32356 OMP_CLAUSE_INDEPENDENT,
32357 clauses, here);
32358 c_name = "independent";
32359 break;
32360 case PRAGMA_OACC_CLAUSE_LINK:
32361 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32362 c_name = "link";
32363 break;
32364 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
32365 code = OMP_CLAUSE_NUM_GANGS;
32366 c_name = "num_gangs";
32367 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32368 clauses);
32369 break;
32370 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
32371 c_name = "num_workers";
32372 code = OMP_CLAUSE_NUM_WORKERS;
32373 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32374 clauses);
32375 break;
32376 case PRAGMA_OACC_CLAUSE_PRESENT:
32377 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32378 c_name = "present";
32379 break;
32380 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
32381 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32382 c_name = "present_or_copy";
32383 break;
32384 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
32385 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32386 c_name = "present_or_copyin";
32387 break;
32388 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
32389 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32390 c_name = "present_or_copyout";
32391 break;
32392 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
32393 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32394 c_name = "present_or_create";
32395 break;
32396 case PRAGMA_OACC_CLAUSE_PRIVATE:
32397 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32398 clauses);
32399 c_name = "private";
32400 break;
32401 case PRAGMA_OACC_CLAUSE_REDUCTION:
32402 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32403 c_name = "reduction";
32404 break;
32405 case PRAGMA_OACC_CLAUSE_SELF:
32406 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32407 c_name = "self";
32408 break;
32409 case PRAGMA_OACC_CLAUSE_SEQ:
32410 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
32411 clauses, here);
32412 c_name = "seq";
32413 break;
32414 case PRAGMA_OACC_CLAUSE_TILE:
32415 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
32416 c_name = "tile";
32417 break;
32418 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
32419 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32420 clauses);
32421 c_name = "use_device";
32422 break;
32423 case PRAGMA_OACC_CLAUSE_VECTOR:
32424 c_name = "vector";
32425 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
32426 c_name, clauses);
32427 break;
32428 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
32429 c_name = "vector_length";
32430 code = OMP_CLAUSE_VECTOR_LENGTH;
32431 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32432 clauses);
32433 break;
32434 case PRAGMA_OACC_CLAUSE_WAIT:
32435 clauses = cp_parser_oacc_clause_wait (parser, clauses);
32436 c_name = "wait";
32437 break;
32438 case PRAGMA_OACC_CLAUSE_WORKER:
32439 c_name = "worker";
32440 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
32441 c_name, clauses);
32442 break;
32443 default:
32444 cp_parser_error (parser, "expected %<#pragma acc%> clause");
32445 goto saw_error;
32448 first = false;
32450 if (((mask >> c_kind) & 1) == 0)
32452 /* Remove the invalid clause(s) from the list to avoid
32453 confusing the rest of the compiler. */
32454 clauses = prev;
32455 error_at (here, "%qs is not valid for %qs", c_name, where);
32459 saw_error:
32460 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32462 if (finish_p)
32463 return finish_omp_clauses (clauses, C_ORT_ACC);
32465 return clauses;
32468 /* Parse all OpenMP clauses. The set clauses allowed by the directive
32469 is a bitmask in MASK. Return the list of clauses found; the result
32470 of clause default goes in *pdefault. */
32472 static tree
32473 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
32474 const char *where, cp_token *pragma_tok,
32475 bool finish_p = true)
32477 tree clauses = NULL;
32478 bool first = true;
32479 cp_token *token = NULL;
32481 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32483 pragma_omp_clause c_kind;
32484 const char *c_name;
32485 tree prev = clauses;
32487 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32488 cp_lexer_consume_token (parser->lexer);
32490 token = cp_lexer_peek_token (parser->lexer);
32491 c_kind = cp_parser_omp_clause_name (parser);
32493 switch (c_kind)
32495 case PRAGMA_OMP_CLAUSE_COLLAPSE:
32496 clauses = cp_parser_omp_clause_collapse (parser, clauses,
32497 token->location);
32498 c_name = "collapse";
32499 break;
32500 case PRAGMA_OMP_CLAUSE_COPYIN:
32501 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
32502 c_name = "copyin";
32503 break;
32504 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
32505 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
32506 clauses);
32507 c_name = "copyprivate";
32508 break;
32509 case PRAGMA_OMP_CLAUSE_DEFAULT:
32510 clauses = cp_parser_omp_clause_default (parser, clauses,
32511 token->location, false);
32512 c_name = "default";
32513 break;
32514 case PRAGMA_OMP_CLAUSE_FINAL:
32515 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
32516 c_name = "final";
32517 break;
32518 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
32519 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32520 clauses);
32521 c_name = "firstprivate";
32522 break;
32523 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
32524 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
32525 token->location);
32526 c_name = "grainsize";
32527 break;
32528 case PRAGMA_OMP_CLAUSE_HINT:
32529 clauses = cp_parser_omp_clause_hint (parser, clauses,
32530 token->location);
32531 c_name = "hint";
32532 break;
32533 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
32534 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
32535 token->location);
32536 c_name = "defaultmap";
32537 break;
32538 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
32539 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32540 clauses);
32541 c_name = "use_device_ptr";
32542 break;
32543 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
32544 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
32545 clauses);
32546 c_name = "is_device_ptr";
32547 break;
32548 case PRAGMA_OMP_CLAUSE_IF:
32549 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
32550 true);
32551 c_name = "if";
32552 break;
32553 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
32554 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32555 clauses);
32556 c_name = "lastprivate";
32557 break;
32558 case PRAGMA_OMP_CLAUSE_MERGEABLE:
32559 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
32560 token->location);
32561 c_name = "mergeable";
32562 break;
32563 case PRAGMA_OMP_CLAUSE_NOWAIT:
32564 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
32565 c_name = "nowait";
32566 break;
32567 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
32568 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
32569 token->location);
32570 c_name = "num_tasks";
32571 break;
32572 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
32573 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
32574 token->location);
32575 c_name = "num_threads";
32576 break;
32577 case PRAGMA_OMP_CLAUSE_ORDERED:
32578 clauses = cp_parser_omp_clause_ordered (parser, clauses,
32579 token->location);
32580 c_name = "ordered";
32581 break;
32582 case PRAGMA_OMP_CLAUSE_PRIORITY:
32583 clauses = cp_parser_omp_clause_priority (parser, clauses,
32584 token->location);
32585 c_name = "priority";
32586 break;
32587 case PRAGMA_OMP_CLAUSE_PRIVATE:
32588 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32589 clauses);
32590 c_name = "private";
32591 break;
32592 case PRAGMA_OMP_CLAUSE_REDUCTION:
32593 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32594 c_name = "reduction";
32595 break;
32596 case PRAGMA_OMP_CLAUSE_SCHEDULE:
32597 clauses = cp_parser_omp_clause_schedule (parser, clauses,
32598 token->location);
32599 c_name = "schedule";
32600 break;
32601 case PRAGMA_OMP_CLAUSE_SHARED:
32602 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
32603 clauses);
32604 c_name = "shared";
32605 break;
32606 case PRAGMA_OMP_CLAUSE_UNTIED:
32607 clauses = cp_parser_omp_clause_untied (parser, clauses,
32608 token->location);
32609 c_name = "untied";
32610 break;
32611 case PRAGMA_OMP_CLAUSE_INBRANCH:
32612 case PRAGMA_CILK_CLAUSE_MASK:
32613 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
32614 clauses, token->location);
32615 c_name = "inbranch";
32616 break;
32617 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
32618 case PRAGMA_CILK_CLAUSE_NOMASK:
32619 clauses = cp_parser_omp_clause_branch (parser,
32620 OMP_CLAUSE_NOTINBRANCH,
32621 clauses, token->location);
32622 c_name = "notinbranch";
32623 break;
32624 case PRAGMA_OMP_CLAUSE_PARALLEL:
32625 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
32626 clauses, token->location);
32627 c_name = "parallel";
32628 if (!first)
32630 clause_not_first:
32631 error_at (token->location, "%qs must be the first clause of %qs",
32632 c_name, where);
32633 clauses = prev;
32635 break;
32636 case PRAGMA_OMP_CLAUSE_FOR:
32637 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
32638 clauses, token->location);
32639 c_name = "for";
32640 if (!first)
32641 goto clause_not_first;
32642 break;
32643 case PRAGMA_OMP_CLAUSE_SECTIONS:
32644 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
32645 clauses, token->location);
32646 c_name = "sections";
32647 if (!first)
32648 goto clause_not_first;
32649 break;
32650 case PRAGMA_OMP_CLAUSE_TASKGROUP:
32651 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
32652 clauses, token->location);
32653 c_name = "taskgroup";
32654 if (!first)
32655 goto clause_not_first;
32656 break;
32657 case PRAGMA_OMP_CLAUSE_LINK:
32658 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
32659 c_name = "to";
32660 break;
32661 case PRAGMA_OMP_CLAUSE_TO:
32662 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
32663 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
32664 clauses);
32665 else
32666 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
32667 c_name = "to";
32668 break;
32669 case PRAGMA_OMP_CLAUSE_FROM:
32670 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
32671 c_name = "from";
32672 break;
32673 case PRAGMA_OMP_CLAUSE_UNIFORM:
32674 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
32675 clauses);
32676 c_name = "uniform";
32677 break;
32678 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
32679 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
32680 token->location);
32681 c_name = "num_teams";
32682 break;
32683 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
32684 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
32685 token->location);
32686 c_name = "thread_limit";
32687 break;
32688 case PRAGMA_OMP_CLAUSE_ALIGNED:
32689 clauses = cp_parser_omp_clause_aligned (parser, clauses);
32690 c_name = "aligned";
32691 break;
32692 case PRAGMA_OMP_CLAUSE_LINEAR:
32694 bool cilk_simd_fn = false, declare_simd = false;
32695 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
32696 cilk_simd_fn = true;
32697 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
32698 declare_simd = true;
32699 clauses = cp_parser_omp_clause_linear (parser, clauses,
32700 cilk_simd_fn, declare_simd);
32702 c_name = "linear";
32703 break;
32704 case PRAGMA_OMP_CLAUSE_DEPEND:
32705 clauses = cp_parser_omp_clause_depend (parser, clauses,
32706 token->location);
32707 c_name = "depend";
32708 break;
32709 case PRAGMA_OMP_CLAUSE_MAP:
32710 clauses = cp_parser_omp_clause_map (parser, clauses);
32711 c_name = "map";
32712 break;
32713 case PRAGMA_OMP_CLAUSE_DEVICE:
32714 clauses = cp_parser_omp_clause_device (parser, clauses,
32715 token->location);
32716 c_name = "device";
32717 break;
32718 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
32719 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
32720 token->location);
32721 c_name = "dist_schedule";
32722 break;
32723 case PRAGMA_OMP_CLAUSE_PROC_BIND:
32724 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
32725 token->location);
32726 c_name = "proc_bind";
32727 break;
32728 case PRAGMA_OMP_CLAUSE_SAFELEN:
32729 clauses = cp_parser_omp_clause_safelen (parser, clauses,
32730 token->location);
32731 c_name = "safelen";
32732 break;
32733 case PRAGMA_OMP_CLAUSE_SIMDLEN:
32734 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
32735 token->location);
32736 c_name = "simdlen";
32737 break;
32738 case PRAGMA_OMP_CLAUSE_NOGROUP:
32739 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
32740 token->location);
32741 c_name = "nogroup";
32742 break;
32743 case PRAGMA_OMP_CLAUSE_THREADS:
32744 clauses
32745 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
32746 clauses, token->location);
32747 c_name = "threads";
32748 break;
32749 case PRAGMA_OMP_CLAUSE_SIMD:
32750 clauses
32751 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
32752 clauses, token->location);
32753 c_name = "simd";
32754 break;
32755 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
32756 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
32757 c_name = "simdlen";
32758 break;
32759 default:
32760 cp_parser_error (parser, "expected %<#pragma omp%> clause");
32761 goto saw_error;
32764 first = false;
32766 if (((mask >> c_kind) & 1) == 0)
32768 /* Remove the invalid clause(s) from the list to avoid
32769 confusing the rest of the compiler. */
32770 clauses = prev;
32771 error_at (token->location, "%qs is not valid for %qs", c_name, where);
32774 saw_error:
32775 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
32776 no reason to skip to the end. */
32777 if (!(flag_cilkplus && pragma_tok == NULL))
32778 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32779 if (finish_p)
32781 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
32782 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
32783 else
32784 return finish_omp_clauses (clauses, C_ORT_OMP);
32786 return clauses;
32789 /* OpenMP 2.5:
32790 structured-block:
32791 statement
32793 In practice, we're also interested in adding the statement to an
32794 outer node. So it is convenient if we work around the fact that
32795 cp_parser_statement calls add_stmt. */
32797 static unsigned
32798 cp_parser_begin_omp_structured_block (cp_parser *parser)
32800 unsigned save = parser->in_statement;
32802 /* Only move the values to IN_OMP_BLOCK if they weren't false.
32803 This preserves the "not within loop or switch" style error messages
32804 for nonsense cases like
32805 void foo() {
32806 #pragma omp single
32807 break;
32810 if (parser->in_statement)
32811 parser->in_statement = IN_OMP_BLOCK;
32813 return save;
32816 static void
32817 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
32819 parser->in_statement = save;
32822 static tree
32823 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
32825 tree stmt = begin_omp_structured_block ();
32826 unsigned int save = cp_parser_begin_omp_structured_block (parser);
32828 cp_parser_statement (parser, NULL_TREE, false, if_p);
32830 cp_parser_end_omp_structured_block (parser, save);
32831 return finish_omp_structured_block (stmt);
32834 /* OpenMP 2.5:
32835 # pragma omp atomic new-line
32836 expression-stmt
32838 expression-stmt:
32839 x binop= expr | x++ | ++x | x-- | --x
32840 binop:
32841 +, *, -, /, &, ^, |, <<, >>
32843 where x is an lvalue expression with scalar type.
32845 OpenMP 3.1:
32846 # pragma omp atomic new-line
32847 update-stmt
32849 # pragma omp atomic read new-line
32850 read-stmt
32852 # pragma omp atomic write new-line
32853 write-stmt
32855 # pragma omp atomic update new-line
32856 update-stmt
32858 # pragma omp atomic capture new-line
32859 capture-stmt
32861 # pragma omp atomic capture new-line
32862 capture-block
32864 read-stmt:
32865 v = x
32866 write-stmt:
32867 x = expr
32868 update-stmt:
32869 expression-stmt | x = x binop expr
32870 capture-stmt:
32871 v = expression-stmt
32872 capture-block:
32873 { v = x; update-stmt; } | { update-stmt; v = x; }
32875 OpenMP 4.0:
32876 update-stmt:
32877 expression-stmt | x = x binop expr | x = expr binop x
32878 capture-stmt:
32879 v = update-stmt
32880 capture-block:
32881 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
32883 where x and v are lvalue expressions with scalar type. */
32885 static void
32886 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
32888 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
32889 tree rhs1 = NULL_TREE, orig_lhs;
32890 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
32891 bool structured_block = false;
32892 bool seq_cst = false;
32894 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32896 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32897 const char *p = IDENTIFIER_POINTER (id);
32899 if (!strcmp (p, "seq_cst"))
32901 seq_cst = true;
32902 cp_lexer_consume_token (parser->lexer);
32903 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32904 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32905 cp_lexer_consume_token (parser->lexer);
32908 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32910 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32911 const char *p = IDENTIFIER_POINTER (id);
32913 if (!strcmp (p, "read"))
32914 code = OMP_ATOMIC_READ;
32915 else if (!strcmp (p, "write"))
32916 code = NOP_EXPR;
32917 else if (!strcmp (p, "update"))
32918 code = OMP_ATOMIC;
32919 else if (!strcmp (p, "capture"))
32920 code = OMP_ATOMIC_CAPTURE_NEW;
32921 else
32922 p = NULL;
32923 if (p)
32924 cp_lexer_consume_token (parser->lexer);
32926 if (!seq_cst)
32928 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32929 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32930 cp_lexer_consume_token (parser->lexer);
32932 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32934 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32935 const char *p = IDENTIFIER_POINTER (id);
32937 if (!strcmp (p, "seq_cst"))
32939 seq_cst = true;
32940 cp_lexer_consume_token (parser->lexer);
32944 cp_parser_require_pragma_eol (parser, pragma_tok);
32946 switch (code)
32948 case OMP_ATOMIC_READ:
32949 case NOP_EXPR: /* atomic write */
32950 v = cp_parser_unary_expression (parser);
32951 if (v == error_mark_node)
32952 goto saw_error;
32953 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32954 goto saw_error;
32955 if (code == NOP_EXPR)
32956 lhs = cp_parser_expression (parser);
32957 else
32958 lhs = cp_parser_unary_expression (parser);
32959 if (lhs == error_mark_node)
32960 goto saw_error;
32961 if (code == NOP_EXPR)
32963 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
32964 opcode. */
32965 code = OMP_ATOMIC;
32966 rhs = lhs;
32967 lhs = v;
32968 v = NULL_TREE;
32970 goto done;
32971 case OMP_ATOMIC_CAPTURE_NEW:
32972 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
32974 cp_lexer_consume_token (parser->lexer);
32975 structured_block = true;
32977 else
32979 v = cp_parser_unary_expression (parser);
32980 if (v == error_mark_node)
32981 goto saw_error;
32982 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32983 goto saw_error;
32985 default:
32986 break;
32989 restart:
32990 lhs = cp_parser_unary_expression (parser);
32991 orig_lhs = lhs;
32992 switch (TREE_CODE (lhs))
32994 case ERROR_MARK:
32995 goto saw_error;
32997 case POSTINCREMENT_EXPR:
32998 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32999 code = OMP_ATOMIC_CAPTURE_OLD;
33000 /* FALLTHROUGH */
33001 case PREINCREMENT_EXPR:
33002 lhs = TREE_OPERAND (lhs, 0);
33003 opcode = PLUS_EXPR;
33004 rhs = integer_one_node;
33005 break;
33007 case POSTDECREMENT_EXPR:
33008 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33009 code = OMP_ATOMIC_CAPTURE_OLD;
33010 /* FALLTHROUGH */
33011 case PREDECREMENT_EXPR:
33012 lhs = TREE_OPERAND (lhs, 0);
33013 opcode = MINUS_EXPR;
33014 rhs = integer_one_node;
33015 break;
33017 case COMPOUND_EXPR:
33018 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
33019 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
33020 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
33021 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
33022 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
33023 (TREE_OPERAND (lhs, 1), 0), 0)))
33024 == BOOLEAN_TYPE)
33025 /* Undo effects of boolean_increment for post {in,de}crement. */
33026 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
33027 /* FALLTHRU */
33028 case MODIFY_EXPR:
33029 if (TREE_CODE (lhs) == MODIFY_EXPR
33030 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
33032 /* Undo effects of boolean_increment. */
33033 if (integer_onep (TREE_OPERAND (lhs, 1)))
33035 /* This is pre or post increment. */
33036 rhs = TREE_OPERAND (lhs, 1);
33037 lhs = TREE_OPERAND (lhs, 0);
33038 opcode = NOP_EXPR;
33039 if (code == OMP_ATOMIC_CAPTURE_NEW
33040 && !structured_block
33041 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
33042 code = OMP_ATOMIC_CAPTURE_OLD;
33043 break;
33046 /* FALLTHRU */
33047 default:
33048 switch (cp_lexer_peek_token (parser->lexer)->type)
33050 case CPP_MULT_EQ:
33051 opcode = MULT_EXPR;
33052 break;
33053 case CPP_DIV_EQ:
33054 opcode = TRUNC_DIV_EXPR;
33055 break;
33056 case CPP_PLUS_EQ:
33057 opcode = PLUS_EXPR;
33058 break;
33059 case CPP_MINUS_EQ:
33060 opcode = MINUS_EXPR;
33061 break;
33062 case CPP_LSHIFT_EQ:
33063 opcode = LSHIFT_EXPR;
33064 break;
33065 case CPP_RSHIFT_EQ:
33066 opcode = RSHIFT_EXPR;
33067 break;
33068 case CPP_AND_EQ:
33069 opcode = BIT_AND_EXPR;
33070 break;
33071 case CPP_OR_EQ:
33072 opcode = BIT_IOR_EXPR;
33073 break;
33074 case CPP_XOR_EQ:
33075 opcode = BIT_XOR_EXPR;
33076 break;
33077 case CPP_EQ:
33078 enum cp_parser_prec oprec;
33079 cp_token *token;
33080 cp_lexer_consume_token (parser->lexer);
33081 cp_parser_parse_tentatively (parser);
33082 rhs1 = cp_parser_simple_cast_expression (parser);
33083 if (rhs1 == error_mark_node)
33085 cp_parser_abort_tentative_parse (parser);
33086 cp_parser_simple_cast_expression (parser);
33087 goto saw_error;
33089 token = cp_lexer_peek_token (parser->lexer);
33090 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
33092 cp_parser_abort_tentative_parse (parser);
33093 cp_parser_parse_tentatively (parser);
33094 rhs = cp_parser_binary_expression (parser, false, true,
33095 PREC_NOT_OPERATOR, NULL);
33096 if (rhs == error_mark_node)
33098 cp_parser_abort_tentative_parse (parser);
33099 cp_parser_binary_expression (parser, false, true,
33100 PREC_NOT_OPERATOR, NULL);
33101 goto saw_error;
33103 switch (TREE_CODE (rhs))
33105 case MULT_EXPR:
33106 case TRUNC_DIV_EXPR:
33107 case RDIV_EXPR:
33108 case PLUS_EXPR:
33109 case MINUS_EXPR:
33110 case LSHIFT_EXPR:
33111 case RSHIFT_EXPR:
33112 case BIT_AND_EXPR:
33113 case BIT_IOR_EXPR:
33114 case BIT_XOR_EXPR:
33115 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
33117 if (cp_parser_parse_definitely (parser))
33119 opcode = TREE_CODE (rhs);
33120 rhs1 = TREE_OPERAND (rhs, 0);
33121 rhs = TREE_OPERAND (rhs, 1);
33122 goto stmt_done;
33124 else
33125 goto saw_error;
33127 break;
33128 default:
33129 break;
33131 cp_parser_abort_tentative_parse (parser);
33132 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
33134 rhs = cp_parser_expression (parser);
33135 if (rhs == error_mark_node)
33136 goto saw_error;
33137 opcode = NOP_EXPR;
33138 rhs1 = NULL_TREE;
33139 goto stmt_done;
33141 cp_parser_error (parser,
33142 "invalid form of %<#pragma omp atomic%>");
33143 goto saw_error;
33145 if (!cp_parser_parse_definitely (parser))
33146 goto saw_error;
33147 switch (token->type)
33149 case CPP_SEMICOLON:
33150 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33152 code = OMP_ATOMIC_CAPTURE_OLD;
33153 v = lhs;
33154 lhs = NULL_TREE;
33155 lhs1 = rhs1;
33156 rhs1 = NULL_TREE;
33157 cp_lexer_consume_token (parser->lexer);
33158 goto restart;
33160 else if (structured_block)
33162 opcode = NOP_EXPR;
33163 rhs = rhs1;
33164 rhs1 = NULL_TREE;
33165 goto stmt_done;
33167 cp_parser_error (parser,
33168 "invalid form of %<#pragma omp atomic%>");
33169 goto saw_error;
33170 case CPP_MULT:
33171 opcode = MULT_EXPR;
33172 break;
33173 case CPP_DIV:
33174 opcode = TRUNC_DIV_EXPR;
33175 break;
33176 case CPP_PLUS:
33177 opcode = PLUS_EXPR;
33178 break;
33179 case CPP_MINUS:
33180 opcode = MINUS_EXPR;
33181 break;
33182 case CPP_LSHIFT:
33183 opcode = LSHIFT_EXPR;
33184 break;
33185 case CPP_RSHIFT:
33186 opcode = RSHIFT_EXPR;
33187 break;
33188 case CPP_AND:
33189 opcode = BIT_AND_EXPR;
33190 break;
33191 case CPP_OR:
33192 opcode = BIT_IOR_EXPR;
33193 break;
33194 case CPP_XOR:
33195 opcode = BIT_XOR_EXPR;
33196 break;
33197 default:
33198 cp_parser_error (parser,
33199 "invalid operator for %<#pragma omp atomic%>");
33200 goto saw_error;
33202 oprec = TOKEN_PRECEDENCE (token);
33203 gcc_assert (oprec != PREC_NOT_OPERATOR);
33204 if (commutative_tree_code (opcode))
33205 oprec = (enum cp_parser_prec) (oprec - 1);
33206 cp_lexer_consume_token (parser->lexer);
33207 rhs = cp_parser_binary_expression (parser, false, false,
33208 oprec, NULL);
33209 if (rhs == error_mark_node)
33210 goto saw_error;
33211 goto stmt_done;
33212 /* FALLTHROUGH */
33213 default:
33214 cp_parser_error (parser,
33215 "invalid operator for %<#pragma omp atomic%>");
33216 goto saw_error;
33218 cp_lexer_consume_token (parser->lexer);
33220 rhs = cp_parser_expression (parser);
33221 if (rhs == error_mark_node)
33222 goto saw_error;
33223 break;
33225 stmt_done:
33226 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33228 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
33229 goto saw_error;
33230 v = cp_parser_unary_expression (parser);
33231 if (v == error_mark_node)
33232 goto saw_error;
33233 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33234 goto saw_error;
33235 lhs1 = cp_parser_unary_expression (parser);
33236 if (lhs1 == error_mark_node)
33237 goto saw_error;
33239 if (structured_block)
33241 cp_parser_consume_semicolon_at_end_of_statement (parser);
33242 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
33244 done:
33245 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
33246 if (!structured_block)
33247 cp_parser_consume_semicolon_at_end_of_statement (parser);
33248 return;
33250 saw_error:
33251 cp_parser_skip_to_end_of_block_or_statement (parser);
33252 if (structured_block)
33254 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33255 cp_lexer_consume_token (parser->lexer);
33256 else if (code == OMP_ATOMIC_CAPTURE_NEW)
33258 cp_parser_skip_to_end_of_block_or_statement (parser);
33259 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33260 cp_lexer_consume_token (parser->lexer);
33266 /* OpenMP 2.5:
33267 # pragma omp barrier new-line */
33269 static void
33270 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
33272 cp_parser_require_pragma_eol (parser, pragma_tok);
33273 finish_omp_barrier ();
33276 /* OpenMP 2.5:
33277 # pragma omp critical [(name)] new-line
33278 structured-block
33280 OpenMP 4.5:
33281 # pragma omp critical [(name) [hint(expression)]] new-line
33282 structured-block */
33284 #define OMP_CRITICAL_CLAUSE_MASK \
33285 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
33287 static tree
33288 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
33290 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
33292 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33294 cp_lexer_consume_token (parser->lexer);
33296 name = cp_parser_identifier (parser);
33298 if (name == error_mark_node
33299 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33300 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33301 /*or_comma=*/false,
33302 /*consume_paren=*/true);
33303 if (name == error_mark_node)
33304 name = NULL;
33306 clauses = cp_parser_omp_all_clauses (parser,
33307 OMP_CRITICAL_CLAUSE_MASK,
33308 "#pragma omp critical", pragma_tok);
33310 else
33311 cp_parser_require_pragma_eol (parser, pragma_tok);
33313 stmt = cp_parser_omp_structured_block (parser, if_p);
33314 return c_finish_omp_critical (input_location, stmt, name, clauses);
33317 /* OpenMP 2.5:
33318 # pragma omp flush flush-vars[opt] new-line
33320 flush-vars:
33321 ( variable-list ) */
33323 static void
33324 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
33326 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33327 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
33328 cp_parser_require_pragma_eol (parser, pragma_tok);
33330 finish_omp_flush ();
33333 /* Helper function, to parse omp for increment expression. */
33335 static tree
33336 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
33338 tree cond = cp_parser_binary_expression (parser, false, true,
33339 PREC_NOT_OPERATOR, NULL);
33340 if (cond == error_mark_node
33341 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33343 cp_parser_skip_to_end_of_statement (parser);
33344 return error_mark_node;
33347 switch (TREE_CODE (cond))
33349 case GT_EXPR:
33350 case GE_EXPR:
33351 case LT_EXPR:
33352 case LE_EXPR:
33353 break;
33354 case NE_EXPR:
33355 if (code == CILK_SIMD || code == CILK_FOR)
33356 break;
33357 /* Fall through: OpenMP disallows NE_EXPR. */
33358 default:
33359 return error_mark_node;
33362 /* If decl is an iterator, preserve LHS and RHS of the relational
33363 expr until finish_omp_for. */
33364 if (decl
33365 && (type_dependent_expression_p (decl)
33366 || CLASS_TYPE_P (TREE_TYPE (decl))))
33367 return cond;
33369 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
33370 TREE_CODE (cond),
33371 TREE_OPERAND (cond, 0), ERROR_MARK,
33372 TREE_OPERAND (cond, 1), ERROR_MARK,
33373 /*overload=*/NULL, tf_warning_or_error);
33376 /* Helper function, to parse omp for increment expression. */
33378 static tree
33379 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
33381 cp_token *token = cp_lexer_peek_token (parser->lexer);
33382 enum tree_code op;
33383 tree lhs, rhs;
33384 cp_id_kind idk;
33385 bool decl_first;
33387 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33389 op = (token->type == CPP_PLUS_PLUS
33390 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
33391 cp_lexer_consume_token (parser->lexer);
33392 lhs = cp_parser_simple_cast_expression (parser);
33393 if (lhs != decl
33394 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33395 return error_mark_node;
33396 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33399 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
33400 if (lhs != decl
33401 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33402 return error_mark_node;
33404 token = cp_lexer_peek_token (parser->lexer);
33405 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33407 op = (token->type == CPP_PLUS_PLUS
33408 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
33409 cp_lexer_consume_token (parser->lexer);
33410 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33413 op = cp_parser_assignment_operator_opt (parser);
33414 if (op == ERROR_MARK)
33415 return error_mark_node;
33417 if (op != NOP_EXPR)
33419 rhs = cp_parser_assignment_expression (parser);
33420 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
33421 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33424 lhs = cp_parser_binary_expression (parser, false, false,
33425 PREC_ADDITIVE_EXPRESSION, NULL);
33426 token = cp_lexer_peek_token (parser->lexer);
33427 decl_first = (lhs == decl
33428 || (processing_template_decl && cp_tree_equal (lhs, decl)));
33429 if (decl_first)
33430 lhs = NULL_TREE;
33431 if (token->type != CPP_PLUS
33432 && token->type != CPP_MINUS)
33433 return error_mark_node;
33437 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
33438 cp_lexer_consume_token (parser->lexer);
33439 rhs = cp_parser_binary_expression (parser, false, false,
33440 PREC_ADDITIVE_EXPRESSION, NULL);
33441 token = cp_lexer_peek_token (parser->lexer);
33442 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
33444 if (lhs == NULL_TREE)
33446 if (op == PLUS_EXPR)
33447 lhs = rhs;
33448 else
33449 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
33450 tf_warning_or_error);
33452 else
33453 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
33454 ERROR_MARK, NULL, tf_warning_or_error);
33457 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
33459 if (!decl_first)
33461 if ((rhs != decl
33462 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
33463 || op == MINUS_EXPR)
33464 return error_mark_node;
33465 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
33467 else
33468 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
33470 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33473 /* Parse the initialization statement of either an OpenMP for loop or
33474 a Cilk Plus for loop.
33476 Return true if the resulting construct should have an
33477 OMP_CLAUSE_PRIVATE added to it. */
33479 static tree
33480 cp_parser_omp_for_loop_init (cp_parser *parser,
33481 enum tree_code code,
33482 tree &this_pre_body,
33483 vec<tree, va_gc> *for_block,
33484 tree &init,
33485 tree &orig_init,
33486 tree &decl,
33487 tree &real_decl)
33489 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33490 return NULL_TREE;
33492 tree add_private_clause = NULL_TREE;
33494 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
33496 init-expr:
33497 var = lb
33498 integer-type var = lb
33499 random-access-iterator-type var = lb
33500 pointer-type var = lb
33502 cp_decl_specifier_seq type_specifiers;
33504 /* First, try to parse as an initialized declaration. See
33505 cp_parser_condition, from whence the bulk of this is copied. */
33507 cp_parser_parse_tentatively (parser);
33508 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
33509 /*is_trailing_return=*/false,
33510 &type_specifiers);
33511 if (cp_parser_parse_definitely (parser))
33513 /* If parsing a type specifier seq succeeded, then this
33514 MUST be a initialized declaration. */
33515 tree asm_specification, attributes;
33516 cp_declarator *declarator;
33518 declarator = cp_parser_declarator (parser,
33519 CP_PARSER_DECLARATOR_NAMED,
33520 /*ctor_dtor_or_conv_p=*/NULL,
33521 /*parenthesized_p=*/NULL,
33522 /*member_p=*/false,
33523 /*friend_p=*/false);
33524 attributes = cp_parser_attributes_opt (parser);
33525 asm_specification = cp_parser_asm_specification_opt (parser);
33527 if (declarator == cp_error_declarator)
33528 cp_parser_skip_to_end_of_statement (parser);
33530 else
33532 tree pushed_scope, auto_node;
33534 decl = start_decl (declarator, &type_specifiers,
33535 SD_INITIALIZED, attributes,
33536 /*prefix_attributes=*/NULL_TREE,
33537 &pushed_scope);
33539 auto_node = type_uses_auto (TREE_TYPE (decl));
33540 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
33542 if (cp_lexer_next_token_is (parser->lexer,
33543 CPP_OPEN_PAREN))
33545 if (code != CILK_SIMD && code != CILK_FOR)
33546 error ("parenthesized initialization is not allowed in "
33547 "OpenMP %<for%> loop");
33548 else
33549 error ("parenthesized initialization is "
33550 "not allowed in for-loop");
33552 else
33553 /* Trigger an error. */
33554 cp_parser_require (parser, CPP_EQ, RT_EQ);
33556 init = error_mark_node;
33557 cp_parser_skip_to_end_of_statement (parser);
33559 else if (CLASS_TYPE_P (TREE_TYPE (decl))
33560 || type_dependent_expression_p (decl)
33561 || auto_node)
33563 bool is_direct_init, is_non_constant_init;
33565 init = cp_parser_initializer (parser,
33566 &is_direct_init,
33567 &is_non_constant_init);
33569 if (auto_node)
33571 TREE_TYPE (decl)
33572 = do_auto_deduction (TREE_TYPE (decl), init,
33573 auto_node);
33575 if (!CLASS_TYPE_P (TREE_TYPE (decl))
33576 && !type_dependent_expression_p (decl))
33577 goto non_class;
33580 cp_finish_decl (decl, init, !is_non_constant_init,
33581 asm_specification,
33582 LOOKUP_ONLYCONVERTING);
33583 orig_init = init;
33584 if (CLASS_TYPE_P (TREE_TYPE (decl)))
33586 vec_safe_push (for_block, this_pre_body);
33587 init = NULL_TREE;
33589 else
33591 init = pop_stmt_list (this_pre_body);
33592 if (init && TREE_CODE (init) == STATEMENT_LIST)
33594 tree_stmt_iterator i = tsi_start (init);
33595 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
33596 while (!tsi_end_p (i))
33598 tree t = tsi_stmt (i);
33599 if (TREE_CODE (t) == DECL_EXPR
33600 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
33602 tsi_delink (&i);
33603 vec_safe_push (for_block, t);
33604 continue;
33606 break;
33608 if (tsi_one_before_end_p (i))
33610 tree t = tsi_stmt (i);
33611 tsi_delink (&i);
33612 free_stmt_list (init);
33613 init = t;
33617 this_pre_body = NULL_TREE;
33619 else
33621 /* Consume '='. */
33622 cp_lexer_consume_token (parser->lexer);
33623 init = cp_parser_assignment_expression (parser);
33625 non_class:
33626 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
33627 init = error_mark_node;
33628 else
33629 cp_finish_decl (decl, NULL_TREE,
33630 /*init_const_expr_p=*/false,
33631 asm_specification,
33632 LOOKUP_ONLYCONVERTING);
33635 if (pushed_scope)
33636 pop_scope (pushed_scope);
33639 else
33641 cp_id_kind idk;
33642 /* If parsing a type specifier sequence failed, then
33643 this MUST be a simple expression. */
33644 if (code == CILK_FOR)
33645 error ("%<_Cilk_for%> allows expression instead of declaration only "
33646 "in C, not in C++");
33647 cp_parser_parse_tentatively (parser);
33648 decl = cp_parser_primary_expression (parser, false, false,
33649 false, &idk);
33650 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
33651 if (!cp_parser_error_occurred (parser)
33652 && decl
33653 && (TREE_CODE (decl) == COMPONENT_REF
33654 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
33656 cp_parser_abort_tentative_parse (parser);
33657 cp_parser_parse_tentatively (parser);
33658 cp_token *token = cp_lexer_peek_token (parser->lexer);
33659 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
33660 /*check_dependency_p=*/true,
33661 /*template_p=*/NULL,
33662 /*declarator_p=*/false,
33663 /*optional_p=*/false);
33664 if (name != error_mark_node
33665 && last_tok == cp_lexer_peek_token (parser->lexer))
33667 decl = cp_parser_lookup_name_simple (parser, name,
33668 token->location);
33669 if (TREE_CODE (decl) == FIELD_DECL)
33670 add_private_clause = omp_privatize_field (decl, false);
33672 cp_parser_abort_tentative_parse (parser);
33673 cp_parser_parse_tentatively (parser);
33674 decl = cp_parser_primary_expression (parser, false, false,
33675 false, &idk);
33677 if (!cp_parser_error_occurred (parser)
33678 && decl
33679 && DECL_P (decl)
33680 && CLASS_TYPE_P (TREE_TYPE (decl)))
33682 tree rhs;
33684 cp_parser_parse_definitely (parser);
33685 cp_parser_require (parser, CPP_EQ, RT_EQ);
33686 rhs = cp_parser_assignment_expression (parser);
33687 orig_init = rhs;
33688 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
33689 decl, NOP_EXPR,
33690 rhs,
33691 tf_warning_or_error));
33692 if (!add_private_clause)
33693 add_private_clause = decl;
33695 else
33697 decl = NULL;
33698 cp_parser_abort_tentative_parse (parser);
33699 init = cp_parser_expression (parser);
33700 if (init)
33702 if (TREE_CODE (init) == MODIFY_EXPR
33703 || TREE_CODE (init) == MODOP_EXPR)
33704 real_decl = TREE_OPERAND (init, 0);
33708 return add_private_clause;
33711 /* Parse the restricted form of the for statement allowed by OpenMP. */
33713 static tree
33714 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
33715 tree *cclauses, bool *if_p)
33717 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
33718 tree real_decl, initv, condv, incrv, declv;
33719 tree this_pre_body, cl, ordered_cl = NULL_TREE;
33720 location_t loc_first;
33721 bool collapse_err = false;
33722 int i, collapse = 1, ordered = 0, count, nbraces = 0;
33723 vec<tree, va_gc> *for_block = make_tree_vector ();
33724 auto_vec<tree, 4> orig_inits;
33726 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
33727 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
33728 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
33729 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
33730 && OMP_CLAUSE_ORDERED_EXPR (cl))
33732 ordered_cl = cl;
33733 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
33736 if (ordered && ordered < collapse)
33738 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
33739 "%<ordered%> clause parameter is less than %<collapse%>");
33740 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
33741 = build_int_cst (NULL_TREE, collapse);
33742 ordered = collapse;
33744 if (ordered)
33746 for (tree *pc = &clauses; *pc; )
33747 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
33749 error_at (OMP_CLAUSE_LOCATION (*pc),
33750 "%<linear%> clause may not be specified together "
33751 "with %<ordered%> clause with a parameter");
33752 *pc = OMP_CLAUSE_CHAIN (*pc);
33754 else
33755 pc = &OMP_CLAUSE_CHAIN (*pc);
33758 gcc_assert (collapse >= 1 && ordered >= 0);
33759 count = ordered ? ordered : collapse;
33761 declv = make_tree_vec (count);
33762 initv = make_tree_vec (count);
33763 condv = make_tree_vec (count);
33764 incrv = make_tree_vec (count);
33766 loc_first = cp_lexer_peek_token (parser->lexer)->location;
33768 for (i = 0; i < count; i++)
33770 int bracecount = 0;
33771 tree add_private_clause = NULL_TREE;
33772 location_t loc;
33774 if (code != CILK_FOR
33775 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33777 cp_parser_error (parser, "for statement expected");
33778 return NULL;
33780 if (code == CILK_FOR
33781 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
33783 cp_parser_error (parser, "_Cilk_for statement expected");
33784 return NULL;
33786 loc = cp_lexer_consume_token (parser->lexer)->location;
33788 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33789 return NULL;
33791 init = orig_init = decl = real_decl = NULL;
33792 this_pre_body = push_stmt_list ();
33794 add_private_clause
33795 = cp_parser_omp_for_loop_init (parser, code,
33796 this_pre_body, for_block,
33797 init, orig_init, decl, real_decl);
33799 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33800 if (this_pre_body)
33802 this_pre_body = pop_stmt_list (this_pre_body);
33803 if (pre_body)
33805 tree t = pre_body;
33806 pre_body = push_stmt_list ();
33807 add_stmt (t);
33808 add_stmt (this_pre_body);
33809 pre_body = pop_stmt_list (pre_body);
33811 else
33812 pre_body = this_pre_body;
33815 if (decl)
33816 real_decl = decl;
33817 if (cclauses != NULL
33818 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
33819 && real_decl != NULL_TREE)
33821 tree *c;
33822 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
33823 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
33824 && OMP_CLAUSE_DECL (*c) == real_decl)
33826 error_at (loc, "iteration variable %qD"
33827 " should not be firstprivate", real_decl);
33828 *c = OMP_CLAUSE_CHAIN (*c);
33830 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
33831 && OMP_CLAUSE_DECL (*c) == real_decl)
33833 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
33834 tree l = *c;
33835 *c = OMP_CLAUSE_CHAIN (*c);
33836 if (code == OMP_SIMD)
33838 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33839 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
33841 else
33843 OMP_CLAUSE_CHAIN (l) = clauses;
33844 clauses = l;
33846 add_private_clause = NULL_TREE;
33848 else
33850 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
33851 && OMP_CLAUSE_DECL (*c) == real_decl)
33852 add_private_clause = NULL_TREE;
33853 c = &OMP_CLAUSE_CHAIN (*c);
33857 if (add_private_clause)
33859 tree c;
33860 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
33862 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
33863 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
33864 && OMP_CLAUSE_DECL (c) == decl)
33865 break;
33866 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
33867 && OMP_CLAUSE_DECL (c) == decl)
33868 error_at (loc, "iteration variable %qD "
33869 "should not be firstprivate",
33870 decl);
33871 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
33872 && OMP_CLAUSE_DECL (c) == decl)
33873 error_at (loc, "iteration variable %qD should not be reduction",
33874 decl);
33876 if (c == NULL)
33878 if (code != OMP_SIMD)
33879 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
33880 else if (collapse == 1)
33881 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33882 else
33883 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
33884 OMP_CLAUSE_DECL (c) = add_private_clause;
33885 c = finish_omp_clauses (c, C_ORT_OMP);
33886 if (c)
33888 OMP_CLAUSE_CHAIN (c) = clauses;
33889 clauses = c;
33890 /* For linear, signal that we need to fill up
33891 the so far unknown linear step. */
33892 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
33893 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
33898 cond = NULL;
33899 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33900 cond = cp_parser_omp_for_cond (parser, decl, code);
33901 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33903 incr = NULL;
33904 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
33906 /* If decl is an iterator, preserve the operator on decl
33907 until finish_omp_for. */
33908 if (real_decl
33909 && ((processing_template_decl
33910 && (TREE_TYPE (real_decl) == NULL_TREE
33911 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
33912 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
33913 incr = cp_parser_omp_for_incr (parser, real_decl);
33914 else
33915 incr = cp_parser_expression (parser);
33916 if (!EXPR_HAS_LOCATION (incr))
33917 protected_set_expr_location (incr, input_location);
33920 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33921 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33922 /*or_comma=*/false,
33923 /*consume_paren=*/true);
33925 TREE_VEC_ELT (declv, i) = decl;
33926 TREE_VEC_ELT (initv, i) = init;
33927 TREE_VEC_ELT (condv, i) = cond;
33928 TREE_VEC_ELT (incrv, i) = incr;
33929 if (orig_init)
33931 orig_inits.safe_grow_cleared (i + 1);
33932 orig_inits[i] = orig_init;
33935 if (i == count - 1)
33936 break;
33938 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
33939 in between the collapsed for loops to be still considered perfectly
33940 nested. Hopefully the final version clarifies this.
33941 For now handle (multiple) {'s and empty statements. */
33942 cp_parser_parse_tentatively (parser);
33945 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33946 break;
33947 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33949 cp_lexer_consume_token (parser->lexer);
33950 bracecount++;
33952 else if (bracecount
33953 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33954 cp_lexer_consume_token (parser->lexer);
33955 else
33957 loc = cp_lexer_peek_token (parser->lexer)->location;
33958 error_at (loc, "not enough collapsed for loops");
33959 collapse_err = true;
33960 cp_parser_abort_tentative_parse (parser);
33961 declv = NULL_TREE;
33962 break;
33965 while (1);
33967 if (declv)
33969 cp_parser_parse_definitely (parser);
33970 nbraces += bracecount;
33974 if (nbraces)
33975 if_p = NULL;
33977 /* Note that we saved the original contents of this flag when we entered
33978 the structured block, and so we don't need to re-save it here. */
33979 if (code == CILK_SIMD || code == CILK_FOR)
33980 parser->in_statement = IN_CILK_SIMD_FOR;
33981 else
33982 parser->in_statement = IN_OMP_FOR;
33984 /* Note that the grammar doesn't call for a structured block here,
33985 though the loop as a whole is a structured block. */
33986 body = push_stmt_list ();
33987 cp_parser_statement (parser, NULL_TREE, false, if_p);
33988 body = pop_stmt_list (body);
33990 if (declv == NULL_TREE)
33991 ret = NULL_TREE;
33992 else
33993 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
33994 body, pre_body, &orig_inits, clauses);
33996 while (nbraces)
33998 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34000 cp_lexer_consume_token (parser->lexer);
34001 nbraces--;
34003 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34004 cp_lexer_consume_token (parser->lexer);
34005 else
34007 if (!collapse_err)
34009 error_at (cp_lexer_peek_token (parser->lexer)->location,
34010 "collapsed loops not perfectly nested");
34012 collapse_err = true;
34013 cp_parser_statement_seq_opt (parser, NULL);
34014 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
34015 break;
34019 while (!for_block->is_empty ())
34021 tree t = for_block->pop ();
34022 if (TREE_CODE (t) == STATEMENT_LIST)
34023 add_stmt (pop_stmt_list (t));
34024 else
34025 add_stmt (t);
34027 release_tree_vector (for_block);
34029 return ret;
34032 /* Helper function for OpenMP parsing, split clauses and call
34033 finish_omp_clauses on each of the set of clauses afterwards. */
34035 static void
34036 cp_omp_split_clauses (location_t loc, enum tree_code code,
34037 omp_clause_mask mask, tree clauses, tree *cclauses)
34039 int i;
34040 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
34041 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
34042 if (cclauses[i])
34043 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
34046 /* OpenMP 4.0:
34047 #pragma omp simd simd-clause[optseq] new-line
34048 for-loop */
34050 #define OMP_SIMD_CLAUSE_MASK \
34051 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
34052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
34053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
34055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34060 static tree
34061 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
34062 char *p_name, omp_clause_mask mask, tree *cclauses,
34063 bool *if_p)
34065 tree clauses, sb, ret;
34066 unsigned int save;
34067 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34069 strcat (p_name, " simd");
34070 mask |= OMP_SIMD_CLAUSE_MASK;
34072 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34073 cclauses == NULL);
34074 if (cclauses)
34076 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
34077 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
34078 tree c = find_omp_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
34079 OMP_CLAUSE_ORDERED);
34080 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
34082 error_at (OMP_CLAUSE_LOCATION (c),
34083 "%<ordered%> clause with parameter may not be specified "
34084 "on %qs construct", p_name);
34085 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
34089 sb = begin_omp_structured_block ();
34090 save = cp_parser_begin_omp_structured_block (parser);
34092 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
34094 cp_parser_end_omp_structured_block (parser, save);
34095 add_stmt (finish_omp_structured_block (sb));
34097 return ret;
34100 /* OpenMP 2.5:
34101 #pragma omp for for-clause[optseq] new-line
34102 for-loop
34104 OpenMP 4.0:
34105 #pragma omp for simd for-simd-clause[optseq] new-line
34106 for-loop */
34108 #define OMP_FOR_CLAUSE_MASK \
34109 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34110 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34111 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34112 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34113 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
34115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
34116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34119 static tree
34120 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
34121 char *p_name, omp_clause_mask mask, tree *cclauses,
34122 bool *if_p)
34124 tree clauses, sb, ret;
34125 unsigned int save;
34126 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34128 strcat (p_name, " for");
34129 mask |= OMP_FOR_CLAUSE_MASK;
34130 /* parallel for{, simd} disallows nowait clause, but for
34131 target {teams distribute ,}parallel for{, simd} it should be accepted. */
34132 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
34133 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34134 /* Composite distribute parallel for{, simd} disallows ordered clause. */
34135 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34136 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
34138 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34140 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34141 const char *p = IDENTIFIER_POINTER (id);
34143 if (strcmp (p, "simd") == 0)
34145 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34146 if (cclauses == NULL)
34147 cclauses = cclauses_buf;
34149 cp_lexer_consume_token (parser->lexer);
34150 if (!flag_openmp) /* flag_openmp_simd */
34151 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34152 cclauses, if_p);
34153 sb = begin_omp_structured_block ();
34154 save = cp_parser_begin_omp_structured_block (parser);
34155 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34156 cclauses, if_p);
34157 cp_parser_end_omp_structured_block (parser, save);
34158 tree body = finish_omp_structured_block (sb);
34159 if (ret == NULL)
34160 return ret;
34161 ret = make_node (OMP_FOR);
34162 TREE_TYPE (ret) = void_type_node;
34163 OMP_FOR_BODY (ret) = body;
34164 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34165 SET_EXPR_LOCATION (ret, loc);
34166 add_stmt (ret);
34167 return ret;
34170 if (!flag_openmp) /* flag_openmp_simd */
34172 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34173 return NULL_TREE;
34176 /* Composite distribute parallel for disallows linear clause. */
34177 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34178 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
34180 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34181 cclauses == NULL);
34182 if (cclauses)
34184 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
34185 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34188 sb = begin_omp_structured_block ();
34189 save = cp_parser_begin_omp_structured_block (parser);
34191 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
34193 cp_parser_end_omp_structured_block (parser, save);
34194 add_stmt (finish_omp_structured_block (sb));
34196 return ret;
34199 /* OpenMP 2.5:
34200 # pragma omp master new-line
34201 structured-block */
34203 static tree
34204 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34206 cp_parser_require_pragma_eol (parser, pragma_tok);
34207 return c_finish_omp_master (input_location,
34208 cp_parser_omp_structured_block (parser, if_p));
34211 /* OpenMP 2.5:
34212 # pragma omp ordered new-line
34213 structured-block
34215 OpenMP 4.5:
34216 # pragma omp ordered ordered-clauses new-line
34217 structured-block */
34219 #define OMP_ORDERED_CLAUSE_MASK \
34220 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
34221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
34223 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
34224 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
34226 static bool
34227 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
34228 enum pragma_context context, bool *if_p)
34230 location_t loc = pragma_tok->location;
34232 if (context != pragma_stmt && context != pragma_compound)
34234 cp_parser_error (parser, "expected declaration specifiers");
34235 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34236 return false;
34239 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34241 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34242 const char *p = IDENTIFIER_POINTER (id);
34244 if (strcmp (p, "depend") == 0)
34246 if (context == pragma_stmt)
34248 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
34249 "%<depend%> clause may only be used in compound "
34250 "statements");
34251 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34252 return false;
34254 tree clauses
34255 = cp_parser_omp_all_clauses (parser,
34256 OMP_ORDERED_DEPEND_CLAUSE_MASK,
34257 "#pragma omp ordered", pragma_tok);
34258 c_finish_omp_ordered (loc, clauses, NULL_TREE);
34259 return false;
34263 tree clauses
34264 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
34265 "#pragma omp ordered", pragma_tok);
34266 c_finish_omp_ordered (loc, clauses,
34267 cp_parser_omp_structured_block (parser, if_p));
34268 return true;
34271 /* OpenMP 2.5:
34273 section-scope:
34274 { section-sequence }
34276 section-sequence:
34277 section-directive[opt] structured-block
34278 section-sequence section-directive structured-block */
34280 static tree
34281 cp_parser_omp_sections_scope (cp_parser *parser)
34283 tree stmt, substmt;
34284 bool error_suppress = false;
34285 cp_token *tok;
34287 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
34288 return NULL_TREE;
34290 stmt = push_stmt_list ();
34292 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
34293 != PRAGMA_OMP_SECTION)
34295 substmt = cp_parser_omp_structured_block (parser, NULL);
34296 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34297 add_stmt (substmt);
34300 while (1)
34302 tok = cp_lexer_peek_token (parser->lexer);
34303 if (tok->type == CPP_CLOSE_BRACE)
34304 break;
34305 if (tok->type == CPP_EOF)
34306 break;
34308 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
34310 cp_lexer_consume_token (parser->lexer);
34311 cp_parser_require_pragma_eol (parser, tok);
34312 error_suppress = false;
34314 else if (!error_suppress)
34316 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
34317 error_suppress = true;
34320 substmt = cp_parser_omp_structured_block (parser, NULL);
34321 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34322 add_stmt (substmt);
34324 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34326 substmt = pop_stmt_list (stmt);
34328 stmt = make_node (OMP_SECTIONS);
34329 TREE_TYPE (stmt) = void_type_node;
34330 OMP_SECTIONS_BODY (stmt) = substmt;
34332 add_stmt (stmt);
34333 return stmt;
34336 /* OpenMP 2.5:
34337 # pragma omp sections sections-clause[optseq] newline
34338 sections-scope */
34340 #define OMP_SECTIONS_CLAUSE_MASK \
34341 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34347 static tree
34348 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
34349 char *p_name, omp_clause_mask mask, tree *cclauses)
34351 tree clauses, ret;
34352 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34354 strcat (p_name, " sections");
34355 mask |= OMP_SECTIONS_CLAUSE_MASK;
34356 if (cclauses)
34357 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34359 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34360 cclauses == NULL);
34361 if (cclauses)
34363 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
34364 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
34367 ret = cp_parser_omp_sections_scope (parser);
34368 if (ret)
34369 OMP_SECTIONS_CLAUSES (ret) = clauses;
34371 return ret;
34374 /* OpenMP 2.5:
34375 # pragma omp parallel parallel-clause[optseq] new-line
34376 structured-block
34377 # pragma omp parallel for parallel-for-clause[optseq] new-line
34378 structured-block
34379 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
34380 structured-block
34382 OpenMP 4.0:
34383 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
34384 structured-block */
34386 #define OMP_PARALLEL_CLAUSE_MASK \
34387 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
34391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
34393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
34395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
34397 static tree
34398 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
34399 char *p_name, omp_clause_mask mask, tree *cclauses,
34400 bool *if_p)
34402 tree stmt, clauses, block;
34403 unsigned int save;
34404 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34406 strcat (p_name, " parallel");
34407 mask |= OMP_PARALLEL_CLAUSE_MASK;
34408 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
34409 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
34410 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
34411 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
34413 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34415 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34416 if (cclauses == NULL)
34417 cclauses = cclauses_buf;
34419 cp_lexer_consume_token (parser->lexer);
34420 if (!flag_openmp) /* flag_openmp_simd */
34421 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
34422 if_p);
34423 block = begin_omp_parallel ();
34424 save = cp_parser_begin_omp_structured_block (parser);
34425 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
34426 if_p);
34427 cp_parser_end_omp_structured_block (parser, save);
34428 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34429 block);
34430 if (ret == NULL_TREE)
34431 return ret;
34432 OMP_PARALLEL_COMBINED (stmt) = 1;
34433 return stmt;
34435 /* When combined with distribute, parallel has to be followed by for.
34436 #pragma omp target parallel is allowed though. */
34437 else if (cclauses
34438 && (mask & (OMP_CLAUSE_MASK_1
34439 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34441 error_at (loc, "expected %<for%> after %qs", p_name);
34442 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34443 return NULL_TREE;
34445 else if (!flag_openmp) /* flag_openmp_simd */
34447 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34448 return NULL_TREE;
34450 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34452 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34453 const char *p = IDENTIFIER_POINTER (id);
34454 if (strcmp (p, "sections") == 0)
34456 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34457 cclauses = cclauses_buf;
34459 cp_lexer_consume_token (parser->lexer);
34460 block = begin_omp_parallel ();
34461 save = cp_parser_begin_omp_structured_block (parser);
34462 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
34463 cp_parser_end_omp_structured_block (parser, save);
34464 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34465 block);
34466 OMP_PARALLEL_COMBINED (stmt) = 1;
34467 return stmt;
34471 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34472 cclauses == NULL);
34473 if (cclauses)
34475 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
34476 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
34479 block = begin_omp_parallel ();
34480 save = cp_parser_begin_omp_structured_block (parser);
34481 cp_parser_statement (parser, NULL_TREE, false, if_p);
34482 cp_parser_end_omp_structured_block (parser, save);
34483 stmt = finish_omp_parallel (clauses, block);
34484 return stmt;
34487 /* OpenMP 2.5:
34488 # pragma omp single single-clause[optseq] new-line
34489 structured-block */
34491 #define OMP_SINGLE_CLAUSE_MASK \
34492 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
34495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34497 static tree
34498 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34500 tree stmt = make_node (OMP_SINGLE);
34501 TREE_TYPE (stmt) = void_type_node;
34503 OMP_SINGLE_CLAUSES (stmt)
34504 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
34505 "#pragma omp single", pragma_tok);
34506 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34508 return add_stmt (stmt);
34511 /* OpenMP 3.0:
34512 # pragma omp task task-clause[optseq] new-line
34513 structured-block */
34515 #define OMP_TASK_CLAUSE_MASK \
34516 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
34518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
34519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
34523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
34524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
34527 static tree
34528 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34530 tree clauses, block;
34531 unsigned int save;
34533 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
34534 "#pragma omp task", pragma_tok);
34535 block = begin_omp_task ();
34536 save = cp_parser_begin_omp_structured_block (parser);
34537 cp_parser_statement (parser, NULL_TREE, false, if_p);
34538 cp_parser_end_omp_structured_block (parser, save);
34539 return finish_omp_task (clauses, block);
34542 /* OpenMP 3.0:
34543 # pragma omp taskwait new-line */
34545 static void
34546 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
34548 cp_parser_require_pragma_eol (parser, pragma_tok);
34549 finish_omp_taskwait ();
34552 /* OpenMP 3.1:
34553 # pragma omp taskyield new-line */
34555 static void
34556 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
34558 cp_parser_require_pragma_eol (parser, pragma_tok);
34559 finish_omp_taskyield ();
34562 /* OpenMP 4.0:
34563 # pragma omp taskgroup new-line
34564 structured-block */
34566 static tree
34567 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34569 cp_parser_require_pragma_eol (parser, pragma_tok);
34570 return c_finish_omp_taskgroup (input_location,
34571 cp_parser_omp_structured_block (parser,
34572 if_p));
34576 /* OpenMP 2.5:
34577 # pragma omp threadprivate (variable-list) */
34579 static void
34580 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
34582 tree vars;
34584 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34585 cp_parser_require_pragma_eol (parser, pragma_tok);
34587 finish_omp_threadprivate (vars);
34590 /* OpenMP 4.0:
34591 # pragma omp cancel cancel-clause[optseq] new-line */
34593 #define OMP_CANCEL_CLAUSE_MASK \
34594 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
34595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
34596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
34597 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
34598 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
34600 static void
34601 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
34603 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
34604 "#pragma omp cancel", pragma_tok);
34605 finish_omp_cancel (clauses);
34608 /* OpenMP 4.0:
34609 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
34611 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
34612 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
34613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
34614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
34615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
34617 static void
34618 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
34619 enum pragma_context context)
34621 tree clauses;
34622 bool point_seen = false;
34624 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34626 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34627 const char *p = IDENTIFIER_POINTER (id);
34629 if (strcmp (p, "point") == 0)
34631 cp_lexer_consume_token (parser->lexer);
34632 point_seen = true;
34635 if (!point_seen)
34637 cp_parser_error (parser, "expected %<point%>");
34638 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34639 return;
34642 if (context != pragma_compound)
34644 if (context == pragma_stmt)
34645 error_at (pragma_tok->location,
34646 "%<#pragma omp cancellation point%> may only be used in"
34647 " compound statements");
34648 else
34649 cp_parser_error (parser, "expected declaration specifiers");
34650 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34651 return;
34654 clauses = cp_parser_omp_all_clauses (parser,
34655 OMP_CANCELLATION_POINT_CLAUSE_MASK,
34656 "#pragma omp cancellation point",
34657 pragma_tok);
34658 finish_omp_cancellation_point (clauses);
34661 /* OpenMP 4.0:
34662 #pragma omp distribute distribute-clause[optseq] new-line
34663 for-loop */
34665 #define OMP_DISTRIBUTE_CLAUSE_MASK \
34666 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
34670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34672 static tree
34673 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
34674 char *p_name, omp_clause_mask mask, tree *cclauses,
34675 bool *if_p)
34677 tree clauses, sb, ret;
34678 unsigned int save;
34679 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34681 strcat (p_name, " distribute");
34682 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
34684 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34686 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34687 const char *p = IDENTIFIER_POINTER (id);
34688 bool simd = false;
34689 bool parallel = false;
34691 if (strcmp (p, "simd") == 0)
34692 simd = true;
34693 else
34694 parallel = strcmp (p, "parallel") == 0;
34695 if (parallel || simd)
34697 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34698 if (cclauses == NULL)
34699 cclauses = cclauses_buf;
34700 cp_lexer_consume_token (parser->lexer);
34701 if (!flag_openmp) /* flag_openmp_simd */
34703 if (simd)
34704 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34705 cclauses, if_p);
34706 else
34707 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34708 cclauses, if_p);
34710 sb = begin_omp_structured_block ();
34711 save = cp_parser_begin_omp_structured_block (parser);
34712 if (simd)
34713 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34714 cclauses, if_p);
34715 else
34716 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34717 cclauses, if_p);
34718 cp_parser_end_omp_structured_block (parser, save);
34719 tree body = finish_omp_structured_block (sb);
34720 if (ret == NULL)
34721 return ret;
34722 ret = make_node (OMP_DISTRIBUTE);
34723 TREE_TYPE (ret) = void_type_node;
34724 OMP_FOR_BODY (ret) = body;
34725 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34726 SET_EXPR_LOCATION (ret, loc);
34727 add_stmt (ret);
34728 return ret;
34731 if (!flag_openmp) /* flag_openmp_simd */
34733 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34734 return NULL_TREE;
34737 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34738 cclauses == NULL);
34739 if (cclauses)
34741 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
34742 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34745 sb = begin_omp_structured_block ();
34746 save = cp_parser_begin_omp_structured_block (parser);
34748 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
34750 cp_parser_end_omp_structured_block (parser, save);
34751 add_stmt (finish_omp_structured_block (sb));
34753 return ret;
34756 /* OpenMP 4.0:
34757 # pragma omp teams teams-clause[optseq] new-line
34758 structured-block */
34760 #define OMP_TEAMS_CLAUSE_MASK \
34761 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
34766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
34767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
34769 static tree
34770 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
34771 char *p_name, omp_clause_mask mask, tree *cclauses,
34772 bool *if_p)
34774 tree clauses, sb, ret;
34775 unsigned int save;
34776 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34778 strcat (p_name, " teams");
34779 mask |= OMP_TEAMS_CLAUSE_MASK;
34781 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34783 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34784 const char *p = IDENTIFIER_POINTER (id);
34785 if (strcmp (p, "distribute") == 0)
34787 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34788 if (cclauses == NULL)
34789 cclauses = cclauses_buf;
34791 cp_lexer_consume_token (parser->lexer);
34792 if (!flag_openmp) /* flag_openmp_simd */
34793 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34794 cclauses, if_p);
34795 sb = begin_omp_structured_block ();
34796 save = cp_parser_begin_omp_structured_block (parser);
34797 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34798 cclauses, if_p);
34799 cp_parser_end_omp_structured_block (parser, save);
34800 tree body = finish_omp_structured_block (sb);
34801 if (ret == NULL)
34802 return ret;
34803 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34804 ret = make_node (OMP_TEAMS);
34805 TREE_TYPE (ret) = void_type_node;
34806 OMP_TEAMS_CLAUSES (ret) = clauses;
34807 OMP_TEAMS_BODY (ret) = body;
34808 OMP_TEAMS_COMBINED (ret) = 1;
34809 return add_stmt (ret);
34812 if (!flag_openmp) /* flag_openmp_simd */
34814 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34815 return NULL_TREE;
34818 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34819 cclauses == NULL);
34820 if (cclauses)
34822 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
34823 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34826 tree stmt = make_node (OMP_TEAMS);
34827 TREE_TYPE (stmt) = void_type_node;
34828 OMP_TEAMS_CLAUSES (stmt) = clauses;
34829 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34831 return add_stmt (stmt);
34834 /* OpenMP 4.0:
34835 # pragma omp target data target-data-clause[optseq] new-line
34836 structured-block */
34838 #define OMP_TARGET_DATA_CLAUSE_MASK \
34839 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
34844 static tree
34845 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34847 tree clauses
34848 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
34849 "#pragma omp target data", pragma_tok);
34850 int map_seen = 0;
34851 for (tree *pc = &clauses; *pc;)
34853 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34854 switch (OMP_CLAUSE_MAP_KIND (*pc))
34856 case GOMP_MAP_TO:
34857 case GOMP_MAP_ALWAYS_TO:
34858 case GOMP_MAP_FROM:
34859 case GOMP_MAP_ALWAYS_FROM:
34860 case GOMP_MAP_TOFROM:
34861 case GOMP_MAP_ALWAYS_TOFROM:
34862 case GOMP_MAP_ALLOC:
34863 map_seen = 3;
34864 break;
34865 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34866 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34867 case GOMP_MAP_ALWAYS_POINTER:
34868 break;
34869 default:
34870 map_seen |= 1;
34871 error_at (OMP_CLAUSE_LOCATION (*pc),
34872 "%<#pragma omp target data%> with map-type other "
34873 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
34874 "on %<map%> clause");
34875 *pc = OMP_CLAUSE_CHAIN (*pc);
34876 continue;
34878 pc = &OMP_CLAUSE_CHAIN (*pc);
34881 if (map_seen != 3)
34883 if (map_seen == 0)
34884 error_at (pragma_tok->location,
34885 "%<#pragma omp target data%> must contain at least "
34886 "one %<map%> clause");
34887 return NULL_TREE;
34890 tree stmt = make_node (OMP_TARGET_DATA);
34891 TREE_TYPE (stmt) = void_type_node;
34892 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
34894 keep_next_level (true);
34895 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34897 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34898 return add_stmt (stmt);
34901 /* OpenMP 4.5:
34902 # pragma omp target enter data target-enter-data-clause[optseq] new-line
34903 structured-block */
34905 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
34906 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34912 static tree
34913 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
34914 enum pragma_context context)
34916 bool data_seen = false;
34917 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34919 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34920 const char *p = IDENTIFIER_POINTER (id);
34922 if (strcmp (p, "data") == 0)
34924 cp_lexer_consume_token (parser->lexer);
34925 data_seen = true;
34928 if (!data_seen)
34930 cp_parser_error (parser, "expected %<data%>");
34931 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34932 return NULL_TREE;
34935 if (context == pragma_stmt)
34937 error_at (pragma_tok->location,
34938 "%<#pragma omp target enter data%> may only be "
34939 "used in compound statements");
34940 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34941 return NULL_TREE;
34944 tree clauses
34945 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
34946 "#pragma omp target enter data", pragma_tok);
34947 int map_seen = 0;
34948 for (tree *pc = &clauses; *pc;)
34950 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34951 switch (OMP_CLAUSE_MAP_KIND (*pc))
34953 case GOMP_MAP_TO:
34954 case GOMP_MAP_ALWAYS_TO:
34955 case GOMP_MAP_ALLOC:
34956 map_seen = 3;
34957 break;
34958 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34959 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34960 case GOMP_MAP_ALWAYS_POINTER:
34961 break;
34962 default:
34963 map_seen |= 1;
34964 error_at (OMP_CLAUSE_LOCATION (*pc),
34965 "%<#pragma omp target enter data%> with map-type other "
34966 "than %<to%> or %<alloc%> on %<map%> clause");
34967 *pc = OMP_CLAUSE_CHAIN (*pc);
34968 continue;
34970 pc = &OMP_CLAUSE_CHAIN (*pc);
34973 if (map_seen != 3)
34975 if (map_seen == 0)
34976 error_at (pragma_tok->location,
34977 "%<#pragma omp target enter data%> must contain at least "
34978 "one %<map%> clause");
34979 return NULL_TREE;
34982 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
34983 TREE_TYPE (stmt) = void_type_node;
34984 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
34985 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34986 return add_stmt (stmt);
34989 /* OpenMP 4.5:
34990 # pragma omp target exit data target-enter-data-clause[optseq] new-line
34991 structured-block */
34993 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
34994 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35000 static tree
35001 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
35002 enum pragma_context context)
35004 bool data_seen = false;
35005 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35007 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35008 const char *p = IDENTIFIER_POINTER (id);
35010 if (strcmp (p, "data") == 0)
35012 cp_lexer_consume_token (parser->lexer);
35013 data_seen = true;
35016 if (!data_seen)
35018 cp_parser_error (parser, "expected %<data%>");
35019 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35020 return NULL_TREE;
35023 if (context == pragma_stmt)
35025 error_at (pragma_tok->location,
35026 "%<#pragma omp target exit data%> may only be "
35027 "used in compound statements");
35028 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35029 return NULL_TREE;
35032 tree clauses
35033 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
35034 "#pragma omp target exit data", pragma_tok);
35035 int map_seen = 0;
35036 for (tree *pc = &clauses; *pc;)
35038 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35039 switch (OMP_CLAUSE_MAP_KIND (*pc))
35041 case GOMP_MAP_FROM:
35042 case GOMP_MAP_ALWAYS_FROM:
35043 case GOMP_MAP_RELEASE:
35044 case GOMP_MAP_DELETE:
35045 map_seen = 3;
35046 break;
35047 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35048 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35049 case GOMP_MAP_ALWAYS_POINTER:
35050 break;
35051 default:
35052 map_seen |= 1;
35053 error_at (OMP_CLAUSE_LOCATION (*pc),
35054 "%<#pragma omp target exit data%> with map-type other "
35055 "than %<from%>, %<release%> or %<delete%> on %<map%>"
35056 " clause");
35057 *pc = OMP_CLAUSE_CHAIN (*pc);
35058 continue;
35060 pc = &OMP_CLAUSE_CHAIN (*pc);
35063 if (map_seen != 3)
35065 if (map_seen == 0)
35066 error_at (pragma_tok->location,
35067 "%<#pragma omp target exit data%> must contain at least "
35068 "one %<map%> clause");
35069 return NULL_TREE;
35072 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
35073 TREE_TYPE (stmt) = void_type_node;
35074 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
35075 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35076 return add_stmt (stmt);
35079 /* OpenMP 4.0:
35080 # pragma omp target update target-update-clause[optseq] new-line */
35082 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
35083 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
35084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35090 static bool
35091 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
35092 enum pragma_context context)
35094 if (context == pragma_stmt)
35096 error_at (pragma_tok->location,
35097 "%<#pragma omp target update%> may only be "
35098 "used in compound statements");
35099 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35100 return false;
35103 tree clauses
35104 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
35105 "#pragma omp target update", pragma_tok);
35106 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
35107 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
35109 error_at (pragma_tok->location,
35110 "%<#pragma omp target update%> must contain at least one "
35111 "%<from%> or %<to%> clauses");
35112 return false;
35115 tree stmt = make_node (OMP_TARGET_UPDATE);
35116 TREE_TYPE (stmt) = void_type_node;
35117 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
35118 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35119 add_stmt (stmt);
35120 return false;
35123 /* OpenMP 4.0:
35124 # pragma omp target target-clause[optseq] new-line
35125 structured-block */
35127 #define OMP_TARGET_CLAUSE_MASK \
35128 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
35136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
35138 static bool
35139 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
35140 enum pragma_context context, bool *if_p)
35142 tree *pc = NULL, stmt;
35144 if (context != pragma_stmt && context != pragma_compound)
35146 cp_parser_error (parser, "expected declaration specifiers");
35147 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35148 return false;
35151 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35153 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35154 const char *p = IDENTIFIER_POINTER (id);
35155 enum tree_code ccode = ERROR_MARK;
35157 if (strcmp (p, "teams") == 0)
35158 ccode = OMP_TEAMS;
35159 else if (strcmp (p, "parallel") == 0)
35160 ccode = OMP_PARALLEL;
35161 else if (strcmp (p, "simd") == 0)
35162 ccode = OMP_SIMD;
35163 if (ccode != ERROR_MARK)
35165 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
35166 char p_name[sizeof ("#pragma omp target teams distribute "
35167 "parallel for simd")];
35169 cp_lexer_consume_token (parser->lexer);
35170 strcpy (p_name, "#pragma omp target");
35171 if (!flag_openmp) /* flag_openmp_simd */
35173 tree stmt;
35174 switch (ccode)
35176 case OMP_TEAMS:
35177 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
35178 OMP_TARGET_CLAUSE_MASK,
35179 cclauses, if_p);
35180 break;
35181 case OMP_PARALLEL:
35182 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35183 OMP_TARGET_CLAUSE_MASK,
35184 cclauses, if_p);
35185 break;
35186 case OMP_SIMD:
35187 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
35188 OMP_TARGET_CLAUSE_MASK,
35189 cclauses, if_p);
35190 break;
35191 default:
35192 gcc_unreachable ();
35194 return stmt != NULL_TREE;
35196 keep_next_level (true);
35197 tree sb = begin_omp_structured_block (), ret;
35198 unsigned save = cp_parser_begin_omp_structured_block (parser);
35199 switch (ccode)
35201 case OMP_TEAMS:
35202 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
35203 OMP_TARGET_CLAUSE_MASK, cclauses,
35204 if_p);
35205 break;
35206 case OMP_PARALLEL:
35207 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35208 OMP_TARGET_CLAUSE_MASK, cclauses,
35209 if_p);
35210 break;
35211 case OMP_SIMD:
35212 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
35213 OMP_TARGET_CLAUSE_MASK, cclauses,
35214 if_p);
35215 break;
35216 default:
35217 gcc_unreachable ();
35219 cp_parser_end_omp_structured_block (parser, save);
35220 tree body = finish_omp_structured_block (sb);
35221 if (ret == NULL_TREE)
35222 return false;
35223 if (ccode == OMP_TEAMS && !processing_template_decl)
35225 /* For combined target teams, ensure the num_teams and
35226 thread_limit clause expressions are evaluated on the host,
35227 before entering the target construct. */
35228 tree c;
35229 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35230 c; c = OMP_CLAUSE_CHAIN (c))
35231 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
35232 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
35233 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
35235 tree expr = OMP_CLAUSE_OPERAND (c, 0);
35236 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
35237 if (expr == error_mark_node)
35238 continue;
35239 tree tmp = TARGET_EXPR_SLOT (expr);
35240 add_stmt (expr);
35241 OMP_CLAUSE_OPERAND (c, 0) = expr;
35242 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
35243 OMP_CLAUSE_FIRSTPRIVATE);
35244 OMP_CLAUSE_DECL (tc) = tmp;
35245 OMP_CLAUSE_CHAIN (tc)
35246 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35247 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
35250 tree stmt = make_node (OMP_TARGET);
35251 TREE_TYPE (stmt) = void_type_node;
35252 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35253 OMP_TARGET_BODY (stmt) = body;
35254 OMP_TARGET_COMBINED (stmt) = 1;
35255 add_stmt (stmt);
35256 pc = &OMP_TARGET_CLAUSES (stmt);
35257 goto check_clauses;
35259 else if (!flag_openmp) /* flag_openmp_simd */
35261 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35262 return false;
35264 else if (strcmp (p, "data") == 0)
35266 cp_lexer_consume_token (parser->lexer);
35267 cp_parser_omp_target_data (parser, pragma_tok, if_p);
35268 return true;
35270 else if (strcmp (p, "enter") == 0)
35272 cp_lexer_consume_token (parser->lexer);
35273 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
35274 return false;
35276 else if (strcmp (p, "exit") == 0)
35278 cp_lexer_consume_token (parser->lexer);
35279 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
35280 return false;
35282 else if (strcmp (p, "update") == 0)
35284 cp_lexer_consume_token (parser->lexer);
35285 return cp_parser_omp_target_update (parser, pragma_tok, context);
35289 stmt = make_node (OMP_TARGET);
35290 TREE_TYPE (stmt) = void_type_node;
35292 OMP_TARGET_CLAUSES (stmt)
35293 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
35294 "#pragma omp target", pragma_tok);
35295 pc = &OMP_TARGET_CLAUSES (stmt);
35296 keep_next_level (true);
35297 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35299 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35300 add_stmt (stmt);
35302 check_clauses:
35303 while (*pc)
35305 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35306 switch (OMP_CLAUSE_MAP_KIND (*pc))
35308 case GOMP_MAP_TO:
35309 case GOMP_MAP_ALWAYS_TO:
35310 case GOMP_MAP_FROM:
35311 case GOMP_MAP_ALWAYS_FROM:
35312 case GOMP_MAP_TOFROM:
35313 case GOMP_MAP_ALWAYS_TOFROM:
35314 case GOMP_MAP_ALLOC:
35315 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35316 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35317 case GOMP_MAP_ALWAYS_POINTER:
35318 break;
35319 default:
35320 error_at (OMP_CLAUSE_LOCATION (*pc),
35321 "%<#pragma omp target%> with map-type other "
35322 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35323 "on %<map%> clause");
35324 *pc = OMP_CLAUSE_CHAIN (*pc);
35325 continue;
35327 pc = &OMP_CLAUSE_CHAIN (*pc);
35329 return true;
35332 /* OpenACC 2.0:
35333 # pragma acc cache (variable-list) new-line
35336 static tree
35337 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
35339 tree stmt, clauses;
35341 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
35342 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
35344 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
35346 stmt = make_node (OACC_CACHE);
35347 TREE_TYPE (stmt) = void_type_node;
35348 OACC_CACHE_CLAUSES (stmt) = clauses;
35349 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35350 add_stmt (stmt);
35352 return stmt;
35355 /* OpenACC 2.0:
35356 # pragma acc data oacc-data-clause[optseq] new-line
35357 structured-block */
35359 #define OACC_DATA_CLAUSE_MASK \
35360 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35362 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35372 static tree
35373 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35375 tree stmt, clauses, block;
35376 unsigned int save;
35378 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
35379 "#pragma acc data", pragma_tok);
35381 block = begin_omp_parallel ();
35382 save = cp_parser_begin_omp_structured_block (parser);
35383 cp_parser_statement (parser, NULL_TREE, false, if_p);
35384 cp_parser_end_omp_structured_block (parser, save);
35385 stmt = finish_oacc_data (clauses, block);
35386 return stmt;
35389 /* OpenACC 2.0:
35390 # pragma acc host_data <clauses> new-line
35391 structured-block */
35393 #define OACC_HOST_DATA_CLAUSE_MASK \
35394 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
35396 static tree
35397 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35399 tree stmt, clauses, block;
35400 unsigned int save;
35402 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
35403 "#pragma acc host_data", pragma_tok);
35405 block = begin_omp_parallel ();
35406 save = cp_parser_begin_omp_structured_block (parser);
35407 cp_parser_statement (parser, NULL_TREE, false, if_p);
35408 cp_parser_end_omp_structured_block (parser, save);
35409 stmt = finish_oacc_host_data (clauses, block);
35410 return stmt;
35413 /* OpenACC 2.0:
35414 # pragma acc declare oacc-data-clause[optseq] new-line
35417 #define OACC_DECLARE_CLAUSE_MASK \
35418 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
35424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
35425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35427 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35431 static tree
35432 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
35434 tree clauses, stmt;
35435 bool error = false;
35437 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
35438 "#pragma acc declare", pragma_tok, true);
35441 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35443 error_at (pragma_tok->location,
35444 "no valid clauses specified in %<#pragma acc declare%>");
35445 return NULL_TREE;
35448 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
35450 location_t loc = OMP_CLAUSE_LOCATION (t);
35451 tree decl = OMP_CLAUSE_DECL (t);
35452 if (!DECL_P (decl))
35454 error_at (loc, "array section in %<#pragma acc declare%>");
35455 error = true;
35456 continue;
35458 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
35459 switch (OMP_CLAUSE_MAP_KIND (t))
35461 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35462 case GOMP_MAP_FORCE_ALLOC:
35463 case GOMP_MAP_FORCE_TO:
35464 case GOMP_MAP_FORCE_DEVICEPTR:
35465 case GOMP_MAP_DEVICE_RESIDENT:
35466 break;
35468 case GOMP_MAP_LINK:
35469 if (!global_bindings_p ()
35470 && (TREE_STATIC (decl)
35471 || !DECL_EXTERNAL (decl)))
35473 error_at (loc,
35474 "%qD must be a global variable in"
35475 "%<#pragma acc declare link%>",
35476 decl);
35477 error = true;
35478 continue;
35480 break;
35482 default:
35483 if (global_bindings_p ())
35485 error_at (loc, "invalid OpenACC clause at file scope");
35486 error = true;
35487 continue;
35489 if (DECL_EXTERNAL (decl))
35491 error_at (loc,
35492 "invalid use of %<extern%> variable %qD "
35493 "in %<#pragma acc declare%>", decl);
35494 error = true;
35495 continue;
35497 else if (TREE_PUBLIC (decl))
35499 error_at (loc,
35500 "invalid use of %<global%> variable %qD "
35501 "in %<#pragma acc declare%>", decl);
35502 error = true;
35503 continue;
35505 break;
35508 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
35509 || lookup_attribute ("omp declare target link",
35510 DECL_ATTRIBUTES (decl)))
35512 error_at (loc, "variable %qD used more than once with "
35513 "%<#pragma acc declare%>", decl);
35514 error = true;
35515 continue;
35518 if (!error)
35520 tree id;
35522 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
35523 id = get_identifier ("omp declare target link");
35524 else
35525 id = get_identifier ("omp declare target");
35527 DECL_ATTRIBUTES (decl)
35528 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
35529 if (global_bindings_p ())
35531 symtab_node *node = symtab_node::get (decl);
35532 if (node != NULL)
35534 node->offloadable = 1;
35535 if (ENABLE_OFFLOADING)
35537 g->have_offload = true;
35538 if (is_a <varpool_node *> (node))
35539 vec_safe_push (offload_vars, decl);
35546 if (error || global_bindings_p ())
35547 return NULL_TREE;
35549 stmt = make_node (OACC_DECLARE);
35550 TREE_TYPE (stmt) = void_type_node;
35551 OACC_DECLARE_CLAUSES (stmt) = clauses;
35552 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35554 add_stmt (stmt);
35556 return NULL_TREE;
35559 /* OpenACC 2.0:
35560 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
35564 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
35566 LOC is the location of the #pragma token.
35569 #define OACC_ENTER_DATA_CLAUSE_MASK \
35570 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35575 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35578 #define OACC_EXIT_DATA_CLAUSE_MASK \
35579 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
35583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35585 static tree
35586 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
35587 bool enter)
35589 tree stmt, clauses;
35591 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
35592 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
35594 cp_parser_error (parser, enter
35595 ? "expected %<data%> in %<#pragma acc enter data%>"
35596 : "expected %<data%> in %<#pragma acc exit data%>");
35597 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35598 return NULL_TREE;
35601 const char *p =
35602 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35603 if (strcmp (p, "data") != 0)
35605 cp_parser_error (parser, "invalid pragma");
35606 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35607 return NULL_TREE;
35610 cp_lexer_consume_token (parser->lexer);
35612 if (enter)
35613 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
35614 "#pragma acc enter data", pragma_tok);
35615 else
35616 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
35617 "#pragma acc exit data", pragma_tok);
35619 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35621 error_at (pragma_tok->location,
35622 "%<#pragma acc enter data%> has no data movement clause");
35623 return NULL_TREE;
35626 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
35627 TREE_TYPE (stmt) = void_type_node;
35628 OMP_STANDALONE_CLAUSES (stmt) = clauses;
35629 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35630 add_stmt (stmt);
35631 return stmt;
35634 /* OpenACC 2.0:
35635 # pragma acc loop oacc-loop-clause[optseq] new-line
35636 structured-block */
35638 #define OACC_LOOP_CLAUSE_MASK \
35639 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
35640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
35641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
35642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
35643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
35644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
35645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
35646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
35647 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
35648 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
35650 static tree
35651 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
35652 omp_clause_mask mask, tree *cclauses, bool *if_p)
35654 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
35656 strcat (p_name, " loop");
35657 mask |= OACC_LOOP_CLAUSE_MASK;
35659 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
35660 cclauses == NULL);
35661 if (cclauses)
35663 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
35664 if (*cclauses)
35665 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
35666 if (clauses)
35667 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
35670 tree block = begin_omp_structured_block ();
35671 int save = cp_parser_begin_omp_structured_block (parser);
35672 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
35673 cp_parser_end_omp_structured_block (parser, save);
35674 add_stmt (finish_omp_structured_block (block));
35676 return stmt;
35679 /* OpenACC 2.0:
35680 # pragma acc kernels oacc-kernels-clause[optseq] new-line
35681 structured-block
35685 # pragma acc parallel oacc-parallel-clause[optseq] new-line
35686 structured-block
35689 #define OACC_KERNELS_CLAUSE_MASK \
35690 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
35696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35705 #define OACC_PARALLEL_CLAUSE_MASK \
35706 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35707 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35708 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35709 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35710 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
35712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
35714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
35716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
35717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
35723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
35724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
35725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35727 static tree
35728 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
35729 char *p_name, bool *if_p)
35731 omp_clause_mask mask;
35732 enum tree_code code;
35733 switch (cp_parser_pragma_kind (pragma_tok))
35735 case PRAGMA_OACC_KERNELS:
35736 strcat (p_name, " kernels");
35737 mask = OACC_KERNELS_CLAUSE_MASK;
35738 code = OACC_KERNELS;
35739 break;
35740 case PRAGMA_OACC_PARALLEL:
35741 strcat (p_name, " parallel");
35742 mask = OACC_PARALLEL_CLAUSE_MASK;
35743 code = OACC_PARALLEL;
35744 break;
35745 default:
35746 gcc_unreachable ();
35749 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35751 const char *p
35752 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35753 if (strcmp (p, "loop") == 0)
35755 cp_lexer_consume_token (parser->lexer);
35756 tree block = begin_omp_parallel ();
35757 tree clauses;
35758 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
35759 if_p);
35760 return finish_omp_construct (code, block, clauses);
35764 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
35766 tree block = begin_omp_parallel ();
35767 unsigned int save = cp_parser_begin_omp_structured_block (parser);
35768 cp_parser_statement (parser, NULL_TREE, false, if_p);
35769 cp_parser_end_omp_structured_block (parser, save);
35770 return finish_omp_construct (code, block, clauses);
35773 /* OpenACC 2.0:
35774 # pragma acc update oacc-update-clause[optseq] new-line
35777 #define OACC_UPDATE_CLAUSE_MASK \
35778 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
35780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
35781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
35783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
35785 static tree
35786 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
35788 tree stmt, clauses;
35790 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
35791 "#pragma acc update", pragma_tok);
35793 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35795 error_at (pragma_tok->location,
35796 "%<#pragma acc update%> must contain at least one "
35797 "%<device%> or %<host%> or %<self%> clause");
35798 return NULL_TREE;
35801 stmt = make_node (OACC_UPDATE);
35802 TREE_TYPE (stmt) = void_type_node;
35803 OACC_UPDATE_CLAUSES (stmt) = clauses;
35804 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35805 add_stmt (stmt);
35806 return stmt;
35809 /* OpenACC 2.0:
35810 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
35812 LOC is the location of the #pragma token.
35815 #define OACC_WAIT_CLAUSE_MASK \
35816 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
35818 static tree
35819 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
35821 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
35822 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35824 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
35825 list = cp_parser_oacc_wait_list (parser, loc, list);
35827 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
35828 "#pragma acc wait", pragma_tok);
35830 stmt = c_finish_oacc_wait (loc, list, clauses);
35831 stmt = finish_expr_stmt (stmt);
35833 return stmt;
35836 /* OpenMP 4.0:
35837 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
35839 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
35840 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
35844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
35845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
35847 static void
35848 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
35849 enum pragma_context context)
35851 bool first_p = parser->omp_declare_simd == NULL;
35852 cp_omp_declare_simd_data data;
35853 if (first_p)
35855 data.error_seen = false;
35856 data.fndecl_seen = false;
35857 data.tokens = vNULL;
35858 data.clauses = NULL_TREE;
35859 /* It is safe to take the address of a local variable; it will only be
35860 used while this scope is live. */
35861 parser->omp_declare_simd = &data;
35864 /* Store away all pragma tokens. */
35865 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
35866 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
35867 cp_lexer_consume_token (parser->lexer);
35868 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35869 parser->omp_declare_simd->error_seen = true;
35870 cp_parser_require_pragma_eol (parser, pragma_tok);
35871 struct cp_token_cache *cp
35872 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
35873 parser->omp_declare_simd->tokens.safe_push (cp);
35875 if (first_p)
35877 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
35878 cp_parser_pragma (parser, context, NULL);
35879 switch (context)
35881 case pragma_external:
35882 cp_parser_declaration (parser);
35883 break;
35884 case pragma_member:
35885 cp_parser_member_declaration (parser);
35886 break;
35887 case pragma_objc_icode:
35888 cp_parser_block_declaration (parser, /*statement_p=*/false);
35889 break;
35890 default:
35891 cp_parser_declaration_statement (parser);
35892 break;
35894 if (parser->omp_declare_simd
35895 && !parser->omp_declare_simd->error_seen
35896 && !parser->omp_declare_simd->fndecl_seen)
35897 error_at (pragma_tok->location,
35898 "%<#pragma omp declare simd%> not immediately followed by "
35899 "function declaration or definition");
35900 data.tokens.release ();
35901 parser->omp_declare_simd = NULL;
35905 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
35906 This function is modelled similar to the late parsing of omp declare
35907 simd. */
35909 static tree
35910 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
35912 struct cp_token_cache *ce;
35913 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
35914 int ii = 0;
35916 if (parser->omp_declare_simd != NULL
35917 || lookup_attribute ("simd", attrs))
35919 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
35920 "used in the same function marked as a Cilk Plus SIMD-enabled "
35921 "function");
35922 parser->cilk_simd_fn_info->tokens.release ();
35923 XDELETE (parser->cilk_simd_fn_info);
35924 parser->cilk_simd_fn_info = NULL;
35925 return attrs;
35927 if (!info->error_seen && info->fndecl_seen)
35929 error ("vector attribute not immediately followed by a single function"
35930 " declaration or definition");
35931 info->error_seen = true;
35933 if (info->error_seen)
35934 return attrs;
35936 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
35938 tree c, cl;
35940 cp_parser_push_lexer_for_tokens (parser, ce);
35941 parser->lexer->in_pragma = true;
35942 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
35943 "SIMD-enabled functions attribute",
35944 NULL);
35945 cp_parser_pop_lexer (parser);
35946 if (cl)
35947 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35949 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
35950 TREE_CHAIN (c) = attrs;
35951 attrs = c;
35953 c = build_tree_list (get_identifier ("omp declare simd"), cl);
35954 TREE_CHAIN (c) = attrs;
35955 if (processing_template_decl)
35956 ATTR_IS_DEPENDENT (c) = 1;
35957 attrs = c;
35959 info->fndecl_seen = true;
35960 parser->cilk_simd_fn_info->tokens.release ();
35961 XDELETE (parser->cilk_simd_fn_info);
35962 parser->cilk_simd_fn_info = NULL;
35963 return attrs;
35966 /* Finalize #pragma omp declare simd clauses after direct declarator has
35967 been parsed, and put that into "omp declare simd" attribute. */
35969 static tree
35970 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
35972 struct cp_token_cache *ce;
35973 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
35974 int i;
35976 if (!data->error_seen && data->fndecl_seen)
35978 error ("%<#pragma omp declare simd%> not immediately followed by "
35979 "a single function declaration or definition");
35980 data->error_seen = true;
35982 if (data->error_seen)
35983 return attrs;
35985 FOR_EACH_VEC_ELT (data->tokens, i, ce)
35987 tree c, cl;
35989 cp_parser_push_lexer_for_tokens (parser, ce);
35990 parser->lexer->in_pragma = true;
35991 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
35992 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
35993 cp_lexer_consume_token (parser->lexer);
35994 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
35995 "#pragma omp declare simd", pragma_tok);
35996 cp_parser_pop_lexer (parser);
35997 if (cl)
35998 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35999 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36000 TREE_CHAIN (c) = attrs;
36001 if (processing_template_decl)
36002 ATTR_IS_DEPENDENT (c) = 1;
36003 attrs = c;
36006 data->fndecl_seen = true;
36007 return attrs;
36011 /* OpenMP 4.0:
36012 # pragma omp declare target new-line
36013 declarations and definitions
36014 # pragma omp end declare target new-line
36016 OpenMP 4.5:
36017 # pragma omp declare target ( extended-list ) new-line
36019 # pragma omp declare target declare-target-clauses[seq] new-line */
36021 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
36022 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
36025 static void
36026 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
36028 tree clauses = NULL_TREE;
36029 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36030 clauses
36031 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
36032 "#pragma omp declare target", pragma_tok);
36033 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36035 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
36036 clauses);
36037 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
36038 cp_parser_require_pragma_eol (parser, pragma_tok);
36040 else
36042 cp_parser_require_pragma_eol (parser, pragma_tok);
36043 scope_chain->omp_declare_target_attribute++;
36044 return;
36046 if (scope_chain->omp_declare_target_attribute)
36047 error_at (pragma_tok->location,
36048 "%<#pragma omp declare target%> with clauses in between "
36049 "%<#pragma omp declare target%> without clauses and "
36050 "%<#pragma omp end declare target%>");
36051 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
36053 tree t = OMP_CLAUSE_DECL (c), id;
36054 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
36055 tree at2 = lookup_attribute ("omp declare target link",
36056 DECL_ATTRIBUTES (t));
36057 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
36059 id = get_identifier ("omp declare target link");
36060 std::swap (at1, at2);
36062 else
36063 id = get_identifier ("omp declare target");
36064 if (at2)
36066 error_at (OMP_CLAUSE_LOCATION (c),
36067 "%qD specified both in declare target %<link%> and %<to%>"
36068 " clauses", t);
36069 continue;
36071 if (!at1)
36073 symtab_node *node = symtab_node::get (t);
36074 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
36075 if (node != NULL)
36077 node->offloadable = 1;
36078 if (ENABLE_OFFLOADING)
36080 g->have_offload = true;
36081 if (is_a <varpool_node *> (node))
36082 vec_safe_push (offload_vars, t);
36089 static void
36090 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
36092 const char *p = "";
36093 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36095 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36096 p = IDENTIFIER_POINTER (id);
36098 if (strcmp (p, "declare") == 0)
36100 cp_lexer_consume_token (parser->lexer);
36101 p = "";
36102 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36104 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36105 p = IDENTIFIER_POINTER (id);
36107 if (strcmp (p, "target") == 0)
36108 cp_lexer_consume_token (parser->lexer);
36109 else
36111 cp_parser_error (parser, "expected %<target%>");
36112 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36113 return;
36116 else
36118 cp_parser_error (parser, "expected %<declare%>");
36119 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36120 return;
36122 cp_parser_require_pragma_eol (parser, pragma_tok);
36123 if (!scope_chain->omp_declare_target_attribute)
36124 error_at (pragma_tok->location,
36125 "%<#pragma omp end declare target%> without corresponding "
36126 "%<#pragma omp declare target%>");
36127 else
36128 scope_chain->omp_declare_target_attribute--;
36131 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
36132 expression and optional initializer clause of
36133 #pragma omp declare reduction. We store the expression(s) as
36134 either 3, 6 or 7 special statements inside of the artificial function's
36135 body. The first two statements are DECL_EXPRs for the artificial
36136 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
36137 expression that uses those variables.
36138 If there was any INITIALIZER clause, this is followed by further statements,
36139 the fourth and fifth statements are DECL_EXPRs for the artificial
36140 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
36141 constructor variant (first token after open paren is not omp_priv),
36142 then the sixth statement is a statement with the function call expression
36143 that uses the OMP_PRIV and optionally OMP_ORIG variable.
36144 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
36145 to initialize the OMP_PRIV artificial variable and there is seventh
36146 statement, a DECL_EXPR of the OMP_PRIV statement again. */
36148 static bool
36149 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
36151 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
36152 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
36153 type = TREE_TYPE (type);
36154 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
36155 DECL_ARTIFICIAL (omp_out) = 1;
36156 pushdecl (omp_out);
36157 add_decl_expr (omp_out);
36158 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
36159 DECL_ARTIFICIAL (omp_in) = 1;
36160 pushdecl (omp_in);
36161 add_decl_expr (omp_in);
36162 tree combiner;
36163 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
36165 keep_next_level (true);
36166 tree block = begin_omp_structured_block ();
36167 combiner = cp_parser_expression (parser);
36168 finish_expr_stmt (combiner);
36169 block = finish_omp_structured_block (block);
36170 add_stmt (block);
36172 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36173 return false;
36175 const char *p = "";
36176 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36178 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36179 p = IDENTIFIER_POINTER (id);
36182 if (strcmp (p, "initializer") == 0)
36184 cp_lexer_consume_token (parser->lexer);
36185 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36186 return false;
36188 p = "";
36189 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36191 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36192 p = IDENTIFIER_POINTER (id);
36195 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
36196 DECL_ARTIFICIAL (omp_priv) = 1;
36197 pushdecl (omp_priv);
36198 add_decl_expr (omp_priv);
36199 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
36200 DECL_ARTIFICIAL (omp_orig) = 1;
36201 pushdecl (omp_orig);
36202 add_decl_expr (omp_orig);
36204 keep_next_level (true);
36205 block = begin_omp_structured_block ();
36207 bool ctor = false;
36208 if (strcmp (p, "omp_priv") == 0)
36210 bool is_direct_init, is_non_constant_init;
36211 ctor = true;
36212 cp_lexer_consume_token (parser->lexer);
36213 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
36214 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
36215 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36216 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
36217 == CPP_CLOSE_PAREN
36218 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
36219 == CPP_CLOSE_PAREN))
36221 finish_omp_structured_block (block);
36222 error ("invalid initializer clause");
36223 return false;
36225 initializer = cp_parser_initializer (parser, &is_direct_init,
36226 &is_non_constant_init);
36227 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
36228 NULL_TREE, LOOKUP_ONLYCONVERTING);
36230 else
36232 cp_parser_parse_tentatively (parser);
36233 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
36234 /*check_dependency_p=*/true,
36235 /*template_p=*/NULL,
36236 /*declarator_p=*/false,
36237 /*optional_p=*/false);
36238 vec<tree, va_gc> *args;
36239 if (fn_name == error_mark_node
36240 || cp_parser_error_occurred (parser)
36241 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36242 || ((args = cp_parser_parenthesized_expression_list
36243 (parser, non_attr, /*cast_p=*/false,
36244 /*allow_expansion_p=*/true,
36245 /*non_constant_p=*/NULL)),
36246 cp_parser_error_occurred (parser)))
36248 finish_omp_structured_block (block);
36249 cp_parser_abort_tentative_parse (parser);
36250 cp_parser_error (parser, "expected id-expression (arguments)");
36251 return false;
36253 unsigned int i;
36254 tree arg;
36255 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
36256 if (arg == omp_priv
36257 || (TREE_CODE (arg) == ADDR_EXPR
36258 && TREE_OPERAND (arg, 0) == omp_priv))
36259 break;
36260 cp_parser_abort_tentative_parse (parser);
36261 if (arg == NULL_TREE)
36262 error ("one of the initializer call arguments should be %<omp_priv%>"
36263 " or %<&omp_priv%>");
36264 initializer = cp_parser_postfix_expression (parser, false, false, false,
36265 false, NULL);
36266 finish_expr_stmt (initializer);
36269 block = finish_omp_structured_block (block);
36270 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
36271 add_stmt (block);
36273 if (ctor)
36274 add_decl_expr (omp_orig);
36276 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36277 return false;
36280 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
36281 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
36283 return true;
36286 /* OpenMP 4.0
36287 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36288 initializer-clause[opt] new-line
36290 initializer-clause:
36291 initializer (omp_priv initializer)
36292 initializer (function-name (argument-list)) */
36294 static void
36295 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
36296 enum pragma_context)
36298 auto_vec<tree> types;
36299 enum tree_code reduc_code = ERROR_MARK;
36300 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
36301 unsigned int i;
36302 cp_token *first_token;
36303 cp_token_cache *cp;
36304 int errs;
36305 void *p;
36307 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
36308 p = obstack_alloc (&declarator_obstack, 0);
36310 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36311 goto fail;
36313 switch (cp_lexer_peek_token (parser->lexer)->type)
36315 case CPP_PLUS:
36316 reduc_code = PLUS_EXPR;
36317 break;
36318 case CPP_MULT:
36319 reduc_code = MULT_EXPR;
36320 break;
36321 case CPP_MINUS:
36322 reduc_code = MINUS_EXPR;
36323 break;
36324 case CPP_AND:
36325 reduc_code = BIT_AND_EXPR;
36326 break;
36327 case CPP_XOR:
36328 reduc_code = BIT_XOR_EXPR;
36329 break;
36330 case CPP_OR:
36331 reduc_code = BIT_IOR_EXPR;
36332 break;
36333 case CPP_AND_AND:
36334 reduc_code = TRUTH_ANDIF_EXPR;
36335 break;
36336 case CPP_OR_OR:
36337 reduc_code = TRUTH_ORIF_EXPR;
36338 break;
36339 case CPP_NAME:
36340 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
36341 break;
36342 default:
36343 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
36344 "%<|%>, %<&&%>, %<||%> or identifier");
36345 goto fail;
36348 if (reduc_code != ERROR_MARK)
36349 cp_lexer_consume_token (parser->lexer);
36351 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
36352 if (reduc_id == error_mark_node)
36353 goto fail;
36355 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36356 goto fail;
36358 /* Types may not be defined in declare reduction type list. */
36359 const char *saved_message;
36360 saved_message = parser->type_definition_forbidden_message;
36361 parser->type_definition_forbidden_message
36362 = G_("types may not be defined in declare reduction type list");
36363 bool saved_colon_corrects_to_scope_p;
36364 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36365 parser->colon_corrects_to_scope_p = false;
36366 bool saved_colon_doesnt_start_class_def_p;
36367 saved_colon_doesnt_start_class_def_p
36368 = parser->colon_doesnt_start_class_def_p;
36369 parser->colon_doesnt_start_class_def_p = true;
36371 while (true)
36373 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36374 type = cp_parser_type_id (parser);
36375 if (type == error_mark_node)
36377 else if (ARITHMETIC_TYPE_P (type)
36378 && (orig_reduc_id == NULL_TREE
36379 || (TREE_CODE (type) != COMPLEX_TYPE
36380 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36381 "min") == 0
36382 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36383 "max") == 0))))
36384 error_at (loc, "predeclared arithmetic type %qT in "
36385 "%<#pragma omp declare reduction%>", type);
36386 else if (TREE_CODE (type) == FUNCTION_TYPE
36387 || TREE_CODE (type) == METHOD_TYPE
36388 || TREE_CODE (type) == ARRAY_TYPE)
36389 error_at (loc, "function or array type %qT in "
36390 "%<#pragma omp declare reduction%>", type);
36391 else if (TREE_CODE (type) == REFERENCE_TYPE)
36392 error_at (loc, "reference type %qT in "
36393 "%<#pragma omp declare reduction%>", type);
36394 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
36395 error_at (loc, "const, volatile or __restrict qualified type %qT in "
36396 "%<#pragma omp declare reduction%>", type);
36397 else
36398 types.safe_push (type);
36400 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36401 cp_lexer_consume_token (parser->lexer);
36402 else
36403 break;
36406 /* Restore the saved message. */
36407 parser->type_definition_forbidden_message = saved_message;
36408 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36409 parser->colon_doesnt_start_class_def_p
36410 = saved_colon_doesnt_start_class_def_p;
36412 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
36413 || types.is_empty ())
36415 fail:
36416 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36417 goto done;
36420 first_token = cp_lexer_peek_token (parser->lexer);
36421 cp = NULL;
36422 errs = errorcount;
36423 FOR_EACH_VEC_ELT (types, i, type)
36425 tree fntype
36426 = build_function_type_list (void_type_node,
36427 cp_build_reference_type (type, false),
36428 NULL_TREE);
36429 tree this_reduc_id = reduc_id;
36430 if (!dependent_type_p (type))
36431 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
36432 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
36433 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
36434 DECL_ARTIFICIAL (fndecl) = 1;
36435 DECL_EXTERNAL (fndecl) = 1;
36436 DECL_DECLARED_INLINE_P (fndecl) = 1;
36437 DECL_IGNORED_P (fndecl) = 1;
36438 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
36439 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
36440 DECL_ATTRIBUTES (fndecl)
36441 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
36442 DECL_ATTRIBUTES (fndecl));
36443 if (processing_template_decl)
36444 fndecl = push_template_decl (fndecl);
36445 bool block_scope = false;
36446 tree block = NULL_TREE;
36447 if (current_function_decl)
36449 block_scope = true;
36450 DECL_CONTEXT (fndecl) = global_namespace;
36451 if (!processing_template_decl)
36452 pushdecl (fndecl);
36454 else if (current_class_type)
36456 if (cp == NULL)
36458 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36459 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36460 cp_lexer_consume_token (parser->lexer);
36461 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36462 goto fail;
36463 cp = cp_token_cache_new (first_token,
36464 cp_lexer_peek_nth_token (parser->lexer,
36465 2));
36467 DECL_STATIC_FUNCTION_P (fndecl) = 1;
36468 finish_member_declaration (fndecl);
36469 DECL_PENDING_INLINE_INFO (fndecl) = cp;
36470 DECL_PENDING_INLINE_P (fndecl) = 1;
36471 vec_safe_push (unparsed_funs_with_definitions, fndecl);
36472 continue;
36474 else
36476 DECL_CONTEXT (fndecl) = current_namespace;
36477 pushdecl (fndecl);
36479 if (!block_scope)
36480 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
36481 else
36482 block = begin_omp_structured_block ();
36483 if (cp)
36485 cp_parser_push_lexer_for_tokens (parser, cp);
36486 parser->lexer->in_pragma = true;
36488 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
36490 if (!block_scope)
36491 finish_function (0);
36492 else
36493 DECL_CONTEXT (fndecl) = current_function_decl;
36494 if (cp)
36495 cp_parser_pop_lexer (parser);
36496 goto fail;
36498 if (cp)
36499 cp_parser_pop_lexer (parser);
36500 if (!block_scope)
36501 finish_function (0);
36502 else
36504 DECL_CONTEXT (fndecl) = current_function_decl;
36505 block = finish_omp_structured_block (block);
36506 if (TREE_CODE (block) == BIND_EXPR)
36507 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
36508 else if (TREE_CODE (block) == STATEMENT_LIST)
36509 DECL_SAVED_TREE (fndecl) = block;
36510 if (processing_template_decl)
36511 add_decl_expr (fndecl);
36513 cp_check_omp_declare_reduction (fndecl);
36514 if (cp == NULL && types.length () > 1)
36515 cp = cp_token_cache_new (first_token,
36516 cp_lexer_peek_nth_token (parser->lexer, 2));
36517 if (errs != errorcount)
36518 break;
36521 cp_parser_require_pragma_eol (parser, pragma_tok);
36523 done:
36524 /* Free any declarators allocated. */
36525 obstack_free (&declarator_obstack, p);
36528 /* OpenMP 4.0
36529 #pragma omp declare simd declare-simd-clauses[optseq] new-line
36530 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36531 initializer-clause[opt] new-line
36532 #pragma omp declare target new-line */
36534 static void
36535 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
36536 enum pragma_context context)
36538 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36540 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36541 const char *p = IDENTIFIER_POINTER (id);
36543 if (strcmp (p, "simd") == 0)
36545 cp_lexer_consume_token (parser->lexer);
36546 cp_parser_omp_declare_simd (parser, pragma_tok,
36547 context);
36548 return;
36550 cp_ensure_no_omp_declare_simd (parser);
36551 if (strcmp (p, "reduction") == 0)
36553 cp_lexer_consume_token (parser->lexer);
36554 cp_parser_omp_declare_reduction (parser, pragma_tok,
36555 context);
36556 return;
36558 if (!flag_openmp) /* flag_openmp_simd */
36560 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36561 return;
36563 if (strcmp (p, "target") == 0)
36565 cp_lexer_consume_token (parser->lexer);
36566 cp_parser_omp_declare_target (parser, pragma_tok);
36567 return;
36570 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
36571 "or %<target%>");
36572 cp_parser_require_pragma_eol (parser, pragma_tok);
36575 /* OpenMP 4.5:
36576 #pragma omp taskloop taskloop-clause[optseq] new-line
36577 for-loop
36579 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
36580 for-loop */
36582 #define OMP_TASKLOOP_CLAUSE_MASK \
36583 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
36588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
36589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
36590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
36591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
36592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
36594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
36595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
36596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
36598 static tree
36599 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
36600 char *p_name, omp_clause_mask mask, tree *cclauses,
36601 bool *if_p)
36603 tree clauses, sb, ret;
36604 unsigned int save;
36605 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36607 strcat (p_name, " taskloop");
36608 mask |= OMP_TASKLOOP_CLAUSE_MASK;
36610 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36612 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36613 const char *p = IDENTIFIER_POINTER (id);
36615 if (strcmp (p, "simd") == 0)
36617 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36618 if (cclauses == NULL)
36619 cclauses = cclauses_buf;
36621 cp_lexer_consume_token (parser->lexer);
36622 if (!flag_openmp) /* flag_openmp_simd */
36623 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36624 cclauses, if_p);
36625 sb = begin_omp_structured_block ();
36626 save = cp_parser_begin_omp_structured_block (parser);
36627 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36628 cclauses, if_p);
36629 cp_parser_end_omp_structured_block (parser, save);
36630 tree body = finish_omp_structured_block (sb);
36631 if (ret == NULL)
36632 return ret;
36633 ret = make_node (OMP_TASKLOOP);
36634 TREE_TYPE (ret) = void_type_node;
36635 OMP_FOR_BODY (ret) = body;
36636 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36637 SET_EXPR_LOCATION (ret, loc);
36638 add_stmt (ret);
36639 return ret;
36642 if (!flag_openmp) /* flag_openmp_simd */
36644 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36645 return NULL_TREE;
36648 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36649 cclauses == NULL);
36650 if (cclauses)
36652 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
36653 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36656 sb = begin_omp_structured_block ();
36657 save = cp_parser_begin_omp_structured_block (parser);
36659 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
36660 if_p);
36662 cp_parser_end_omp_structured_block (parser, save);
36663 add_stmt (finish_omp_structured_block (sb));
36665 return ret;
36669 /* OpenACC 2.0:
36670 # pragma acc routine oacc-routine-clause[optseq] new-line
36671 function-definition
36673 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
36676 #define OACC_ROUTINE_CLAUSE_MASK \
36677 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
36683 /* Parse the OpenACC routine pragma. This has an optional '( name )'
36684 component, which must resolve to a declared namespace-scope
36685 function. The clauses are either processed directly (for a named
36686 function), or defered until the immediatley following declaration
36687 is parsed. */
36689 static void
36690 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
36691 enum pragma_context context)
36693 gcc_checking_assert (context == pragma_external);
36694 /* The checking for "another pragma following this one" in the "no optional
36695 '( name )'" case makes sure that we dont re-enter. */
36696 gcc_checking_assert (parser->oacc_routine == NULL);
36698 cp_oacc_routine_data data;
36699 data.error_seen = false;
36700 data.fndecl_seen = false;
36701 data.tokens = vNULL;
36702 data.clauses = NULL_TREE;
36703 data.loc = pragma_tok->location;
36704 /* It is safe to take the address of a local variable; it will only be
36705 used while this scope is live. */
36706 parser->oacc_routine = &data;
36708 /* Look for optional '( name )'. */
36709 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36711 cp_lexer_consume_token (parser->lexer); /* '(' */
36713 /* We parse the name as an id-expression. If it resolves to
36714 anything other than a non-overloaded function at namespace
36715 scope, it's an error. */
36716 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
36717 tree name = cp_parser_id_expression (parser,
36718 /*template_keyword_p=*/false,
36719 /*check_dependency_p=*/false,
36720 /*template_p=*/NULL,
36721 /*declarator_p=*/false,
36722 /*optional_p=*/false);
36723 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
36724 if (name != error_mark_node && decl == error_mark_node)
36725 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
36727 if (decl == error_mark_node
36728 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36730 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36731 parser->oacc_routine = NULL;
36732 return;
36735 data.clauses
36736 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36737 "#pragma acc routine",
36738 cp_lexer_peek_token (parser->lexer));
36740 if (decl && is_overloaded_fn (decl)
36741 && (TREE_CODE (decl) != FUNCTION_DECL
36742 || DECL_FUNCTION_TEMPLATE_P (decl)))
36744 error_at (name_loc,
36745 "%<#pragma acc routine%> names a set of overloads");
36746 parser->oacc_routine = NULL;
36747 return;
36750 /* Perhaps we should use the same rule as declarations in different
36751 namespaces? */
36752 if (!DECL_NAMESPACE_SCOPE_P (decl))
36754 error_at (name_loc,
36755 "%qD does not refer to a namespace scope function", decl);
36756 parser->oacc_routine = NULL;
36757 return;
36760 if (TREE_CODE (decl) != FUNCTION_DECL)
36762 error_at (name_loc, "%qD does not refer to a function", decl);
36763 parser->oacc_routine = NULL;
36764 return;
36767 cp_finalize_oacc_routine (parser, decl, false);
36768 parser->oacc_routine = NULL;
36770 else /* No optional '( name )'. */
36772 /* Store away all pragma tokens. */
36773 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36774 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36775 cp_lexer_consume_token (parser->lexer);
36776 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36777 parser->oacc_routine->error_seen = true;
36778 cp_parser_require_pragma_eol (parser, pragma_tok);
36779 struct cp_token_cache *cp
36780 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36781 parser->oacc_routine->tokens.safe_push (cp);
36783 /* Emit a helpful diagnostic if there's another pragma following this
36784 one. */
36785 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36787 cp_ensure_no_oacc_routine (parser);
36788 data.tokens.release ();
36789 /* ..., and then just keep going. */
36790 return;
36793 /* We only have to consider the pragma_external case here. */
36794 cp_parser_declaration (parser);
36795 if (parser->oacc_routine
36796 && !parser->oacc_routine->fndecl_seen)
36797 cp_ensure_no_oacc_routine (parser);
36798 else
36799 parser->oacc_routine = NULL;
36800 data.tokens.release ();
36804 /* Finalize #pragma acc routine clauses after direct declarator has
36805 been parsed. */
36807 static tree
36808 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
36810 struct cp_token_cache *ce;
36811 cp_oacc_routine_data *data = parser->oacc_routine;
36813 if (!data->error_seen && data->fndecl_seen)
36815 error_at (data->loc,
36816 "%<#pragma acc routine%> not immediately followed by "
36817 "a single function declaration or definition");
36818 data->error_seen = true;
36820 if (data->error_seen)
36821 return attrs;
36823 gcc_checking_assert (data->tokens.length () == 1);
36824 ce = data->tokens[0];
36826 cp_parser_push_lexer_for_tokens (parser, ce);
36827 parser->lexer->in_pragma = true;
36828 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36830 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36831 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
36832 parser->oacc_routine->clauses
36833 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36834 "#pragma acc routine", pragma_tok);
36835 cp_parser_pop_lexer (parser);
36836 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
36837 fndecl_seen. */
36839 return attrs;
36842 /* Apply any saved OpenACC routine clauses to a just-parsed
36843 declaration. */
36845 static void
36846 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
36848 if (__builtin_expect (parser->oacc_routine != NULL, 0))
36850 /* Keep going if we're in error reporting mode. */
36851 if (parser->oacc_routine->error_seen
36852 || fndecl == error_mark_node)
36853 return;
36855 if (parser->oacc_routine->fndecl_seen)
36857 error_at (parser->oacc_routine->loc,
36858 "%<#pragma acc routine%> not immediately followed by"
36859 " a single function declaration or definition");
36860 parser->oacc_routine = NULL;
36861 return;
36863 if (TREE_CODE (fndecl) != FUNCTION_DECL)
36865 cp_ensure_no_oacc_routine (parser);
36866 return;
36869 if (get_oacc_fn_attrib (fndecl))
36871 error_at (parser->oacc_routine->loc,
36872 "%<#pragma acc routine%> already applied to %qD", fndecl);
36873 parser->oacc_routine = NULL;
36874 return;
36877 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
36879 error_at (parser->oacc_routine->loc,
36880 "%<#pragma acc routine%> must be applied before %s",
36881 TREE_USED (fndecl) ? "use" : "definition");
36882 parser->oacc_routine = NULL;
36883 return;
36886 /* Process the routine's dimension clauses. */
36887 tree dims = build_oacc_routine_dims (parser->oacc_routine->clauses);
36888 replace_oacc_fn_attrib (fndecl, dims);
36890 /* Add an "omp declare target" attribute. */
36891 DECL_ATTRIBUTES (fndecl)
36892 = tree_cons (get_identifier ("omp declare target"),
36893 NULL_TREE, DECL_ATTRIBUTES (fndecl));
36895 /* Don't unset parser->oacc_routine here: we may still need it to
36896 diagnose wrong usage. But, remember that we've used this "#pragma acc
36897 routine". */
36898 parser->oacc_routine->fndecl_seen = true;
36902 /* Main entry point to OpenMP statement pragmas. */
36904 static void
36905 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36907 tree stmt;
36908 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
36909 omp_clause_mask mask (0);
36911 switch (cp_parser_pragma_kind (pragma_tok))
36913 case PRAGMA_OACC_ATOMIC:
36914 cp_parser_omp_atomic (parser, pragma_tok);
36915 return;
36916 case PRAGMA_OACC_CACHE:
36917 stmt = cp_parser_oacc_cache (parser, pragma_tok);
36918 break;
36919 case PRAGMA_OACC_DATA:
36920 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
36921 break;
36922 case PRAGMA_OACC_ENTER_DATA:
36923 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
36924 break;
36925 case PRAGMA_OACC_EXIT_DATA:
36926 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
36927 break;
36928 case PRAGMA_OACC_HOST_DATA:
36929 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
36930 break;
36931 case PRAGMA_OACC_KERNELS:
36932 case PRAGMA_OACC_PARALLEL:
36933 strcpy (p_name, "#pragma acc");
36934 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
36935 if_p);
36936 break;
36937 case PRAGMA_OACC_LOOP:
36938 strcpy (p_name, "#pragma acc");
36939 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
36940 if_p);
36941 break;
36942 case PRAGMA_OACC_UPDATE:
36943 stmt = cp_parser_oacc_update (parser, pragma_tok);
36944 break;
36945 case PRAGMA_OACC_WAIT:
36946 stmt = cp_parser_oacc_wait (parser, pragma_tok);
36947 break;
36948 case PRAGMA_OMP_ATOMIC:
36949 cp_parser_omp_atomic (parser, pragma_tok);
36950 return;
36951 case PRAGMA_OMP_CRITICAL:
36952 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
36953 break;
36954 case PRAGMA_OMP_DISTRIBUTE:
36955 strcpy (p_name, "#pragma omp");
36956 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
36957 if_p);
36958 break;
36959 case PRAGMA_OMP_FOR:
36960 strcpy (p_name, "#pragma omp");
36961 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
36962 if_p);
36963 break;
36964 case PRAGMA_OMP_MASTER:
36965 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
36966 break;
36967 case PRAGMA_OMP_PARALLEL:
36968 strcpy (p_name, "#pragma omp");
36969 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
36970 if_p);
36971 break;
36972 case PRAGMA_OMP_SECTIONS:
36973 strcpy (p_name, "#pragma omp");
36974 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
36975 break;
36976 case PRAGMA_OMP_SIMD:
36977 strcpy (p_name, "#pragma omp");
36978 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
36979 if_p);
36980 break;
36981 case PRAGMA_OMP_SINGLE:
36982 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
36983 break;
36984 case PRAGMA_OMP_TASK:
36985 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
36986 break;
36987 case PRAGMA_OMP_TASKGROUP:
36988 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
36989 break;
36990 case PRAGMA_OMP_TASKLOOP:
36991 strcpy (p_name, "#pragma omp");
36992 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
36993 if_p);
36994 break;
36995 case PRAGMA_OMP_TEAMS:
36996 strcpy (p_name, "#pragma omp");
36997 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
36998 if_p);
36999 break;
37000 default:
37001 gcc_unreachable ();
37004 protected_set_expr_location (stmt, pragma_tok->location);
37007 /* Transactional Memory parsing routines. */
37009 /* Parse a transaction attribute.
37011 txn-attribute:
37012 attribute
37013 [ [ identifier ] ]
37015 We use this instead of cp_parser_attributes_opt for transactions to avoid
37016 the pedwarn in C++98 mode. */
37018 static tree
37019 cp_parser_txn_attribute_opt (cp_parser *parser)
37021 cp_token *token;
37022 tree attr_name, attr = NULL;
37024 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
37025 return cp_parser_attributes_opt (parser);
37027 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
37028 return NULL_TREE;
37029 cp_lexer_consume_token (parser->lexer);
37030 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
37031 goto error1;
37033 token = cp_lexer_peek_token (parser->lexer);
37034 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
37036 token = cp_lexer_consume_token (parser->lexer);
37038 attr_name = (token->type == CPP_KEYWORD
37039 /* For keywords, use the canonical spelling,
37040 not the parsed identifier. */
37041 ? ridpointers[(int) token->keyword]
37042 : token->u.value);
37043 attr = build_tree_list (attr_name, NULL_TREE);
37045 else
37046 cp_parser_error (parser, "expected identifier");
37048 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37049 error1:
37050 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37051 return attr;
37054 /* Parse a __transaction_atomic or __transaction_relaxed statement.
37056 transaction-statement:
37057 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
37058 compound-statement
37059 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
37062 static tree
37063 cp_parser_transaction (cp_parser *parser, cp_token *token)
37065 unsigned char old_in = parser->in_transaction;
37066 unsigned char this_in = 1, new_in;
37067 enum rid keyword = token->keyword;
37068 tree stmt, attrs, noex;
37070 cp_lexer_consume_token (parser->lexer);
37072 if (keyword == RID_TRANSACTION_RELAXED
37073 || keyword == RID_SYNCHRONIZED)
37074 this_in |= TM_STMT_ATTR_RELAXED;
37075 else
37077 attrs = cp_parser_txn_attribute_opt (parser);
37078 if (attrs)
37079 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37082 /* Parse a noexcept specification. */
37083 if (keyword == RID_ATOMIC_NOEXCEPT)
37084 noex = boolean_true_node;
37085 else if (keyword == RID_ATOMIC_CANCEL)
37087 /* cancel-and-throw is unimplemented. */
37088 sorry ("atomic_cancel");
37089 noex = NULL_TREE;
37091 else
37092 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
37094 /* Keep track if we're in the lexical scope of an outer transaction. */
37095 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
37097 stmt = begin_transaction_stmt (token->location, NULL, this_in);
37099 parser->in_transaction = new_in;
37100 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
37101 parser->in_transaction = old_in;
37103 finish_transaction_stmt (stmt, NULL, this_in, noex);
37105 return stmt;
37108 /* Parse a __transaction_atomic or __transaction_relaxed expression.
37110 transaction-expression:
37111 __transaction_atomic txn-noexcept-spec[opt] ( expression )
37112 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
37115 static tree
37116 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
37118 unsigned char old_in = parser->in_transaction;
37119 unsigned char this_in = 1;
37120 cp_token *token;
37121 tree expr, noex;
37122 bool noex_expr;
37123 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37125 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37126 || keyword == RID_TRANSACTION_RELAXED);
37128 if (!flag_tm)
37129 error_at (loc,
37130 keyword == RID_TRANSACTION_RELAXED
37131 ? G_("%<__transaction_relaxed%> without transactional memory "
37132 "support enabled")
37133 : G_("%<__transaction_atomic%> without transactional memory "
37134 "support enabled"));
37136 token = cp_parser_require_keyword (parser, keyword,
37137 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37138 : RT_TRANSACTION_RELAXED));
37139 gcc_assert (token != NULL);
37141 if (keyword == RID_TRANSACTION_RELAXED)
37142 this_in |= TM_STMT_ATTR_RELAXED;
37144 /* Set this early. This might mean that we allow transaction_cancel in
37145 an expression that we find out later actually has to be a constexpr.
37146 However, we expect that cxx_constant_value will be able to deal with
37147 this; also, if the noexcept has no constexpr, then what we parse next
37148 really is a transaction's body. */
37149 parser->in_transaction = this_in;
37151 /* Parse a noexcept specification. */
37152 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
37153 true);
37155 if (!noex || !noex_expr
37156 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37158 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
37160 expr = cp_parser_expression (parser);
37161 expr = finish_parenthesized_expr (expr);
37163 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
37165 else
37167 /* The only expression that is available got parsed for the noexcept
37168 already. noexcept is true then. */
37169 expr = noex;
37170 noex = boolean_true_node;
37173 expr = build_transaction_expr (token->location, expr, this_in, noex);
37174 parser->in_transaction = old_in;
37176 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
37177 return error_mark_node;
37179 return (flag_tm ? expr : error_mark_node);
37182 /* Parse a function-transaction-block.
37184 function-transaction-block:
37185 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
37186 function-body
37187 __transaction_atomic txn-attribute[opt] function-try-block
37188 __transaction_relaxed ctor-initializer[opt] function-body
37189 __transaction_relaxed function-try-block
37192 static bool
37193 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
37195 unsigned char old_in = parser->in_transaction;
37196 unsigned char new_in = 1;
37197 tree compound_stmt, stmt, attrs;
37198 bool ctor_initializer_p;
37199 cp_token *token;
37201 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37202 || keyword == RID_TRANSACTION_RELAXED);
37203 token = cp_parser_require_keyword (parser, keyword,
37204 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37205 : RT_TRANSACTION_RELAXED));
37206 gcc_assert (token != NULL);
37208 if (keyword == RID_TRANSACTION_RELAXED)
37209 new_in |= TM_STMT_ATTR_RELAXED;
37210 else
37212 attrs = cp_parser_txn_attribute_opt (parser);
37213 if (attrs)
37214 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37217 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
37219 parser->in_transaction = new_in;
37221 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
37222 ctor_initializer_p = cp_parser_function_try_block (parser);
37223 else
37224 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
37225 (parser, /*in_function_try_block=*/false);
37227 parser->in_transaction = old_in;
37229 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
37231 return ctor_initializer_p;
37234 /* Parse a __transaction_cancel statement.
37236 cancel-statement:
37237 __transaction_cancel txn-attribute[opt] ;
37238 __transaction_cancel txn-attribute[opt] throw-expression ;
37240 ??? Cancel and throw is not yet implemented. */
37242 static tree
37243 cp_parser_transaction_cancel (cp_parser *parser)
37245 cp_token *token;
37246 bool is_outer = false;
37247 tree stmt, attrs;
37249 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
37250 RT_TRANSACTION_CANCEL);
37251 gcc_assert (token != NULL);
37253 attrs = cp_parser_txn_attribute_opt (parser);
37254 if (attrs)
37255 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
37257 /* ??? Parse cancel-and-throw here. */
37259 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37261 if (!flag_tm)
37263 error_at (token->location, "%<__transaction_cancel%> without "
37264 "transactional memory support enabled");
37265 return error_mark_node;
37267 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
37269 error_at (token->location, "%<__transaction_cancel%> within a "
37270 "%<__transaction_relaxed%>");
37271 return error_mark_node;
37273 else if (is_outer)
37275 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
37276 && !is_tm_may_cancel_outer (current_function_decl))
37278 error_at (token->location, "outer %<__transaction_cancel%> not "
37279 "within outer %<__transaction_atomic%>");
37280 error_at (token->location,
37281 " or a %<transaction_may_cancel_outer%> function");
37282 return error_mark_node;
37285 else if (parser->in_transaction == 0)
37287 error_at (token->location, "%<__transaction_cancel%> not within "
37288 "%<__transaction_atomic%>");
37289 return error_mark_node;
37292 stmt = build_tm_abort_call (token->location, is_outer);
37293 add_stmt (stmt);
37295 return stmt;
37298 /* The parser. */
37300 static GTY (()) cp_parser *the_parser;
37303 /* Special handling for the first token or line in the file. The first
37304 thing in the file might be #pragma GCC pch_preprocess, which loads a
37305 PCH file, which is a GC collection point. So we need to handle this
37306 first pragma without benefit of an existing lexer structure.
37308 Always returns one token to the caller in *FIRST_TOKEN. This is
37309 either the true first token of the file, or the first token after
37310 the initial pragma. */
37312 static void
37313 cp_parser_initial_pragma (cp_token *first_token)
37315 tree name = NULL;
37317 cp_lexer_get_preprocessor_token (NULL, first_token);
37318 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
37319 return;
37321 cp_lexer_get_preprocessor_token (NULL, first_token);
37322 if (first_token->type == CPP_STRING)
37324 name = first_token->u.value;
37326 cp_lexer_get_preprocessor_token (NULL, first_token);
37327 if (first_token->type != CPP_PRAGMA_EOL)
37328 error_at (first_token->location,
37329 "junk at end of %<#pragma GCC pch_preprocess%>");
37331 else
37332 error_at (first_token->location, "expected string literal");
37334 /* Skip to the end of the pragma. */
37335 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
37336 cp_lexer_get_preprocessor_token (NULL, first_token);
37338 /* Now actually load the PCH file. */
37339 if (name)
37340 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
37342 /* Read one more token to return to our caller. We have to do this
37343 after reading the PCH file in, since its pointers have to be
37344 live. */
37345 cp_lexer_get_preprocessor_token (NULL, first_token);
37348 /* Parses the grainsize pragma for the _Cilk_for statement.
37349 Syntax:
37350 #pragma cilk grainsize = <VALUE>. */
37352 static void
37353 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37355 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
37357 tree exp = cp_parser_binary_expression (parser, false, false,
37358 PREC_NOT_OPERATOR, NULL);
37359 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37360 if (!exp || exp == error_mark_node)
37362 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
37363 return;
37366 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
37367 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
37368 cp_parser_cilk_for (parser, exp, if_p);
37369 else
37370 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
37371 "%<#pragma cilk grainsize%> is not followed by "
37372 "%<_Cilk_for%>");
37373 return;
37375 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37378 /* Normal parsing of a pragma token. Here we can (and must) use the
37379 regular lexer. */
37381 static bool
37382 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
37384 cp_token *pragma_tok;
37385 unsigned int id;
37386 tree stmt;
37387 bool ret;
37389 pragma_tok = cp_lexer_consume_token (parser->lexer);
37390 gcc_assert (pragma_tok->type == CPP_PRAGMA);
37391 parser->lexer->in_pragma = true;
37393 id = cp_parser_pragma_kind (pragma_tok);
37394 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
37395 cp_ensure_no_omp_declare_simd (parser);
37396 switch (id)
37398 case PRAGMA_GCC_PCH_PREPROCESS:
37399 error_at (pragma_tok->location,
37400 "%<#pragma GCC pch_preprocess%> must be first");
37401 break;
37403 case PRAGMA_OMP_BARRIER:
37404 switch (context)
37406 case pragma_compound:
37407 cp_parser_omp_barrier (parser, pragma_tok);
37408 return false;
37409 case pragma_stmt:
37410 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
37411 "used in compound statements");
37412 break;
37413 default:
37414 goto bad_stmt;
37416 break;
37418 case PRAGMA_OMP_FLUSH:
37419 switch (context)
37421 case pragma_compound:
37422 cp_parser_omp_flush (parser, pragma_tok);
37423 return false;
37424 case pragma_stmt:
37425 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
37426 "used in compound statements");
37427 break;
37428 default:
37429 goto bad_stmt;
37431 break;
37433 case PRAGMA_OMP_TASKWAIT:
37434 switch (context)
37436 case pragma_compound:
37437 cp_parser_omp_taskwait (parser, pragma_tok);
37438 return false;
37439 case pragma_stmt:
37440 error_at (pragma_tok->location,
37441 "%<#pragma omp taskwait%> may only be "
37442 "used in compound statements");
37443 break;
37444 default:
37445 goto bad_stmt;
37447 break;
37449 case PRAGMA_OMP_TASKYIELD:
37450 switch (context)
37452 case pragma_compound:
37453 cp_parser_omp_taskyield (parser, pragma_tok);
37454 return false;
37455 case pragma_stmt:
37456 error_at (pragma_tok->location,
37457 "%<#pragma omp taskyield%> may only be "
37458 "used in compound statements");
37459 break;
37460 default:
37461 goto bad_stmt;
37463 break;
37465 case PRAGMA_OMP_CANCEL:
37466 switch (context)
37468 case pragma_compound:
37469 cp_parser_omp_cancel (parser, pragma_tok);
37470 return false;
37471 case pragma_stmt:
37472 error_at (pragma_tok->location,
37473 "%<#pragma omp cancel%> may only be "
37474 "used in compound statements");
37475 break;
37476 default:
37477 goto bad_stmt;
37479 break;
37481 case PRAGMA_OMP_CANCELLATION_POINT:
37482 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
37483 return false;
37485 case PRAGMA_OMP_THREADPRIVATE:
37486 cp_parser_omp_threadprivate (parser, pragma_tok);
37487 return false;
37489 case PRAGMA_OMP_DECLARE:
37490 cp_parser_omp_declare (parser, pragma_tok, context);
37491 return false;
37493 case PRAGMA_OACC_DECLARE:
37494 cp_parser_oacc_declare (parser, pragma_tok);
37495 return false;
37497 case PRAGMA_OACC_ROUTINE:
37498 if (context != pragma_external)
37500 error_at (pragma_tok->location,
37501 "%<#pragma acc routine%> must be at file scope");
37502 break;
37504 cp_parser_oacc_routine (parser, pragma_tok, context);
37505 return false;
37507 case PRAGMA_OACC_ATOMIC:
37508 case PRAGMA_OACC_CACHE:
37509 case PRAGMA_OACC_DATA:
37510 case PRAGMA_OACC_ENTER_DATA:
37511 case PRAGMA_OACC_EXIT_DATA:
37512 case PRAGMA_OACC_HOST_DATA:
37513 case PRAGMA_OACC_KERNELS:
37514 case PRAGMA_OACC_PARALLEL:
37515 case PRAGMA_OACC_LOOP:
37516 case PRAGMA_OACC_UPDATE:
37517 case PRAGMA_OACC_WAIT:
37518 case PRAGMA_OMP_ATOMIC:
37519 case PRAGMA_OMP_CRITICAL:
37520 case PRAGMA_OMP_DISTRIBUTE:
37521 case PRAGMA_OMP_FOR:
37522 case PRAGMA_OMP_MASTER:
37523 case PRAGMA_OMP_PARALLEL:
37524 case PRAGMA_OMP_SECTIONS:
37525 case PRAGMA_OMP_SIMD:
37526 case PRAGMA_OMP_SINGLE:
37527 case PRAGMA_OMP_TASK:
37528 case PRAGMA_OMP_TASKGROUP:
37529 case PRAGMA_OMP_TASKLOOP:
37530 case PRAGMA_OMP_TEAMS:
37531 if (context != pragma_stmt && context != pragma_compound)
37532 goto bad_stmt;
37533 stmt = push_omp_privatization_clauses (false);
37534 cp_parser_omp_construct (parser, pragma_tok, if_p);
37535 pop_omp_privatization_clauses (stmt);
37536 return true;
37538 case PRAGMA_OMP_ORDERED:
37539 stmt = push_omp_privatization_clauses (false);
37540 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
37541 pop_omp_privatization_clauses (stmt);
37542 return ret;
37544 case PRAGMA_OMP_TARGET:
37545 stmt = push_omp_privatization_clauses (false);
37546 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
37547 pop_omp_privatization_clauses (stmt);
37548 return ret;
37550 case PRAGMA_OMP_END_DECLARE_TARGET:
37551 cp_parser_omp_end_declare_target (parser, pragma_tok);
37552 return false;
37554 case PRAGMA_OMP_SECTION:
37555 error_at (pragma_tok->location,
37556 "%<#pragma omp section%> may only be used in "
37557 "%<#pragma omp sections%> construct");
37558 break;
37560 case PRAGMA_IVDEP:
37562 if (context == pragma_external)
37564 error_at (pragma_tok->location,
37565 "%<#pragma GCC ivdep%> must be inside a function");
37566 break;
37568 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37569 cp_token *tok;
37570 tok = cp_lexer_peek_token (the_parser->lexer);
37571 if (tok->type != CPP_KEYWORD
37572 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
37573 && tok->keyword != RID_DO))
37575 cp_parser_error (parser, "for, while or do statement expected");
37576 return false;
37578 cp_parser_iteration_statement (parser, if_p, true);
37579 return true;
37582 case PRAGMA_CILK_SIMD:
37583 if (context == pragma_external)
37585 error_at (pragma_tok->location,
37586 "%<#pragma simd%> must be inside a function");
37587 break;
37589 stmt = push_omp_privatization_clauses (false);
37590 cp_parser_cilk_simd (parser, pragma_tok, if_p);
37591 pop_omp_privatization_clauses (stmt);
37592 return true;
37594 case PRAGMA_CILK_GRAINSIZE:
37595 if (context == pragma_external)
37597 error_at (pragma_tok->location,
37598 "%<#pragma cilk grainsize%> must be inside a function");
37599 break;
37602 /* Ignore the pragma if Cilk Plus is not enabled. */
37603 if (flag_cilkplus)
37605 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
37606 return true;
37608 else
37610 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
37611 "%<#pragma cilk grainsize%>");
37612 break;
37615 default:
37616 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
37617 c_invoke_pragma_handler (id);
37618 break;
37620 bad_stmt:
37621 cp_parser_error (parser, "expected declaration specifiers");
37622 break;
37625 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37626 return false;
37629 /* The interface the pragma parsers have to the lexer. */
37631 enum cpp_ttype
37632 pragma_lex (tree *value, location_t *loc)
37634 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
37635 enum cpp_ttype ret = tok->type;
37637 *value = tok->u.value;
37638 if (loc)
37639 *loc = tok->location;
37641 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
37642 ret = CPP_EOF;
37643 else if (ret == CPP_STRING)
37644 *value = cp_parser_string_literal (the_parser, false, false);
37645 else
37647 if (ret == CPP_KEYWORD)
37648 ret = CPP_NAME;
37649 cp_lexer_consume_token (the_parser->lexer);
37652 return ret;
37656 /* External interface. */
37658 /* Parse one entire translation unit. */
37660 void
37661 c_parse_file (void)
37663 static bool already_called = false;
37665 if (already_called)
37666 fatal_error (input_location,
37667 "inter-module optimizations not implemented for C++");
37668 already_called = true;
37670 the_parser = cp_parser_new ();
37671 push_deferring_access_checks (flag_access_control
37672 ? dk_no_deferred : dk_no_check);
37673 cp_parser_translation_unit (the_parser);
37674 the_parser = NULL;
37677 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
37678 vectorlength clause:
37679 Syntax:
37680 vectorlength ( constant-expression ) */
37682 static tree
37683 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
37684 bool is_simd_fn)
37686 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37687 tree expr;
37688 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
37689 safelen clause. Thus, vectorlength is represented as OMP 4.0
37690 safelen. For SIMD-enabled function it is represented by OMP 4.0
37691 simdlen. */
37692 if (!is_simd_fn)
37693 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
37694 loc);
37695 else
37696 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
37697 loc);
37699 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37700 return error_mark_node;
37702 expr = cp_parser_constant_expression (parser);
37703 expr = maybe_constant_value (expr);
37705 /* If expr == error_mark_node, then don't emit any errors nor
37706 create a clause. if any of the above functions returns
37707 error mark node then they would have emitted an error message. */
37708 if (expr == error_mark_node)
37710 else if (!TREE_TYPE (expr)
37711 || !TREE_CONSTANT (expr)
37712 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
37713 error_at (loc, "vectorlength must be an integer constant");
37714 else if (TREE_CONSTANT (expr)
37715 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
37716 error_at (loc, "vectorlength must be a power of 2");
37717 else
37719 tree c;
37720 if (!is_simd_fn)
37722 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
37723 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
37724 OMP_CLAUSE_CHAIN (c) = clauses;
37725 clauses = c;
37727 else
37729 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
37730 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
37731 OMP_CLAUSE_CHAIN (c) = clauses;
37732 clauses = c;
37736 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37737 return error_mark_node;
37738 return clauses;
37741 /* Handles the Cilk Plus #pragma simd linear clause.
37742 Syntax:
37743 linear ( simd-linear-variable-list )
37745 simd-linear-variable-list:
37746 simd-linear-variable
37747 simd-linear-variable-list , simd-linear-variable
37749 simd-linear-variable:
37750 id-expression
37751 id-expression : simd-linear-step
37753 simd-linear-step:
37754 conditional-expression */
37756 static tree
37757 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
37759 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37761 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37762 return clauses;
37763 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37765 cp_parser_error (parser, "expected identifier");
37766 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37767 return error_mark_node;
37770 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37771 parser->colon_corrects_to_scope_p = false;
37772 while (1)
37774 cp_token *token = cp_lexer_peek_token (parser->lexer);
37775 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37777 cp_parser_error (parser, "expected variable-name");
37778 clauses = error_mark_node;
37779 break;
37782 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
37783 false, false);
37784 tree decl = cp_parser_lookup_name_simple (parser, var_name,
37785 token->location);
37786 if (decl == error_mark_node)
37788 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
37789 token->location);
37790 clauses = error_mark_node;
37792 else
37794 tree e = NULL_TREE;
37795 tree step_size = integer_one_node;
37797 /* If present, parse the linear step. Otherwise, assume the default
37798 value of 1. */
37799 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
37801 cp_lexer_consume_token (parser->lexer);
37803 e = cp_parser_assignment_expression (parser);
37804 e = maybe_constant_value (e);
37806 if (e == error_mark_node)
37808 /* If an error has occurred, then the whole pragma is
37809 considered ill-formed. Thus, no reason to keep
37810 parsing. */
37811 clauses = error_mark_node;
37812 break;
37814 else if (type_dependent_expression_p (e)
37815 || value_dependent_expression_p (e)
37816 || (TREE_TYPE (e)
37817 && INTEGRAL_TYPE_P (TREE_TYPE (e))
37818 && (TREE_CONSTANT (e)
37819 || DECL_P (e))))
37820 step_size = e;
37821 else
37822 cp_parser_error (parser,
37823 "step size must be an integer constant "
37824 "expression or an integer variable");
37827 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
37828 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
37829 OMP_CLAUSE_DECL (l) = decl;
37830 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
37831 OMP_CLAUSE_CHAIN (l) = clauses;
37832 clauses = l;
37834 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37835 cp_lexer_consume_token (parser->lexer);
37836 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37837 break;
37838 else
37840 error_at (cp_lexer_peek_token (parser->lexer)->location,
37841 "expected %<,%> or %<)%> after %qE", decl);
37842 clauses = error_mark_node;
37843 break;
37846 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37847 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37848 return clauses;
37851 /* Returns the name of the next clause. If the clause is not
37852 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
37853 token is not consumed. Otherwise, the appropriate enum from the
37854 pragma_simd_clause is returned and the token is consumed. */
37856 static pragma_omp_clause
37857 cp_parser_cilk_simd_clause_name (cp_parser *parser)
37859 pragma_omp_clause clause_type;
37860 cp_token *token = cp_lexer_peek_token (parser->lexer);
37862 if (token->keyword == RID_PRIVATE)
37863 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
37864 else if (!token->u.value || token->type != CPP_NAME)
37865 return PRAGMA_CILK_CLAUSE_NONE;
37866 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
37867 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
37868 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
37869 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
37870 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
37871 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
37872 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
37873 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
37874 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
37875 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
37876 else
37877 return PRAGMA_CILK_CLAUSE_NONE;
37879 cp_lexer_consume_token (parser->lexer);
37880 return clause_type;
37883 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
37885 static tree
37886 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
37888 tree clauses = NULL_TREE;
37890 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37891 && clauses != error_mark_node)
37893 pragma_omp_clause c_kind;
37894 c_kind = cp_parser_cilk_simd_clause_name (parser);
37895 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
37896 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
37897 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
37898 clauses = cp_parser_cilk_simd_linear (parser, clauses);
37899 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
37900 /* Use the OpenMP 4.0 equivalent function. */
37901 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
37902 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
37903 /* Use the OpenMP 4.0 equivalent function. */
37904 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
37905 clauses);
37906 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
37907 /* Use the OMP 4.0 equivalent function. */
37908 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
37909 clauses);
37910 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
37911 /* Use the OMP 4.0 equivalent function. */
37912 clauses = cp_parser_omp_clause_reduction (parser, clauses);
37913 else
37915 clauses = error_mark_node;
37916 cp_parser_error (parser, "expected %<#pragma simd%> clause");
37917 break;
37921 cp_parser_skip_to_pragma_eol (parser, pragma_token);
37923 if (clauses == error_mark_node)
37924 return error_mark_node;
37925 else
37926 return finish_omp_clauses (clauses, C_ORT_CILK);
37929 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
37931 static void
37932 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
37934 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
37936 if (clauses == error_mark_node)
37937 return;
37939 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
37941 error_at (cp_lexer_peek_token (parser->lexer)->location,
37942 "for statement expected");
37943 return;
37946 tree sb = begin_omp_structured_block ();
37947 int save = cp_parser_begin_omp_structured_block (parser);
37948 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
37949 if (ret)
37950 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
37951 cp_parser_end_omp_structured_block (parser, save);
37952 add_stmt (finish_omp_structured_block (sb));
37955 /* Main entry-point for parsing Cilk Plus _Cilk_for
37956 loops. The return value is error_mark_node
37957 when errors happen and CILK_FOR tree on success. */
37959 static tree
37960 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
37962 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
37963 gcc_unreachable ();
37965 tree sb = begin_omp_structured_block ();
37966 int save = cp_parser_begin_omp_structured_block (parser);
37968 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
37969 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
37970 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
37971 clauses = finish_omp_clauses (clauses, C_ORT_CILK);
37973 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
37974 if (ret)
37975 cpp_validate_cilk_plus_loop (ret);
37976 else
37977 ret = error_mark_node;
37979 cp_parser_end_omp_structured_block (parser, save);
37980 add_stmt (finish_omp_structured_block (sb));
37981 return ret;
37984 /* Create an identifier for a generic parameter type (a synthesized
37985 template parameter implied by `auto' or a concept identifier). */
37987 static GTY(()) int generic_parm_count;
37988 static tree
37989 make_generic_type_name ()
37991 char buf[32];
37992 sprintf (buf, "auto:%d", ++generic_parm_count);
37993 return get_identifier (buf);
37996 /* Predicate that behaves as is_auto_or_concept but matches the parent
37997 node of the generic type rather than the generic type itself. This
37998 allows for type transformation in add_implicit_template_parms. */
38000 static inline bool
38001 tree_type_is_auto_or_concept (const_tree t)
38003 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
38006 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38007 (creating a new template parameter list if necessary). Returns the newly
38008 created template type parm. */
38010 static tree
38011 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
38013 gcc_assert (current_binding_level->kind == sk_function_parms);
38015 /* Before committing to modifying any scope, if we're in an
38016 implicit template scope, and we're trying to synthesize a
38017 constrained parameter, try to find a previous parameter with
38018 the same name. This is the same-type rule for abbreviated
38019 function templates.
38021 NOTE: We can generate implicit parameters when tentatively
38022 parsing a nested name specifier, only to reject that parse
38023 later. However, matching the same template-id as part of a
38024 direct-declarator should generate an identical template
38025 parameter, so this rule will merge them. */
38026 if (parser->implicit_template_scope && constr)
38028 tree t = parser->implicit_template_parms;
38029 while (t)
38031 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
38033 tree d = TREE_VALUE (t);
38034 if (TREE_CODE (d) == PARM_DECL)
38035 /* Return the TEMPLATE_PARM_INDEX. */
38036 d = DECL_INITIAL (d);
38037 return d;
38039 t = TREE_CHAIN (t);
38043 /* We are either continuing a function template that already contains implicit
38044 template parameters, creating a new fully-implicit function template, or
38045 extending an existing explicit function template with implicit template
38046 parameters. */
38048 cp_binding_level *const entry_scope = current_binding_level;
38050 bool become_template = false;
38051 cp_binding_level *parent_scope = 0;
38053 if (parser->implicit_template_scope)
38055 gcc_assert (parser->implicit_template_parms);
38057 current_binding_level = parser->implicit_template_scope;
38059 else
38061 /* Roll back to the existing template parameter scope (in the case of
38062 extending an explicit function template) or introduce a new template
38063 parameter scope ahead of the function parameter scope (or class scope
38064 in the case of out-of-line member definitions). The function scope is
38065 added back after template parameter synthesis below. */
38067 cp_binding_level *scope = entry_scope;
38069 while (scope->kind == sk_function_parms)
38071 parent_scope = scope;
38072 scope = scope->level_chain;
38074 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
38076 /* If not defining a class, then any class scope is a scope level in
38077 an out-of-line member definition. In this case simply wind back
38078 beyond the first such scope to inject the template parameter list.
38079 Otherwise wind back to the class being defined. The latter can
38080 occur in class member friend declarations such as:
38082 class A {
38083 void foo (auto);
38085 class B {
38086 friend void A::foo (auto);
38089 The template parameter list synthesized for the friend declaration
38090 must be injected in the scope of 'B'. This can also occur in
38091 erroneous cases such as:
38093 struct A {
38094 struct B {
38095 void foo (auto);
38097 void B::foo (auto) {}
38100 Here the attempted definition of 'B::foo' within 'A' is ill-formed
38101 but, nevertheless, the template parameter list synthesized for the
38102 declarator should be injected into the scope of 'A' as if the
38103 ill-formed template was specified explicitly. */
38105 while (scope->kind == sk_class && !scope->defining_class_p)
38107 parent_scope = scope;
38108 scope = scope->level_chain;
38112 current_binding_level = scope;
38114 if (scope->kind != sk_template_parms
38115 || !function_being_declared_is_template_p (parser))
38117 /* Introduce a new template parameter list for implicit template
38118 parameters. */
38120 become_template = true;
38122 parser->implicit_template_scope
38123 = begin_scope (sk_template_parms, NULL);
38125 ++processing_template_decl;
38127 parser->fully_implicit_function_template_p = true;
38128 ++parser->num_template_parameter_lists;
38130 else
38132 /* Synthesize implicit template parameters at the end of the explicit
38133 template parameter list. */
38135 gcc_assert (current_template_parms);
38137 parser->implicit_template_scope = scope;
38139 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38140 parser->implicit_template_parms
38141 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
38145 /* Synthesize a new template parameter and track the current template
38146 parameter chain with implicit_template_parms. */
38148 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
38149 tree synth_id = make_generic_type_name ();
38150 tree synth_tmpl_parm;
38151 bool non_type = false;
38153 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
38154 synth_tmpl_parm
38155 = finish_template_type_parm (class_type_node, synth_id);
38156 else if (TREE_CODE (proto) == TEMPLATE_DECL)
38157 synth_tmpl_parm
38158 = finish_constrained_template_template_parm (proto, synth_id);
38159 else
38161 synth_tmpl_parm = copy_decl (proto);
38162 DECL_NAME (synth_tmpl_parm) = synth_id;
38163 non_type = true;
38166 // Attach the constraint to the parm before processing.
38167 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
38168 TREE_TYPE (node) = constr;
38169 tree new_parm
38170 = process_template_parm (parser->implicit_template_parms,
38171 input_location,
38172 node,
38173 /*non_type=*/non_type,
38174 /*param_pack=*/false);
38176 // Chain the new parameter to the list of implicit parameters.
38177 if (parser->implicit_template_parms)
38178 parser->implicit_template_parms
38179 = TREE_CHAIN (parser->implicit_template_parms);
38180 else
38181 parser->implicit_template_parms = new_parm;
38183 tree new_decl = getdecls ();
38184 if (non_type)
38185 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
38186 new_decl = DECL_INITIAL (new_decl);
38188 /* If creating a fully implicit function template, start the new implicit
38189 template parameter list with this synthesized type, otherwise grow the
38190 current template parameter list. */
38192 if (become_template)
38194 parent_scope->level_chain = current_binding_level;
38196 tree new_parms = make_tree_vec (1);
38197 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
38198 current_template_parms = tree_cons (size_int (processing_template_decl),
38199 new_parms, current_template_parms);
38201 else
38203 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38204 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
38205 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
38206 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
38209 // If the new parameter was constrained, we need to add that to the
38210 // constraints in the template parameter list.
38211 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
38213 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
38214 reqs = conjoin_constraints (reqs, req);
38215 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
38218 current_binding_level = entry_scope;
38220 return new_decl;
38223 /* Finish the declaration of a fully implicit function template. Such a
38224 template has no explicit template parameter list so has not been through the
38225 normal template head and tail processing. synthesize_implicit_template_parm
38226 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
38227 provided if the declaration is a class member such that its template
38228 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
38229 form is returned. Otherwise NULL_TREE is returned. */
38231 static tree
38232 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
38234 gcc_assert (parser->fully_implicit_function_template_p);
38236 if (member_decl_opt && member_decl_opt != error_mark_node
38237 && DECL_VIRTUAL_P (member_decl_opt))
38239 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
38240 "implicit templates may not be %<virtual%>");
38241 DECL_VIRTUAL_P (member_decl_opt) = false;
38244 if (member_decl_opt)
38245 member_decl_opt = finish_member_template_decl (member_decl_opt);
38246 end_template_decl ();
38248 parser->fully_implicit_function_template_p = false;
38249 --parser->num_template_parameter_lists;
38251 return member_decl_opt;
38254 #include "gt-cp-parser.h"