2018-05-17 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / cp / parser.c
blobc0058085ee924820f950d1524c247fd2eb16bb09
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2018 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 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "c-family/name-hint.h"
49 /* The lexer. */
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
54 static cp_token eof_token =
56 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
59 /* The various kinds of non integral constant we encounter. */
60 enum non_integral_constant {
61 NIC_NONE,
62 /* floating-point literal */
63 NIC_FLOAT,
64 /* %<this%> */
65 NIC_THIS,
66 /* %<__FUNCTION__%> */
67 NIC_FUNC_NAME,
68 /* %<__PRETTY_FUNCTION__%> */
69 NIC_PRETTY_FUNC,
70 /* %<__func__%> */
71 NIC_C99_FUNC,
72 /* "%<va_arg%> */
73 NIC_VA_ARG,
74 /* a cast */
75 NIC_CAST,
76 /* %<typeid%> operator */
77 NIC_TYPEID,
78 /* non-constant compound literals */
79 NIC_NCC,
80 /* a function call */
81 NIC_FUNC_CALL,
82 /* an increment */
83 NIC_INC,
84 /* an decrement */
85 NIC_DEC,
86 /* an array reference */
87 NIC_ARRAY_REF,
88 /* %<->%> */
89 NIC_ARROW,
90 /* %<.%> */
91 NIC_POINT,
92 /* the address of a label */
93 NIC_ADDR_LABEL,
94 /* %<*%> */
95 NIC_STAR,
96 /* %<&%> */
97 NIC_ADDR,
98 /* %<++%> */
99 NIC_PREINCREMENT,
100 /* %<--%> */
101 NIC_PREDECREMENT,
102 /* %<new%> */
103 NIC_NEW,
104 /* %<delete%> */
105 NIC_DEL,
106 /* calls to overloaded operators */
107 NIC_OVERLOADED,
108 /* an assignment */
109 NIC_ASSIGNMENT,
110 /* a comma operator */
111 NIC_COMMA,
112 /* a call to a constructor */
113 NIC_CONSTRUCTOR,
114 /* a transaction expression */
115 NIC_TRANSACTION
118 /* The various kinds of errors about name-lookup failing. */
119 enum name_lookup_error {
120 /* NULL */
121 NLE_NULL,
122 /* is not a type */
123 NLE_TYPE,
124 /* is not a class or namespace */
125 NLE_CXX98,
126 /* is not a class, namespace, or enumeration */
127 NLE_NOT_CXX98
130 /* The various kinds of required token */
131 enum required_token {
132 RT_NONE,
133 RT_SEMICOLON, /* ';' */
134 RT_OPEN_PAREN, /* '(' */
135 RT_CLOSE_BRACE, /* '}' */
136 RT_OPEN_BRACE, /* '{' */
137 RT_CLOSE_SQUARE, /* ']' */
138 RT_OPEN_SQUARE, /* '[' */
139 RT_COMMA, /* ',' */
140 RT_SCOPE, /* '::' */
141 RT_LESS, /* '<' */
142 RT_GREATER, /* '>' */
143 RT_EQ, /* '=' */
144 RT_ELLIPSIS, /* '...' */
145 RT_MULT, /* '*' */
146 RT_COMPL, /* '~' */
147 RT_COLON, /* ':' */
148 RT_COLON_SCOPE, /* ':' or '::' */
149 RT_CLOSE_PAREN, /* ')' */
150 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
151 RT_PRAGMA_EOL, /* end of line */
152 RT_NAME, /* identifier */
154 /* The type is CPP_KEYWORD */
155 RT_NEW, /* new */
156 RT_DELETE, /* delete */
157 RT_RETURN, /* return */
158 RT_WHILE, /* while */
159 RT_EXTERN, /* extern */
160 RT_STATIC_ASSERT, /* static_assert */
161 RT_DECLTYPE, /* decltype */
162 RT_OPERATOR, /* operator */
163 RT_CLASS, /* class */
164 RT_TEMPLATE, /* template */
165 RT_NAMESPACE, /* namespace */
166 RT_USING, /* using */
167 RT_ASM, /* asm */
168 RT_TRY, /* try */
169 RT_CATCH, /* catch */
170 RT_THROW, /* throw */
171 RT_LABEL, /* __label__ */
172 RT_AT_TRY, /* @try */
173 RT_AT_SYNCHRONIZED, /* @synchronized */
174 RT_AT_THROW, /* @throw */
176 RT_SELECT, /* selection-statement */
177 RT_ITERATION, /* iteration-statement */
178 RT_JUMP, /* jump-statement */
179 RT_CLASS_KEY, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL /* __transaction_cancel */
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
189 class type_id_in_expr_sentinel
191 cp_parser *parser;
192 bool saved;
193 public:
194 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
195 : parser (parser),
196 saved (parser->in_type_id_in_expr_p)
197 { parser->in_type_id_in_expr_p = set; }
198 ~type_id_in_expr_sentinel ()
199 { parser->in_type_id_in_expr_p = saved; }
202 /* Prototypes. */
204 static cp_lexer *cp_lexer_new_main
205 (void);
206 static cp_lexer *cp_lexer_new_from_tokens
207 (cp_token_cache *tokens);
208 static void cp_lexer_destroy
209 (cp_lexer *);
210 static int cp_lexer_saving_tokens
211 (const cp_lexer *);
212 static cp_token *cp_lexer_token_at
213 (cp_lexer *, cp_token_position);
214 static void cp_lexer_get_preprocessor_token
215 (cp_lexer *, cp_token *);
216 static inline cp_token *cp_lexer_peek_token
217 (cp_lexer *);
218 static cp_token *cp_lexer_peek_nth_token
219 (cp_lexer *, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer *, enum cpp_ttype);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer *, enum cpp_ttype);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer *, enum rid);
226 static cp_token *cp_lexer_consume_token
227 (cp_lexer *);
228 static void cp_lexer_purge_token
229 (cp_lexer *);
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer *, cp_token_position);
232 static void cp_lexer_save_tokens
233 (cp_lexer *);
234 static void cp_lexer_commit_tokens
235 (cp_lexer *);
236 static void cp_lexer_rollback_tokens
237 (cp_lexer *);
238 static void cp_lexer_print_token
239 (FILE *, cp_token *);
240 static inline bool cp_lexer_debugging_p
241 (cp_lexer *);
242 static void cp_lexer_start_debugging
243 (cp_lexer *) ATTRIBUTE_UNUSED;
244 static void cp_lexer_stop_debugging
245 (cp_lexer *) ATTRIBUTE_UNUSED;
247 static cp_token_cache *cp_token_cache_new
248 (cp_token *, cp_token *);
250 static void cp_parser_initial_pragma
251 (cp_token *);
253 static bool cp_parser_omp_declare_reduction_exprs
254 (tree, cp_parser *);
255 static void cp_finalize_oacc_routine
256 (cp_parser *, tree, bool);
258 /* Manifest constants. */
259 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
260 #define CP_SAVED_TOKEN_STACK 5
262 /* Variables. */
264 /* The stream to which debugging output should be written. */
265 static FILE *cp_lexer_debug_stream;
267 /* Nonzero if we are parsing an unevaluated operand: an operand to
268 sizeof, typeof, or alignof. */
269 int cp_unevaluated_operand;
271 /* Dump up to NUM tokens in BUFFER to FILE starting with token
272 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
273 first token in BUFFER. If NUM is 0, dump all the tokens. If
274 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
275 highlighted by surrounding it in [[ ]]. */
277 static void
278 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
279 cp_token *start_token, unsigned num,
280 cp_token *curr_token)
282 unsigned i, nprinted;
283 cp_token *token;
284 bool do_print;
286 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
288 if (buffer == NULL)
289 return;
291 if (num == 0)
292 num = buffer->length ();
294 if (start_token == NULL)
295 start_token = buffer->address ();
297 if (start_token > buffer->address ())
299 cp_lexer_print_token (file, &(*buffer)[0]);
300 fprintf (file, " ... ");
303 do_print = false;
304 nprinted = 0;
305 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
307 if (token == start_token)
308 do_print = true;
310 if (!do_print)
311 continue;
313 nprinted++;
314 if (token == curr_token)
315 fprintf (file, "[[");
317 cp_lexer_print_token (file, token);
319 if (token == curr_token)
320 fprintf (file, "]]");
322 switch (token->type)
324 case CPP_SEMICOLON:
325 case CPP_OPEN_BRACE:
326 case CPP_CLOSE_BRACE:
327 case CPP_EOF:
328 fputc ('\n', file);
329 break;
331 default:
332 fputc (' ', file);
336 if (i == num && i < buffer->length ())
338 fprintf (file, " ... ");
339 cp_lexer_print_token (file, &buffer->last ());
342 fprintf (file, "\n");
346 /* Dump all tokens in BUFFER to stderr. */
348 void
349 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
351 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
354 DEBUG_FUNCTION void
355 debug (vec<cp_token, va_gc> &ref)
357 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
360 DEBUG_FUNCTION void
361 debug (vec<cp_token, va_gc> *ptr)
363 if (ptr)
364 debug (*ptr);
365 else
366 fprintf (stderr, "<nil>\n");
370 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
371 description for T. */
373 static void
374 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
376 if (t)
378 fprintf (file, "%s: ", desc);
379 print_node_brief (file, "", t, 0);
384 /* Dump parser context C to FILE. */
386 static void
387 cp_debug_print_context (FILE *file, cp_parser_context *c)
389 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
390 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
391 print_node_brief (file, "", c->object_type, 0);
392 fprintf (file, "}\n");
396 /* Print the stack of parsing contexts to FILE starting with FIRST. */
398 static void
399 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
401 unsigned i;
402 cp_parser_context *c;
404 fprintf (file, "Parsing context stack:\n");
405 for (i = 0, c = first; c; c = c->next, i++)
407 fprintf (file, "\t#%u: ", i);
408 cp_debug_print_context (file, c);
413 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
415 static void
416 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
418 if (flag)
419 fprintf (file, "%s: true\n", desc);
423 /* Print an unparsed function entry UF to FILE. */
425 static void
426 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
428 unsigned i;
429 cp_default_arg_entry *default_arg_fn;
430 tree fn;
432 fprintf (file, "\tFunctions with default args:\n");
433 for (i = 0;
434 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
435 i++)
437 fprintf (file, "\t\tClass type: ");
438 print_node_brief (file, "", default_arg_fn->class_type, 0);
439 fprintf (file, "\t\tDeclaration: ");
440 print_node_brief (file, "", default_arg_fn->decl, 0);
441 fprintf (file, "\n");
444 fprintf (file, "\n\tFunctions with definitions that require "
445 "post-processing\n\t\t");
446 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
448 print_node_brief (file, "", fn, 0);
449 fprintf (file, " ");
451 fprintf (file, "\n");
453 fprintf (file, "\n\tNon-static data members with initializers that require "
454 "post-processing\n\t\t");
455 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
457 print_node_brief (file, "", fn, 0);
458 fprintf (file, " ");
460 fprintf (file, "\n");
464 /* Print the stack of unparsed member functions S to FILE. */
466 static void
467 cp_debug_print_unparsed_queues (FILE *file,
468 vec<cp_unparsed_functions_entry, va_gc> *s)
470 unsigned i;
471 cp_unparsed_functions_entry *uf;
473 fprintf (file, "Unparsed functions\n");
474 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
476 fprintf (file, "#%u:\n", i);
477 cp_debug_print_unparsed_function (file, uf);
482 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
483 the given PARSER. If FILE is NULL, the output is printed on stderr. */
485 static void
486 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
488 cp_token *next_token, *first_token, *start_token;
490 if (file == NULL)
491 file = stderr;
493 next_token = parser->lexer->next_token;
494 first_token = parser->lexer->buffer->address ();
495 start_token = (next_token > first_token + window_size / 2)
496 ? next_token - window_size / 2
497 : first_token;
498 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
499 next_token);
503 /* Dump debugging information for the given PARSER. If FILE is NULL,
504 the output is printed on stderr. */
506 void
507 cp_debug_parser (FILE *file, cp_parser *parser)
509 const size_t window_size = 20;
510 cp_token *token;
511 expanded_location eloc;
513 if (file == NULL)
514 file = stderr;
516 fprintf (file, "Parser state\n\n");
517 fprintf (file, "Number of tokens: %u\n",
518 vec_safe_length (parser->lexer->buffer));
519 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
520 cp_debug_print_tree_if_set (file, "Object scope",
521 parser->object_scope);
522 cp_debug_print_tree_if_set (file, "Qualifying scope",
523 parser->qualifying_scope);
524 cp_debug_print_context_stack (file, parser->context);
525 cp_debug_print_flag (file, "Allow GNU extensions",
526 parser->allow_gnu_extensions_p);
527 cp_debug_print_flag (file, "'>' token is greater-than",
528 parser->greater_than_is_operator_p);
529 cp_debug_print_flag (file, "Default args allowed in current "
530 "parameter list", parser->default_arg_ok_p);
531 cp_debug_print_flag (file, "Parsing integral constant-expression",
532 parser->integral_constant_expression_p);
533 cp_debug_print_flag (file, "Allow non-constant expression in current "
534 "constant-expression",
535 parser->allow_non_integral_constant_expression_p);
536 cp_debug_print_flag (file, "Seen non-constant expression",
537 parser->non_integral_constant_expression_p);
538 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
539 "current context",
540 parser->local_variables_forbidden_p);
541 cp_debug_print_flag (file, "In unbraced linkage specification",
542 parser->in_unbraced_linkage_specification_p);
543 cp_debug_print_flag (file, "Parsing a declarator",
544 parser->in_declarator_p);
545 cp_debug_print_flag (file, "In template argument list",
546 parser->in_template_argument_list_p);
547 cp_debug_print_flag (file, "Parsing an iteration statement",
548 parser->in_statement & IN_ITERATION_STMT);
549 cp_debug_print_flag (file, "Parsing a switch statement",
550 parser->in_statement & IN_SWITCH_STMT);
551 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
552 parser->in_statement & IN_OMP_BLOCK);
553 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
554 parser->in_statement & IN_OMP_FOR);
555 cp_debug_print_flag (file, "Parsing an if statement",
556 parser->in_statement & IN_IF_STMT);
557 cp_debug_print_flag (file, "Parsing a type-id in an expression "
558 "context", parser->in_type_id_in_expr_p);
559 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
560 parser->implicit_extern_c);
561 cp_debug_print_flag (file, "String expressions should be translated "
562 "to execution character set",
563 parser->translate_strings_p);
564 cp_debug_print_flag (file, "Parsing function body outside of a "
565 "local class", parser->in_function_body);
566 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
567 parser->colon_corrects_to_scope_p);
568 cp_debug_print_flag (file, "Colon doesn't start a class definition",
569 parser->colon_doesnt_start_class_def_p);
570 if (parser->type_definition_forbidden_message)
571 fprintf (file, "Error message for forbidden type definitions: %s\n",
572 parser->type_definition_forbidden_message);
573 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
574 fprintf (file, "Number of class definitions in progress: %u\n",
575 parser->num_classes_being_defined);
576 fprintf (file, "Number of template parameter lists for the current "
577 "declaration: %u\n", parser->num_template_parameter_lists);
578 cp_debug_parser_tokens (file, parser, window_size);
579 token = parser->lexer->next_token;
580 fprintf (file, "Next token to parse:\n");
581 fprintf (file, "\tToken: ");
582 cp_lexer_print_token (file, token);
583 eloc = expand_location (token->location);
584 fprintf (file, "\n\tFile: %s\n", eloc.file);
585 fprintf (file, "\tLine: %d\n", eloc.line);
586 fprintf (file, "\tColumn: %d\n", eloc.column);
589 DEBUG_FUNCTION void
590 debug (cp_parser &ref)
592 cp_debug_parser (stderr, &ref);
595 DEBUG_FUNCTION void
596 debug (cp_parser *ptr)
598 if (ptr)
599 debug (*ptr);
600 else
601 fprintf (stderr, "<nil>\n");
604 /* Allocate memory for a new lexer object and return it. */
606 static cp_lexer *
607 cp_lexer_alloc (void)
609 cp_lexer *lexer;
611 c_common_no_more_pch ();
613 /* Allocate the memory. */
614 lexer = ggc_cleared_alloc<cp_lexer> ();
616 /* Initially we are not debugging. */
617 lexer->debugging_p = false;
619 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
621 /* Create the buffer. */
622 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
624 return lexer;
628 /* Create a new main C++ lexer, the lexer that gets tokens from the
629 preprocessor. */
631 static cp_lexer *
632 cp_lexer_new_main (void)
634 cp_lexer *lexer;
635 cp_token token;
637 /* It's possible that parsing the first pragma will load a PCH file,
638 which is a GC collection point. So we have to do that before
639 allocating any memory. */
640 cp_parser_initial_pragma (&token);
642 lexer = cp_lexer_alloc ();
644 /* Put the first token in the buffer. */
645 lexer->buffer->quick_push (token);
647 /* Get the remaining tokens from the preprocessor. */
648 while (token.type != CPP_EOF)
650 cp_lexer_get_preprocessor_token (lexer, &token);
651 vec_safe_push (lexer->buffer, token);
654 lexer->last_token = lexer->buffer->address ()
655 + lexer->buffer->length ()
656 - 1;
657 lexer->next_token = lexer->buffer->length ()
658 ? lexer->buffer->address ()
659 : &eof_token;
661 /* Subsequent preprocessor diagnostics should use compiler
662 diagnostic functions to get the compiler source location. */
663 done_lexing = true;
665 gcc_assert (!lexer->next_token->purged_p);
666 return lexer;
669 /* Create a new lexer whose token stream is primed with the tokens in
670 CACHE. When these tokens are exhausted, no new tokens will be read. */
672 static cp_lexer *
673 cp_lexer_new_from_tokens (cp_token_cache *cache)
675 cp_token *first = cache->first;
676 cp_token *last = cache->last;
677 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
679 /* We do not own the buffer. */
680 lexer->buffer = NULL;
681 lexer->next_token = first == last ? &eof_token : first;
682 lexer->last_token = last;
684 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
686 /* Initially we are not debugging. */
687 lexer->debugging_p = false;
689 gcc_assert (!lexer->next_token->purged_p);
690 return lexer;
693 /* Frees all resources associated with LEXER. */
695 static void
696 cp_lexer_destroy (cp_lexer *lexer)
698 vec_free (lexer->buffer);
699 lexer->saved_tokens.release ();
700 ggc_free (lexer);
703 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
704 be used. The point of this flag is to help the compiler to fold away calls
705 to cp_lexer_debugging_p within this source file at compile time, when the
706 lexer is not being debugged. */
708 #define LEXER_DEBUGGING_ENABLED_P false
710 /* Returns nonzero if debugging information should be output. */
712 static inline bool
713 cp_lexer_debugging_p (cp_lexer *lexer)
715 if (!LEXER_DEBUGGING_ENABLED_P)
716 return false;
718 return lexer->debugging_p;
722 static inline cp_token_position
723 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
725 gcc_assert (!previous_p || lexer->next_token != &eof_token);
727 return lexer->next_token - previous_p;
730 static inline cp_token *
731 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
733 return pos;
736 static inline void
737 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
739 lexer->next_token = cp_lexer_token_at (lexer, pos);
742 static inline cp_token_position
743 cp_lexer_previous_token_position (cp_lexer *lexer)
745 if (lexer->next_token == &eof_token)
746 return lexer->last_token - 1;
747 else
748 return cp_lexer_token_position (lexer, true);
751 static inline cp_token *
752 cp_lexer_previous_token (cp_lexer *lexer)
754 cp_token_position tp = cp_lexer_previous_token_position (lexer);
756 /* Skip past purged tokens. */
757 while (tp->purged_p)
759 gcc_assert (tp != vec_safe_address (lexer->buffer));
760 tp--;
763 return cp_lexer_token_at (lexer, tp);
766 /* nonzero if we are presently saving tokens. */
768 static inline int
769 cp_lexer_saving_tokens (const cp_lexer* lexer)
771 return lexer->saved_tokens.length () != 0;
774 /* Store the next token from the preprocessor in *TOKEN. Return true
775 if we reach EOF. If LEXER is NULL, assume we are handling an
776 initial #pragma pch_preprocess, and thus want the lexer to return
777 processed strings. */
779 static void
780 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
782 static int is_extern_c = 0;
784 /* Get a new token from the preprocessor. */
785 token->type
786 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
787 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
788 token->keyword = RID_MAX;
789 token->purged_p = false;
790 token->error_reported = false;
792 /* On some systems, some header files are surrounded by an
793 implicit extern "C" block. Set a flag in the token if it
794 comes from such a header. */
795 is_extern_c += pending_lang_change;
796 pending_lang_change = 0;
797 token->implicit_extern_c = is_extern_c > 0;
799 /* Check to see if this token is a keyword. */
800 if (token->type == CPP_NAME)
802 if (IDENTIFIER_KEYWORD_P (token->u.value))
804 /* Mark this token as a keyword. */
805 token->type = CPP_KEYWORD;
806 /* Record which keyword. */
807 token->keyword = C_RID_CODE (token->u.value);
809 else
811 if (warn_cxx11_compat
812 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
813 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
815 /* Warn about the C++0x keyword (but still treat it as
816 an identifier). */
817 warning (OPT_Wc__11_compat,
818 "identifier %qE is a keyword in C++11",
819 token->u.value);
821 /* Clear out the C_RID_CODE so we don't warn about this
822 particular identifier-turned-keyword again. */
823 C_SET_RID_CODE (token->u.value, RID_MAX);
826 token->keyword = RID_MAX;
829 else if (token->type == CPP_AT_NAME)
831 /* This only happens in Objective-C++; it must be a keyword. */
832 token->type = CPP_KEYWORD;
833 switch (C_RID_CODE (token->u.value))
835 /* Replace 'class' with '@class', 'private' with '@private',
836 etc. This prevents confusion with the C++ keyword
837 'class', and makes the tokens consistent with other
838 Objective-C 'AT' keywords. For example '@class' is
839 reported as RID_AT_CLASS which is consistent with
840 '@synchronized', which is reported as
841 RID_AT_SYNCHRONIZED.
843 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
844 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
845 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
846 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
847 case RID_THROW: token->keyword = RID_AT_THROW; break;
848 case RID_TRY: token->keyword = RID_AT_TRY; break;
849 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
850 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
851 default: token->keyword = C_RID_CODE (token->u.value);
856 /* Update the globals input_location and the input file stack from TOKEN. */
857 static inline void
858 cp_lexer_set_source_position_from_token (cp_token *token)
860 if (token->type != CPP_EOF)
862 input_location = token->location;
866 /* Update the globals input_location and the input file stack from LEXER. */
867 static inline void
868 cp_lexer_set_source_position (cp_lexer *lexer)
870 cp_token *token = cp_lexer_peek_token (lexer);
871 cp_lexer_set_source_position_from_token (token);
874 /* Return a pointer to the next token in the token stream, but do not
875 consume it. */
877 static inline cp_token *
878 cp_lexer_peek_token (cp_lexer *lexer)
880 if (cp_lexer_debugging_p (lexer))
882 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
883 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
884 putc ('\n', cp_lexer_debug_stream);
886 return lexer->next_token;
889 /* Return true if the next token has the indicated TYPE. */
891 static inline bool
892 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
894 return cp_lexer_peek_token (lexer)->type == type;
897 /* Return true if the next token does not have the indicated TYPE. */
899 static inline bool
900 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
902 return !cp_lexer_next_token_is (lexer, type);
905 /* Return true if the next token is the indicated KEYWORD. */
907 static inline bool
908 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
910 return cp_lexer_peek_token (lexer)->keyword == keyword;
913 static inline bool
914 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
916 return cp_lexer_peek_nth_token (lexer, n)->type == type;
919 static inline bool
920 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
922 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
925 /* Return true if the next token is not the indicated KEYWORD. */
927 static inline bool
928 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
930 return cp_lexer_peek_token (lexer)->keyword != keyword;
933 /* Return true if KEYWORD can start a decl-specifier. */
935 bool
936 cp_keyword_starts_decl_specifier_p (enum rid keyword)
938 switch (keyword)
940 /* auto specifier: storage-class-specifier in C++,
941 simple-type-specifier in C++0x. */
942 case RID_AUTO:
943 /* Storage classes. */
944 case RID_REGISTER:
945 case RID_STATIC:
946 case RID_EXTERN:
947 case RID_MUTABLE:
948 case RID_THREAD:
949 /* Elaborated type specifiers. */
950 case RID_ENUM:
951 case RID_CLASS:
952 case RID_STRUCT:
953 case RID_UNION:
954 case RID_TYPENAME:
955 /* Simple type specifiers. */
956 case RID_CHAR:
957 case RID_CHAR16:
958 case RID_CHAR32:
959 case RID_WCHAR:
960 case RID_BOOL:
961 case RID_SHORT:
962 case RID_INT:
963 case RID_LONG:
964 case RID_SIGNED:
965 case RID_UNSIGNED:
966 case RID_FLOAT:
967 case RID_DOUBLE:
968 case RID_VOID:
969 /* GNU extensions. */
970 case RID_ATTRIBUTE:
971 case RID_TYPEOF:
972 /* C++0x extensions. */
973 case RID_DECLTYPE:
974 case RID_UNDERLYING_TYPE:
975 case RID_CONSTEXPR:
976 return true;
978 default:
979 if (keyword >= RID_FIRST_INT_N
980 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
981 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
982 return true;
983 return false;
987 /* Return true if the next token is a keyword for a decl-specifier. */
989 static bool
990 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
992 cp_token *token;
994 token = cp_lexer_peek_token (lexer);
995 return cp_keyword_starts_decl_specifier_p (token->keyword);
998 /* Returns TRUE iff the token T begins a decltype type. */
1000 static bool
1001 token_is_decltype (cp_token *t)
1003 return (t->keyword == RID_DECLTYPE
1004 || t->type == CPP_DECLTYPE);
1007 /* Returns TRUE iff the next token begins a decltype type. */
1009 static bool
1010 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1012 cp_token *t = cp_lexer_peek_token (lexer);
1013 return token_is_decltype (t);
1016 /* Called when processing a token with tree_check_value; perform or defer the
1017 associated checks and return the value. */
1019 static tree
1020 saved_checks_value (struct tree_check *check_value)
1022 /* Perform any access checks that were deferred. */
1023 vec<deferred_access_check, va_gc> *checks;
1024 deferred_access_check *chk;
1025 checks = check_value->checks;
1026 if (checks)
1028 int i;
1029 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1030 perform_or_defer_access_check (chk->binfo,
1031 chk->decl,
1032 chk->diag_decl, tf_warning_or_error);
1034 /* Return the stored value. */
1035 return check_value->value;
1038 /* Return a pointer to the Nth token in the token stream. If N is 1,
1039 then this is precisely equivalent to cp_lexer_peek_token (except
1040 that it is not inline). One would like to disallow that case, but
1041 there is one case (cp_parser_nth_token_starts_template_id) where
1042 the caller passes a variable for N and it might be 1. */
1044 static cp_token *
1045 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1047 cp_token *token;
1049 /* N is 1-based, not zero-based. */
1050 gcc_assert (n > 0);
1052 if (cp_lexer_debugging_p (lexer))
1053 fprintf (cp_lexer_debug_stream,
1054 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1056 --n;
1057 token = lexer->next_token;
1058 gcc_assert (!n || token != &eof_token);
1059 while (n != 0)
1061 ++token;
1062 if (token == lexer->last_token)
1064 token = &eof_token;
1065 break;
1068 if (!token->purged_p)
1069 --n;
1072 if (cp_lexer_debugging_p (lexer))
1074 cp_lexer_print_token (cp_lexer_debug_stream, token);
1075 putc ('\n', cp_lexer_debug_stream);
1078 return token;
1081 /* Return the next token, and advance the lexer's next_token pointer
1082 to point to the next non-purged token. */
1084 static cp_token *
1085 cp_lexer_consume_token (cp_lexer* lexer)
1087 cp_token *token = lexer->next_token;
1089 gcc_assert (token != &eof_token);
1090 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1094 lexer->next_token++;
1095 if (lexer->next_token == lexer->last_token)
1097 lexer->next_token = &eof_token;
1098 break;
1102 while (lexer->next_token->purged_p);
1104 cp_lexer_set_source_position_from_token (token);
1106 /* Provide debugging output. */
1107 if (cp_lexer_debugging_p (lexer))
1109 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1110 cp_lexer_print_token (cp_lexer_debug_stream, token);
1111 putc ('\n', cp_lexer_debug_stream);
1114 return token;
1117 /* Permanently remove the next token from the token stream, and
1118 advance the next_token pointer to refer to the next non-purged
1119 token. */
1121 static void
1122 cp_lexer_purge_token (cp_lexer *lexer)
1124 cp_token *tok = lexer->next_token;
1126 gcc_assert (tok != &eof_token);
1127 tok->purged_p = true;
1128 tok->location = UNKNOWN_LOCATION;
1129 tok->u.value = NULL_TREE;
1130 tok->keyword = RID_MAX;
1134 tok++;
1135 if (tok == lexer->last_token)
1137 tok = &eof_token;
1138 break;
1141 while (tok->purged_p);
1142 lexer->next_token = tok;
1145 /* Permanently remove all tokens after TOK, up to, but not
1146 including, the token that will be returned next by
1147 cp_lexer_peek_token. */
1149 static void
1150 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1152 cp_token *peek = lexer->next_token;
1154 if (peek == &eof_token)
1155 peek = lexer->last_token;
1157 gcc_assert (tok < peek);
1159 for ( tok += 1; tok != peek; tok += 1)
1161 tok->purged_p = true;
1162 tok->location = UNKNOWN_LOCATION;
1163 tok->u.value = NULL_TREE;
1164 tok->keyword = RID_MAX;
1168 /* Begin saving tokens. All tokens consumed after this point will be
1169 preserved. */
1171 static void
1172 cp_lexer_save_tokens (cp_lexer* lexer)
1174 /* Provide debugging output. */
1175 if (cp_lexer_debugging_p (lexer))
1176 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1178 lexer->saved_tokens.safe_push (lexer->next_token);
1181 /* Commit to the portion of the token stream most recently saved. */
1183 static void
1184 cp_lexer_commit_tokens (cp_lexer* lexer)
1186 /* Provide debugging output. */
1187 if (cp_lexer_debugging_p (lexer))
1188 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1190 lexer->saved_tokens.pop ();
1193 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1194 to the token stream. Stop saving tokens. */
1196 static void
1197 cp_lexer_rollback_tokens (cp_lexer* lexer)
1199 /* Provide debugging output. */
1200 if (cp_lexer_debugging_p (lexer))
1201 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1203 lexer->next_token = lexer->saved_tokens.pop ();
1206 /* RAII wrapper around the above functions, with sanity checking. Creating
1207 a variable saves tokens, which are committed when the variable is
1208 destroyed unless they are explicitly rolled back by calling the rollback
1209 member function. */
1211 struct saved_token_sentinel
1213 cp_lexer *lexer;
1214 unsigned len;
1215 bool commit;
1216 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1218 len = lexer->saved_tokens.length ();
1219 cp_lexer_save_tokens (lexer);
1221 void rollback ()
1223 cp_lexer_rollback_tokens (lexer);
1224 commit = false;
1226 ~saved_token_sentinel()
1228 if (commit)
1229 cp_lexer_commit_tokens (lexer);
1230 gcc_assert (lexer->saved_tokens.length () == len);
1234 /* Print a representation of the TOKEN on the STREAM. */
1236 static void
1237 cp_lexer_print_token (FILE * stream, cp_token *token)
1239 /* We don't use cpp_type2name here because the parser defines
1240 a few tokens of its own. */
1241 static const char *const token_names[] = {
1242 /* cpplib-defined token types */
1243 #define OP(e, s) #e,
1244 #define TK(e, s) #e,
1245 TTYPE_TABLE
1246 #undef OP
1247 #undef TK
1248 /* C++ parser token types - see "Manifest constants", above. */
1249 "KEYWORD",
1250 "TEMPLATE_ID",
1251 "NESTED_NAME_SPECIFIER",
1254 /* For some tokens, print the associated data. */
1255 switch (token->type)
1257 case CPP_KEYWORD:
1258 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1259 For example, `struct' is mapped to an INTEGER_CST. */
1260 if (!identifier_p (token->u.value))
1261 break;
1262 /* fall through */
1263 case CPP_NAME:
1264 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1265 break;
1267 case CPP_STRING:
1268 case CPP_STRING16:
1269 case CPP_STRING32:
1270 case CPP_WSTRING:
1271 case CPP_UTF8STRING:
1272 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1273 break;
1275 case CPP_NUMBER:
1276 print_generic_expr (stream, token->u.value);
1277 break;
1279 default:
1280 /* If we have a name for the token, print it out. Otherwise, we
1281 simply give the numeric code. */
1282 if (token->type < ARRAY_SIZE(token_names))
1283 fputs (token_names[token->type], stream);
1284 else
1285 fprintf (stream, "[%d]", token->type);
1286 break;
1290 DEBUG_FUNCTION void
1291 debug (cp_token &ref)
1293 cp_lexer_print_token (stderr, &ref);
1294 fprintf (stderr, "\n");
1297 DEBUG_FUNCTION void
1298 debug (cp_token *ptr)
1300 if (ptr)
1301 debug (*ptr);
1302 else
1303 fprintf (stderr, "<nil>\n");
1307 /* Start emitting debugging information. */
1309 static void
1310 cp_lexer_start_debugging (cp_lexer* lexer)
1312 if (!LEXER_DEBUGGING_ENABLED_P)
1313 fatal_error (input_location,
1314 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1316 lexer->debugging_p = true;
1317 cp_lexer_debug_stream = stderr;
1320 /* Stop emitting debugging information. */
1322 static void
1323 cp_lexer_stop_debugging (cp_lexer* lexer)
1325 if (!LEXER_DEBUGGING_ENABLED_P)
1326 fatal_error (input_location,
1327 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1329 lexer->debugging_p = false;
1330 cp_lexer_debug_stream = NULL;
1333 /* Create a new cp_token_cache, representing a range of tokens. */
1335 static cp_token_cache *
1336 cp_token_cache_new (cp_token *first, cp_token *last)
1338 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1339 cache->first = first;
1340 cache->last = last;
1341 return cache;
1344 /* Diagnose if #pragma omp declare simd isn't followed immediately
1345 by function declaration or definition. */
1347 static inline void
1348 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1350 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1352 error ("%<#pragma omp declare simd%> not immediately followed by "
1353 "function declaration or definition");
1354 parser->omp_declare_simd = NULL;
1358 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1359 and put that into "omp declare simd" attribute. */
1361 static inline void
1362 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1364 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1366 if (fndecl == error_mark_node)
1368 parser->omp_declare_simd = NULL;
1369 return;
1371 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1373 cp_ensure_no_omp_declare_simd (parser);
1374 return;
1379 /* Diagnose if #pragma acc routine isn't followed immediately by function
1380 declaration or definition. */
1382 static inline void
1383 cp_ensure_no_oacc_routine (cp_parser *parser)
1385 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1387 error_at (parser->oacc_routine->loc,
1388 "%<#pragma acc routine%> not immediately followed by "
1389 "function declaration or definition");
1390 parser->oacc_routine = NULL;
1394 /* Decl-specifiers. */
1396 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1398 static void
1399 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1401 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1404 /* Declarators. */
1406 /* Nothing other than the parser should be creating declarators;
1407 declarators are a semi-syntactic representation of C++ entities.
1408 Other parts of the front end that need to create entities (like
1409 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1411 static cp_declarator *make_call_declarator
1412 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1413 static cp_declarator *make_array_declarator
1414 (cp_declarator *, tree);
1415 static cp_declarator *make_pointer_declarator
1416 (cp_cv_quals, cp_declarator *, tree);
1417 static cp_declarator *make_reference_declarator
1418 (cp_cv_quals, cp_declarator *, bool, tree);
1419 static cp_declarator *make_ptrmem_declarator
1420 (cp_cv_quals, tree, cp_declarator *, tree);
1422 /* An erroneous declarator. */
1423 static cp_declarator *cp_error_declarator;
1425 /* The obstack on which declarators and related data structures are
1426 allocated. */
1427 static struct obstack declarator_obstack;
1429 /* Alloc BYTES from the declarator memory pool. */
1431 static inline void *
1432 alloc_declarator (size_t bytes)
1434 return obstack_alloc (&declarator_obstack, bytes);
1437 /* Allocate a declarator of the indicated KIND. Clear fields that are
1438 common to all declarators. */
1440 static cp_declarator *
1441 make_declarator (cp_declarator_kind kind)
1443 cp_declarator *declarator;
1445 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1446 declarator->kind = kind;
1447 declarator->parenthesized = UNKNOWN_LOCATION;
1448 declarator->attributes = NULL_TREE;
1449 declarator->std_attributes = NULL_TREE;
1450 declarator->declarator = NULL;
1451 declarator->parameter_pack_p = false;
1452 declarator->id_loc = UNKNOWN_LOCATION;
1454 return declarator;
1457 /* Make a declarator for a generalized identifier. If
1458 QUALIFYING_SCOPE is non-NULL, the identifier is
1459 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1460 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1461 is, if any. */
1463 static cp_declarator *
1464 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1465 special_function_kind sfk)
1467 cp_declarator *declarator;
1469 /* It is valid to write:
1471 class C { void f(); };
1472 typedef C D;
1473 void D::f();
1475 The standard is not clear about whether `typedef const C D' is
1476 legal; as of 2002-09-15 the committee is considering that
1477 question. EDG 3.0 allows that syntax. Therefore, we do as
1478 well. */
1479 if (qualifying_scope && TYPE_P (qualifying_scope))
1480 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1482 gcc_assert (identifier_p (unqualified_name)
1483 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1484 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1486 declarator = make_declarator (cdk_id);
1487 declarator->u.id.qualifying_scope = qualifying_scope;
1488 declarator->u.id.unqualified_name = unqualified_name;
1489 declarator->u.id.sfk = sfk;
1491 return declarator;
1494 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1495 of modifiers such as const or volatile to apply to the pointer
1496 type, represented as identifiers. ATTRIBUTES represent the attributes that
1497 appertain to the pointer or reference. */
1499 cp_declarator *
1500 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1501 tree attributes)
1503 cp_declarator *declarator;
1505 declarator = make_declarator (cdk_pointer);
1506 declarator->declarator = target;
1507 declarator->u.pointer.qualifiers = cv_qualifiers;
1508 declarator->u.pointer.class_type = NULL_TREE;
1509 if (target)
1511 declarator->id_loc = target->id_loc;
1512 declarator->parameter_pack_p = target->parameter_pack_p;
1513 target->parameter_pack_p = false;
1515 else
1516 declarator->parameter_pack_p = false;
1518 declarator->std_attributes = attributes;
1520 return declarator;
1523 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1524 represent the attributes that appertain to the pointer or
1525 reference. */
1527 cp_declarator *
1528 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1529 bool rvalue_ref, tree attributes)
1531 cp_declarator *declarator;
1533 declarator = make_declarator (cdk_reference);
1534 declarator->declarator = target;
1535 declarator->u.reference.qualifiers = cv_qualifiers;
1536 declarator->u.reference.rvalue_ref = rvalue_ref;
1537 if (target)
1539 declarator->id_loc = target->id_loc;
1540 declarator->parameter_pack_p = target->parameter_pack_p;
1541 target->parameter_pack_p = false;
1543 else
1544 declarator->parameter_pack_p = false;
1546 declarator->std_attributes = attributes;
1548 return declarator;
1551 /* Like make_pointer_declarator -- but for a pointer to a non-static
1552 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1553 appertain to the pointer or reference. */
1555 cp_declarator *
1556 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1557 cp_declarator *pointee,
1558 tree attributes)
1560 cp_declarator *declarator;
1562 declarator = make_declarator (cdk_ptrmem);
1563 declarator->declarator = pointee;
1564 declarator->u.pointer.qualifiers = cv_qualifiers;
1565 declarator->u.pointer.class_type = class_type;
1567 if (pointee)
1569 declarator->parameter_pack_p = pointee->parameter_pack_p;
1570 pointee->parameter_pack_p = false;
1572 else
1573 declarator->parameter_pack_p = false;
1575 declarator->std_attributes = attributes;
1577 return declarator;
1580 /* Make a declarator for the function given by TARGET, with the
1581 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1582 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1583 indicates what exceptions can be thrown. */
1585 cp_declarator *
1586 make_call_declarator (cp_declarator *target,
1587 tree parms,
1588 cp_cv_quals cv_qualifiers,
1589 cp_virt_specifiers virt_specifiers,
1590 cp_ref_qualifier ref_qualifier,
1591 tree tx_qualifier,
1592 tree exception_specification,
1593 tree late_return_type,
1594 tree requires_clause)
1596 cp_declarator *declarator;
1598 declarator = make_declarator (cdk_function);
1599 declarator->declarator = target;
1600 declarator->u.function.parameters = parms;
1601 declarator->u.function.qualifiers = cv_qualifiers;
1602 declarator->u.function.virt_specifiers = virt_specifiers;
1603 declarator->u.function.ref_qualifier = ref_qualifier;
1604 declarator->u.function.tx_qualifier = tx_qualifier;
1605 declarator->u.function.exception_specification = exception_specification;
1606 declarator->u.function.late_return_type = late_return_type;
1607 declarator->u.function.requires_clause = requires_clause;
1608 if (target)
1610 declarator->id_loc = target->id_loc;
1611 declarator->parameter_pack_p = target->parameter_pack_p;
1612 target->parameter_pack_p = false;
1614 else
1615 declarator->parameter_pack_p = false;
1617 return declarator;
1620 /* Make a declarator for an array of BOUNDS elements, each of which is
1621 defined by ELEMENT. */
1623 cp_declarator *
1624 make_array_declarator (cp_declarator *element, tree bounds)
1626 cp_declarator *declarator;
1628 declarator = make_declarator (cdk_array);
1629 declarator->declarator = element;
1630 declarator->u.array.bounds = bounds;
1631 if (element)
1633 declarator->id_loc = element->id_loc;
1634 declarator->parameter_pack_p = element->parameter_pack_p;
1635 element->parameter_pack_p = false;
1637 else
1638 declarator->parameter_pack_p = false;
1640 return declarator;
1643 /* Determine whether the declarator we've seen so far can be a
1644 parameter pack, when followed by an ellipsis. */
1645 static bool
1646 declarator_can_be_parameter_pack (cp_declarator *declarator)
1648 if (declarator && declarator->parameter_pack_p)
1649 /* We already saw an ellipsis. */
1650 return false;
1652 /* Search for a declarator name, or any other declarator that goes
1653 after the point where the ellipsis could appear in a parameter
1654 pack. If we find any of these, then this declarator can not be
1655 made into a parameter pack. */
1656 bool found = false;
1657 while (declarator && !found)
1659 switch ((int)declarator->kind)
1661 case cdk_id:
1662 case cdk_array:
1663 case cdk_decomp:
1664 found = true;
1665 break;
1667 case cdk_error:
1668 return true;
1670 default:
1671 declarator = declarator->declarator;
1672 break;
1676 return !found;
1679 cp_parameter_declarator *no_parameters;
1681 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1682 DECLARATOR and DEFAULT_ARGUMENT. */
1684 cp_parameter_declarator *
1685 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1686 cp_declarator *declarator,
1687 tree default_argument,
1688 location_t loc,
1689 bool template_parameter_pack_p = false)
1691 cp_parameter_declarator *parameter;
1693 parameter = ((cp_parameter_declarator *)
1694 alloc_declarator (sizeof (cp_parameter_declarator)));
1695 parameter->next = NULL;
1696 if (decl_specifiers)
1697 parameter->decl_specifiers = *decl_specifiers;
1698 else
1699 clear_decl_specs (&parameter->decl_specifiers);
1700 parameter->declarator = declarator;
1701 parameter->default_argument = default_argument;
1702 parameter->template_parameter_pack_p = template_parameter_pack_p;
1703 parameter->loc = loc;
1705 return parameter;
1708 /* Returns true iff DECLARATOR is a declaration for a function. */
1710 static bool
1711 function_declarator_p (const cp_declarator *declarator)
1713 while (declarator)
1715 if (declarator->kind == cdk_function
1716 && declarator->declarator->kind == cdk_id)
1717 return true;
1718 if (declarator->kind == cdk_id
1719 || declarator->kind == cdk_decomp
1720 || declarator->kind == cdk_error)
1721 return false;
1722 declarator = declarator->declarator;
1724 return false;
1727 /* The parser. */
1729 /* Overview
1730 --------
1732 A cp_parser parses the token stream as specified by the C++
1733 grammar. Its job is purely parsing, not semantic analysis. For
1734 example, the parser breaks the token stream into declarators,
1735 expressions, statements, and other similar syntactic constructs.
1736 It does not check that the types of the expressions on either side
1737 of an assignment-statement are compatible, or that a function is
1738 not declared with a parameter of type `void'.
1740 The parser invokes routines elsewhere in the compiler to perform
1741 semantic analysis and to build up the abstract syntax tree for the
1742 code processed.
1744 The parser (and the template instantiation code, which is, in a
1745 way, a close relative of parsing) are the only parts of the
1746 compiler that should be calling push_scope and pop_scope, or
1747 related functions. The parser (and template instantiation code)
1748 keeps track of what scope is presently active; everything else
1749 should simply honor that. (The code that generates static
1750 initializers may also need to set the scope, in order to check
1751 access control correctly when emitting the initializers.)
1753 Methodology
1754 -----------
1756 The parser is of the standard recursive-descent variety. Upcoming
1757 tokens in the token stream are examined in order to determine which
1758 production to use when parsing a non-terminal. Some C++ constructs
1759 require arbitrary look ahead to disambiguate. For example, it is
1760 impossible, in the general case, to tell whether a statement is an
1761 expression or declaration without scanning the entire statement.
1762 Therefore, the parser is capable of "parsing tentatively." When the
1763 parser is not sure what construct comes next, it enters this mode.
1764 Then, while we attempt to parse the construct, the parser queues up
1765 error messages, rather than issuing them immediately, and saves the
1766 tokens it consumes. If the construct is parsed successfully, the
1767 parser "commits", i.e., it issues any queued error messages and
1768 the tokens that were being preserved are permanently discarded.
1769 If, however, the construct is not parsed successfully, the parser
1770 rolls back its state completely so that it can resume parsing using
1771 a different alternative.
1773 Future Improvements
1774 -------------------
1776 The performance of the parser could probably be improved substantially.
1777 We could often eliminate the need to parse tentatively by looking ahead
1778 a little bit. In some places, this approach might not entirely eliminate
1779 the need to parse tentatively, but it might still speed up the average
1780 case. */
1782 /* Flags that are passed to some parsing functions. These values can
1783 be bitwise-ored together. */
1785 enum
1787 /* No flags. */
1788 CP_PARSER_FLAGS_NONE = 0x0,
1789 /* The construct is optional. If it is not present, then no error
1790 should be issued. */
1791 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1792 /* When parsing a type-specifier, treat user-defined type-names
1793 as non-type identifiers. */
1794 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1795 /* When parsing a type-specifier, do not try to parse a class-specifier
1796 or enum-specifier. */
1797 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1798 /* When parsing a decl-specifier-seq, only allow type-specifier or
1799 constexpr. */
1800 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1801 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1802 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1805 /* This type is used for parameters and variables which hold
1806 combinations of the above flags. */
1807 typedef int cp_parser_flags;
1809 /* The different kinds of declarators we want to parse. */
1811 enum cp_parser_declarator_kind
1813 /* We want an abstract declarator. */
1814 CP_PARSER_DECLARATOR_ABSTRACT,
1815 /* We want a named declarator. */
1816 CP_PARSER_DECLARATOR_NAMED,
1817 /* We don't mind, but the name must be an unqualified-id. */
1818 CP_PARSER_DECLARATOR_EITHER
1821 /* The precedence values used to parse binary expressions. The minimum value
1822 of PREC must be 1, because zero is reserved to quickly discriminate
1823 binary operators from other tokens. */
1825 enum cp_parser_prec
1827 PREC_NOT_OPERATOR,
1828 PREC_LOGICAL_OR_EXPRESSION,
1829 PREC_LOGICAL_AND_EXPRESSION,
1830 PREC_INCLUSIVE_OR_EXPRESSION,
1831 PREC_EXCLUSIVE_OR_EXPRESSION,
1832 PREC_AND_EXPRESSION,
1833 PREC_EQUALITY_EXPRESSION,
1834 PREC_RELATIONAL_EXPRESSION,
1835 PREC_SHIFT_EXPRESSION,
1836 PREC_ADDITIVE_EXPRESSION,
1837 PREC_MULTIPLICATIVE_EXPRESSION,
1838 PREC_PM_EXPRESSION,
1839 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1842 /* A mapping from a token type to a corresponding tree node type, with a
1843 precedence value. */
1845 struct cp_parser_binary_operations_map_node
1847 /* The token type. */
1848 enum cpp_ttype token_type;
1849 /* The corresponding tree code. */
1850 enum tree_code tree_type;
1851 /* The precedence of this operator. */
1852 enum cp_parser_prec prec;
1855 struct cp_parser_expression_stack_entry
1857 /* Left hand side of the binary operation we are currently
1858 parsing. */
1859 cp_expr lhs;
1860 /* Original tree code for left hand side, if it was a binary
1861 expression itself (used for -Wparentheses). */
1862 enum tree_code lhs_type;
1863 /* Tree code for the binary operation we are parsing. */
1864 enum tree_code tree_type;
1865 /* Precedence of the binary operation we are parsing. */
1866 enum cp_parser_prec prec;
1867 /* Location of the binary operation we are parsing. */
1868 location_t loc;
1871 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1872 entries because precedence levels on the stack are monotonically
1873 increasing. */
1874 typedef struct cp_parser_expression_stack_entry
1875 cp_parser_expression_stack[NUM_PREC_VALUES];
1877 /* Prototypes. */
1879 /* Constructors and destructors. */
1881 static cp_parser_context *cp_parser_context_new
1882 (cp_parser_context *);
1884 /* Class variables. */
1886 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1888 /* The operator-precedence table used by cp_parser_binary_expression.
1889 Transformed into an associative array (binops_by_token) by
1890 cp_parser_new. */
1892 static const cp_parser_binary_operations_map_node binops[] = {
1893 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1894 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1896 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1897 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1898 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1900 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1901 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1903 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1904 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1906 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1907 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1908 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1909 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1911 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1912 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1914 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1916 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1918 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1920 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1922 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1925 /* The same as binops, but initialized by cp_parser_new so that
1926 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1927 for speed. */
1928 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1930 /* Constructors and destructors. */
1932 /* Construct a new context. The context below this one on the stack
1933 is given by NEXT. */
1935 static cp_parser_context *
1936 cp_parser_context_new (cp_parser_context* next)
1938 cp_parser_context *context;
1940 /* Allocate the storage. */
1941 if (cp_parser_context_free_list != NULL)
1943 /* Pull the first entry from the free list. */
1944 context = cp_parser_context_free_list;
1945 cp_parser_context_free_list = context->next;
1946 memset (context, 0, sizeof (*context));
1948 else
1949 context = ggc_cleared_alloc<cp_parser_context> ();
1951 /* No errors have occurred yet in this context. */
1952 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1953 /* If this is not the bottommost context, copy information that we
1954 need from the previous context. */
1955 if (next)
1957 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1958 expression, then we are parsing one in this context, too. */
1959 context->object_type = next->object_type;
1960 /* Thread the stack. */
1961 context->next = next;
1964 return context;
1967 /* Managing the unparsed function queues. */
1969 #define unparsed_funs_with_default_args \
1970 parser->unparsed_queues->last ().funs_with_default_args
1971 #define unparsed_funs_with_definitions \
1972 parser->unparsed_queues->last ().funs_with_definitions
1973 #define unparsed_nsdmis \
1974 parser->unparsed_queues->last ().nsdmis
1975 #define unparsed_classes \
1976 parser->unparsed_queues->last ().classes
1978 static void
1979 push_unparsed_function_queues (cp_parser *parser)
1981 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1982 vec_safe_push (parser->unparsed_queues, e);
1985 static void
1986 pop_unparsed_function_queues (cp_parser *parser)
1988 release_tree_vector (unparsed_funs_with_definitions);
1989 parser->unparsed_queues->pop ();
1992 /* Prototypes. */
1994 /* Constructors and destructors. */
1996 static cp_parser *cp_parser_new
1997 (void);
1999 /* Routines to parse various constructs.
2001 Those that return `tree' will return the error_mark_node (rather
2002 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2003 Sometimes, they will return an ordinary node if error-recovery was
2004 attempted, even though a parse error occurred. So, to check
2005 whether or not a parse error occurred, you should always use
2006 cp_parser_error_occurred. If the construct is optional (indicated
2007 either by an `_opt' in the name of the function that does the
2008 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2009 the construct is not present. */
2011 /* Lexical conventions [gram.lex] */
2013 static cp_expr cp_parser_identifier
2014 (cp_parser *);
2015 static cp_expr cp_parser_string_literal
2016 (cp_parser *, bool, bool, bool);
2017 static cp_expr cp_parser_userdef_char_literal
2018 (cp_parser *);
2019 static tree cp_parser_userdef_string_literal
2020 (tree);
2021 static cp_expr cp_parser_userdef_numeric_literal
2022 (cp_parser *);
2024 /* Basic concepts [gram.basic] */
2026 static bool cp_parser_translation_unit
2027 (cp_parser *);
2029 /* Expressions [gram.expr] */
2031 static cp_expr cp_parser_primary_expression
2032 (cp_parser *, bool, bool, bool, cp_id_kind *);
2033 static cp_expr cp_parser_id_expression
2034 (cp_parser *, bool, bool, bool *, bool, bool);
2035 static cp_expr cp_parser_unqualified_id
2036 (cp_parser *, bool, bool, bool, bool);
2037 static tree cp_parser_nested_name_specifier_opt
2038 (cp_parser *, bool, bool, bool, bool, bool = false);
2039 static tree cp_parser_nested_name_specifier
2040 (cp_parser *, bool, bool, bool, bool);
2041 static tree cp_parser_qualifying_entity
2042 (cp_parser *, bool, bool, bool, bool, bool);
2043 static cp_expr cp_parser_postfix_expression
2044 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2045 static tree cp_parser_postfix_open_square_expression
2046 (cp_parser *, tree, bool, bool);
2047 static tree cp_parser_postfix_dot_deref_expression
2048 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2049 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2050 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2051 bool = false);
2052 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2053 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2054 static void cp_parser_pseudo_destructor_name
2055 (cp_parser *, tree, tree *, tree *);
2056 static cp_expr cp_parser_unary_expression
2057 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2058 static enum tree_code cp_parser_unary_operator
2059 (cp_token *);
2060 static tree cp_parser_new_expression
2061 (cp_parser *);
2062 static vec<tree, va_gc> *cp_parser_new_placement
2063 (cp_parser *);
2064 static tree cp_parser_new_type_id
2065 (cp_parser *, tree *);
2066 static cp_declarator *cp_parser_new_declarator_opt
2067 (cp_parser *);
2068 static cp_declarator *cp_parser_direct_new_declarator
2069 (cp_parser *);
2070 static vec<tree, va_gc> *cp_parser_new_initializer
2071 (cp_parser *);
2072 static tree cp_parser_delete_expression
2073 (cp_parser *);
2074 static cp_expr cp_parser_cast_expression
2075 (cp_parser *, bool, bool, bool, cp_id_kind *);
2076 static cp_expr cp_parser_binary_expression
2077 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2078 static tree cp_parser_question_colon_clause
2079 (cp_parser *, cp_expr);
2080 static cp_expr cp_parser_assignment_expression
2081 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2082 static enum tree_code cp_parser_assignment_operator_opt
2083 (cp_parser *);
2084 static cp_expr cp_parser_expression
2085 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2086 static cp_expr cp_parser_constant_expression
2087 (cp_parser *, bool = false, bool * = NULL, bool = false);
2088 static cp_expr cp_parser_builtin_offsetof
2089 (cp_parser *);
2090 static cp_expr cp_parser_lambda_expression
2091 (cp_parser *);
2092 static void cp_parser_lambda_introducer
2093 (cp_parser *, tree);
2094 static bool cp_parser_lambda_declarator_opt
2095 (cp_parser *, tree);
2096 static void cp_parser_lambda_body
2097 (cp_parser *, tree);
2099 /* Statements [gram.stmt.stmt] */
2101 static void cp_parser_statement
2102 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2103 static void cp_parser_label_for_labeled_statement
2104 (cp_parser *, tree);
2105 static tree cp_parser_expression_statement
2106 (cp_parser *, tree);
2107 static tree cp_parser_compound_statement
2108 (cp_parser *, tree, int, bool);
2109 static void cp_parser_statement_seq_opt
2110 (cp_parser *, tree);
2111 static tree cp_parser_selection_statement
2112 (cp_parser *, bool *, vec<tree> *);
2113 static tree cp_parser_condition
2114 (cp_parser *);
2115 static tree cp_parser_iteration_statement
2116 (cp_parser *, bool *, bool, unsigned short);
2117 static bool cp_parser_init_statement
2118 (cp_parser *, tree *decl);
2119 static tree cp_parser_for
2120 (cp_parser *, bool, unsigned short);
2121 static tree cp_parser_c_for
2122 (cp_parser *, tree, tree, bool, unsigned short);
2123 static tree cp_parser_range_for
2124 (cp_parser *, tree, tree, tree, bool, unsigned short);
2125 static void do_range_for_auto_deduction
2126 (tree, tree);
2127 static tree cp_parser_perform_range_for_lookup
2128 (tree, tree *, tree *);
2129 static tree cp_parser_range_for_member_function
2130 (tree, tree);
2131 static tree cp_parser_jump_statement
2132 (cp_parser *);
2133 static void cp_parser_declaration_statement
2134 (cp_parser *);
2136 static tree cp_parser_implicitly_scoped_statement
2137 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2138 static void cp_parser_already_scoped_statement
2139 (cp_parser *, bool *, const token_indent_info &);
2141 /* Declarations [gram.dcl.dcl] */
2143 static void cp_parser_declaration_seq_opt
2144 (cp_parser *);
2145 static void cp_parser_declaration
2146 (cp_parser *);
2147 static void cp_parser_block_declaration
2148 (cp_parser *, bool);
2149 static void cp_parser_simple_declaration
2150 (cp_parser *, bool, tree *);
2151 static void cp_parser_decl_specifier_seq
2152 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2153 static tree cp_parser_storage_class_specifier_opt
2154 (cp_parser *);
2155 static tree cp_parser_function_specifier_opt
2156 (cp_parser *, cp_decl_specifier_seq *);
2157 static tree cp_parser_type_specifier
2158 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2159 int *, bool *);
2160 static tree cp_parser_simple_type_specifier
2161 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2162 static tree cp_parser_type_name
2163 (cp_parser *, bool);
2164 static tree cp_parser_type_name
2165 (cp_parser *);
2166 static tree cp_parser_nonclass_name
2167 (cp_parser* parser);
2168 static tree cp_parser_elaborated_type_specifier
2169 (cp_parser *, bool, bool);
2170 static tree cp_parser_enum_specifier
2171 (cp_parser *);
2172 static void cp_parser_enumerator_list
2173 (cp_parser *, tree);
2174 static void cp_parser_enumerator_definition
2175 (cp_parser *, tree);
2176 static tree cp_parser_namespace_name
2177 (cp_parser *);
2178 static void cp_parser_namespace_definition
2179 (cp_parser *);
2180 static void cp_parser_namespace_body
2181 (cp_parser *);
2182 static tree cp_parser_qualified_namespace_specifier
2183 (cp_parser *);
2184 static void cp_parser_namespace_alias_definition
2185 (cp_parser *);
2186 static bool cp_parser_using_declaration
2187 (cp_parser *, bool);
2188 static void cp_parser_using_directive
2189 (cp_parser *);
2190 static tree cp_parser_alias_declaration
2191 (cp_parser *);
2192 static void cp_parser_asm_definition
2193 (cp_parser *);
2194 static void cp_parser_linkage_specification
2195 (cp_parser *);
2196 static void cp_parser_static_assert
2197 (cp_parser *, bool);
2198 static tree cp_parser_decltype
2199 (cp_parser *);
2200 static tree cp_parser_decomposition_declaration
2201 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2203 /* Declarators [gram.dcl.decl] */
2205 static tree cp_parser_init_declarator
2206 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2207 bool, bool, int, bool *, tree *, location_t *, tree *);
2208 static cp_declarator *cp_parser_declarator
2209 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2210 static cp_declarator *cp_parser_direct_declarator
2211 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2212 static enum tree_code cp_parser_ptr_operator
2213 (cp_parser *, tree *, cp_cv_quals *, tree *);
2214 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2215 (cp_parser *);
2216 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2217 (cp_parser *);
2218 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2219 (cp_parser *);
2220 static tree cp_parser_tx_qualifier_opt
2221 (cp_parser *);
2222 static tree cp_parser_late_return_type_opt
2223 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2224 static tree cp_parser_declarator_id
2225 (cp_parser *, bool);
2226 static tree cp_parser_type_id
2227 (cp_parser *);
2228 static tree cp_parser_template_type_arg
2229 (cp_parser *);
2230 static tree cp_parser_trailing_type_id (cp_parser *);
2231 static tree cp_parser_type_id_1
2232 (cp_parser *, bool, bool);
2233 static void cp_parser_type_specifier_seq
2234 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2235 static tree cp_parser_parameter_declaration_clause
2236 (cp_parser *);
2237 static tree cp_parser_parameter_declaration_list
2238 (cp_parser *, bool *);
2239 static cp_parameter_declarator *cp_parser_parameter_declaration
2240 (cp_parser *, bool, bool *);
2241 static tree cp_parser_default_argument
2242 (cp_parser *, bool);
2243 static void cp_parser_function_body
2244 (cp_parser *, bool);
2245 static tree cp_parser_initializer
2246 (cp_parser *, bool *, bool *, bool = false);
2247 static cp_expr cp_parser_initializer_clause
2248 (cp_parser *, bool *);
2249 static cp_expr cp_parser_braced_list
2250 (cp_parser*, bool*);
2251 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2252 (cp_parser *, bool *);
2254 static void cp_parser_ctor_initializer_opt_and_function_body
2255 (cp_parser *, bool);
2257 static tree cp_parser_late_parsing_omp_declare_simd
2258 (cp_parser *, tree);
2260 static tree cp_parser_late_parsing_oacc_routine
2261 (cp_parser *, tree);
2263 static tree synthesize_implicit_template_parm
2264 (cp_parser *, tree);
2265 static tree finish_fully_implicit_template
2266 (cp_parser *, tree);
2267 static void abort_fully_implicit_template
2268 (cp_parser *);
2270 /* Classes [gram.class] */
2272 static tree cp_parser_class_name
2273 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2274 static tree cp_parser_class_specifier
2275 (cp_parser *);
2276 static tree cp_parser_class_head
2277 (cp_parser *, bool *);
2278 static enum tag_types cp_parser_class_key
2279 (cp_parser *);
2280 static void cp_parser_type_parameter_key
2281 (cp_parser* parser);
2282 static void cp_parser_member_specification_opt
2283 (cp_parser *);
2284 static void cp_parser_member_declaration
2285 (cp_parser *);
2286 static tree cp_parser_pure_specifier
2287 (cp_parser *);
2288 static tree cp_parser_constant_initializer
2289 (cp_parser *);
2291 /* Derived classes [gram.class.derived] */
2293 static tree cp_parser_base_clause
2294 (cp_parser *);
2295 static tree cp_parser_base_specifier
2296 (cp_parser *);
2298 /* Special member functions [gram.special] */
2300 static tree cp_parser_conversion_function_id
2301 (cp_parser *);
2302 static tree cp_parser_conversion_type_id
2303 (cp_parser *);
2304 static cp_declarator *cp_parser_conversion_declarator_opt
2305 (cp_parser *);
2306 static void cp_parser_ctor_initializer_opt
2307 (cp_parser *);
2308 static void cp_parser_mem_initializer_list
2309 (cp_parser *);
2310 static tree cp_parser_mem_initializer
2311 (cp_parser *);
2312 static tree cp_parser_mem_initializer_id
2313 (cp_parser *);
2315 /* Overloading [gram.over] */
2317 static cp_expr cp_parser_operator_function_id
2318 (cp_parser *);
2319 static cp_expr cp_parser_operator
2320 (cp_parser *);
2322 /* Templates [gram.temp] */
2324 static void cp_parser_template_declaration
2325 (cp_parser *, bool);
2326 static tree cp_parser_template_parameter_list
2327 (cp_parser *);
2328 static tree cp_parser_template_parameter
2329 (cp_parser *, bool *, bool *);
2330 static tree cp_parser_type_parameter
2331 (cp_parser *, bool *);
2332 static tree cp_parser_template_id
2333 (cp_parser *, bool, bool, enum tag_types, bool);
2334 static tree cp_parser_template_name
2335 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2336 static tree cp_parser_template_argument_list
2337 (cp_parser *);
2338 static tree cp_parser_template_argument
2339 (cp_parser *);
2340 static void cp_parser_explicit_instantiation
2341 (cp_parser *);
2342 static void cp_parser_explicit_specialization
2343 (cp_parser *);
2345 /* Exception handling [gram.exception] */
2347 static tree cp_parser_try_block
2348 (cp_parser *);
2349 static void cp_parser_function_try_block
2350 (cp_parser *);
2351 static void cp_parser_handler_seq
2352 (cp_parser *);
2353 static void cp_parser_handler
2354 (cp_parser *);
2355 static tree cp_parser_exception_declaration
2356 (cp_parser *);
2357 static tree cp_parser_throw_expression
2358 (cp_parser *);
2359 static tree cp_parser_exception_specification_opt
2360 (cp_parser *);
2361 static tree cp_parser_type_id_list
2362 (cp_parser *);
2364 /* GNU Extensions */
2366 static tree cp_parser_asm_specification_opt
2367 (cp_parser *);
2368 static tree cp_parser_asm_operand_list
2369 (cp_parser *);
2370 static tree cp_parser_asm_clobber_list
2371 (cp_parser *);
2372 static tree cp_parser_asm_label_list
2373 (cp_parser *);
2374 static bool cp_next_tokens_can_be_attribute_p
2375 (cp_parser *);
2376 static bool cp_next_tokens_can_be_gnu_attribute_p
2377 (cp_parser *);
2378 static bool cp_next_tokens_can_be_std_attribute_p
2379 (cp_parser *);
2380 static bool cp_nth_tokens_can_be_std_attribute_p
2381 (cp_parser *, size_t);
2382 static bool cp_nth_tokens_can_be_gnu_attribute_p
2383 (cp_parser *, size_t);
2384 static bool cp_nth_tokens_can_be_attribute_p
2385 (cp_parser *, size_t);
2386 static tree cp_parser_attributes_opt
2387 (cp_parser *);
2388 static tree cp_parser_gnu_attributes_opt
2389 (cp_parser *);
2390 static tree cp_parser_gnu_attribute_list
2391 (cp_parser *);
2392 static tree cp_parser_std_attribute
2393 (cp_parser *, tree);
2394 static tree cp_parser_std_attribute_spec
2395 (cp_parser *);
2396 static tree cp_parser_std_attribute_spec_seq
2397 (cp_parser *);
2398 static size_t cp_parser_skip_attributes_opt
2399 (cp_parser *, size_t);
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 void 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, bool, 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 cp_expr 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, location_t);
2553 static cp_token *cp_parser_require
2554 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
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_init_statement_p
2646 (cp_parser *);
2647 static bool cp_parser_skip_to_closing_square_bracket
2648 (cp_parser *);
2650 /* Concept-related syntactic transformations */
2652 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2653 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2655 // -------------------------------------------------------------------------- //
2656 // Unevaluated Operand Guard
2658 // Implementation of an RAII helper for unevaluated operand parsing.
2659 cp_unevaluated::cp_unevaluated ()
2661 ++cp_unevaluated_operand;
2662 ++c_inhibit_evaluation_warnings;
2665 cp_unevaluated::~cp_unevaluated ()
2667 --c_inhibit_evaluation_warnings;
2668 --cp_unevaluated_operand;
2671 // -------------------------------------------------------------------------- //
2672 // Tentative Parsing
2674 /* Returns nonzero if we are parsing tentatively. */
2676 static inline bool
2677 cp_parser_parsing_tentatively (cp_parser* parser)
2679 return parser->context->next != NULL;
2682 /* Returns nonzero if TOKEN is a string literal. */
2684 static bool
2685 cp_parser_is_pure_string_literal (cp_token* token)
2687 return (token->type == CPP_STRING ||
2688 token->type == CPP_STRING16 ||
2689 token->type == CPP_STRING32 ||
2690 token->type == CPP_WSTRING ||
2691 token->type == CPP_UTF8STRING);
2694 /* Returns nonzero if TOKEN is a string literal
2695 of a user-defined string literal. */
2697 static bool
2698 cp_parser_is_string_literal (cp_token* token)
2700 return (cp_parser_is_pure_string_literal (token) ||
2701 token->type == CPP_STRING_USERDEF ||
2702 token->type == CPP_STRING16_USERDEF ||
2703 token->type == CPP_STRING32_USERDEF ||
2704 token->type == CPP_WSTRING_USERDEF ||
2705 token->type == CPP_UTF8STRING_USERDEF);
2708 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2710 static bool
2711 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2713 return token->keyword == keyword;
2716 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2717 PRAGMA_NONE. */
2719 static enum pragma_kind
2720 cp_parser_pragma_kind (cp_token *token)
2722 if (token->type != CPP_PRAGMA)
2723 return PRAGMA_NONE;
2724 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2725 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2728 /* Helper function for cp_parser_error.
2729 Having peeked a token of kind TOK1_KIND that might signify
2730 a conflict marker, peek successor tokens to determine
2731 if we actually do have a conflict marker.
2732 Specifically, we consider a run of 7 '<', '=' or '>' characters
2733 at the start of a line as a conflict marker.
2734 These come through the lexer as three pairs and a single,
2735 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2736 If it returns true, *OUT_LOC is written to with the location/range
2737 of the marker. */
2739 static bool
2740 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2741 location_t *out_loc)
2743 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2744 if (token2->type != tok1_kind)
2745 return false;
2746 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2747 if (token3->type != tok1_kind)
2748 return false;
2749 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2750 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2751 return false;
2753 /* It must be at the start of the line. */
2754 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2755 if (LOCATION_COLUMN (start_loc) != 1)
2756 return false;
2758 /* We have a conflict marker. Construct a location of the form:
2759 <<<<<<<
2760 ^~~~~~~
2761 with start == caret, finishing at the end of the marker. */
2762 location_t finish_loc = get_finish (token4->location);
2763 *out_loc = make_location (start_loc, start_loc, finish_loc);
2765 return true;
2768 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2769 RT_CLOSE_PAREN. */
2771 static const char *
2772 get_matching_symbol (required_token token_desc)
2774 switch (token_desc)
2776 default:
2777 gcc_unreachable ();
2778 return "";
2779 case RT_CLOSE_BRACE:
2780 return "{";
2781 case RT_CLOSE_PAREN:
2782 return "(";
2786 /* Attempt to convert TOKEN_DESC from a required_token to an
2787 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2789 static enum cpp_ttype
2790 get_required_cpp_ttype (required_token token_desc)
2792 switch (token_desc)
2794 case RT_SEMICOLON:
2795 return CPP_SEMICOLON;
2796 case RT_OPEN_PAREN:
2797 return CPP_OPEN_PAREN;
2798 case RT_CLOSE_BRACE:
2799 return CPP_CLOSE_BRACE;
2800 case RT_OPEN_BRACE:
2801 return CPP_OPEN_BRACE;
2802 case RT_CLOSE_SQUARE:
2803 return CPP_CLOSE_SQUARE;
2804 case RT_OPEN_SQUARE:
2805 return CPP_OPEN_SQUARE;
2806 case RT_COMMA:
2807 return CPP_COMMA;
2808 case RT_COLON:
2809 return CPP_COLON;
2810 case RT_CLOSE_PAREN:
2811 return CPP_CLOSE_PAREN;
2813 default:
2814 /* Use CPP_EOF as a "no completions possible" code. */
2815 return CPP_EOF;
2820 /* Subroutine of cp_parser_error and cp_parser_required_error.
2822 Issue a diagnostic of the form
2823 FILE:LINE: MESSAGE before TOKEN
2824 where TOKEN is the next token in the input stream. MESSAGE
2825 (specified by the caller) is usually of the form "expected
2826 OTHER-TOKEN".
2828 This bypasses the check for tentative passing, and potentially
2829 adds material needed by cp_parser_required_error.
2831 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2832 suggesting insertion of the missing token.
2834 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2835 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2836 location. */
2838 static void
2839 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2840 required_token missing_token_desc,
2841 location_t matching_location)
2843 cp_token *token = cp_lexer_peek_token (parser->lexer);
2844 /* This diagnostic makes more sense if it is tagged to the line
2845 of the token we just peeked at. */
2846 cp_lexer_set_source_position_from_token (token);
2848 if (token->type == CPP_PRAGMA)
2850 error_at (token->location,
2851 "%<#pragma%> is not allowed here");
2852 cp_parser_skip_to_pragma_eol (parser, token);
2853 return;
2856 /* If this is actually a conflict marker, report it as such. */
2857 if (token->type == CPP_LSHIFT
2858 || token->type == CPP_RSHIFT
2859 || token->type == CPP_EQ_EQ)
2861 location_t loc;
2862 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2864 error_at (loc, "version control conflict marker in file");
2865 return;
2869 gcc_rich_location richloc (input_location);
2871 bool added_matching_location = false;
2873 if (missing_token_desc != RT_NONE)
2875 /* Potentially supply a fix-it hint, suggesting to add the
2876 missing token immediately after the *previous* token.
2877 This may move the primary location within richloc. */
2878 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2879 location_t prev_token_loc
2880 = cp_lexer_previous_token (parser->lexer)->location;
2881 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2883 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2884 Attempt to consolidate diagnostics by printing it as a
2885 secondary range within the main diagnostic. */
2886 if (matching_location != UNKNOWN_LOCATION)
2887 added_matching_location
2888 = richloc.add_location_if_nearby (matching_location);
2891 /* Actually emit the error. */
2892 c_parse_error (gmsgid,
2893 /* Because c_parser_error does not understand
2894 CPP_KEYWORD, keywords are treated like
2895 identifiers. */
2896 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2897 token->u.value, token->flags, &richloc);
2899 if (missing_token_desc != RT_NONE)
2901 /* If we weren't able to consolidate matching_location, then
2902 print it as a secondary diagnostic. */
2903 if (matching_location != UNKNOWN_LOCATION
2904 && !added_matching_location)
2905 inform (matching_location, "to match this %qs",
2906 get_matching_symbol (missing_token_desc));
2910 /* If not parsing tentatively, issue a diagnostic of the form
2911 FILE:LINE: MESSAGE before TOKEN
2912 where TOKEN is the next token in the input stream. MESSAGE
2913 (specified by the caller) is usually of the form "expected
2914 OTHER-TOKEN". */
2916 static void
2917 cp_parser_error (cp_parser* parser, const char* gmsgid)
2919 if (!cp_parser_simulate_error (parser))
2920 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2923 /* Issue an error about name-lookup failing. NAME is the
2924 IDENTIFIER_NODE DECL is the result of
2925 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2926 the thing that we hoped to find. */
2928 static void
2929 cp_parser_name_lookup_error (cp_parser* parser,
2930 tree name,
2931 tree decl,
2932 name_lookup_error desired,
2933 location_t location)
2935 /* If name lookup completely failed, tell the user that NAME was not
2936 declared. */
2937 if (decl == error_mark_node)
2939 if (parser->scope && parser->scope != global_namespace)
2940 error_at (location, "%<%E::%E%> has not been declared",
2941 parser->scope, name);
2942 else if (parser->scope == global_namespace)
2943 error_at (location, "%<::%E%> has not been declared", name);
2944 else if (parser->object_scope
2945 && !CLASS_TYPE_P (parser->object_scope))
2946 error_at (location, "request for member %qE in non-class type %qT",
2947 name, parser->object_scope);
2948 else if (parser->object_scope)
2949 error_at (location, "%<%T::%E%> has not been declared",
2950 parser->object_scope, name);
2951 else
2952 error_at (location, "%qE has not been declared", name);
2954 else if (parser->scope && parser->scope != global_namespace)
2956 switch (desired)
2958 case NLE_TYPE:
2959 error_at (location, "%<%E::%E%> is not a type",
2960 parser->scope, name);
2961 break;
2962 case NLE_CXX98:
2963 error_at (location, "%<%E::%E%> is not a class or namespace",
2964 parser->scope, name);
2965 break;
2966 case NLE_NOT_CXX98:
2967 error_at (location,
2968 "%<%E::%E%> is not a class, namespace, or enumeration",
2969 parser->scope, name);
2970 break;
2971 default:
2972 gcc_unreachable ();
2976 else if (parser->scope == global_namespace)
2978 switch (desired)
2980 case NLE_TYPE:
2981 error_at (location, "%<::%E%> is not a type", name);
2982 break;
2983 case NLE_CXX98:
2984 error_at (location, "%<::%E%> is not a class or namespace", name);
2985 break;
2986 case NLE_NOT_CXX98:
2987 error_at (location,
2988 "%<::%E%> is not a class, namespace, or enumeration",
2989 name);
2990 break;
2991 default:
2992 gcc_unreachable ();
2995 else
2997 switch (desired)
2999 case NLE_TYPE:
3000 error_at (location, "%qE is not a type", name);
3001 break;
3002 case NLE_CXX98:
3003 error_at (location, "%qE is not a class or namespace", name);
3004 break;
3005 case NLE_NOT_CXX98:
3006 error_at (location,
3007 "%qE is not a class, namespace, or enumeration", name);
3008 break;
3009 default:
3010 gcc_unreachable ();
3015 /* If we are parsing tentatively, remember that an error has occurred
3016 during this tentative parse. Returns true if the error was
3017 simulated; false if a message should be issued by the caller. */
3019 static bool
3020 cp_parser_simulate_error (cp_parser* parser)
3022 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3024 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3025 return true;
3027 return false;
3030 /* This function is called when a type is defined. If type
3031 definitions are forbidden at this point, an error message is
3032 issued. */
3034 static bool
3035 cp_parser_check_type_definition (cp_parser* parser)
3037 /* If types are forbidden here, issue a message. */
3038 if (parser->type_definition_forbidden_message)
3040 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3041 in the message need to be interpreted. */
3042 error (parser->type_definition_forbidden_message);
3043 return false;
3045 return true;
3048 /* This function is called when the DECLARATOR is processed. The TYPE
3049 was a type defined in the decl-specifiers. If it is invalid to
3050 define a type in the decl-specifiers for DECLARATOR, an error is
3051 issued. TYPE_LOCATION is the location of TYPE and is used
3052 for error reporting. */
3054 static void
3055 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3056 tree type, location_t type_location)
3058 /* [dcl.fct] forbids type definitions in return types.
3059 Unfortunately, it's not easy to know whether or not we are
3060 processing a return type until after the fact. */
3061 while (declarator
3062 && (declarator->kind == cdk_pointer
3063 || declarator->kind == cdk_reference
3064 || declarator->kind == cdk_ptrmem))
3065 declarator = declarator->declarator;
3066 if (declarator
3067 && declarator->kind == cdk_function)
3069 error_at (type_location,
3070 "new types may not be defined in a return type");
3071 inform (type_location,
3072 "(perhaps a semicolon is missing after the definition of %qT)",
3073 type);
3077 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3078 "<" in any valid C++ program. If the next token is indeed "<",
3079 issue a message warning the user about what appears to be an
3080 invalid attempt to form a template-id. LOCATION is the location
3081 of the type-specifier (TYPE) */
3083 static void
3084 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3085 tree type,
3086 enum tag_types tag_type,
3087 location_t location)
3089 cp_token_position start = 0;
3091 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3093 if (TREE_CODE (type) == TYPE_DECL)
3094 type = TREE_TYPE (type);
3095 if (TYPE_P (type) && !template_placeholder_p (type))
3096 error_at (location, "%qT is not a template", type);
3097 else if (identifier_p (type))
3099 if (tag_type != none_type)
3100 error_at (location, "%qE is not a class template", type);
3101 else
3102 error_at (location, "%qE is not a template", type);
3104 else
3105 error_at (location, "invalid template-id");
3106 /* Remember the location of the invalid "<". */
3107 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3108 start = cp_lexer_token_position (parser->lexer, true);
3109 /* Consume the "<". */
3110 cp_lexer_consume_token (parser->lexer);
3111 /* Parse the template arguments. */
3112 cp_parser_enclosed_template_argument_list (parser);
3113 /* Permanently remove the invalid template arguments so that
3114 this error message is not issued again. */
3115 if (start)
3116 cp_lexer_purge_tokens_after (parser->lexer, start);
3120 /* If parsing an integral constant-expression, issue an error message
3121 about the fact that THING appeared and return true. Otherwise,
3122 return false. In either case, set
3123 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3125 static bool
3126 cp_parser_non_integral_constant_expression (cp_parser *parser,
3127 non_integral_constant thing)
3129 parser->non_integral_constant_expression_p = true;
3130 if (parser->integral_constant_expression_p)
3132 if (!parser->allow_non_integral_constant_expression_p)
3134 const char *msg = NULL;
3135 switch (thing)
3137 case NIC_FLOAT:
3138 pedwarn (input_location, OPT_Wpedantic,
3139 "ISO C++ forbids using a floating-point literal "
3140 "in a constant-expression");
3141 return true;
3142 case NIC_CAST:
3143 error ("a cast to a type other than an integral or "
3144 "enumeration type cannot appear in a "
3145 "constant-expression");
3146 return true;
3147 case NIC_TYPEID:
3148 error ("%<typeid%> operator "
3149 "cannot appear in a constant-expression");
3150 return true;
3151 case NIC_NCC:
3152 error ("non-constant compound literals "
3153 "cannot appear in a constant-expression");
3154 return true;
3155 case NIC_FUNC_CALL:
3156 error ("a function call "
3157 "cannot appear in a constant-expression");
3158 return true;
3159 case NIC_INC:
3160 error ("an increment "
3161 "cannot appear in a constant-expression");
3162 return true;
3163 case NIC_DEC:
3164 error ("an decrement "
3165 "cannot appear in a constant-expression");
3166 return true;
3167 case NIC_ARRAY_REF:
3168 error ("an array reference "
3169 "cannot appear in a constant-expression");
3170 return true;
3171 case NIC_ADDR_LABEL:
3172 error ("the address of a label "
3173 "cannot appear in a constant-expression");
3174 return true;
3175 case NIC_OVERLOADED:
3176 error ("calls to overloaded operators "
3177 "cannot appear in a constant-expression");
3178 return true;
3179 case NIC_ASSIGNMENT:
3180 error ("an assignment cannot appear in a constant-expression");
3181 return true;
3182 case NIC_COMMA:
3183 error ("a comma operator "
3184 "cannot appear in a constant-expression");
3185 return true;
3186 case NIC_CONSTRUCTOR:
3187 error ("a call to a constructor "
3188 "cannot appear in a constant-expression");
3189 return true;
3190 case NIC_TRANSACTION:
3191 error ("a transaction expression "
3192 "cannot appear in a constant-expression");
3193 return true;
3194 case NIC_THIS:
3195 msg = "this";
3196 break;
3197 case NIC_FUNC_NAME:
3198 msg = "__FUNCTION__";
3199 break;
3200 case NIC_PRETTY_FUNC:
3201 msg = "__PRETTY_FUNCTION__";
3202 break;
3203 case NIC_C99_FUNC:
3204 msg = "__func__";
3205 break;
3206 case NIC_VA_ARG:
3207 msg = "va_arg";
3208 break;
3209 case NIC_ARROW:
3210 msg = "->";
3211 break;
3212 case NIC_POINT:
3213 msg = ".";
3214 break;
3215 case NIC_STAR:
3216 msg = "*";
3217 break;
3218 case NIC_ADDR:
3219 msg = "&";
3220 break;
3221 case NIC_PREINCREMENT:
3222 msg = "++";
3223 break;
3224 case NIC_PREDECREMENT:
3225 msg = "--";
3226 break;
3227 case NIC_NEW:
3228 msg = "new";
3229 break;
3230 case NIC_DEL:
3231 msg = "delete";
3232 break;
3233 default:
3234 gcc_unreachable ();
3236 if (msg)
3237 error ("%qs cannot appear in a constant-expression", msg);
3238 return true;
3241 return false;
3244 /* Emit a diagnostic for an invalid type name. This function commits
3245 to the current active tentative parse, if any. (Otherwise, the
3246 problematic construct might be encountered again later, resulting
3247 in duplicate error messages.) LOCATION is the location of ID. */
3249 static void
3250 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3251 location_t location)
3253 tree decl, ambiguous_decls;
3254 cp_parser_commit_to_tentative_parse (parser);
3255 /* Try to lookup the identifier. */
3256 decl = cp_parser_lookup_name (parser, id, none_type,
3257 /*is_template=*/false,
3258 /*is_namespace=*/false,
3259 /*check_dependency=*/true,
3260 &ambiguous_decls, location);
3261 if (ambiguous_decls)
3262 /* If the lookup was ambiguous, an error will already have
3263 been issued. */
3264 return;
3265 /* If the lookup found a template-name, it means that the user forgot
3266 to specify an argument list. Emit a useful error message. */
3267 if (DECL_TYPE_TEMPLATE_P (decl))
3269 error_at (location,
3270 "invalid use of template-name %qE without an argument list",
3271 decl);
3272 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3273 inform (location, "class template argument deduction is only available "
3274 "with -std=c++17 or -std=gnu++17");
3275 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3277 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3278 error_at (location, "invalid use of destructor %qD as a type", id);
3279 else if (TREE_CODE (decl) == TYPE_DECL)
3280 /* Something like 'unsigned A a;' */
3281 error_at (location, "invalid combination of multiple type-specifiers");
3282 else if (!parser->scope)
3284 /* Issue an error message. */
3285 name_hint hint;
3286 if (TREE_CODE (id) == IDENTIFIER_NODE)
3287 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3288 if (hint)
3290 gcc_rich_location richloc (location);
3291 richloc.add_fixit_replace (hint.suggestion ());
3292 error_at (&richloc,
3293 "%qE does not name a type; did you mean %qs?",
3294 id, hint.suggestion ());
3296 else
3297 error_at (location, "%qE does not name a type", id);
3298 /* If we're in a template class, it's possible that the user was
3299 referring to a type from a base class. For example:
3301 template <typename T> struct A { typedef T X; };
3302 template <typename T> struct B : public A<T> { X x; };
3304 The user should have said "typename A<T>::X". */
3305 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3306 inform (location, "C++11 %<constexpr%> only available with "
3307 "-std=c++11 or -std=gnu++11");
3308 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3309 inform (location, "C++11 %<noexcept%> only available with "
3310 "-std=c++11 or -std=gnu++11");
3311 else if (cxx_dialect < cxx11
3312 && TREE_CODE (id) == IDENTIFIER_NODE
3313 && id_equal (id, "thread_local"))
3314 inform (location, "C++11 %<thread_local%> only available with "
3315 "-std=c++11 or -std=gnu++11");
3316 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3317 inform (location, "%<concept%> only available with -fconcepts");
3318 else if (processing_template_decl && current_class_type
3319 && TYPE_BINFO (current_class_type))
3321 tree b;
3323 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3325 b = TREE_CHAIN (b))
3327 tree base_type = BINFO_TYPE (b);
3328 if (CLASS_TYPE_P (base_type)
3329 && dependent_type_p (base_type))
3331 tree field;
3332 /* Go from a particular instantiation of the
3333 template (which will have an empty TYPE_FIELDs),
3334 to the main version. */
3335 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3336 for (field = TYPE_FIELDS (base_type);
3337 field;
3338 field = DECL_CHAIN (field))
3339 if (TREE_CODE (field) == TYPE_DECL
3340 && DECL_NAME (field) == id)
3342 inform (location,
3343 "(perhaps %<typename %T::%E%> was intended)",
3344 BINFO_TYPE (b), id);
3345 break;
3347 if (field)
3348 break;
3353 /* Here we diagnose qualified-ids where the scope is actually correct,
3354 but the identifier does not resolve to a valid type name. */
3355 else if (parser->scope != error_mark_node)
3357 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3359 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3360 error_at (location_of (id),
3361 "%qE in namespace %qE does not name a template type",
3362 id, parser->scope);
3363 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3364 error_at (location_of (id),
3365 "%qE in namespace %qE does not name a template type",
3366 TREE_OPERAND (id, 0), parser->scope);
3367 else
3368 error_at (location_of (id),
3369 "%qE in namespace %qE does not name a type",
3370 id, parser->scope);
3371 if (DECL_P (decl))
3372 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3373 else if (decl == error_mark_node)
3374 suggest_alternative_in_explicit_scope (location, id,
3375 parser->scope);
3377 else if (CLASS_TYPE_P (parser->scope)
3378 && constructor_name_p (id, parser->scope))
3380 /* A<T>::A<T>() */
3381 error_at (location, "%<%T::%E%> names the constructor, not"
3382 " the type", parser->scope, id);
3383 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3384 error_at (location, "and %qT has no template constructors",
3385 parser->scope);
3387 else if (TYPE_P (parser->scope)
3388 && dependent_scope_p (parser->scope))
3390 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3391 error_at (location,
3392 "need %<typename%> before %<%T::%D::%E%> because "
3393 "%<%T::%D%> is a dependent scope",
3394 TYPE_CONTEXT (parser->scope),
3395 TYPENAME_TYPE_FULLNAME (parser->scope),
3397 TYPE_CONTEXT (parser->scope),
3398 TYPENAME_TYPE_FULLNAME (parser->scope));
3399 else
3400 error_at (location, "need %<typename%> before %<%T::%E%> because "
3401 "%qT is a dependent scope",
3402 parser->scope, id, parser->scope);
3404 else if (TYPE_P (parser->scope))
3406 if (!COMPLETE_TYPE_P (parser->scope))
3407 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3408 parser->scope);
3409 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3410 error_at (location_of (id),
3411 "%qE in %q#T does not name a template type",
3412 id, parser->scope);
3413 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3414 error_at (location_of (id),
3415 "%qE in %q#T does not name a template type",
3416 TREE_OPERAND (id, 0), parser->scope);
3417 else
3418 error_at (location_of (id),
3419 "%qE in %q#T does not name a type",
3420 id, parser->scope);
3421 if (DECL_P (decl))
3422 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3424 else
3425 gcc_unreachable ();
3429 /* Check for a common situation where a type-name should be present,
3430 but is not, and issue a sensible error message. Returns true if an
3431 invalid type-name was detected.
3433 The situation handled by this function are variable declarations of the
3434 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3435 Usually, `ID' should name a type, but if we got here it means that it
3436 does not. We try to emit the best possible error message depending on
3437 how exactly the id-expression looks like. */
3439 static bool
3440 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3442 tree id;
3443 cp_token *token = cp_lexer_peek_token (parser->lexer);
3445 /* Avoid duplicate error about ambiguous lookup. */
3446 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3448 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3449 if (next->type == CPP_NAME && next->error_reported)
3450 goto out;
3453 cp_parser_parse_tentatively (parser);
3454 id = cp_parser_id_expression (parser,
3455 /*template_keyword_p=*/false,
3456 /*check_dependency_p=*/true,
3457 /*template_p=*/NULL,
3458 /*declarator_p=*/false,
3459 /*optional_p=*/false);
3460 /* If the next token is a (, this is a function with no explicit return
3461 type, i.e. constructor, destructor or conversion op. */
3462 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3463 || TREE_CODE (id) == TYPE_DECL)
3465 cp_parser_abort_tentative_parse (parser);
3466 return false;
3468 if (!cp_parser_parse_definitely (parser))
3469 return false;
3471 /* Emit a diagnostic for the invalid type. */
3472 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3473 out:
3474 /* If we aren't in the middle of a declarator (i.e. in a
3475 parameter-declaration-clause), skip to the end of the declaration;
3476 there's no point in trying to process it. */
3477 if (!parser->in_declarator_p)
3478 cp_parser_skip_to_end_of_block_or_statement (parser);
3479 return true;
3482 /* Consume tokens up to, and including, the next non-nested closing `)'.
3483 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3484 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3485 found an unnested token of that type. */
3487 static int
3488 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3489 bool recovering,
3490 cpp_ttype or_ttype,
3491 bool consume_paren)
3493 unsigned paren_depth = 0;
3494 unsigned brace_depth = 0;
3495 unsigned square_depth = 0;
3497 if (recovering && or_ttype == CPP_EOF
3498 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3499 return 0;
3501 while (true)
3503 cp_token * token = cp_lexer_peek_token (parser->lexer);
3505 /* Have we found what we're looking for before the closing paren? */
3506 if (token->type == or_ttype && or_ttype != CPP_EOF
3507 && !brace_depth && !paren_depth && !square_depth)
3508 return -1;
3510 switch (token->type)
3512 case CPP_EOF:
3513 case CPP_PRAGMA_EOL:
3514 /* If we've run out of tokens, then there is no closing `)'. */
3515 return 0;
3517 /* This is good for lambda expression capture-lists. */
3518 case CPP_OPEN_SQUARE:
3519 ++square_depth;
3520 break;
3521 case CPP_CLOSE_SQUARE:
3522 if (!square_depth--)
3523 return 0;
3524 break;
3526 case CPP_SEMICOLON:
3527 /* This matches the processing in skip_to_end_of_statement. */
3528 if (!brace_depth)
3529 return 0;
3530 break;
3532 case CPP_OPEN_BRACE:
3533 ++brace_depth;
3534 break;
3535 case CPP_CLOSE_BRACE:
3536 if (!brace_depth--)
3537 return 0;
3538 break;
3540 case CPP_OPEN_PAREN:
3541 if (!brace_depth)
3542 ++paren_depth;
3543 break;
3545 case CPP_CLOSE_PAREN:
3546 if (!brace_depth && !paren_depth--)
3548 if (consume_paren)
3549 cp_lexer_consume_token (parser->lexer);
3550 return 1;
3552 break;
3554 default:
3555 break;
3558 /* Consume the token. */
3559 cp_lexer_consume_token (parser->lexer);
3563 /* Consume tokens up to, and including, the next non-nested closing `)'.
3564 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3565 are doing error recovery. Returns -1 if OR_COMMA is true and we
3566 found an unnested token of that type. */
3568 static int
3569 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3570 bool recovering,
3571 bool or_comma,
3572 bool consume_paren)
3574 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3575 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3576 ttype, consume_paren);
3579 /* Consume tokens until we reach the end of the current statement.
3580 Normally, that will be just before consuming a `;'. However, if a
3581 non-nested `}' comes first, then we stop before consuming that. */
3583 static void
3584 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3586 unsigned nesting_depth = 0;
3588 /* Unwind generic function template scope if necessary. */
3589 if (parser->fully_implicit_function_template_p)
3590 abort_fully_implicit_template (parser);
3592 while (true)
3594 cp_token *token = cp_lexer_peek_token (parser->lexer);
3596 switch (token->type)
3598 case CPP_EOF:
3599 case CPP_PRAGMA_EOL:
3600 /* If we've run out of tokens, stop. */
3601 return;
3603 case CPP_SEMICOLON:
3604 /* If the next token is a `;', we have reached the end of the
3605 statement. */
3606 if (!nesting_depth)
3607 return;
3608 break;
3610 case CPP_CLOSE_BRACE:
3611 /* If this is a non-nested '}', stop before consuming it.
3612 That way, when confronted with something like:
3614 { 3 + }
3616 we stop before consuming the closing '}', even though we
3617 have not yet reached a `;'. */
3618 if (nesting_depth == 0)
3619 return;
3621 /* If it is the closing '}' for a block that we have
3622 scanned, stop -- but only after consuming the token.
3623 That way given:
3625 void f g () { ... }
3626 typedef int I;
3628 we will stop after the body of the erroneously declared
3629 function, but before consuming the following `typedef'
3630 declaration. */
3631 if (--nesting_depth == 0)
3633 cp_lexer_consume_token (parser->lexer);
3634 return;
3636 break;
3638 case CPP_OPEN_BRACE:
3639 ++nesting_depth;
3640 break;
3642 default:
3643 break;
3646 /* Consume the token. */
3647 cp_lexer_consume_token (parser->lexer);
3651 /* This function is called at the end of a statement or declaration.
3652 If the next token is a semicolon, it is consumed; otherwise, error
3653 recovery is attempted. */
3655 static void
3656 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3658 /* Look for the trailing `;'. */
3659 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3661 /* If there is additional (erroneous) input, skip to the end of
3662 the statement. */
3663 cp_parser_skip_to_end_of_statement (parser);
3664 /* If the next token is now a `;', consume it. */
3665 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3666 cp_lexer_consume_token (parser->lexer);
3670 /* Skip tokens until we have consumed an entire block, or until we
3671 have consumed a non-nested `;'. */
3673 static void
3674 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3676 int nesting_depth = 0;
3678 /* Unwind generic function template scope if necessary. */
3679 if (parser->fully_implicit_function_template_p)
3680 abort_fully_implicit_template (parser);
3682 while (nesting_depth >= 0)
3684 cp_token *token = cp_lexer_peek_token (parser->lexer);
3686 switch (token->type)
3688 case CPP_EOF:
3689 case CPP_PRAGMA_EOL:
3690 /* If we've run out of tokens, stop. */
3691 return;
3693 case CPP_SEMICOLON:
3694 /* Stop if this is an unnested ';'. */
3695 if (!nesting_depth)
3696 nesting_depth = -1;
3697 break;
3699 case CPP_CLOSE_BRACE:
3700 /* Stop if this is an unnested '}', or closes the outermost
3701 nesting level. */
3702 nesting_depth--;
3703 if (nesting_depth < 0)
3704 return;
3705 if (!nesting_depth)
3706 nesting_depth = -1;
3707 break;
3709 case CPP_OPEN_BRACE:
3710 /* Nest. */
3711 nesting_depth++;
3712 break;
3714 default:
3715 break;
3718 /* Consume the token. */
3719 cp_lexer_consume_token (parser->lexer);
3723 /* Skip tokens until a non-nested closing curly brace is the next
3724 token, or there are no more tokens. Return true in the first case,
3725 false otherwise. */
3727 static bool
3728 cp_parser_skip_to_closing_brace (cp_parser *parser)
3730 unsigned nesting_depth = 0;
3732 while (true)
3734 cp_token *token = cp_lexer_peek_token (parser->lexer);
3736 switch (token->type)
3738 case CPP_EOF:
3739 case CPP_PRAGMA_EOL:
3740 /* If we've run out of tokens, stop. */
3741 return false;
3743 case CPP_CLOSE_BRACE:
3744 /* If the next token is a non-nested `}', then we have reached
3745 the end of the current block. */
3746 if (nesting_depth-- == 0)
3747 return true;
3748 break;
3750 case CPP_OPEN_BRACE:
3751 /* If it the next token is a `{', then we are entering a new
3752 block. Consume the entire block. */
3753 ++nesting_depth;
3754 break;
3756 default:
3757 break;
3760 /* Consume the token. */
3761 cp_lexer_consume_token (parser->lexer);
3765 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3766 parameter is the PRAGMA token, allowing us to purge the entire pragma
3767 sequence. */
3769 static void
3770 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3772 cp_token *token;
3774 parser->lexer->in_pragma = false;
3777 token = cp_lexer_consume_token (parser->lexer);
3778 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3780 /* Ensure that the pragma is not parsed again. */
3781 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3784 /* Require pragma end of line, resyncing with it as necessary. The
3785 arguments are as for cp_parser_skip_to_pragma_eol. */
3787 static void
3788 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3790 parser->lexer->in_pragma = false;
3791 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3792 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3795 /* This is a simple wrapper around make_typename_type. When the id is
3796 an unresolved identifier node, we can provide a superior diagnostic
3797 using cp_parser_diagnose_invalid_type_name. */
3799 static tree
3800 cp_parser_make_typename_type (cp_parser *parser, tree id,
3801 location_t id_location)
3803 tree result;
3804 if (identifier_p (id))
3806 result = make_typename_type (parser->scope, id, typename_type,
3807 /*complain=*/tf_none);
3808 if (result == error_mark_node)
3809 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3810 return result;
3812 return make_typename_type (parser->scope, id, typename_type, tf_error);
3815 /* This is a wrapper around the
3816 make_{pointer,ptrmem,reference}_declarator functions that decides
3817 which one to call based on the CODE and CLASS_TYPE arguments. The
3818 CODE argument should be one of the values returned by
3819 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3820 appertain to the pointer or reference. */
3822 static cp_declarator *
3823 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3824 cp_cv_quals cv_qualifiers,
3825 cp_declarator *target,
3826 tree attributes)
3828 if (code == ERROR_MARK || target == cp_error_declarator)
3829 return cp_error_declarator;
3831 if (code == INDIRECT_REF)
3832 if (class_type == NULL_TREE)
3833 return make_pointer_declarator (cv_qualifiers, target, attributes);
3834 else
3835 return make_ptrmem_declarator (cv_qualifiers, class_type,
3836 target, attributes);
3837 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3838 return make_reference_declarator (cv_qualifiers, target,
3839 false, attributes);
3840 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3841 return make_reference_declarator (cv_qualifiers, target,
3842 true, attributes);
3843 gcc_unreachable ();
3846 /* Create a new C++ parser. */
3848 static cp_parser *
3849 cp_parser_new (void)
3851 cp_parser *parser;
3852 cp_lexer *lexer;
3853 unsigned i;
3855 /* cp_lexer_new_main is called before doing GC allocation because
3856 cp_lexer_new_main might load a PCH file. */
3857 lexer = cp_lexer_new_main ();
3859 /* Initialize the binops_by_token so that we can get the tree
3860 directly from the token. */
3861 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3862 binops_by_token[binops[i].token_type] = binops[i];
3864 parser = ggc_cleared_alloc<cp_parser> ();
3865 parser->lexer = lexer;
3866 parser->context = cp_parser_context_new (NULL);
3868 /* For now, we always accept GNU extensions. */
3869 parser->allow_gnu_extensions_p = 1;
3871 /* The `>' token is a greater-than operator, not the end of a
3872 template-id. */
3873 parser->greater_than_is_operator_p = true;
3875 parser->default_arg_ok_p = true;
3877 /* We are not parsing a constant-expression. */
3878 parser->integral_constant_expression_p = false;
3879 parser->allow_non_integral_constant_expression_p = false;
3880 parser->non_integral_constant_expression_p = false;
3882 /* Local variable names are not forbidden. */
3883 parser->local_variables_forbidden_p = false;
3885 /* We are not processing an `extern "C"' declaration. */
3886 parser->in_unbraced_linkage_specification_p = false;
3888 /* We are not processing a declarator. */
3889 parser->in_declarator_p = false;
3891 /* We are not processing a template-argument-list. */
3892 parser->in_template_argument_list_p = false;
3894 /* We are not in an iteration statement. */
3895 parser->in_statement = 0;
3897 /* We are not in a switch statement. */
3898 parser->in_switch_statement_p = false;
3900 /* We are not parsing a type-id inside an expression. */
3901 parser->in_type_id_in_expr_p = false;
3903 /* Declarations aren't implicitly extern "C". */
3904 parser->implicit_extern_c = false;
3906 /* String literals should be translated to the execution character set. */
3907 parser->translate_strings_p = true;
3909 /* We are not parsing a function body. */
3910 parser->in_function_body = false;
3912 /* We can correct until told otherwise. */
3913 parser->colon_corrects_to_scope_p = true;
3915 /* The unparsed function queue is empty. */
3916 push_unparsed_function_queues (parser);
3918 /* There are no classes being defined. */
3919 parser->num_classes_being_defined = 0;
3921 /* No template parameters apply. */
3922 parser->num_template_parameter_lists = 0;
3924 /* Special parsing data structures. */
3925 parser->omp_declare_simd = NULL;
3926 parser->oacc_routine = NULL;
3928 /* Not declaring an implicit function template. */
3929 parser->auto_is_implicit_function_template_parm_p = false;
3930 parser->fully_implicit_function_template_p = false;
3931 parser->implicit_template_parms = 0;
3932 parser->implicit_template_scope = 0;
3934 /* Allow constrained-type-specifiers. */
3935 parser->prevent_constrained_type_specifiers = 0;
3937 /* We haven't yet seen an 'extern "C"'. */
3938 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3940 return parser;
3943 /* Create a cp_lexer structure which will emit the tokens in CACHE
3944 and push it onto the parser's lexer stack. This is used for delayed
3945 parsing of in-class method bodies and default arguments, and should
3946 not be confused with tentative parsing. */
3947 static void
3948 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3950 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3951 lexer->next = parser->lexer;
3952 parser->lexer = lexer;
3954 /* Move the current source position to that of the first token in the
3955 new lexer. */
3956 cp_lexer_set_source_position_from_token (lexer->next_token);
3959 /* Pop the top lexer off the parser stack. This is never used for the
3960 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3961 static void
3962 cp_parser_pop_lexer (cp_parser *parser)
3964 cp_lexer *lexer = parser->lexer;
3965 parser->lexer = lexer->next;
3966 cp_lexer_destroy (lexer);
3968 /* Put the current source position back where it was before this
3969 lexer was pushed. */
3970 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3973 /* Lexical conventions [gram.lex] */
3975 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3976 identifier. */
3978 static cp_expr
3979 cp_parser_identifier (cp_parser* parser)
3981 cp_token *token;
3983 /* Look for the identifier. */
3984 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3985 /* Return the value. */
3986 if (token)
3987 return cp_expr (token->u.value, token->location);
3988 else
3989 return error_mark_node;
3992 /* Parse a sequence of adjacent string constants. Returns a
3993 TREE_STRING representing the combined, nul-terminated string
3994 constant. If TRANSLATE is true, translate the string to the
3995 execution character set. If WIDE_OK is true, a wide string is
3996 invalid here.
3998 C++98 [lex.string] says that if a narrow string literal token is
3999 adjacent to a wide string literal token, the behavior is undefined.
4000 However, C99 6.4.5p4 says that this results in a wide string literal.
4001 We follow C99 here, for consistency with the C front end.
4003 This code is largely lifted from lex_string() in c-lex.c.
4005 FUTURE: ObjC++ will need to handle @-strings here. */
4006 static cp_expr
4007 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4008 bool lookup_udlit = true)
4010 tree value;
4011 size_t count;
4012 struct obstack str_ob;
4013 cpp_string str, istr, *strs;
4014 cp_token *tok;
4015 enum cpp_ttype type, curr_type;
4016 int have_suffix_p = 0;
4017 tree string_tree;
4018 tree suffix_id = NULL_TREE;
4019 bool curr_tok_is_userdef_p = false;
4021 tok = cp_lexer_peek_token (parser->lexer);
4022 if (!cp_parser_is_string_literal (tok))
4024 cp_parser_error (parser, "expected string-literal");
4025 return error_mark_node;
4028 location_t loc = tok->location;
4030 if (cpp_userdef_string_p (tok->type))
4032 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4033 curr_type = cpp_userdef_string_remove_type (tok->type);
4034 curr_tok_is_userdef_p = true;
4036 else
4038 string_tree = tok->u.value;
4039 curr_type = tok->type;
4041 type = curr_type;
4043 /* Try to avoid the overhead of creating and destroying an obstack
4044 for the common case of just one string. */
4045 if (!cp_parser_is_string_literal
4046 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4048 cp_lexer_consume_token (parser->lexer);
4050 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4051 str.len = TREE_STRING_LENGTH (string_tree);
4052 count = 1;
4054 if (curr_tok_is_userdef_p)
4056 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4057 have_suffix_p = 1;
4058 curr_type = cpp_userdef_string_remove_type (tok->type);
4060 else
4061 curr_type = tok->type;
4063 strs = &str;
4065 else
4067 location_t last_tok_loc = tok->location;
4068 gcc_obstack_init (&str_ob);
4069 count = 0;
4073 cp_lexer_consume_token (parser->lexer);
4074 count++;
4075 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4076 str.len = TREE_STRING_LENGTH (string_tree);
4078 if (curr_tok_is_userdef_p)
4080 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4081 if (have_suffix_p == 0)
4083 suffix_id = curr_suffix_id;
4084 have_suffix_p = 1;
4086 else if (have_suffix_p == 1
4087 && curr_suffix_id != suffix_id)
4089 error ("inconsistent user-defined literal suffixes"
4090 " %qD and %qD in string literal",
4091 suffix_id, curr_suffix_id);
4092 have_suffix_p = -1;
4094 curr_type = cpp_userdef_string_remove_type (tok->type);
4096 else
4097 curr_type = tok->type;
4099 if (type != curr_type)
4101 if (type == CPP_STRING)
4102 type = curr_type;
4103 else if (curr_type != CPP_STRING)
4105 rich_location rich_loc (line_table, tok->location);
4106 rich_loc.add_range (last_tok_loc, false);
4107 error_at (&rich_loc,
4108 "unsupported non-standard concatenation "
4109 "of string literals");
4113 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4115 last_tok_loc = tok->location;
4117 tok = cp_lexer_peek_token (parser->lexer);
4118 if (cpp_userdef_string_p (tok->type))
4120 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4121 curr_type = cpp_userdef_string_remove_type (tok->type);
4122 curr_tok_is_userdef_p = true;
4124 else
4126 string_tree = tok->u.value;
4127 curr_type = tok->type;
4128 curr_tok_is_userdef_p = false;
4131 while (cp_parser_is_string_literal (tok));
4133 /* A string literal built by concatenation has its caret=start at
4134 the start of the initial string, and its finish at the finish of
4135 the final string literal. */
4136 loc = make_location (loc, loc, get_finish (last_tok_loc));
4138 strs = (cpp_string *) obstack_finish (&str_ob);
4141 if (type != CPP_STRING && !wide_ok)
4143 cp_parser_error (parser, "a wide string is invalid in this context");
4144 type = CPP_STRING;
4147 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4148 (parse_in, strs, count, &istr, type))
4150 value = build_string (istr.len, (const char *)istr.text);
4151 free (CONST_CAST (unsigned char *, istr.text));
4153 switch (type)
4155 default:
4156 case CPP_STRING:
4157 case CPP_UTF8STRING:
4158 TREE_TYPE (value) = char_array_type_node;
4159 break;
4160 case CPP_STRING16:
4161 TREE_TYPE (value) = char16_array_type_node;
4162 break;
4163 case CPP_STRING32:
4164 TREE_TYPE (value) = char32_array_type_node;
4165 break;
4166 case CPP_WSTRING:
4167 TREE_TYPE (value) = wchar_array_type_node;
4168 break;
4171 value = fix_string_type (value);
4173 if (have_suffix_p)
4175 tree literal = build_userdef_literal (suffix_id, value,
4176 OT_NONE, NULL_TREE);
4177 if (lookup_udlit)
4178 value = cp_parser_userdef_string_literal (literal);
4179 else
4180 value = literal;
4183 else
4184 /* cpp_interpret_string has issued an error. */
4185 value = error_mark_node;
4187 if (count > 1)
4188 obstack_free (&str_ob, 0);
4190 return cp_expr (value, loc);
4193 /* Look up a literal operator with the name and the exact arguments. */
4195 static tree
4196 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4198 tree decl;
4199 decl = lookup_name (name);
4200 if (!decl || !is_overloaded_fn (decl))
4201 return error_mark_node;
4203 for (lkp_iterator iter (decl); iter; ++iter)
4205 unsigned int ix;
4206 bool found = true;
4207 tree fn = *iter;
4208 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4209 if (parmtypes != NULL_TREE)
4211 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4212 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4214 tree tparm = TREE_VALUE (parmtypes);
4215 tree targ = TREE_TYPE ((*args)[ix]);
4216 bool ptr = TYPE_PTR_P (tparm);
4217 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4218 if ((ptr || arr || !same_type_p (tparm, targ))
4219 && (!ptr || !arr
4220 || !same_type_p (TREE_TYPE (tparm),
4221 TREE_TYPE (targ))))
4222 found = false;
4224 if (found
4225 && ix == vec_safe_length (args)
4226 /* May be this should be sufficient_parms_p instead,
4227 depending on how exactly should user-defined literals
4228 work in presence of default arguments on the literal
4229 operator parameters. */
4230 && parmtypes == void_list_node)
4231 return decl;
4235 return error_mark_node;
4238 /* Parse a user-defined char constant. Returns a call to a user-defined
4239 literal operator taking the character as an argument. */
4241 static cp_expr
4242 cp_parser_userdef_char_literal (cp_parser *parser)
4244 cp_token *token = cp_lexer_consume_token (parser->lexer);
4245 tree literal = token->u.value;
4246 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4247 tree value = USERDEF_LITERAL_VALUE (literal);
4248 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4249 tree decl, result;
4251 /* Build up a call to the user-defined operator */
4252 /* Lookup the name we got back from the id-expression. */
4253 vec<tree, va_gc> *args = make_tree_vector ();
4254 vec_safe_push (args, value);
4255 decl = lookup_literal_operator (name, args);
4256 if (!decl || decl == error_mark_node)
4258 error ("unable to find character literal operator %qD with %qT argument",
4259 name, TREE_TYPE (value));
4260 release_tree_vector (args);
4261 return error_mark_node;
4263 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4264 release_tree_vector (args);
4265 return result;
4268 /* A subroutine of cp_parser_userdef_numeric_literal to
4269 create a char... template parameter pack from a string node. */
4271 static tree
4272 make_char_string_pack (tree value)
4274 tree charvec;
4275 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4276 const char *str = TREE_STRING_POINTER (value);
4277 int i, len = TREE_STRING_LENGTH (value) - 1;
4278 tree argvec = make_tree_vec (1);
4280 /* Fill in CHARVEC with all of the parameters. */
4281 charvec = make_tree_vec (len);
4282 for (i = 0; i < len; ++i)
4283 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4285 /* Build the argument packs. */
4286 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4288 TREE_VEC_ELT (argvec, 0) = argpack;
4290 return argvec;
4293 /* A subroutine of cp_parser_userdef_numeric_literal to
4294 create a char... template parameter pack from a string node. */
4296 static tree
4297 make_string_pack (tree value)
4299 tree charvec;
4300 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4301 const unsigned char *str
4302 = (const unsigned char *) TREE_STRING_POINTER (value);
4303 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4304 int len = TREE_STRING_LENGTH (value) / sz - 1;
4305 tree argvec = make_tree_vec (2);
4307 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4308 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4310 /* First template parm is character type. */
4311 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4313 /* Fill in CHARVEC with all of the parameters. */
4314 charvec = make_tree_vec (len);
4315 for (int i = 0; i < len; ++i)
4316 TREE_VEC_ELT (charvec, i)
4317 = double_int_to_tree (str_char_type_node,
4318 double_int::from_buffer (str + i * sz, sz));
4320 /* Build the argument packs. */
4321 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4323 TREE_VEC_ELT (argvec, 1) = argpack;
4325 return argvec;
4328 /* Parse a user-defined numeric constant. returns a call to a user-defined
4329 literal operator. */
4331 static cp_expr
4332 cp_parser_userdef_numeric_literal (cp_parser *parser)
4334 cp_token *token = cp_lexer_consume_token (parser->lexer);
4335 tree literal = token->u.value;
4336 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4337 tree value = USERDEF_LITERAL_VALUE (literal);
4338 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4339 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4340 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4341 tree decl, result;
4342 vec<tree, va_gc> *args;
4344 /* Look for a literal operator taking the exact type of numeric argument
4345 as the literal value. */
4346 args = make_tree_vector ();
4347 vec_safe_push (args, value);
4348 decl = lookup_literal_operator (name, args);
4349 if (decl && decl != error_mark_node)
4351 result = finish_call_expr (decl, &args, false, true,
4352 tf_warning_or_error);
4354 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4356 warning_at (token->location, OPT_Woverflow,
4357 "integer literal exceeds range of %qT type",
4358 long_long_unsigned_type_node);
4360 else
4362 if (overflow > 0)
4363 warning_at (token->location, OPT_Woverflow,
4364 "floating literal exceeds range of %qT type",
4365 long_double_type_node);
4366 else if (overflow < 0)
4367 warning_at (token->location, OPT_Woverflow,
4368 "floating literal truncated to zero");
4371 release_tree_vector (args);
4372 return result;
4374 release_tree_vector (args);
4376 /* If the numeric argument didn't work, look for a raw literal
4377 operator taking a const char* argument consisting of the number
4378 in string format. */
4379 args = make_tree_vector ();
4380 vec_safe_push (args, num_string);
4381 decl = lookup_literal_operator (name, args);
4382 if (decl && decl != error_mark_node)
4384 result = finish_call_expr (decl, &args, false, true,
4385 tf_warning_or_error);
4386 release_tree_vector (args);
4387 return result;
4389 release_tree_vector (args);
4391 /* If the raw literal didn't work, look for a non-type template
4392 function with parameter pack char.... Call the function with
4393 template parameter characters representing the number. */
4394 args = make_tree_vector ();
4395 decl = lookup_literal_operator (name, args);
4396 if (decl && decl != error_mark_node)
4398 tree tmpl_args = make_char_string_pack (num_string);
4399 decl = lookup_template_function (decl, tmpl_args);
4400 result = finish_call_expr (decl, &args, false, true,
4401 tf_warning_or_error);
4402 release_tree_vector (args);
4403 return result;
4406 release_tree_vector (args);
4408 /* In C++14 the standard library defines complex number suffixes that
4409 conflict with GNU extensions. Prefer them if <complex> is #included. */
4410 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4411 bool i14 = (cxx_dialect > cxx11
4412 && (id_equal (suffix_id, "i")
4413 || id_equal (suffix_id, "if")
4414 || id_equal (suffix_id, "il")));
4415 diagnostic_t kind = DK_ERROR;
4416 int opt = 0;
4418 if (i14 && ext)
4420 tree cxlit = lookup_qualified_name (std_node,
4421 get_identifier ("complex_literals"),
4422 0, false, false);
4423 if (cxlit == error_mark_node)
4425 /* No <complex>, so pedwarn and use GNU semantics. */
4426 kind = DK_PEDWARN;
4427 opt = OPT_Wpedantic;
4431 bool complained
4432 = emit_diagnostic (kind, input_location, opt,
4433 "unable to find numeric literal operator %qD", name);
4435 if (!complained)
4436 /* Don't inform either. */;
4437 else if (i14)
4439 inform (token->location, "add %<using namespace std::complex_literals%> "
4440 "(from <complex>) to enable the C++14 user-defined literal "
4441 "suffixes");
4442 if (ext)
4443 inform (token->location, "or use %<j%> instead of %<i%> for the "
4444 "GNU built-in suffix");
4446 else if (!ext)
4447 inform (token->location, "use -fext-numeric-literals "
4448 "to enable more built-in suffixes");
4450 if (kind == DK_ERROR)
4451 value = error_mark_node;
4452 else
4454 /* Use the built-in semantics. */
4455 tree type;
4456 if (id_equal (suffix_id, "i"))
4458 if (TREE_CODE (value) == INTEGER_CST)
4459 type = integer_type_node;
4460 else
4461 type = double_type_node;
4463 else if (id_equal (suffix_id, "if"))
4464 type = float_type_node;
4465 else /* if (id_equal (suffix_id, "il")) */
4466 type = long_double_type_node;
4468 value = build_complex (build_complex_type (type),
4469 fold_convert (type, integer_zero_node),
4470 fold_convert (type, value));
4473 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4474 /* Avoid repeated diagnostics. */
4475 token->u.value = value;
4476 return value;
4479 /* Parse a user-defined string constant. Returns a call to a user-defined
4480 literal operator taking a character pointer and the length of the string
4481 as arguments. */
4483 static tree
4484 cp_parser_userdef_string_literal (tree literal)
4486 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4487 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4488 tree value = USERDEF_LITERAL_VALUE (literal);
4489 int len = TREE_STRING_LENGTH (value)
4490 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4491 tree decl, result;
4492 vec<tree, va_gc> *args;
4494 /* Build up a call to the user-defined operator. */
4495 /* Lookup the name we got back from the id-expression. */
4496 args = make_tree_vector ();
4497 vec_safe_push (args, value);
4498 vec_safe_push (args, build_int_cst (size_type_node, len));
4499 decl = lookup_literal_operator (name, args);
4501 if (decl && decl != error_mark_node)
4503 result = finish_call_expr (decl, &args, false, true,
4504 tf_warning_or_error);
4505 release_tree_vector (args);
4506 return result;
4508 release_tree_vector (args);
4510 /* Look for a template function with typename parameter CharT
4511 and parameter pack CharT... Call the function with
4512 template parameter characters representing the string. */
4513 args = make_tree_vector ();
4514 decl = lookup_literal_operator (name, args);
4515 if (decl && decl != error_mark_node)
4517 tree tmpl_args = make_string_pack (value);
4518 decl = lookup_template_function (decl, tmpl_args);
4519 result = finish_call_expr (decl, &args, false, true,
4520 tf_warning_or_error);
4521 release_tree_vector (args);
4522 return result;
4524 release_tree_vector (args);
4526 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4527 name, TREE_TYPE (value), size_type_node);
4528 return error_mark_node;
4532 /* Basic concepts [gram.basic] */
4534 /* Parse a translation-unit.
4536 translation-unit:
4537 declaration-seq [opt]
4539 Returns TRUE if all went well. */
4541 static bool
4542 cp_parser_translation_unit (cp_parser* parser)
4544 /* The address of the first non-permanent object on the declarator
4545 obstack. */
4546 static void *declarator_obstack_base;
4548 bool success;
4550 /* Create the declarator obstack, if necessary. */
4551 if (!cp_error_declarator)
4553 gcc_obstack_init (&declarator_obstack);
4554 /* Create the error declarator. */
4555 cp_error_declarator = make_declarator (cdk_error);
4556 /* Create the empty parameter list. */
4557 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4558 UNKNOWN_LOCATION);
4559 /* Remember where the base of the declarator obstack lies. */
4560 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4563 cp_parser_declaration_seq_opt (parser);
4565 /* If there are no tokens left then all went well. */
4566 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4568 /* Get rid of the token array; we don't need it any more. */
4569 cp_lexer_destroy (parser->lexer);
4570 parser->lexer = NULL;
4572 /* This file might have been a context that's implicitly extern
4573 "C". If so, pop the lang context. (Only relevant for PCH.) */
4574 if (parser->implicit_extern_c)
4576 pop_lang_context ();
4577 parser->implicit_extern_c = false;
4580 /* Finish up. */
4581 finish_translation_unit ();
4583 success = true;
4585 else
4587 cp_parser_error (parser, "expected declaration");
4588 success = false;
4591 /* Make sure the declarator obstack was fully cleaned up. */
4592 gcc_assert (obstack_next_free (&declarator_obstack)
4593 == declarator_obstack_base);
4595 /* All went well. */
4596 return success;
4599 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4600 decltype context. */
4602 static inline tsubst_flags_t
4603 complain_flags (bool decltype_p)
4605 tsubst_flags_t complain = tf_warning_or_error;
4606 if (decltype_p)
4607 complain |= tf_decltype;
4608 return complain;
4611 /* We're about to parse a collection of statements. If we're currently
4612 parsing tentatively, set up a firewall so that any nested
4613 cp_parser_commit_to_tentative_parse won't affect the current context. */
4615 static cp_token_position
4616 cp_parser_start_tentative_firewall (cp_parser *parser)
4618 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4619 return 0;
4621 cp_parser_parse_tentatively (parser);
4622 cp_parser_commit_to_topmost_tentative_parse (parser);
4623 return cp_lexer_token_position (parser->lexer, false);
4626 /* We've finished parsing the collection of statements. Wrap up the
4627 firewall and replace the relevant tokens with the parsed form. */
4629 static void
4630 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4631 tree expr)
4633 if (!start)
4634 return;
4636 /* Finish the firewall level. */
4637 cp_parser_parse_definitely (parser);
4638 /* And remember the result of the parse for when we try again. */
4639 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4640 token->type = CPP_PREPARSED_EXPR;
4641 token->u.value = expr;
4642 token->keyword = RID_MAX;
4643 cp_lexer_purge_tokens_after (parser->lexer, start);
4646 /* Like the above functions, but let the user modify the tokens. Used by
4647 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4648 later parses, so it makes sense to localize the effects of
4649 cp_parser_commit_to_tentative_parse. */
4651 struct tentative_firewall
4653 cp_parser *parser;
4654 bool set;
4656 tentative_firewall (cp_parser *p): parser(p)
4658 /* If we're currently parsing tentatively, start a committed level as a
4659 firewall and then an inner tentative parse. */
4660 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4662 cp_parser_parse_tentatively (parser);
4663 cp_parser_commit_to_topmost_tentative_parse (parser);
4664 cp_parser_parse_tentatively (parser);
4668 ~tentative_firewall()
4670 if (set)
4672 /* Finish the inner tentative parse and the firewall, propagating any
4673 uncommitted error state to the outer tentative parse. */
4674 bool err = cp_parser_error_occurred (parser);
4675 cp_parser_parse_definitely (parser);
4676 cp_parser_parse_definitely (parser);
4677 if (err)
4678 cp_parser_simulate_error (parser);
4683 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4684 This class is for tracking such a matching pair of symbols.
4685 In particular, it tracks the location of the first token,
4686 so that if the second token is missing, we can highlight the
4687 location of the first token when notifying the user about the
4688 problem. */
4690 template <typename traits_t>
4691 class token_pair
4693 public:
4694 /* token_pair's ctor. */
4695 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4697 /* If the next token is the opening symbol for this pair, consume it and
4698 return true.
4699 Otherwise, issue an error and return false.
4700 In either case, record the location of the opening token. */
4702 bool require_open (cp_parser *parser)
4704 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4705 return cp_parser_require (parser, traits_t::open_token_type,
4706 traits_t::required_token_open);
4709 /* Consume the next token from PARSER, recording its location as
4710 that of the opening token within the pair. */
4712 cp_token * consume_open (cp_parser *parser)
4714 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4715 gcc_assert (tok->type == traits_t::open_token_type);
4716 m_open_loc = tok->location;
4717 return tok;
4720 /* If the next token is the closing symbol for this pair, consume it
4721 and return it.
4722 Otherwise, issue an error, highlighting the location of the
4723 corresponding opening token, and return NULL. */
4725 cp_token *require_close (cp_parser *parser) const
4727 return cp_parser_require (parser, traits_t::close_token_type,
4728 traits_t::required_token_close,
4729 m_open_loc);
4732 private:
4733 location_t m_open_loc;
4736 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4738 struct matching_paren_traits
4740 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4741 static const enum required_token required_token_open = RT_OPEN_PAREN;
4742 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4743 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4746 /* "matching_parens" is a token_pair<T> class for tracking matching
4747 pairs of parentheses. */
4749 typedef token_pair<matching_paren_traits> matching_parens;
4751 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4753 struct matching_brace_traits
4755 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4756 static const enum required_token required_token_open = RT_OPEN_BRACE;
4757 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4758 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4761 /* "matching_braces" is a token_pair<T> class for tracking matching
4762 pairs of braces. */
4764 typedef token_pair<matching_brace_traits> matching_braces;
4767 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4768 enclosing parentheses. */
4770 static cp_expr
4771 cp_parser_statement_expr (cp_parser *parser)
4773 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4775 /* Consume the '('. */
4776 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4777 matching_parens parens;
4778 parens.consume_open (parser);
4779 /* Start the statement-expression. */
4780 tree expr = begin_stmt_expr ();
4781 /* Parse the compound-statement. */
4782 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4783 /* Finish up. */
4784 expr = finish_stmt_expr (expr, false);
4785 /* Consume the ')'. */
4786 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4787 if (!parens.require_close (parser))
4788 cp_parser_skip_to_end_of_statement (parser);
4790 cp_parser_end_tentative_firewall (parser, start, expr);
4791 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4792 return cp_expr (expr, combined_loc);
4795 /* Expressions [gram.expr] */
4797 /* Parse a fold-operator.
4799 fold-operator:
4800 - * / % ^ & | = < > << >>
4801 = -= *= /= %= ^= &= |= <<= >>=
4802 == != <= >= && || , .* ->*
4804 This returns the tree code corresponding to the matched operator
4805 as an int. When the current token matches a compound assignment
4806 opertor, the resulting tree code is the negative value of the
4807 non-assignment operator. */
4809 static int
4810 cp_parser_fold_operator (cp_token *token)
4812 switch (token->type)
4814 case CPP_PLUS: return PLUS_EXPR;
4815 case CPP_MINUS: return MINUS_EXPR;
4816 case CPP_MULT: return MULT_EXPR;
4817 case CPP_DIV: return TRUNC_DIV_EXPR;
4818 case CPP_MOD: return TRUNC_MOD_EXPR;
4819 case CPP_XOR: return BIT_XOR_EXPR;
4820 case CPP_AND: return BIT_AND_EXPR;
4821 case CPP_OR: return BIT_IOR_EXPR;
4822 case CPP_LSHIFT: return LSHIFT_EXPR;
4823 case CPP_RSHIFT: return RSHIFT_EXPR;
4825 case CPP_EQ: return -NOP_EXPR;
4826 case CPP_PLUS_EQ: return -PLUS_EXPR;
4827 case CPP_MINUS_EQ: return -MINUS_EXPR;
4828 case CPP_MULT_EQ: return -MULT_EXPR;
4829 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4830 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4831 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4832 case CPP_AND_EQ: return -BIT_AND_EXPR;
4833 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4834 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4835 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4837 case CPP_EQ_EQ: return EQ_EXPR;
4838 case CPP_NOT_EQ: return NE_EXPR;
4839 case CPP_LESS: return LT_EXPR;
4840 case CPP_GREATER: return GT_EXPR;
4841 case CPP_LESS_EQ: return LE_EXPR;
4842 case CPP_GREATER_EQ: return GE_EXPR;
4844 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4845 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4847 case CPP_COMMA: return COMPOUND_EXPR;
4849 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4850 case CPP_DEREF_STAR: return MEMBER_REF;
4852 default: return ERROR_MARK;
4856 /* Returns true if CODE indicates a binary expression, which is not allowed in
4857 the LHS of a fold-expression. More codes will need to be added to use this
4858 function in other contexts. */
4860 static bool
4861 is_binary_op (tree_code code)
4863 switch (code)
4865 case PLUS_EXPR:
4866 case POINTER_PLUS_EXPR:
4867 case MINUS_EXPR:
4868 case MULT_EXPR:
4869 case TRUNC_DIV_EXPR:
4870 case TRUNC_MOD_EXPR:
4871 case BIT_XOR_EXPR:
4872 case BIT_AND_EXPR:
4873 case BIT_IOR_EXPR:
4874 case LSHIFT_EXPR:
4875 case RSHIFT_EXPR:
4877 case MODOP_EXPR:
4879 case EQ_EXPR:
4880 case NE_EXPR:
4881 case LE_EXPR:
4882 case GE_EXPR:
4883 case LT_EXPR:
4884 case GT_EXPR:
4886 case TRUTH_ANDIF_EXPR:
4887 case TRUTH_ORIF_EXPR:
4889 case COMPOUND_EXPR:
4891 case DOTSTAR_EXPR:
4892 case MEMBER_REF:
4893 return true;
4895 default:
4896 return false;
4900 /* If the next token is a suitable fold operator, consume it and return as
4901 the function above. */
4903 static int
4904 cp_parser_fold_operator (cp_parser *parser)
4906 cp_token* token = cp_lexer_peek_token (parser->lexer);
4907 int code = cp_parser_fold_operator (token);
4908 if (code != ERROR_MARK)
4909 cp_lexer_consume_token (parser->lexer);
4910 return code;
4913 /* Parse a fold-expression.
4915 fold-expression:
4916 ( ... folding-operator cast-expression)
4917 ( cast-expression folding-operator ... )
4918 ( cast-expression folding operator ... folding-operator cast-expression)
4920 Note that the '(' and ')' are matched in primary expression. */
4922 static cp_expr
4923 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4925 cp_id_kind pidk;
4927 // Left fold.
4928 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4930 cp_lexer_consume_token (parser->lexer);
4931 int op = cp_parser_fold_operator (parser);
4932 if (op == ERROR_MARK)
4934 cp_parser_error (parser, "expected binary operator");
4935 return error_mark_node;
4938 tree expr = cp_parser_cast_expression (parser, false, false,
4939 false, &pidk);
4940 if (expr == error_mark_node)
4941 return error_mark_node;
4942 return finish_left_unary_fold_expr (expr, op);
4945 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4946 int op = cp_parser_fold_operator (parser);
4947 if (op == ERROR_MARK)
4949 cp_parser_error (parser, "expected binary operator");
4950 return error_mark_node;
4953 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4955 cp_parser_error (parser, "expected ...");
4956 return error_mark_node;
4958 cp_lexer_consume_token (parser->lexer);
4960 /* The operands of a fold-expression are cast-expressions, so binary or
4961 conditional expressions are not allowed. We check this here to avoid
4962 tentative parsing. */
4963 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4964 /* OK, the expression was parenthesized. */;
4965 else if (is_binary_op (TREE_CODE (expr1)))
4966 error_at (location_of (expr1),
4967 "binary expression in operand of fold-expression");
4968 else if (TREE_CODE (expr1) == COND_EXPR
4969 || (REFERENCE_REF_P (expr1)
4970 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
4971 error_at (location_of (expr1),
4972 "conditional expression in operand of fold-expression");
4974 // Right fold.
4975 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4976 return finish_right_unary_fold_expr (expr1, op);
4978 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4980 cp_parser_error (parser, "mismatched operator in fold-expression");
4981 return error_mark_node;
4983 cp_lexer_consume_token (parser->lexer);
4985 // Binary left or right fold.
4986 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4987 if (expr2 == error_mark_node)
4988 return error_mark_node;
4989 return finish_binary_fold_expr (expr1, expr2, op);
4992 /* Parse a primary-expression.
4994 primary-expression:
4995 literal
4996 this
4997 ( expression )
4998 id-expression
4999 lambda-expression (C++11)
5001 GNU Extensions:
5003 primary-expression:
5004 ( compound-statement )
5005 __builtin_va_arg ( assignment-expression , type-id )
5006 __builtin_offsetof ( type-id , offsetof-expression )
5008 C++ Extensions:
5009 __has_nothrow_assign ( type-id )
5010 __has_nothrow_constructor ( type-id )
5011 __has_nothrow_copy ( type-id )
5012 __has_trivial_assign ( type-id )
5013 __has_trivial_constructor ( type-id )
5014 __has_trivial_copy ( type-id )
5015 __has_trivial_destructor ( type-id )
5016 __has_virtual_destructor ( type-id )
5017 __is_abstract ( type-id )
5018 __is_base_of ( type-id , type-id )
5019 __is_class ( type-id )
5020 __is_empty ( type-id )
5021 __is_enum ( type-id )
5022 __is_final ( type-id )
5023 __is_literal_type ( type-id )
5024 __is_pod ( type-id )
5025 __is_polymorphic ( type-id )
5026 __is_std_layout ( type-id )
5027 __is_trivial ( type-id )
5028 __is_union ( type-id )
5030 Objective-C++ Extension:
5032 primary-expression:
5033 objc-expression
5035 literal:
5036 __null
5038 ADDRESS_P is true iff this expression was immediately preceded by
5039 "&" and therefore might denote a pointer-to-member. CAST_P is true
5040 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5041 true iff this expression is a template argument.
5043 Returns a representation of the expression. Upon return, *IDK
5044 indicates what kind of id-expression (if any) was present. */
5046 static cp_expr
5047 cp_parser_primary_expression (cp_parser *parser,
5048 bool address_p,
5049 bool cast_p,
5050 bool template_arg_p,
5051 bool decltype_p,
5052 cp_id_kind *idk)
5054 cp_token *token = NULL;
5056 /* Assume the primary expression is not an id-expression. */
5057 *idk = CP_ID_KIND_NONE;
5059 /* Peek at the next token. */
5060 token = cp_lexer_peek_token (parser->lexer);
5061 switch ((int) token->type)
5063 /* literal:
5064 integer-literal
5065 character-literal
5066 floating-literal
5067 string-literal
5068 boolean-literal
5069 pointer-literal
5070 user-defined-literal */
5071 case CPP_CHAR:
5072 case CPP_CHAR16:
5073 case CPP_CHAR32:
5074 case CPP_WCHAR:
5075 case CPP_UTF8CHAR:
5076 case CPP_NUMBER:
5077 case CPP_PREPARSED_EXPR:
5078 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5079 return cp_parser_userdef_numeric_literal (parser);
5080 token = cp_lexer_consume_token (parser->lexer);
5081 if (TREE_CODE (token->u.value) == FIXED_CST)
5083 error_at (token->location,
5084 "fixed-point types not supported in C++");
5085 return error_mark_node;
5087 /* Floating-point literals are only allowed in an integral
5088 constant expression if they are cast to an integral or
5089 enumeration type. */
5090 if (TREE_CODE (token->u.value) == REAL_CST
5091 && parser->integral_constant_expression_p
5092 && pedantic)
5094 /* CAST_P will be set even in invalid code like "int(2.7 +
5095 ...)". Therefore, we have to check that the next token
5096 is sure to end the cast. */
5097 if (cast_p)
5099 cp_token *next_token;
5101 next_token = cp_lexer_peek_token (parser->lexer);
5102 if (/* The comma at the end of an
5103 enumerator-definition. */
5104 next_token->type != CPP_COMMA
5105 /* The curly brace at the end of an enum-specifier. */
5106 && next_token->type != CPP_CLOSE_BRACE
5107 /* The end of a statement. */
5108 && next_token->type != CPP_SEMICOLON
5109 /* The end of the cast-expression. */
5110 && next_token->type != CPP_CLOSE_PAREN
5111 /* The end of an array bound. */
5112 && next_token->type != CPP_CLOSE_SQUARE
5113 /* The closing ">" in a template-argument-list. */
5114 && (next_token->type != CPP_GREATER
5115 || parser->greater_than_is_operator_p)
5116 /* C++0x only: A ">>" treated like two ">" tokens,
5117 in a template-argument-list. */
5118 && (next_token->type != CPP_RSHIFT
5119 || (cxx_dialect == cxx98)
5120 || parser->greater_than_is_operator_p))
5121 cast_p = false;
5124 /* If we are within a cast, then the constraint that the
5125 cast is to an integral or enumeration type will be
5126 checked at that point. If we are not within a cast, then
5127 this code is invalid. */
5128 if (!cast_p)
5129 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5131 return cp_expr (token->u.value, token->location);
5133 case CPP_CHAR_USERDEF:
5134 case CPP_CHAR16_USERDEF:
5135 case CPP_CHAR32_USERDEF:
5136 case CPP_WCHAR_USERDEF:
5137 case CPP_UTF8CHAR_USERDEF:
5138 return cp_parser_userdef_char_literal (parser);
5140 case CPP_STRING:
5141 case CPP_STRING16:
5142 case CPP_STRING32:
5143 case CPP_WSTRING:
5144 case CPP_UTF8STRING:
5145 case CPP_STRING_USERDEF:
5146 case CPP_STRING16_USERDEF:
5147 case CPP_STRING32_USERDEF:
5148 case CPP_WSTRING_USERDEF:
5149 case CPP_UTF8STRING_USERDEF:
5150 /* ??? Should wide strings be allowed when parser->translate_strings_p
5151 is false (i.e. in attributes)? If not, we can kill the third
5152 argument to cp_parser_string_literal. */
5153 return cp_parser_string_literal (parser,
5154 parser->translate_strings_p,
5155 true);
5157 case CPP_OPEN_PAREN:
5158 /* If we see `( { ' then we are looking at the beginning of
5159 a GNU statement-expression. */
5160 if (cp_parser_allow_gnu_extensions_p (parser)
5161 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5163 /* Statement-expressions are not allowed by the standard. */
5164 pedwarn (token->location, OPT_Wpedantic,
5165 "ISO C++ forbids braced-groups within expressions");
5167 /* And they're not allowed outside of a function-body; you
5168 cannot, for example, write:
5170 int i = ({ int j = 3; j + 1; });
5172 at class or namespace scope. */
5173 if (!parser->in_function_body
5174 || parser->in_template_argument_list_p)
5176 error_at (token->location,
5177 "statement-expressions are not allowed outside "
5178 "functions nor in template-argument lists");
5179 cp_parser_skip_to_end_of_block_or_statement (parser);
5180 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5181 cp_lexer_consume_token (parser->lexer);
5182 return error_mark_node;
5184 else
5185 return cp_parser_statement_expr (parser);
5187 /* Otherwise it's a normal parenthesized expression. */
5189 cp_expr expr;
5190 bool saved_greater_than_is_operator_p;
5192 location_t open_paren_loc = token->location;
5194 /* Consume the `('. */
5195 matching_parens parens;
5196 parens.consume_open (parser);
5197 /* Within a parenthesized expression, a `>' token is always
5198 the greater-than operator. */
5199 saved_greater_than_is_operator_p
5200 = parser->greater_than_is_operator_p;
5201 parser->greater_than_is_operator_p = true;
5203 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5204 /* Left fold expression. */
5205 expr = NULL_TREE;
5206 else
5207 /* Parse the parenthesized expression. */
5208 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5210 token = cp_lexer_peek_token (parser->lexer);
5211 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5213 expr = cp_parser_fold_expression (parser, expr);
5214 if (expr != error_mark_node
5215 && cxx_dialect < cxx17
5216 && !in_system_header_at (input_location))
5217 pedwarn (input_location, 0, "fold-expressions only available "
5218 "with -std=c++17 or -std=gnu++17");
5220 else
5221 /* Let the front end know that this expression was
5222 enclosed in parentheses. This matters in case, for
5223 example, the expression is of the form `A::B', since
5224 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5225 not. */
5226 expr = finish_parenthesized_expr (expr);
5228 /* DR 705: Wrapping an unqualified name in parentheses
5229 suppresses arg-dependent lookup. We want to pass back
5230 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5231 (c++/37862), but none of the others. */
5232 if (*idk != CP_ID_KIND_QUALIFIED)
5233 *idk = CP_ID_KIND_NONE;
5235 /* The `>' token might be the end of a template-id or
5236 template-parameter-list now. */
5237 parser->greater_than_is_operator_p
5238 = saved_greater_than_is_operator_p;
5240 /* Consume the `)'. */
5241 token = cp_lexer_peek_token (parser->lexer);
5242 location_t close_paren_loc = token->location;
5243 expr.set_range (open_paren_loc, close_paren_loc);
5244 if (!parens.require_close (parser)
5245 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5246 cp_parser_skip_to_end_of_statement (parser);
5248 return expr;
5251 case CPP_OPEN_SQUARE:
5253 if (c_dialect_objc ())
5255 /* We might have an Objective-C++ message. */
5256 cp_parser_parse_tentatively (parser);
5257 tree msg = cp_parser_objc_message_expression (parser);
5258 /* If that works out, we're done ... */
5259 if (cp_parser_parse_definitely (parser))
5260 return msg;
5261 /* ... else, fall though to see if it's a lambda. */
5263 cp_expr lam = cp_parser_lambda_expression (parser);
5264 /* Don't warn about a failed tentative parse. */
5265 if (cp_parser_error_occurred (parser))
5266 return error_mark_node;
5267 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5268 return lam;
5271 case CPP_OBJC_STRING:
5272 if (c_dialect_objc ())
5273 /* We have an Objective-C++ string literal. */
5274 return cp_parser_objc_expression (parser);
5275 cp_parser_error (parser, "expected primary-expression");
5276 return error_mark_node;
5278 case CPP_KEYWORD:
5279 switch (token->keyword)
5281 /* These two are the boolean literals. */
5282 case RID_TRUE:
5283 cp_lexer_consume_token (parser->lexer);
5284 return cp_expr (boolean_true_node, token->location);
5285 case RID_FALSE:
5286 cp_lexer_consume_token (parser->lexer);
5287 return cp_expr (boolean_false_node, token->location);
5289 /* The `__null' literal. */
5290 case RID_NULL:
5291 cp_lexer_consume_token (parser->lexer);
5292 return cp_expr (null_node, token->location);
5294 /* The `nullptr' literal. */
5295 case RID_NULLPTR:
5296 cp_lexer_consume_token (parser->lexer);
5297 return cp_expr (nullptr_node, token->location);
5299 /* Recognize the `this' keyword. */
5300 case RID_THIS:
5301 cp_lexer_consume_token (parser->lexer);
5302 if (parser->local_variables_forbidden_p)
5304 error_at (token->location,
5305 "%<this%> may not be used in this context");
5306 return error_mark_node;
5308 /* Pointers cannot appear in constant-expressions. */
5309 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5310 return error_mark_node;
5311 return cp_expr (finish_this_expr (), token->location);
5313 /* The `operator' keyword can be the beginning of an
5314 id-expression. */
5315 case RID_OPERATOR:
5316 goto id_expression;
5318 case RID_FUNCTION_NAME:
5319 case RID_PRETTY_FUNCTION_NAME:
5320 case RID_C99_FUNCTION_NAME:
5322 non_integral_constant name;
5324 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5325 __func__ are the names of variables -- but they are
5326 treated specially. Therefore, they are handled here,
5327 rather than relying on the generic id-expression logic
5328 below. Grammatically, these names are id-expressions.
5330 Consume the token. */
5331 token = cp_lexer_consume_token (parser->lexer);
5333 switch (token->keyword)
5335 case RID_FUNCTION_NAME:
5336 name = NIC_FUNC_NAME;
5337 break;
5338 case RID_PRETTY_FUNCTION_NAME:
5339 name = NIC_PRETTY_FUNC;
5340 break;
5341 case RID_C99_FUNCTION_NAME:
5342 name = NIC_C99_FUNC;
5343 break;
5344 default:
5345 gcc_unreachable ();
5348 if (cp_parser_non_integral_constant_expression (parser, name))
5349 return error_mark_node;
5351 /* Look up the name. */
5352 return finish_fname (token->u.value);
5355 case RID_VA_ARG:
5357 tree expression;
5358 tree type;
5359 source_location type_location;
5360 location_t start_loc
5361 = cp_lexer_peek_token (parser->lexer)->location;
5362 /* The `__builtin_va_arg' construct is used to handle
5363 `va_arg'. Consume the `__builtin_va_arg' token. */
5364 cp_lexer_consume_token (parser->lexer);
5365 /* Look for the opening `('. */
5366 matching_parens parens;
5367 parens.require_open (parser);
5368 /* Now, parse the assignment-expression. */
5369 expression = cp_parser_assignment_expression (parser);
5370 /* Look for the `,'. */
5371 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5372 type_location = cp_lexer_peek_token (parser->lexer)->location;
5373 /* Parse the type-id. */
5375 type_id_in_expr_sentinel s (parser);
5376 type = cp_parser_type_id (parser);
5378 /* Look for the closing `)'. */
5379 location_t finish_loc
5380 = cp_lexer_peek_token (parser->lexer)->location;
5381 parens.require_close (parser);
5382 /* Using `va_arg' in a constant-expression is not
5383 allowed. */
5384 if (cp_parser_non_integral_constant_expression (parser,
5385 NIC_VA_ARG))
5386 return error_mark_node;
5387 /* Construct a location of the form:
5388 __builtin_va_arg (v, int)
5389 ~~~~~~~~~~~~~~~~~~~~~^~~~
5390 with the caret at the type, ranging from the start of the
5391 "__builtin_va_arg" token to the close paren. */
5392 location_t combined_loc
5393 = make_location (type_location, start_loc, finish_loc);
5394 return build_x_va_arg (combined_loc, expression, type);
5397 case RID_OFFSETOF:
5398 return cp_parser_builtin_offsetof (parser);
5400 case RID_HAS_NOTHROW_ASSIGN:
5401 case RID_HAS_NOTHROW_CONSTRUCTOR:
5402 case RID_HAS_NOTHROW_COPY:
5403 case RID_HAS_TRIVIAL_ASSIGN:
5404 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5405 case RID_HAS_TRIVIAL_COPY:
5406 case RID_HAS_TRIVIAL_DESTRUCTOR:
5407 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5408 case RID_HAS_VIRTUAL_DESTRUCTOR:
5409 case RID_IS_ABSTRACT:
5410 case RID_IS_AGGREGATE:
5411 case RID_IS_BASE_OF:
5412 case RID_IS_CLASS:
5413 case RID_IS_EMPTY:
5414 case RID_IS_ENUM:
5415 case RID_IS_FINAL:
5416 case RID_IS_LITERAL_TYPE:
5417 case RID_IS_POD:
5418 case RID_IS_POLYMORPHIC:
5419 case RID_IS_SAME_AS:
5420 case RID_IS_STD_LAYOUT:
5421 case RID_IS_TRIVIAL:
5422 case RID_IS_TRIVIALLY_ASSIGNABLE:
5423 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5424 case RID_IS_TRIVIALLY_COPYABLE:
5425 case RID_IS_UNION:
5426 case RID_IS_ASSIGNABLE:
5427 case RID_IS_CONSTRUCTIBLE:
5428 return cp_parser_trait_expr (parser, token->keyword);
5430 // C++ concepts
5431 case RID_REQUIRES:
5432 return cp_parser_requires_expression (parser);
5434 /* Objective-C++ expressions. */
5435 case RID_AT_ENCODE:
5436 case RID_AT_PROTOCOL:
5437 case RID_AT_SELECTOR:
5438 return cp_parser_objc_expression (parser);
5440 case RID_TEMPLATE:
5441 if (parser->in_function_body
5442 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5443 == CPP_LESS))
5445 error_at (token->location,
5446 "a template declaration cannot appear at block scope");
5447 cp_parser_skip_to_end_of_block_or_statement (parser);
5448 return error_mark_node;
5450 /* FALLTHRU */
5451 default:
5452 cp_parser_error (parser, "expected primary-expression");
5453 return error_mark_node;
5456 /* An id-expression can start with either an identifier, a
5457 `::' as the beginning of a qualified-id, or the "operator"
5458 keyword. */
5459 case CPP_NAME:
5460 case CPP_SCOPE:
5461 case CPP_TEMPLATE_ID:
5462 case CPP_NESTED_NAME_SPECIFIER:
5464 id_expression:
5465 cp_expr id_expression;
5466 cp_expr decl;
5467 const char *error_msg;
5468 bool template_p;
5469 bool done;
5470 cp_token *id_expr_token;
5472 /* Parse the id-expression. */
5473 id_expression
5474 = cp_parser_id_expression (parser,
5475 /*template_keyword_p=*/false,
5476 /*check_dependency_p=*/true,
5477 &template_p,
5478 /*declarator_p=*/false,
5479 /*optional_p=*/false);
5480 if (id_expression == error_mark_node)
5481 return error_mark_node;
5482 id_expr_token = token;
5483 token = cp_lexer_peek_token (parser->lexer);
5484 done = (token->type != CPP_OPEN_SQUARE
5485 && token->type != CPP_OPEN_PAREN
5486 && token->type != CPP_DOT
5487 && token->type != CPP_DEREF
5488 && token->type != CPP_PLUS_PLUS
5489 && token->type != CPP_MINUS_MINUS);
5490 /* If we have a template-id, then no further lookup is
5491 required. If the template-id was for a template-class, we
5492 will sometimes have a TYPE_DECL at this point. */
5493 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5494 || TREE_CODE (id_expression) == TYPE_DECL)
5495 decl = id_expression;
5496 /* Look up the name. */
5497 else
5499 tree ambiguous_decls;
5501 /* If we already know that this lookup is ambiguous, then
5502 we've already issued an error message; there's no reason
5503 to check again. */
5504 if (id_expr_token->type == CPP_NAME
5505 && id_expr_token->error_reported)
5507 cp_parser_simulate_error (parser);
5508 return error_mark_node;
5511 decl = cp_parser_lookup_name (parser, id_expression,
5512 none_type,
5513 template_p,
5514 /*is_namespace=*/false,
5515 /*check_dependency=*/true,
5516 &ambiguous_decls,
5517 id_expr_token->location);
5518 /* If the lookup was ambiguous, an error will already have
5519 been issued. */
5520 if (ambiguous_decls)
5521 return error_mark_node;
5523 /* In Objective-C++, we may have an Objective-C 2.0
5524 dot-syntax for classes here. */
5525 if (c_dialect_objc ()
5526 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5527 && TREE_CODE (decl) == TYPE_DECL
5528 && objc_is_class_name (decl))
5530 tree component;
5531 cp_lexer_consume_token (parser->lexer);
5532 component = cp_parser_identifier (parser);
5533 if (component == error_mark_node)
5534 return error_mark_node;
5536 tree result = objc_build_class_component_ref (id_expression,
5537 component);
5538 /* Build a location of the form:
5539 expr.component
5540 ~~~~~^~~~~~~~~
5541 with caret at the start of the component name (at
5542 input_location), ranging from the start of the id_expression
5543 to the end of the component name. */
5544 location_t combined_loc
5545 = make_location (input_location, id_expression.get_start (),
5546 get_finish (input_location));
5547 protected_set_expr_location (result, combined_loc);
5548 return result;
5551 /* In Objective-C++, an instance variable (ivar) may be preferred
5552 to whatever cp_parser_lookup_name() found.
5553 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5554 rest of c-family, we have to do a little extra work to preserve
5555 any location information in cp_expr "decl". Given that
5556 objc_lookup_ivar is implemented in "c-family" and "objc", we
5557 have a trip through the pure "tree" type, rather than cp_expr.
5558 Naively copying it back to "decl" would implicitly give the
5559 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5560 store an EXPR_LOCATION. Hence we only update "decl" (and
5561 hence its location_t) if we get back a different tree node. */
5562 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5563 id_expression);
5564 if (decl_tree != decl.get_value ())
5565 decl = cp_expr (decl_tree);
5567 /* If name lookup gives us a SCOPE_REF, then the
5568 qualifying scope was dependent. */
5569 if (TREE_CODE (decl) == SCOPE_REF)
5571 /* At this point, we do not know if DECL is a valid
5572 integral constant expression. We assume that it is
5573 in fact such an expression, so that code like:
5575 template <int N> struct A {
5576 int a[B<N>::i];
5579 is accepted. At template-instantiation time, we
5580 will check that B<N>::i is actually a constant. */
5581 return decl;
5583 /* Check to see if DECL is a local variable in a context
5584 where that is forbidden. */
5585 if (parser->local_variables_forbidden_p
5586 && local_variable_p (decl))
5588 error_at (id_expr_token->location,
5589 "local variable %qD may not appear in this context",
5590 decl.get_value ());
5591 return error_mark_node;
5595 decl = (finish_id_expression
5596 (id_expression, decl, parser->scope,
5597 idk,
5598 parser->integral_constant_expression_p,
5599 parser->allow_non_integral_constant_expression_p,
5600 &parser->non_integral_constant_expression_p,
5601 template_p, done, address_p,
5602 template_arg_p,
5603 &error_msg,
5604 id_expression.get_location ()));
5605 if (error_msg)
5606 cp_parser_error (parser, error_msg);
5607 decl.set_location (id_expr_token->location);
5608 return decl;
5611 /* Anything else is an error. */
5612 default:
5613 cp_parser_error (parser, "expected primary-expression");
5614 return error_mark_node;
5618 static inline cp_expr
5619 cp_parser_primary_expression (cp_parser *parser,
5620 bool address_p,
5621 bool cast_p,
5622 bool template_arg_p,
5623 cp_id_kind *idk)
5625 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5626 /*decltype*/false, idk);
5629 /* Parse an id-expression.
5631 id-expression:
5632 unqualified-id
5633 qualified-id
5635 qualified-id:
5636 :: [opt] nested-name-specifier template [opt] unqualified-id
5637 :: identifier
5638 :: operator-function-id
5639 :: template-id
5641 Return a representation of the unqualified portion of the
5642 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5643 a `::' or nested-name-specifier.
5645 Often, if the id-expression was a qualified-id, the caller will
5646 want to make a SCOPE_REF to represent the qualified-id. This
5647 function does not do this in order to avoid wastefully creating
5648 SCOPE_REFs when they are not required.
5650 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5651 `template' keyword.
5653 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5654 uninstantiated templates.
5656 If *TEMPLATE_P is non-NULL, it is set to true iff the
5657 `template' keyword is used to explicitly indicate that the entity
5658 named is a template.
5660 If DECLARATOR_P is true, the id-expression is appearing as part of
5661 a declarator, rather than as part of an expression. */
5663 static cp_expr
5664 cp_parser_id_expression (cp_parser *parser,
5665 bool template_keyword_p,
5666 bool check_dependency_p,
5667 bool *template_p,
5668 bool declarator_p,
5669 bool optional_p)
5671 bool global_scope_p;
5672 bool nested_name_specifier_p;
5674 /* Assume the `template' keyword was not used. */
5675 if (template_p)
5676 *template_p = template_keyword_p;
5678 /* Look for the optional `::' operator. */
5679 global_scope_p
5680 = (!template_keyword_p
5681 && (cp_parser_global_scope_opt (parser,
5682 /*current_scope_valid_p=*/false)
5683 != NULL_TREE));
5685 /* Look for the optional nested-name-specifier. */
5686 nested_name_specifier_p
5687 = (cp_parser_nested_name_specifier_opt (parser,
5688 /*typename_keyword_p=*/false,
5689 check_dependency_p,
5690 /*type_p=*/false,
5691 declarator_p,
5692 template_keyword_p)
5693 != NULL_TREE);
5695 /* If there is a nested-name-specifier, then we are looking at
5696 the first qualified-id production. */
5697 if (nested_name_specifier_p)
5699 tree saved_scope;
5700 tree saved_object_scope;
5701 tree saved_qualifying_scope;
5702 cp_expr unqualified_id;
5703 bool is_template;
5705 /* See if the next token is the `template' keyword. */
5706 if (!template_p)
5707 template_p = &is_template;
5708 *template_p = cp_parser_optional_template_keyword (parser);
5709 /* Name lookup we do during the processing of the
5710 unqualified-id might obliterate SCOPE. */
5711 saved_scope = parser->scope;
5712 saved_object_scope = parser->object_scope;
5713 saved_qualifying_scope = parser->qualifying_scope;
5714 /* Process the final unqualified-id. */
5715 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5716 check_dependency_p,
5717 declarator_p,
5718 /*optional_p=*/false);
5719 /* Restore the SAVED_SCOPE for our caller. */
5720 parser->scope = saved_scope;
5721 parser->object_scope = saved_object_scope;
5722 parser->qualifying_scope = saved_qualifying_scope;
5724 return unqualified_id;
5726 /* Otherwise, if we are in global scope, then we are looking at one
5727 of the other qualified-id productions. */
5728 else if (global_scope_p)
5730 cp_token *token;
5731 tree id;
5733 /* Peek at the next token. */
5734 token = cp_lexer_peek_token (parser->lexer);
5736 /* If it's an identifier, and the next token is not a "<", then
5737 we can avoid the template-id case. This is an optimization
5738 for this common case. */
5739 if (token->type == CPP_NAME
5740 && !cp_parser_nth_token_starts_template_argument_list_p
5741 (parser, 2))
5742 return cp_parser_identifier (parser);
5744 cp_parser_parse_tentatively (parser);
5745 /* Try a template-id. */
5746 id = cp_parser_template_id (parser,
5747 /*template_keyword_p=*/false,
5748 /*check_dependency_p=*/true,
5749 none_type,
5750 declarator_p);
5751 /* If that worked, we're done. */
5752 if (cp_parser_parse_definitely (parser))
5753 return id;
5755 /* Peek at the next token. (Changes in the token buffer may
5756 have invalidated the pointer obtained above.) */
5757 token = cp_lexer_peek_token (parser->lexer);
5759 switch (token->type)
5761 case CPP_NAME:
5762 return cp_parser_identifier (parser);
5764 case CPP_KEYWORD:
5765 if (token->keyword == RID_OPERATOR)
5766 return cp_parser_operator_function_id (parser);
5767 /* Fall through. */
5769 default:
5770 cp_parser_error (parser, "expected id-expression");
5771 return error_mark_node;
5774 else
5775 return cp_parser_unqualified_id (parser, template_keyword_p,
5776 /*check_dependency_p=*/true,
5777 declarator_p,
5778 optional_p);
5781 /* Parse an unqualified-id.
5783 unqualified-id:
5784 identifier
5785 operator-function-id
5786 conversion-function-id
5787 ~ class-name
5788 template-id
5790 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5791 keyword, in a construct like `A::template ...'.
5793 Returns a representation of unqualified-id. For the `identifier'
5794 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5795 production a BIT_NOT_EXPR is returned; the operand of the
5796 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5797 other productions, see the documentation accompanying the
5798 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5799 names are looked up in uninstantiated templates. If DECLARATOR_P
5800 is true, the unqualified-id is appearing as part of a declarator,
5801 rather than as part of an expression. */
5803 static cp_expr
5804 cp_parser_unqualified_id (cp_parser* parser,
5805 bool template_keyword_p,
5806 bool check_dependency_p,
5807 bool declarator_p,
5808 bool optional_p)
5810 cp_token *token;
5812 /* Peek at the next token. */
5813 token = cp_lexer_peek_token (parser->lexer);
5815 switch ((int) token->type)
5817 case CPP_NAME:
5819 tree id;
5821 /* We don't know yet whether or not this will be a
5822 template-id. */
5823 cp_parser_parse_tentatively (parser);
5824 /* Try a template-id. */
5825 id = cp_parser_template_id (parser, template_keyword_p,
5826 check_dependency_p,
5827 none_type,
5828 declarator_p);
5829 /* If it worked, we're done. */
5830 if (cp_parser_parse_definitely (parser))
5831 return id;
5832 /* Otherwise, it's an ordinary identifier. */
5833 return cp_parser_identifier (parser);
5836 case CPP_TEMPLATE_ID:
5837 return cp_parser_template_id (parser, template_keyword_p,
5838 check_dependency_p,
5839 none_type,
5840 declarator_p);
5842 case CPP_COMPL:
5844 tree type_decl;
5845 tree qualifying_scope;
5846 tree object_scope;
5847 tree scope;
5848 bool done;
5850 /* Consume the `~' token. */
5851 cp_lexer_consume_token (parser->lexer);
5852 /* Parse the class-name. The standard, as written, seems to
5853 say that:
5855 template <typename T> struct S { ~S (); };
5856 template <typename T> S<T>::~S() {}
5858 is invalid, since `~' must be followed by a class-name, but
5859 `S<T>' is dependent, and so not known to be a class.
5860 That's not right; we need to look in uninstantiated
5861 templates. A further complication arises from:
5863 template <typename T> void f(T t) {
5864 t.T::~T();
5867 Here, it is not possible to look up `T' in the scope of `T'
5868 itself. We must look in both the current scope, and the
5869 scope of the containing complete expression.
5871 Yet another issue is:
5873 struct S {
5874 int S;
5875 ~S();
5878 S::~S() {}
5880 The standard does not seem to say that the `S' in `~S'
5881 should refer to the type `S' and not the data member
5882 `S::S'. */
5884 /* DR 244 says that we look up the name after the "~" in the
5885 same scope as we looked up the qualifying name. That idea
5886 isn't fully worked out; it's more complicated than that. */
5887 scope = parser->scope;
5888 object_scope = parser->object_scope;
5889 qualifying_scope = parser->qualifying_scope;
5891 /* Check for invalid scopes. */
5892 if (scope == error_mark_node)
5894 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5895 cp_lexer_consume_token (parser->lexer);
5896 return error_mark_node;
5898 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5900 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5901 error_at (token->location,
5902 "scope %qT before %<~%> is not a class-name",
5903 scope);
5904 cp_parser_simulate_error (parser);
5905 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5906 cp_lexer_consume_token (parser->lexer);
5907 return error_mark_node;
5909 gcc_assert (!scope || TYPE_P (scope));
5911 /* If the name is of the form "X::~X" it's OK even if X is a
5912 typedef. */
5913 token = cp_lexer_peek_token (parser->lexer);
5914 if (scope
5915 && token->type == CPP_NAME
5916 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5917 != CPP_LESS)
5918 && (token->u.value == TYPE_IDENTIFIER (scope)
5919 || (CLASS_TYPE_P (scope)
5920 && constructor_name_p (token->u.value, scope))))
5922 cp_lexer_consume_token (parser->lexer);
5923 return build_nt (BIT_NOT_EXPR, scope);
5926 /* ~auto means the destructor of whatever the object is. */
5927 if (cp_parser_is_keyword (token, RID_AUTO))
5929 if (cxx_dialect < cxx14)
5930 pedwarn (input_location, 0,
5931 "%<~auto%> only available with "
5932 "-std=c++14 or -std=gnu++14");
5933 cp_lexer_consume_token (parser->lexer);
5934 return build_nt (BIT_NOT_EXPR, make_auto ());
5937 /* If there was an explicit qualification (S::~T), first look
5938 in the scope given by the qualification (i.e., S).
5940 Note: in the calls to cp_parser_class_name below we pass
5941 typename_type so that lookup finds the injected-class-name
5942 rather than the constructor. */
5943 done = false;
5944 type_decl = NULL_TREE;
5945 if (scope)
5947 cp_parser_parse_tentatively (parser);
5948 type_decl = cp_parser_class_name (parser,
5949 /*typename_keyword_p=*/false,
5950 /*template_keyword_p=*/false,
5951 typename_type,
5952 /*check_dependency=*/false,
5953 /*class_head_p=*/false,
5954 declarator_p);
5955 if (cp_parser_parse_definitely (parser))
5956 done = true;
5958 /* In "N::S::~S", look in "N" as well. */
5959 if (!done && scope && qualifying_scope)
5961 cp_parser_parse_tentatively (parser);
5962 parser->scope = qualifying_scope;
5963 parser->object_scope = NULL_TREE;
5964 parser->qualifying_scope = NULL_TREE;
5965 type_decl
5966 = cp_parser_class_name (parser,
5967 /*typename_keyword_p=*/false,
5968 /*template_keyword_p=*/false,
5969 typename_type,
5970 /*check_dependency=*/false,
5971 /*class_head_p=*/false,
5972 declarator_p);
5973 if (cp_parser_parse_definitely (parser))
5974 done = true;
5976 /* In "p->S::~T", look in the scope given by "*p" as well. */
5977 else if (!done && object_scope)
5979 cp_parser_parse_tentatively (parser);
5980 parser->scope = object_scope;
5981 parser->object_scope = NULL_TREE;
5982 parser->qualifying_scope = NULL_TREE;
5983 type_decl
5984 = cp_parser_class_name (parser,
5985 /*typename_keyword_p=*/false,
5986 /*template_keyword_p=*/false,
5987 typename_type,
5988 /*check_dependency=*/false,
5989 /*class_head_p=*/false,
5990 declarator_p);
5991 if (cp_parser_parse_definitely (parser))
5992 done = true;
5994 /* Look in the surrounding context. */
5995 if (!done)
5997 parser->scope = NULL_TREE;
5998 parser->object_scope = NULL_TREE;
5999 parser->qualifying_scope = NULL_TREE;
6000 if (processing_template_decl)
6001 cp_parser_parse_tentatively (parser);
6002 type_decl
6003 = cp_parser_class_name (parser,
6004 /*typename_keyword_p=*/false,
6005 /*template_keyword_p=*/false,
6006 typename_type,
6007 /*check_dependency=*/false,
6008 /*class_head_p=*/false,
6009 declarator_p);
6010 if (processing_template_decl
6011 && ! cp_parser_parse_definitely (parser))
6013 /* We couldn't find a type with this name. If we're parsing
6014 tentatively, fail and try something else. */
6015 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6017 cp_parser_simulate_error (parser);
6018 return error_mark_node;
6020 /* Otherwise, accept it and check for a match at instantiation
6021 time. */
6022 type_decl = cp_parser_identifier (parser);
6023 if (type_decl != error_mark_node)
6024 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6025 return type_decl;
6028 /* If an error occurred, assume that the name of the
6029 destructor is the same as the name of the qualifying
6030 class. That allows us to keep parsing after running
6031 into ill-formed destructor names. */
6032 if (type_decl == error_mark_node && scope)
6033 return build_nt (BIT_NOT_EXPR, scope);
6034 else if (type_decl == error_mark_node)
6035 return error_mark_node;
6037 /* Check that destructor name and scope match. */
6038 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6040 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6041 error_at (token->location,
6042 "declaration of %<~%T%> as member of %qT",
6043 type_decl, scope);
6044 cp_parser_simulate_error (parser);
6045 return error_mark_node;
6048 /* [class.dtor]
6050 A typedef-name that names a class shall not be used as the
6051 identifier in the declarator for a destructor declaration. */
6052 if (declarator_p
6053 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6054 && !DECL_SELF_REFERENCE_P (type_decl)
6055 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6056 error_at (token->location,
6057 "typedef-name %qD used as destructor declarator",
6058 type_decl);
6060 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6063 case CPP_KEYWORD:
6064 if (token->keyword == RID_OPERATOR)
6066 cp_expr id;
6068 /* This could be a template-id, so we try that first. */
6069 cp_parser_parse_tentatively (parser);
6070 /* Try a template-id. */
6071 id = cp_parser_template_id (parser, template_keyword_p,
6072 /*check_dependency_p=*/true,
6073 none_type,
6074 declarator_p);
6075 /* If that worked, we're done. */
6076 if (cp_parser_parse_definitely (parser))
6077 return id;
6078 /* We still don't know whether we're looking at an
6079 operator-function-id or a conversion-function-id. */
6080 cp_parser_parse_tentatively (parser);
6081 /* Try an operator-function-id. */
6082 id = cp_parser_operator_function_id (parser);
6083 /* If that didn't work, try a conversion-function-id. */
6084 if (!cp_parser_parse_definitely (parser))
6085 id = cp_parser_conversion_function_id (parser);
6087 return id;
6089 /* Fall through. */
6091 default:
6092 if (optional_p)
6093 return NULL_TREE;
6094 cp_parser_error (parser, "expected unqualified-id");
6095 return error_mark_node;
6099 /* Parse an (optional) nested-name-specifier.
6101 nested-name-specifier: [C++98]
6102 class-or-namespace-name :: nested-name-specifier [opt]
6103 class-or-namespace-name :: template nested-name-specifier [opt]
6105 nested-name-specifier: [C++0x]
6106 type-name ::
6107 namespace-name ::
6108 nested-name-specifier identifier ::
6109 nested-name-specifier template [opt] simple-template-id ::
6111 PARSER->SCOPE should be set appropriately before this function is
6112 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6113 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6114 in name lookups.
6116 Sets PARSER->SCOPE to the class (TYPE) or namespace
6117 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6118 it unchanged if there is no nested-name-specifier. Returns the new
6119 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6121 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6122 part of a declaration and/or decl-specifier. */
6124 static tree
6125 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6126 bool typename_keyword_p,
6127 bool check_dependency_p,
6128 bool type_p,
6129 bool is_declaration,
6130 bool template_keyword_p /* = false */)
6132 bool success = false;
6133 cp_token_position start = 0;
6134 cp_token *token;
6136 /* Remember where the nested-name-specifier starts. */
6137 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6139 start = cp_lexer_token_position (parser->lexer, false);
6140 push_deferring_access_checks (dk_deferred);
6143 while (true)
6145 tree new_scope;
6146 tree old_scope;
6147 tree saved_qualifying_scope;
6149 /* Spot cases that cannot be the beginning of a
6150 nested-name-specifier. */
6151 token = cp_lexer_peek_token (parser->lexer);
6153 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6154 the already parsed nested-name-specifier. */
6155 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6157 /* Grab the nested-name-specifier and continue the loop. */
6158 cp_parser_pre_parsed_nested_name_specifier (parser);
6159 /* If we originally encountered this nested-name-specifier
6160 with IS_DECLARATION set to false, we will not have
6161 resolved TYPENAME_TYPEs, so we must do so here. */
6162 if (is_declaration
6163 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6165 new_scope = resolve_typename_type (parser->scope,
6166 /*only_current_p=*/false);
6167 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6168 parser->scope = new_scope;
6170 success = true;
6171 continue;
6174 /* Spot cases that cannot be the beginning of a
6175 nested-name-specifier. On the second and subsequent times
6176 through the loop, we look for the `template' keyword. */
6177 if (success && token->keyword == RID_TEMPLATE)
6179 /* A template-id can start a nested-name-specifier. */
6180 else if (token->type == CPP_TEMPLATE_ID)
6182 /* DR 743: decltype can be used in a nested-name-specifier. */
6183 else if (token_is_decltype (token))
6185 else
6187 /* If the next token is not an identifier, then it is
6188 definitely not a type-name or namespace-name. */
6189 if (token->type != CPP_NAME)
6190 break;
6191 /* If the following token is neither a `<' (to begin a
6192 template-id), nor a `::', then we are not looking at a
6193 nested-name-specifier. */
6194 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6196 if (token->type == CPP_COLON
6197 && parser->colon_corrects_to_scope_p
6198 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6200 gcc_rich_location richloc (token->location);
6201 richloc.add_fixit_replace ("::");
6202 error_at (&richloc,
6203 "found %<:%> in nested-name-specifier, "
6204 "expected %<::%>");
6205 token->type = CPP_SCOPE;
6208 if (token->type != CPP_SCOPE
6209 && !cp_parser_nth_token_starts_template_argument_list_p
6210 (parser, 2))
6211 break;
6214 /* The nested-name-specifier is optional, so we parse
6215 tentatively. */
6216 cp_parser_parse_tentatively (parser);
6218 /* Look for the optional `template' keyword, if this isn't the
6219 first time through the loop. */
6220 if (success)
6221 template_keyword_p = cp_parser_optional_template_keyword (parser);
6223 /* Save the old scope since the name lookup we are about to do
6224 might destroy it. */
6225 old_scope = parser->scope;
6226 saved_qualifying_scope = parser->qualifying_scope;
6227 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6228 look up names in "X<T>::I" in order to determine that "Y" is
6229 a template. So, if we have a typename at this point, we make
6230 an effort to look through it. */
6231 if (is_declaration
6232 && !typename_keyword_p
6233 && parser->scope
6234 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6235 parser->scope = resolve_typename_type (parser->scope,
6236 /*only_current_p=*/false);
6237 /* Parse the qualifying entity. */
6238 new_scope
6239 = cp_parser_qualifying_entity (parser,
6240 typename_keyword_p,
6241 template_keyword_p,
6242 check_dependency_p,
6243 type_p,
6244 is_declaration);
6245 /* Look for the `::' token. */
6246 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6248 /* If we found what we wanted, we keep going; otherwise, we're
6249 done. */
6250 if (!cp_parser_parse_definitely (parser))
6252 bool error_p = false;
6254 /* Restore the OLD_SCOPE since it was valid before the
6255 failed attempt at finding the last
6256 class-or-namespace-name. */
6257 parser->scope = old_scope;
6258 parser->qualifying_scope = saved_qualifying_scope;
6260 /* If the next token is a decltype, and the one after that is a
6261 `::', then the decltype has failed to resolve to a class or
6262 enumeration type. Give this error even when parsing
6263 tentatively since it can't possibly be valid--and we're going
6264 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6265 won't get another chance.*/
6266 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6267 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6268 == CPP_SCOPE))
6270 token = cp_lexer_consume_token (parser->lexer);
6271 error_at (token->location, "decltype evaluates to %qT, "
6272 "which is not a class or enumeration type",
6273 token->u.tree_check_value->value);
6274 parser->scope = error_mark_node;
6275 error_p = true;
6276 /* As below. */
6277 success = true;
6278 cp_lexer_consume_token (parser->lexer);
6281 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6282 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6284 /* If we have a non-type template-id followed by ::, it can't
6285 possibly be valid. */
6286 token = cp_lexer_peek_token (parser->lexer);
6287 tree tid = token->u.tree_check_value->value;
6288 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6289 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6291 tree tmpl = NULL_TREE;
6292 if (is_overloaded_fn (tid))
6294 tree fns = get_fns (tid);
6295 if (OVL_SINGLE_P (fns))
6296 tmpl = OVL_FIRST (fns);
6297 error_at (token->location, "function template-id %qD "
6298 "in nested-name-specifier", tid);
6300 else
6302 /* Variable template. */
6303 tmpl = TREE_OPERAND (tid, 0);
6304 gcc_assert (variable_template_p (tmpl));
6305 error_at (token->location, "variable template-id %qD "
6306 "in nested-name-specifier", tid);
6308 if (tmpl)
6309 inform (DECL_SOURCE_LOCATION (tmpl),
6310 "%qD declared here", tmpl);
6312 parser->scope = error_mark_node;
6313 error_p = true;
6314 /* As below. */
6315 success = true;
6316 cp_lexer_consume_token (parser->lexer);
6317 cp_lexer_consume_token (parser->lexer);
6321 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6322 break;
6323 /* If the next token is an identifier, and the one after
6324 that is a `::', then any valid interpretation would have
6325 found a class-or-namespace-name. */
6326 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6327 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6328 == CPP_SCOPE)
6329 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6330 != CPP_COMPL))
6332 token = cp_lexer_consume_token (parser->lexer);
6333 if (!error_p)
6335 if (!token->error_reported)
6337 tree decl;
6338 tree ambiguous_decls;
6340 decl = cp_parser_lookup_name (parser, token->u.value,
6341 none_type,
6342 /*is_template=*/false,
6343 /*is_namespace=*/false,
6344 /*check_dependency=*/true,
6345 &ambiguous_decls,
6346 token->location);
6347 if (TREE_CODE (decl) == TEMPLATE_DECL)
6348 error_at (token->location,
6349 "%qD used without template arguments",
6350 decl);
6351 else if (ambiguous_decls)
6353 // cp_parser_lookup_name has the same diagnostic,
6354 // thus make sure to emit it at most once.
6355 if (cp_parser_uncommitted_to_tentative_parse_p
6356 (parser))
6358 error_at (token->location,
6359 "reference to %qD is ambiguous",
6360 token->u.value);
6361 print_candidates (ambiguous_decls);
6363 decl = error_mark_node;
6365 else
6367 if (cxx_dialect != cxx98)
6368 cp_parser_name_lookup_error
6369 (parser, token->u.value, decl, NLE_NOT_CXX98,
6370 token->location);
6371 else
6372 cp_parser_name_lookup_error
6373 (parser, token->u.value, decl, NLE_CXX98,
6374 token->location);
6377 parser->scope = error_mark_node;
6378 error_p = true;
6379 /* Treat this as a successful nested-name-specifier
6380 due to:
6382 [basic.lookup.qual]
6384 If the name found is not a class-name (clause
6385 _class_) or namespace-name (_namespace.def_), the
6386 program is ill-formed. */
6387 success = true;
6389 cp_lexer_consume_token (parser->lexer);
6391 break;
6393 /* We've found one valid nested-name-specifier. */
6394 success = true;
6395 /* Name lookup always gives us a DECL. */
6396 if (TREE_CODE (new_scope) == TYPE_DECL)
6397 new_scope = TREE_TYPE (new_scope);
6398 /* Uses of "template" must be followed by actual templates. */
6399 if (template_keyword_p
6400 && !(CLASS_TYPE_P (new_scope)
6401 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6402 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6403 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6404 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6405 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6406 == TEMPLATE_ID_EXPR)))
6407 permerror (input_location, TYPE_P (new_scope)
6408 ? G_("%qT is not a template")
6409 : G_("%qD is not a template"),
6410 new_scope);
6411 /* If it is a class scope, try to complete it; we are about to
6412 be looking up names inside the class. */
6413 if (TYPE_P (new_scope)
6414 /* Since checking types for dependency can be expensive,
6415 avoid doing it if the type is already complete. */
6416 && !COMPLETE_TYPE_P (new_scope)
6417 /* Do not try to complete dependent types. */
6418 && !dependent_type_p (new_scope))
6420 new_scope = complete_type (new_scope);
6421 /* If it is a typedef to current class, use the current
6422 class instead, as the typedef won't have any names inside
6423 it yet. */
6424 if (!COMPLETE_TYPE_P (new_scope)
6425 && currently_open_class (new_scope))
6426 new_scope = TYPE_MAIN_VARIANT (new_scope);
6428 /* Make sure we look in the right scope the next time through
6429 the loop. */
6430 parser->scope = new_scope;
6433 /* If parsing tentatively, replace the sequence of tokens that makes
6434 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6435 token. That way, should we re-parse the token stream, we will
6436 not have to repeat the effort required to do the parse, nor will
6437 we issue duplicate error messages. */
6438 if (success && start)
6440 cp_token *token;
6442 token = cp_lexer_token_at (parser->lexer, start);
6443 /* Reset the contents of the START token. */
6444 token->type = CPP_NESTED_NAME_SPECIFIER;
6445 /* Retrieve any deferred checks. Do not pop this access checks yet
6446 so the memory will not be reclaimed during token replacing below. */
6447 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6448 token->u.tree_check_value->value = parser->scope;
6449 token->u.tree_check_value->checks = get_deferred_access_checks ();
6450 token->u.tree_check_value->qualifying_scope =
6451 parser->qualifying_scope;
6452 token->keyword = RID_MAX;
6454 /* Purge all subsequent tokens. */
6455 cp_lexer_purge_tokens_after (parser->lexer, start);
6458 if (start)
6459 pop_to_parent_deferring_access_checks ();
6461 return success ? parser->scope : NULL_TREE;
6464 /* Parse a nested-name-specifier. See
6465 cp_parser_nested_name_specifier_opt for details. This function
6466 behaves identically, except that it will an issue an error if no
6467 nested-name-specifier is present. */
6469 static tree
6470 cp_parser_nested_name_specifier (cp_parser *parser,
6471 bool typename_keyword_p,
6472 bool check_dependency_p,
6473 bool type_p,
6474 bool is_declaration)
6476 tree scope;
6478 /* Look for the nested-name-specifier. */
6479 scope = cp_parser_nested_name_specifier_opt (parser,
6480 typename_keyword_p,
6481 check_dependency_p,
6482 type_p,
6483 is_declaration);
6484 /* If it was not present, issue an error message. */
6485 if (!scope)
6487 cp_parser_error (parser, "expected nested-name-specifier");
6488 parser->scope = NULL_TREE;
6491 return scope;
6494 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6495 this is either a class-name or a namespace-name (which corresponds
6496 to the class-or-namespace-name production in the grammar). For
6497 C++0x, it can also be a type-name that refers to an enumeration
6498 type or a simple-template-id.
6500 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6501 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6502 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6503 TYPE_P is TRUE iff the next name should be taken as a class-name,
6504 even the same name is declared to be another entity in the same
6505 scope.
6507 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6508 specified by the class-or-namespace-name. If neither is found the
6509 ERROR_MARK_NODE is returned. */
6511 static tree
6512 cp_parser_qualifying_entity (cp_parser *parser,
6513 bool typename_keyword_p,
6514 bool template_keyword_p,
6515 bool check_dependency_p,
6516 bool type_p,
6517 bool is_declaration)
6519 tree saved_scope;
6520 tree saved_qualifying_scope;
6521 tree saved_object_scope;
6522 tree scope;
6523 bool only_class_p;
6524 bool successful_parse_p;
6526 /* DR 743: decltype can appear in a nested-name-specifier. */
6527 if (cp_lexer_next_token_is_decltype (parser->lexer))
6529 scope = cp_parser_decltype (parser);
6530 if (TREE_CODE (scope) != ENUMERAL_TYPE
6531 && !MAYBE_CLASS_TYPE_P (scope))
6533 cp_parser_simulate_error (parser);
6534 return error_mark_node;
6536 if (TYPE_NAME (scope))
6537 scope = TYPE_NAME (scope);
6538 return scope;
6541 /* Before we try to parse the class-name, we must save away the
6542 current PARSER->SCOPE since cp_parser_class_name will destroy
6543 it. */
6544 saved_scope = parser->scope;
6545 saved_qualifying_scope = parser->qualifying_scope;
6546 saved_object_scope = parser->object_scope;
6547 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6548 there is no need to look for a namespace-name. */
6549 only_class_p = template_keyword_p
6550 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6551 if (!only_class_p)
6552 cp_parser_parse_tentatively (parser);
6553 scope = cp_parser_class_name (parser,
6554 typename_keyword_p,
6555 template_keyword_p,
6556 type_p ? class_type : none_type,
6557 check_dependency_p,
6558 /*class_head_p=*/false,
6559 is_declaration,
6560 /*enum_ok=*/cxx_dialect > cxx98);
6561 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6562 /* If that didn't work, try for a namespace-name. */
6563 if (!only_class_p && !successful_parse_p)
6565 /* Restore the saved scope. */
6566 parser->scope = saved_scope;
6567 parser->qualifying_scope = saved_qualifying_scope;
6568 parser->object_scope = saved_object_scope;
6569 /* If we are not looking at an identifier followed by the scope
6570 resolution operator, then this is not part of a
6571 nested-name-specifier. (Note that this function is only used
6572 to parse the components of a nested-name-specifier.) */
6573 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6574 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6575 return error_mark_node;
6576 scope = cp_parser_namespace_name (parser);
6579 return scope;
6582 /* Return true if we are looking at a compound-literal, false otherwise. */
6584 static bool
6585 cp_parser_compound_literal_p (cp_parser *parser)
6587 cp_lexer_save_tokens (parser->lexer);
6589 /* Skip tokens until the next token is a closing parenthesis.
6590 If we find the closing `)', and the next token is a `{', then
6591 we are looking at a compound-literal. */
6592 bool compound_literal_p
6593 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6594 /*consume_paren=*/true)
6595 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6597 /* Roll back the tokens we skipped. */
6598 cp_lexer_rollback_tokens (parser->lexer);
6600 return compound_literal_p;
6603 /* Return true if EXPR is the integer constant zero or a complex constant
6604 of zero, without any folding, but ignoring location wrappers. */
6606 static bool
6607 literal_integer_zerop (const_tree expr)
6609 STRIP_ANY_LOCATION_WRAPPER (expr);
6610 return integer_zerop (expr);
6613 /* Parse a postfix-expression.
6615 postfix-expression:
6616 primary-expression
6617 postfix-expression [ expression ]
6618 postfix-expression ( expression-list [opt] )
6619 simple-type-specifier ( expression-list [opt] )
6620 typename :: [opt] nested-name-specifier identifier
6621 ( expression-list [opt] )
6622 typename :: [opt] nested-name-specifier template [opt] template-id
6623 ( expression-list [opt] )
6624 postfix-expression . template [opt] id-expression
6625 postfix-expression -> template [opt] id-expression
6626 postfix-expression . pseudo-destructor-name
6627 postfix-expression -> pseudo-destructor-name
6628 postfix-expression ++
6629 postfix-expression --
6630 dynamic_cast < type-id > ( expression )
6631 static_cast < type-id > ( expression )
6632 reinterpret_cast < type-id > ( expression )
6633 const_cast < type-id > ( expression )
6634 typeid ( expression )
6635 typeid ( type-id )
6637 GNU Extension:
6639 postfix-expression:
6640 ( type-id ) { initializer-list , [opt] }
6642 This extension is a GNU version of the C99 compound-literal
6643 construct. (The C99 grammar uses `type-name' instead of `type-id',
6644 but they are essentially the same concept.)
6646 If ADDRESS_P is true, the postfix expression is the operand of the
6647 `&' operator. CAST_P is true if this expression is the target of a
6648 cast.
6650 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6651 class member access expressions [expr.ref].
6653 Returns a representation of the expression. */
6655 static cp_expr
6656 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6657 bool member_access_only_p, bool decltype_p,
6658 cp_id_kind * pidk_return)
6660 cp_token *token;
6661 location_t loc;
6662 enum rid keyword;
6663 cp_id_kind idk = CP_ID_KIND_NONE;
6664 cp_expr postfix_expression = NULL_TREE;
6665 bool is_member_access = false;
6667 /* Peek at the next token. */
6668 token = cp_lexer_peek_token (parser->lexer);
6669 loc = token->location;
6670 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6672 /* Some of the productions are determined by keywords. */
6673 keyword = token->keyword;
6674 switch (keyword)
6676 case RID_DYNCAST:
6677 case RID_STATCAST:
6678 case RID_REINTCAST:
6679 case RID_CONSTCAST:
6681 tree type;
6682 cp_expr expression;
6683 const char *saved_message;
6684 bool saved_in_type_id_in_expr_p;
6686 /* All of these can be handled in the same way from the point
6687 of view of parsing. Begin by consuming the token
6688 identifying the cast. */
6689 cp_lexer_consume_token (parser->lexer);
6691 /* New types cannot be defined in the cast. */
6692 saved_message = parser->type_definition_forbidden_message;
6693 parser->type_definition_forbidden_message
6694 = G_("types may not be defined in casts");
6696 /* Look for the opening `<'. */
6697 cp_parser_require (parser, CPP_LESS, RT_LESS);
6698 /* Parse the type to which we are casting. */
6699 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6700 parser->in_type_id_in_expr_p = true;
6701 type = cp_parser_type_id (parser);
6702 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6703 /* Look for the closing `>'. */
6704 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6705 /* Restore the old message. */
6706 parser->type_definition_forbidden_message = saved_message;
6708 bool saved_greater_than_is_operator_p
6709 = parser->greater_than_is_operator_p;
6710 parser->greater_than_is_operator_p = true;
6712 /* And the expression which is being cast. */
6713 matching_parens parens;
6714 parens.require_open (parser);
6715 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6716 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6717 RT_CLOSE_PAREN);
6718 location_t end_loc = close_paren ?
6719 close_paren->location : UNKNOWN_LOCATION;
6721 parser->greater_than_is_operator_p
6722 = saved_greater_than_is_operator_p;
6724 /* Only type conversions to integral or enumeration types
6725 can be used in constant-expressions. */
6726 if (!cast_valid_in_integral_constant_expression_p (type)
6727 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6729 postfix_expression = error_mark_node;
6730 break;
6733 switch (keyword)
6735 case RID_DYNCAST:
6736 postfix_expression
6737 = build_dynamic_cast (type, expression, tf_warning_or_error);
6738 break;
6739 case RID_STATCAST:
6740 postfix_expression
6741 = build_static_cast (type, expression, tf_warning_or_error);
6742 break;
6743 case RID_REINTCAST:
6744 postfix_expression
6745 = build_reinterpret_cast (type, expression,
6746 tf_warning_or_error);
6747 break;
6748 case RID_CONSTCAST:
6749 postfix_expression
6750 = build_const_cast (type, expression, tf_warning_or_error);
6751 break;
6752 default:
6753 gcc_unreachable ();
6756 /* Construct a location e.g. :
6757 reinterpret_cast <int *> (expr)
6758 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6759 ranging from the start of the "*_cast" token to the final closing
6760 paren, with the caret at the start. */
6761 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6762 postfix_expression.set_location (cp_cast_loc);
6764 break;
6766 case RID_TYPEID:
6768 tree type;
6769 const char *saved_message;
6770 bool saved_in_type_id_in_expr_p;
6772 /* Consume the `typeid' token. */
6773 cp_lexer_consume_token (parser->lexer);
6774 /* Look for the `(' token. */
6775 matching_parens parens;
6776 parens.require_open (parser);
6777 /* Types cannot be defined in a `typeid' expression. */
6778 saved_message = parser->type_definition_forbidden_message;
6779 parser->type_definition_forbidden_message
6780 = G_("types may not be defined in a %<typeid%> expression");
6781 /* We can't be sure yet whether we're looking at a type-id or an
6782 expression. */
6783 cp_parser_parse_tentatively (parser);
6784 /* Try a type-id first. */
6785 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6786 parser->in_type_id_in_expr_p = true;
6787 type = cp_parser_type_id (parser);
6788 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6789 /* Look for the `)' token. Otherwise, we can't be sure that
6790 we're not looking at an expression: consider `typeid (int
6791 (3))', for example. */
6792 cp_token *close_paren = parens.require_close (parser);
6793 /* If all went well, simply lookup the type-id. */
6794 if (cp_parser_parse_definitely (parser))
6795 postfix_expression = get_typeid (type, tf_warning_or_error);
6796 /* Otherwise, fall back to the expression variant. */
6797 else
6799 tree expression;
6801 /* Look for an expression. */
6802 expression = cp_parser_expression (parser, & idk);
6803 /* Compute its typeid. */
6804 postfix_expression = build_typeid (expression, tf_warning_or_error);
6805 /* Look for the `)' token. */
6806 close_paren = parens.require_close (parser);
6808 /* Restore the saved message. */
6809 parser->type_definition_forbidden_message = saved_message;
6810 /* `typeid' may not appear in an integral constant expression. */
6811 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6812 postfix_expression = error_mark_node;
6814 /* Construct a location e.g. :
6815 typeid (expr)
6816 ^~~~~~~~~~~~~
6817 ranging from the start of the "typeid" token to the final closing
6818 paren, with the caret at the start. */
6819 if (close_paren)
6821 location_t typeid_loc
6822 = make_location (start_loc, start_loc, close_paren->location);
6823 postfix_expression.set_location (typeid_loc);
6824 postfix_expression.maybe_add_location_wrapper ();
6827 break;
6829 case RID_TYPENAME:
6831 tree type;
6832 /* The syntax permitted here is the same permitted for an
6833 elaborated-type-specifier. */
6834 ++parser->prevent_constrained_type_specifiers;
6835 type = cp_parser_elaborated_type_specifier (parser,
6836 /*is_friend=*/false,
6837 /*is_declaration=*/false);
6838 --parser->prevent_constrained_type_specifiers;
6839 postfix_expression = cp_parser_functional_cast (parser, type);
6841 break;
6843 case RID_ADDRESSOF:
6844 case RID_BUILTIN_SHUFFLE:
6845 case RID_BUILTIN_LAUNDER:
6847 vec<tree, va_gc> *vec;
6848 unsigned int i;
6849 tree p;
6851 cp_lexer_consume_token (parser->lexer);
6852 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6853 /*cast_p=*/false, /*allow_expansion_p=*/true,
6854 /*non_constant_p=*/NULL);
6855 if (vec == NULL)
6857 postfix_expression = error_mark_node;
6858 break;
6861 FOR_EACH_VEC_ELT (*vec, i, p)
6862 mark_exp_read (p);
6864 switch (keyword)
6866 case RID_ADDRESSOF:
6867 if (vec->length () == 1)
6868 postfix_expression
6869 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6870 else
6872 error_at (loc, "wrong number of arguments to "
6873 "%<__builtin_addressof%>");
6874 postfix_expression = error_mark_node;
6876 break;
6878 case RID_BUILTIN_LAUNDER:
6879 if (vec->length () == 1)
6880 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6881 tf_warning_or_error);
6882 else
6884 error_at (loc, "wrong number of arguments to "
6885 "%<__builtin_launder%>");
6886 postfix_expression = error_mark_node;
6888 break;
6890 case RID_BUILTIN_SHUFFLE:
6891 if (vec->length () == 2)
6892 postfix_expression
6893 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6894 (*vec)[1], tf_warning_or_error);
6895 else if (vec->length () == 3)
6896 postfix_expression
6897 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6898 (*vec)[2], tf_warning_or_error);
6899 else
6901 error_at (loc, "wrong number of arguments to "
6902 "%<__builtin_shuffle%>");
6903 postfix_expression = error_mark_node;
6905 break;
6907 default:
6908 gcc_unreachable ();
6910 break;
6913 default:
6915 tree type;
6917 /* If the next thing is a simple-type-specifier, we may be
6918 looking at a functional cast. We could also be looking at
6919 an id-expression. So, we try the functional cast, and if
6920 that doesn't work we fall back to the primary-expression. */
6921 cp_parser_parse_tentatively (parser);
6922 /* Look for the simple-type-specifier. */
6923 ++parser->prevent_constrained_type_specifiers;
6924 type = cp_parser_simple_type_specifier (parser,
6925 /*decl_specs=*/NULL,
6926 CP_PARSER_FLAGS_NONE);
6927 --parser->prevent_constrained_type_specifiers;
6928 /* Parse the cast itself. */
6929 if (!cp_parser_error_occurred (parser))
6930 postfix_expression
6931 = cp_parser_functional_cast (parser, type);
6932 /* If that worked, we're done. */
6933 if (cp_parser_parse_definitely (parser))
6934 break;
6936 /* If the functional-cast didn't work out, try a
6937 compound-literal. */
6938 if (cp_parser_allow_gnu_extensions_p (parser)
6939 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6941 cp_expr initializer = NULL_TREE;
6943 cp_parser_parse_tentatively (parser);
6945 matching_parens parens;
6946 parens.consume_open (parser);
6948 /* Avoid calling cp_parser_type_id pointlessly, see comment
6949 in cp_parser_cast_expression about c++/29234. */
6950 if (!cp_parser_compound_literal_p (parser))
6951 cp_parser_simulate_error (parser);
6952 else
6954 /* Parse the type. */
6955 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6956 parser->in_type_id_in_expr_p = true;
6957 type = cp_parser_type_id (parser);
6958 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6959 parens.require_close (parser);
6962 /* If things aren't going well, there's no need to
6963 keep going. */
6964 if (!cp_parser_error_occurred (parser))
6966 bool non_constant_p;
6967 /* Parse the brace-enclosed initializer list. */
6968 initializer = cp_parser_braced_list (parser,
6969 &non_constant_p);
6971 /* If that worked, we're definitely looking at a
6972 compound-literal expression. */
6973 if (cp_parser_parse_definitely (parser))
6975 /* Warn the user that a compound literal is not
6976 allowed in standard C++. */
6977 pedwarn (input_location, OPT_Wpedantic,
6978 "ISO C++ forbids compound-literals");
6979 /* For simplicity, we disallow compound literals in
6980 constant-expressions. We could
6981 allow compound literals of integer type, whose
6982 initializer was a constant, in constant
6983 expressions. Permitting that usage, as a further
6984 extension, would not change the meaning of any
6985 currently accepted programs. (Of course, as
6986 compound literals are not part of ISO C++, the
6987 standard has nothing to say.) */
6988 if (cp_parser_non_integral_constant_expression (parser,
6989 NIC_NCC))
6991 postfix_expression = error_mark_node;
6992 break;
6994 /* Form the representation of the compound-literal. */
6995 postfix_expression
6996 = finish_compound_literal (type, initializer,
6997 tf_warning_or_error, fcl_c99);
6998 postfix_expression.set_location (initializer.get_location ());
6999 break;
7003 /* It must be a primary-expression. */
7004 postfix_expression
7005 = cp_parser_primary_expression (parser, address_p, cast_p,
7006 /*template_arg_p=*/false,
7007 decltype_p,
7008 &idk);
7010 break;
7013 /* Note that we don't need to worry about calling build_cplus_new on a
7014 class-valued CALL_EXPR in decltype when it isn't the end of the
7015 postfix-expression; unary_complex_lvalue will take care of that for
7016 all these cases. */
7018 /* Keep looping until the postfix-expression is complete. */
7019 while (true)
7021 if (idk == CP_ID_KIND_UNQUALIFIED
7022 && identifier_p (postfix_expression)
7023 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7024 /* It is not a Koenig lookup function call. */
7025 postfix_expression
7026 = unqualified_name_lookup_error (postfix_expression);
7028 /* Peek at the next token. */
7029 token = cp_lexer_peek_token (parser->lexer);
7031 switch (token->type)
7033 case CPP_OPEN_SQUARE:
7034 if (cp_next_tokens_can_be_std_attribute_p (parser))
7036 cp_parser_error (parser,
7037 "two consecutive %<[%> shall "
7038 "only introduce an attribute");
7039 return error_mark_node;
7041 postfix_expression
7042 = cp_parser_postfix_open_square_expression (parser,
7043 postfix_expression,
7044 false,
7045 decltype_p);
7046 postfix_expression.set_range (start_loc,
7047 postfix_expression.get_location ());
7049 idk = CP_ID_KIND_NONE;
7050 is_member_access = false;
7051 break;
7053 case CPP_OPEN_PAREN:
7054 /* postfix-expression ( expression-list [opt] ) */
7056 bool koenig_p;
7057 bool is_builtin_constant_p;
7058 bool saved_integral_constant_expression_p = false;
7059 bool saved_non_integral_constant_expression_p = false;
7060 tsubst_flags_t complain = complain_flags (decltype_p);
7061 vec<tree, va_gc> *args;
7062 location_t close_paren_loc = UNKNOWN_LOCATION;
7064 is_member_access = false;
7066 is_builtin_constant_p
7067 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7068 if (is_builtin_constant_p)
7070 /* The whole point of __builtin_constant_p is to allow
7071 non-constant expressions to appear as arguments. */
7072 saved_integral_constant_expression_p
7073 = parser->integral_constant_expression_p;
7074 saved_non_integral_constant_expression_p
7075 = parser->non_integral_constant_expression_p;
7076 parser->integral_constant_expression_p = false;
7078 args = (cp_parser_parenthesized_expression_list
7079 (parser, non_attr,
7080 /*cast_p=*/false, /*allow_expansion_p=*/true,
7081 /*non_constant_p=*/NULL,
7082 /*close_paren_loc=*/&close_paren_loc,
7083 /*wrap_locations_p=*/true));
7084 if (is_builtin_constant_p)
7086 parser->integral_constant_expression_p
7087 = saved_integral_constant_expression_p;
7088 parser->non_integral_constant_expression_p
7089 = saved_non_integral_constant_expression_p;
7092 if (args == NULL)
7094 postfix_expression = error_mark_node;
7095 break;
7098 /* Function calls are not permitted in
7099 constant-expressions. */
7100 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7101 && cp_parser_non_integral_constant_expression (parser,
7102 NIC_FUNC_CALL))
7104 postfix_expression = error_mark_node;
7105 release_tree_vector (args);
7106 break;
7109 koenig_p = false;
7110 if (idk == CP_ID_KIND_UNQUALIFIED
7111 || idk == CP_ID_KIND_TEMPLATE_ID)
7113 if (identifier_p (postfix_expression))
7115 if (!args->is_empty ())
7117 koenig_p = true;
7118 if (!any_type_dependent_arguments_p (args))
7119 postfix_expression
7120 = perform_koenig_lookup (postfix_expression, args,
7121 complain);
7123 else
7124 postfix_expression
7125 = unqualified_fn_lookup_error (postfix_expression);
7127 /* We do not perform argument-dependent lookup if
7128 normal lookup finds a non-function, in accordance
7129 with the expected resolution of DR 218. */
7130 else if (!args->is_empty ()
7131 && is_overloaded_fn (postfix_expression))
7133 tree fn = get_first_fn (postfix_expression);
7134 fn = STRIP_TEMPLATE (fn);
7136 /* Do not do argument dependent lookup if regular
7137 lookup finds a member function or a block-scope
7138 function declaration. [basic.lookup.argdep]/3 */
7139 if (!DECL_FUNCTION_MEMBER_P (fn)
7140 && !DECL_LOCAL_FUNCTION_P (fn))
7142 koenig_p = true;
7143 if (!any_type_dependent_arguments_p (args))
7144 postfix_expression
7145 = perform_koenig_lookup (postfix_expression, args,
7146 complain);
7151 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
7152 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
7153 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
7154 && vec_safe_length (args) == 3)
7156 tree arg0 = (*args)[0];
7157 tree arg1 = (*args)[1];
7158 tree arg2 = (*args)[2];
7159 int literal_mask = ((literal_integer_zerop (arg1) << 1)
7160 | (literal_integer_zerop (arg2) << 2));
7161 warn_for_memset (input_location, arg0, arg2, literal_mask);
7164 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7166 tree instance = TREE_OPERAND (postfix_expression, 0);
7167 tree fn = TREE_OPERAND (postfix_expression, 1);
7169 if (processing_template_decl
7170 && (type_dependent_object_expression_p (instance)
7171 || (!BASELINK_P (fn)
7172 && TREE_CODE (fn) != FIELD_DECL)
7173 || type_dependent_expression_p (fn)
7174 || any_type_dependent_arguments_p (args)))
7176 maybe_generic_this_capture (instance, fn);
7177 postfix_expression
7178 = build_min_nt_call_vec (postfix_expression, args);
7179 release_tree_vector (args);
7180 break;
7183 if (BASELINK_P (fn))
7185 postfix_expression
7186 = (build_new_method_call
7187 (instance, fn, &args, NULL_TREE,
7188 (idk == CP_ID_KIND_QUALIFIED
7189 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7190 : LOOKUP_NORMAL),
7191 /*fn_p=*/NULL,
7192 complain));
7194 else
7195 postfix_expression
7196 = finish_call_expr (postfix_expression, &args,
7197 /*disallow_virtual=*/false,
7198 /*koenig_p=*/false,
7199 complain);
7201 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7202 || TREE_CODE (postfix_expression) == MEMBER_REF
7203 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7204 postfix_expression = (build_offset_ref_call_from_tree
7205 (postfix_expression, &args,
7206 complain));
7207 else if (idk == CP_ID_KIND_QUALIFIED)
7208 /* A call to a static class member, or a namespace-scope
7209 function. */
7210 postfix_expression
7211 = finish_call_expr (postfix_expression, &args,
7212 /*disallow_virtual=*/true,
7213 koenig_p,
7214 complain);
7215 else
7216 /* All other function calls. */
7217 postfix_expression
7218 = finish_call_expr (postfix_expression, &args,
7219 /*disallow_virtual=*/false,
7220 koenig_p,
7221 complain);
7223 if (close_paren_loc != UNKNOWN_LOCATION)
7225 location_t combined_loc = make_location (token->location,
7226 start_loc,
7227 close_paren_loc);
7228 postfix_expression.set_location (combined_loc);
7231 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7232 idk = CP_ID_KIND_NONE;
7234 release_tree_vector (args);
7236 break;
7238 case CPP_DOT:
7239 case CPP_DEREF:
7240 /* postfix-expression . template [opt] id-expression
7241 postfix-expression . pseudo-destructor-name
7242 postfix-expression -> template [opt] id-expression
7243 postfix-expression -> pseudo-destructor-name */
7245 /* Consume the `.' or `->' operator. */
7246 cp_lexer_consume_token (parser->lexer);
7248 postfix_expression
7249 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7250 postfix_expression,
7251 false, &idk, loc);
7253 is_member_access = true;
7254 break;
7256 case CPP_PLUS_PLUS:
7257 /* postfix-expression ++ */
7258 /* Consume the `++' token. */
7259 cp_lexer_consume_token (parser->lexer);
7260 /* Generate a representation for the complete expression. */
7261 postfix_expression
7262 = finish_increment_expr (postfix_expression,
7263 POSTINCREMENT_EXPR);
7264 /* Increments may not appear in constant-expressions. */
7265 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7266 postfix_expression = error_mark_node;
7267 idk = CP_ID_KIND_NONE;
7268 is_member_access = false;
7269 break;
7271 case CPP_MINUS_MINUS:
7272 /* postfix-expression -- */
7273 /* Consume the `--' token. */
7274 cp_lexer_consume_token (parser->lexer);
7275 /* Generate a representation for the complete expression. */
7276 postfix_expression
7277 = finish_increment_expr (postfix_expression,
7278 POSTDECREMENT_EXPR);
7279 /* Decrements may not appear in constant-expressions. */
7280 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7281 postfix_expression = error_mark_node;
7282 idk = CP_ID_KIND_NONE;
7283 is_member_access = false;
7284 break;
7286 default:
7287 if (pidk_return != NULL)
7288 * pidk_return = idk;
7289 if (member_access_only_p)
7290 return is_member_access
7291 ? postfix_expression
7292 : cp_expr (error_mark_node);
7293 else
7294 return postfix_expression;
7298 /* We should never get here. */
7299 gcc_unreachable ();
7300 return error_mark_node;
7303 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7304 by cp_parser_builtin_offsetof. We're looking for
7306 postfix-expression [ expression ]
7307 postfix-expression [ braced-init-list ] (C++11)
7309 FOR_OFFSETOF is set if we're being called in that context, which
7310 changes how we deal with integer constant expressions. */
7312 static tree
7313 cp_parser_postfix_open_square_expression (cp_parser *parser,
7314 tree postfix_expression,
7315 bool for_offsetof,
7316 bool decltype_p)
7318 tree index = NULL_TREE;
7319 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7320 bool saved_greater_than_is_operator_p;
7322 /* Consume the `[' token. */
7323 cp_lexer_consume_token (parser->lexer);
7325 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7326 parser->greater_than_is_operator_p = true;
7328 /* Parse the index expression. */
7329 /* ??? For offsetof, there is a question of what to allow here. If
7330 offsetof is not being used in an integral constant expression context,
7331 then we *could* get the right answer by computing the value at runtime.
7332 If we are in an integral constant expression context, then we might
7333 could accept any constant expression; hard to say without analysis.
7334 Rather than open the barn door too wide right away, allow only integer
7335 constant expressions here. */
7336 if (for_offsetof)
7337 index = cp_parser_constant_expression (parser);
7338 else
7340 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7342 bool expr_nonconst_p;
7343 cp_lexer_set_source_position (parser->lexer);
7344 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7345 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7347 else
7348 index = cp_parser_expression (parser);
7351 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7353 /* Look for the closing `]'. */
7354 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7356 /* Build the ARRAY_REF. */
7357 postfix_expression = grok_array_decl (loc, postfix_expression,
7358 index, decltype_p);
7360 /* When not doing offsetof, array references are not permitted in
7361 constant-expressions. */
7362 if (!for_offsetof
7363 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7364 postfix_expression = error_mark_node;
7366 return postfix_expression;
7369 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7370 dereference of incomplete type, returns true if error_mark_node should
7371 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7372 and *DEPENDENT_P. */
7374 bool
7375 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7376 bool *dependent_p)
7378 /* In a template, be permissive by treating an object expression
7379 of incomplete type as dependent (after a pedwarn). */
7380 diagnostic_t kind = (processing_template_decl
7381 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7383 switch (TREE_CODE (*postfix_expression))
7385 case CAST_EXPR:
7386 case REINTERPRET_CAST_EXPR:
7387 case CONST_CAST_EXPR:
7388 case STATIC_CAST_EXPR:
7389 case DYNAMIC_CAST_EXPR:
7390 case IMPLICIT_CONV_EXPR:
7391 case VIEW_CONVERT_EXPR:
7392 case NON_LVALUE_EXPR:
7393 kind = DK_ERROR;
7394 break;
7395 case OVERLOAD:
7396 /* Don't emit any diagnostic for OVERLOADs. */
7397 kind = DK_IGNORED;
7398 break;
7399 default:
7400 /* Avoid clobbering e.g. DECLs. */
7401 if (!EXPR_P (*postfix_expression))
7402 kind = DK_ERROR;
7403 break;
7406 if (kind == DK_IGNORED)
7407 return false;
7409 location_t exploc = location_of (*postfix_expression);
7410 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7411 if (!MAYBE_CLASS_TYPE_P (*scope))
7412 return true;
7413 if (kind == DK_ERROR)
7414 *scope = *postfix_expression = error_mark_node;
7415 else if (processing_template_decl)
7417 *dependent_p = true;
7418 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7420 return false;
7423 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7424 by cp_parser_builtin_offsetof. We're looking for
7426 postfix-expression . template [opt] id-expression
7427 postfix-expression . pseudo-destructor-name
7428 postfix-expression -> template [opt] id-expression
7429 postfix-expression -> pseudo-destructor-name
7431 FOR_OFFSETOF is set if we're being called in that context. That sorta
7432 limits what of the above we'll actually accept, but nevermind.
7433 TOKEN_TYPE is the "." or "->" token, which will already have been
7434 removed from the stream. */
7436 static tree
7437 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7438 enum cpp_ttype token_type,
7439 cp_expr postfix_expression,
7440 bool for_offsetof, cp_id_kind *idk,
7441 location_t location)
7443 tree name;
7444 bool dependent_p;
7445 bool pseudo_destructor_p;
7446 tree scope = NULL_TREE;
7447 location_t start_loc = postfix_expression.get_start ();
7449 /* If this is a `->' operator, dereference the pointer. */
7450 if (token_type == CPP_DEREF)
7451 postfix_expression = build_x_arrow (location, postfix_expression,
7452 tf_warning_or_error);
7453 /* Check to see whether or not the expression is type-dependent and
7454 not the current instantiation. */
7455 dependent_p = type_dependent_object_expression_p (postfix_expression);
7456 /* The identifier following the `->' or `.' is not qualified. */
7457 parser->scope = NULL_TREE;
7458 parser->qualifying_scope = NULL_TREE;
7459 parser->object_scope = NULL_TREE;
7460 *idk = CP_ID_KIND_NONE;
7462 /* Enter the scope corresponding to the type of the object
7463 given by the POSTFIX_EXPRESSION. */
7464 if (!dependent_p)
7466 scope = TREE_TYPE (postfix_expression);
7467 /* According to the standard, no expression should ever have
7468 reference type. Unfortunately, we do not currently match
7469 the standard in this respect in that our internal representation
7470 of an expression may have reference type even when the standard
7471 says it does not. Therefore, we have to manually obtain the
7472 underlying type here. */
7473 scope = non_reference (scope);
7474 /* The type of the POSTFIX_EXPRESSION must be complete. */
7475 /* Unlike the object expression in other contexts, *this is not
7476 required to be of complete type for purposes of class member
7477 access (5.2.5) outside the member function body. */
7478 if (postfix_expression != current_class_ref
7479 && scope != error_mark_node
7480 && !(processing_template_decl
7481 && current_class_type
7482 && (same_type_ignoring_top_level_qualifiers_p
7483 (scope, current_class_type))))
7485 scope = complete_type (scope);
7486 if (!COMPLETE_TYPE_P (scope)
7487 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7488 &dependent_p))
7489 return error_mark_node;
7492 if (!dependent_p)
7494 /* Let the name lookup machinery know that we are processing a
7495 class member access expression. */
7496 parser->context->object_type = scope;
7497 /* If something went wrong, we want to be able to discern that case,
7498 as opposed to the case where there was no SCOPE due to the type
7499 of expression being dependent. */
7500 if (!scope)
7501 scope = error_mark_node;
7502 /* If the SCOPE was erroneous, make the various semantic analysis
7503 functions exit quickly -- and without issuing additional error
7504 messages. */
7505 if (scope == error_mark_node)
7506 postfix_expression = error_mark_node;
7510 if (dependent_p)
7511 /* Tell cp_parser_lookup_name that there was an object, even though it's
7512 type-dependent. */
7513 parser->context->object_type = unknown_type_node;
7515 /* Assume this expression is not a pseudo-destructor access. */
7516 pseudo_destructor_p = false;
7518 /* If the SCOPE is a scalar type, then, if this is a valid program,
7519 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7520 is type dependent, it can be pseudo-destructor-name or something else.
7521 Try to parse it as pseudo-destructor-name first. */
7522 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7524 tree s;
7525 tree type;
7527 cp_parser_parse_tentatively (parser);
7528 /* Parse the pseudo-destructor-name. */
7529 s = NULL_TREE;
7530 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7531 &s, &type);
7532 if (dependent_p
7533 && (cp_parser_error_occurred (parser)
7534 || !SCALAR_TYPE_P (type)))
7535 cp_parser_abort_tentative_parse (parser);
7536 else if (cp_parser_parse_definitely (parser))
7538 pseudo_destructor_p = true;
7539 postfix_expression
7540 = finish_pseudo_destructor_expr (postfix_expression,
7541 s, type, location);
7545 if (!pseudo_destructor_p)
7547 /* If the SCOPE is not a scalar type, we are looking at an
7548 ordinary class member access expression, rather than a
7549 pseudo-destructor-name. */
7550 bool template_p;
7551 cp_token *token = cp_lexer_peek_token (parser->lexer);
7552 /* Parse the id-expression. */
7553 name = (cp_parser_id_expression
7554 (parser,
7555 cp_parser_optional_template_keyword (parser),
7556 /*check_dependency_p=*/true,
7557 &template_p,
7558 /*declarator_p=*/false,
7559 /*optional_p=*/false));
7560 /* In general, build a SCOPE_REF if the member name is qualified.
7561 However, if the name was not dependent and has already been
7562 resolved; there is no need to build the SCOPE_REF. For example;
7564 struct X { void f(); };
7565 template <typename T> void f(T* t) { t->X::f(); }
7567 Even though "t" is dependent, "X::f" is not and has been resolved
7568 to a BASELINK; there is no need to include scope information. */
7570 /* But we do need to remember that there was an explicit scope for
7571 virtual function calls. */
7572 if (parser->scope)
7573 *idk = CP_ID_KIND_QUALIFIED;
7575 /* If the name is a template-id that names a type, we will get a
7576 TYPE_DECL here. That is invalid code. */
7577 if (TREE_CODE (name) == TYPE_DECL)
7579 error_at (token->location, "invalid use of %qD", name);
7580 postfix_expression = error_mark_node;
7582 else
7584 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7586 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7588 error_at (token->location, "%<%D::%D%> is not a class member",
7589 parser->scope, name);
7590 postfix_expression = error_mark_node;
7592 else
7593 name = build_qualified_name (/*type=*/NULL_TREE,
7594 parser->scope,
7595 name,
7596 template_p);
7597 parser->scope = NULL_TREE;
7598 parser->qualifying_scope = NULL_TREE;
7599 parser->object_scope = NULL_TREE;
7601 if (parser->scope && name && BASELINK_P (name))
7602 adjust_result_of_qualified_name_lookup
7603 (name, parser->scope, scope);
7604 postfix_expression
7605 = finish_class_member_access_expr (postfix_expression, name,
7606 template_p,
7607 tf_warning_or_error);
7608 /* Build a location e.g.:
7609 ptr->access_expr
7610 ~~~^~~~~~~~~~~~~
7611 where the caret is at the deref token, ranging from
7612 the start of postfix_expression to the end of the access expr. */
7613 location_t end_loc
7614 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7615 location_t combined_loc
7616 = make_location (input_location, start_loc, end_loc);
7617 protected_set_expr_location (postfix_expression, combined_loc);
7621 /* We no longer need to look up names in the scope of the object on
7622 the left-hand side of the `.' or `->' operator. */
7623 parser->context->object_type = NULL_TREE;
7625 /* Outside of offsetof, these operators may not appear in
7626 constant-expressions. */
7627 if (!for_offsetof
7628 && (cp_parser_non_integral_constant_expression
7629 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7630 postfix_expression = error_mark_node;
7632 return postfix_expression;
7635 /* Parse a parenthesized expression-list.
7637 expression-list:
7638 assignment-expression
7639 expression-list, assignment-expression
7641 attribute-list:
7642 expression-list
7643 identifier
7644 identifier, expression-list
7646 CAST_P is true if this expression is the target of a cast.
7648 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7649 argument pack.
7651 WRAP_LOCATIONS_P is true if expressions within this list for which
7652 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7653 their source locations.
7655 Returns a vector of trees. Each element is a representation of an
7656 assignment-expression. NULL is returned if the ( and or ) are
7657 missing. An empty, but allocated, vector is returned on no
7658 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7659 if we are parsing an attribute list for an attribute that wants a
7660 plain identifier argument, normal_attr for an attribute that wants
7661 an expression, or non_attr if we aren't parsing an attribute list. If
7662 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7663 not all of the expressions in the list were constant.
7664 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7665 will be written to with the location of the closing parenthesis. If
7666 an error occurs, it may or may not be written to. */
7668 static vec<tree, va_gc> *
7669 cp_parser_parenthesized_expression_list (cp_parser* parser,
7670 int is_attribute_list,
7671 bool cast_p,
7672 bool allow_expansion_p,
7673 bool *non_constant_p,
7674 location_t *close_paren_loc,
7675 bool wrap_locations_p)
7677 vec<tree, va_gc> *expression_list;
7678 bool fold_expr_p = is_attribute_list != non_attr;
7679 tree identifier = NULL_TREE;
7680 bool saved_greater_than_is_operator_p;
7682 /* Assume all the expressions will be constant. */
7683 if (non_constant_p)
7684 *non_constant_p = false;
7686 matching_parens parens;
7687 if (!parens.require_open (parser))
7688 return NULL;
7690 expression_list = make_tree_vector ();
7692 /* Within a parenthesized expression, a `>' token is always
7693 the greater-than operator. */
7694 saved_greater_than_is_operator_p
7695 = parser->greater_than_is_operator_p;
7696 parser->greater_than_is_operator_p = true;
7698 cp_expr expr (NULL_TREE);
7700 /* Consume expressions until there are no more. */
7701 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7702 while (true)
7704 /* At the beginning of attribute lists, check to see if the
7705 next token is an identifier. */
7706 if (is_attribute_list == id_attr
7707 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7709 cp_token *token;
7711 /* Consume the identifier. */
7712 token = cp_lexer_consume_token (parser->lexer);
7713 /* Save the identifier. */
7714 identifier = token->u.value;
7716 else
7718 bool expr_non_constant_p;
7720 /* Parse the next assignment-expression. */
7721 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7723 /* A braced-init-list. */
7724 cp_lexer_set_source_position (parser->lexer);
7725 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7726 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7727 if (non_constant_p && expr_non_constant_p)
7728 *non_constant_p = true;
7730 else if (non_constant_p)
7732 expr = (cp_parser_constant_expression
7733 (parser, /*allow_non_constant_p=*/true,
7734 &expr_non_constant_p));
7735 if (expr_non_constant_p)
7736 *non_constant_p = true;
7738 else
7739 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7740 cast_p);
7742 if (fold_expr_p)
7743 expr = instantiate_non_dependent_expr (expr);
7745 /* If we have an ellipsis, then this is an expression
7746 expansion. */
7747 if (allow_expansion_p
7748 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7750 /* Consume the `...'. */
7751 cp_lexer_consume_token (parser->lexer);
7753 /* Build the argument pack. */
7754 expr = make_pack_expansion (expr);
7757 if (wrap_locations_p)
7758 expr.maybe_add_location_wrapper ();
7760 /* Add it to the list. We add error_mark_node
7761 expressions to the list, so that we can still tell if
7762 the correct form for a parenthesized expression-list
7763 is found. That gives better errors. */
7764 vec_safe_push (expression_list, expr.get_value ());
7766 if (expr == error_mark_node)
7767 goto skip_comma;
7770 /* After the first item, attribute lists look the same as
7771 expression lists. */
7772 is_attribute_list = non_attr;
7774 get_comma:;
7775 /* If the next token isn't a `,', then we are done. */
7776 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7777 break;
7779 /* Otherwise, consume the `,' and keep going. */
7780 cp_lexer_consume_token (parser->lexer);
7783 if (close_paren_loc)
7784 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7786 if (!parens.require_close (parser))
7788 int ending;
7790 skip_comma:;
7791 /* We try and resync to an unnested comma, as that will give the
7792 user better diagnostics. */
7793 ending = cp_parser_skip_to_closing_parenthesis (parser,
7794 /*recovering=*/true,
7795 /*or_comma=*/true,
7796 /*consume_paren=*/true);
7797 if (ending < 0)
7798 goto get_comma;
7799 if (!ending)
7801 parser->greater_than_is_operator_p
7802 = saved_greater_than_is_operator_p;
7803 return NULL;
7807 parser->greater_than_is_operator_p
7808 = saved_greater_than_is_operator_p;
7810 if (identifier)
7811 vec_safe_insert (expression_list, 0, identifier);
7813 return expression_list;
7816 /* Parse a pseudo-destructor-name.
7818 pseudo-destructor-name:
7819 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7820 :: [opt] nested-name-specifier template template-id :: ~ type-name
7821 :: [opt] nested-name-specifier [opt] ~ type-name
7823 If either of the first two productions is used, sets *SCOPE to the
7824 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7825 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7826 or ERROR_MARK_NODE if the parse fails. */
7828 static void
7829 cp_parser_pseudo_destructor_name (cp_parser* parser,
7830 tree object,
7831 tree* scope,
7832 tree* type)
7834 bool nested_name_specifier_p;
7836 /* Handle ~auto. */
7837 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7838 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7839 && !type_dependent_expression_p (object))
7841 if (cxx_dialect < cxx14)
7842 pedwarn (input_location, 0,
7843 "%<~auto%> only available with "
7844 "-std=c++14 or -std=gnu++14");
7845 cp_lexer_consume_token (parser->lexer);
7846 cp_lexer_consume_token (parser->lexer);
7847 *scope = NULL_TREE;
7848 *type = TREE_TYPE (object);
7849 return;
7852 /* Assume that things will not work out. */
7853 *type = error_mark_node;
7855 /* Look for the optional `::' operator. */
7856 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7857 /* Look for the optional nested-name-specifier. */
7858 nested_name_specifier_p
7859 = (cp_parser_nested_name_specifier_opt (parser,
7860 /*typename_keyword_p=*/false,
7861 /*check_dependency_p=*/true,
7862 /*type_p=*/false,
7863 /*is_declaration=*/false)
7864 != NULL_TREE);
7865 /* Now, if we saw a nested-name-specifier, we might be doing the
7866 second production. */
7867 if (nested_name_specifier_p
7868 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7870 /* Consume the `template' keyword. */
7871 cp_lexer_consume_token (parser->lexer);
7872 /* Parse the template-id. */
7873 cp_parser_template_id (parser,
7874 /*template_keyword_p=*/true,
7875 /*check_dependency_p=*/false,
7876 class_type,
7877 /*is_declaration=*/true);
7878 /* Look for the `::' token. */
7879 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7881 /* If the next token is not a `~', then there might be some
7882 additional qualification. */
7883 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7885 /* At this point, we're looking for "type-name :: ~". The type-name
7886 must not be a class-name, since this is a pseudo-destructor. So,
7887 it must be either an enum-name, or a typedef-name -- both of which
7888 are just identifiers. So, we peek ahead to check that the "::"
7889 and "~" tokens are present; if they are not, then we can avoid
7890 calling type_name. */
7891 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7892 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7893 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7895 cp_parser_error (parser, "non-scalar type");
7896 return;
7899 /* Look for the type-name. */
7900 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7901 if (*scope == error_mark_node)
7902 return;
7904 /* Look for the `::' token. */
7905 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7907 else
7908 *scope = NULL_TREE;
7910 /* Look for the `~'. */
7911 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7913 /* Once we see the ~, this has to be a pseudo-destructor. */
7914 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7915 cp_parser_commit_to_topmost_tentative_parse (parser);
7917 /* Look for the type-name again. We are not responsible for
7918 checking that it matches the first type-name. */
7919 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7922 /* Parse a unary-expression.
7924 unary-expression:
7925 postfix-expression
7926 ++ cast-expression
7927 -- cast-expression
7928 unary-operator cast-expression
7929 sizeof unary-expression
7930 sizeof ( type-id )
7931 alignof ( type-id ) [C++0x]
7932 new-expression
7933 delete-expression
7935 GNU Extensions:
7937 unary-expression:
7938 __extension__ cast-expression
7939 __alignof__ unary-expression
7940 __alignof__ ( type-id )
7941 alignof unary-expression [C++0x]
7942 __real__ cast-expression
7943 __imag__ cast-expression
7944 && identifier
7945 sizeof ( type-id ) { initializer-list , [opt] }
7946 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7947 __alignof__ ( type-id ) { initializer-list , [opt] }
7949 ADDRESS_P is true iff the unary-expression is appearing as the
7950 operand of the `&' operator. CAST_P is true if this expression is
7951 the target of a cast.
7953 Returns a representation of the expression. */
7955 static cp_expr
7956 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7957 bool address_p, bool cast_p, bool decltype_p)
7959 cp_token *token;
7960 enum tree_code unary_operator;
7962 /* Peek at the next token. */
7963 token = cp_lexer_peek_token (parser->lexer);
7964 /* Some keywords give away the kind of expression. */
7965 if (token->type == CPP_KEYWORD)
7967 enum rid keyword = token->keyword;
7969 switch (keyword)
7971 case RID_ALIGNOF:
7972 case RID_SIZEOF:
7974 tree operand, ret;
7975 enum tree_code op;
7976 location_t start_loc = token->location;
7978 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7979 bool std_alignof = id_equal (token->u.value, "alignof");
7981 /* Consume the token. */
7982 cp_lexer_consume_token (parser->lexer);
7983 /* Parse the operand. */
7984 operand = cp_parser_sizeof_operand (parser, keyword);
7986 if (TYPE_P (operand))
7987 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
7988 true);
7989 else
7991 /* ISO C++ defines alignof only with types, not with
7992 expressions. So pedwarn if alignof is used with a non-
7993 type expression. However, __alignof__ is ok. */
7994 if (std_alignof)
7995 pedwarn (token->location, OPT_Wpedantic,
7996 "ISO C++ does not allow %<alignof%> "
7997 "with a non-type");
7999 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8001 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8002 SIZEOF_EXPR with the original operand. */
8003 if (op == SIZEOF_EXPR && ret != error_mark_node)
8005 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8007 if (!processing_template_decl && TYPE_P (operand))
8009 ret = build_min (SIZEOF_EXPR, size_type_node,
8010 build1 (NOP_EXPR, operand,
8011 error_mark_node));
8012 SIZEOF_EXPR_TYPE_P (ret) = 1;
8014 else
8015 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8016 TREE_SIDE_EFFECTS (ret) = 0;
8017 TREE_READONLY (ret) = 1;
8021 /* Construct a location e.g. :
8022 alignof (expr)
8023 ^~~~~~~~~~~~~~
8024 with start == caret at the start of the "alignof"/"sizeof"
8025 token, with the endpoint at the final closing paren. */
8026 location_t finish_loc
8027 = cp_lexer_previous_token (parser->lexer)->location;
8028 location_t compound_loc
8029 = make_location (start_loc, start_loc, finish_loc);
8031 cp_expr ret_expr (ret);
8032 ret_expr.set_location (compound_loc);
8033 ret_expr = ret_expr.maybe_add_location_wrapper ();
8034 return ret_expr;
8037 case RID_NEW:
8038 return cp_parser_new_expression (parser);
8040 case RID_DELETE:
8041 return cp_parser_delete_expression (parser);
8043 case RID_EXTENSION:
8045 /* The saved value of the PEDANTIC flag. */
8046 int saved_pedantic;
8047 tree expr;
8049 /* Save away the PEDANTIC flag. */
8050 cp_parser_extension_opt (parser, &saved_pedantic);
8051 /* Parse the cast-expression. */
8052 expr = cp_parser_simple_cast_expression (parser);
8053 /* Restore the PEDANTIC flag. */
8054 pedantic = saved_pedantic;
8056 return expr;
8059 case RID_REALPART:
8060 case RID_IMAGPART:
8062 tree expression;
8064 /* Consume the `__real__' or `__imag__' token. */
8065 cp_lexer_consume_token (parser->lexer);
8066 /* Parse the cast-expression. */
8067 expression = cp_parser_simple_cast_expression (parser);
8068 /* Create the complete representation. */
8069 return build_x_unary_op (token->location,
8070 (keyword == RID_REALPART
8071 ? REALPART_EXPR : IMAGPART_EXPR),
8072 expression,
8073 tf_warning_or_error);
8075 break;
8077 case RID_TRANSACTION_ATOMIC:
8078 case RID_TRANSACTION_RELAXED:
8079 return cp_parser_transaction_expression (parser, keyword);
8081 case RID_NOEXCEPT:
8083 tree expr;
8084 const char *saved_message;
8085 bool saved_integral_constant_expression_p;
8086 bool saved_non_integral_constant_expression_p;
8087 bool saved_greater_than_is_operator_p;
8089 location_t start_loc = token->location;
8091 cp_lexer_consume_token (parser->lexer);
8092 matching_parens parens;
8093 parens.require_open (parser);
8095 saved_message = parser->type_definition_forbidden_message;
8096 parser->type_definition_forbidden_message
8097 = G_("types may not be defined in %<noexcept%> expressions");
8099 saved_integral_constant_expression_p
8100 = parser->integral_constant_expression_p;
8101 saved_non_integral_constant_expression_p
8102 = parser->non_integral_constant_expression_p;
8103 parser->integral_constant_expression_p = false;
8105 saved_greater_than_is_operator_p
8106 = parser->greater_than_is_operator_p;
8107 parser->greater_than_is_operator_p = true;
8109 ++cp_unevaluated_operand;
8110 ++c_inhibit_evaluation_warnings;
8111 ++cp_noexcept_operand;
8112 expr = cp_parser_expression (parser);
8113 --cp_noexcept_operand;
8114 --c_inhibit_evaluation_warnings;
8115 --cp_unevaluated_operand;
8117 parser->greater_than_is_operator_p
8118 = saved_greater_than_is_operator_p;
8120 parser->integral_constant_expression_p
8121 = saved_integral_constant_expression_p;
8122 parser->non_integral_constant_expression_p
8123 = saved_non_integral_constant_expression_p;
8125 parser->type_definition_forbidden_message = saved_message;
8127 location_t finish_loc
8128 = cp_lexer_peek_token (parser->lexer)->location;
8129 parens.require_close (parser);
8131 /* Construct a location of the form:
8132 noexcept (expr)
8133 ^~~~~~~~~~~~~~~
8134 with start == caret, finishing at the close-paren. */
8135 location_t noexcept_loc
8136 = make_location (start_loc, start_loc, finish_loc);
8138 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8139 noexcept_loc);
8142 default:
8143 break;
8147 /* Look for the `:: new' and `:: delete', which also signal the
8148 beginning of a new-expression, or delete-expression,
8149 respectively. If the next token is `::', then it might be one of
8150 these. */
8151 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8153 enum rid keyword;
8155 /* See if the token after the `::' is one of the keywords in
8156 which we're interested. */
8157 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8158 /* If it's `new', we have a new-expression. */
8159 if (keyword == RID_NEW)
8160 return cp_parser_new_expression (parser);
8161 /* Similarly, for `delete'. */
8162 else if (keyword == RID_DELETE)
8163 return cp_parser_delete_expression (parser);
8166 /* Look for a unary operator. */
8167 unary_operator = cp_parser_unary_operator (token);
8168 /* The `++' and `--' operators can be handled similarly, even though
8169 they are not technically unary-operators in the grammar. */
8170 if (unary_operator == ERROR_MARK)
8172 if (token->type == CPP_PLUS_PLUS)
8173 unary_operator = PREINCREMENT_EXPR;
8174 else if (token->type == CPP_MINUS_MINUS)
8175 unary_operator = PREDECREMENT_EXPR;
8176 /* Handle the GNU address-of-label extension. */
8177 else if (cp_parser_allow_gnu_extensions_p (parser)
8178 && token->type == CPP_AND_AND)
8180 tree identifier;
8181 tree expression;
8182 location_t start_loc = token->location;
8184 /* Consume the '&&' token. */
8185 cp_lexer_consume_token (parser->lexer);
8186 /* Look for the identifier. */
8187 location_t finish_loc
8188 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8189 identifier = cp_parser_identifier (parser);
8190 /* Construct a location of the form:
8191 &&label
8192 ^~~~~~~
8193 with caret==start at the "&&", finish at the end of the label. */
8194 location_t combined_loc
8195 = make_location (start_loc, start_loc, finish_loc);
8196 /* Create an expression representing the address. */
8197 expression = finish_label_address_expr (identifier, combined_loc);
8198 if (cp_parser_non_integral_constant_expression (parser,
8199 NIC_ADDR_LABEL))
8200 expression = error_mark_node;
8201 return expression;
8204 if (unary_operator != ERROR_MARK)
8206 cp_expr cast_expression;
8207 cp_expr expression = error_mark_node;
8208 non_integral_constant non_constant_p = NIC_NONE;
8209 location_t loc = token->location;
8210 tsubst_flags_t complain = complain_flags (decltype_p);
8212 /* Consume the operator token. */
8213 token = cp_lexer_consume_token (parser->lexer);
8214 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8216 /* Parse the cast-expression. */
8217 cast_expression
8218 = cp_parser_cast_expression (parser,
8219 unary_operator == ADDR_EXPR,
8220 /*cast_p=*/false,
8221 /*decltype*/false,
8222 pidk);
8224 /* Make a location:
8225 OP_TOKEN CAST_EXPRESSION
8226 ^~~~~~~~~~~~~~~~~~~~~~~~~
8227 with start==caret at the operator token, and
8228 extending to the end of the cast_expression. */
8229 loc = make_location (loc, loc, cast_expression.get_finish ());
8231 /* Now, build an appropriate representation. */
8232 switch (unary_operator)
8234 case INDIRECT_REF:
8235 non_constant_p = NIC_STAR;
8236 expression = build_x_indirect_ref (loc, cast_expression,
8237 RO_UNARY_STAR,
8238 complain);
8239 /* TODO: build_x_indirect_ref does not always honor the
8240 location, so ensure it is set. */
8241 expression.set_location (loc);
8242 break;
8244 case ADDR_EXPR:
8245 non_constant_p = NIC_ADDR;
8246 /* Fall through. */
8247 case BIT_NOT_EXPR:
8248 expression = build_x_unary_op (loc, unary_operator,
8249 cast_expression,
8250 complain);
8251 /* TODO: build_x_unary_op does not always honor the location,
8252 so ensure it is set. */
8253 expression.set_location (loc);
8254 break;
8256 case PREINCREMENT_EXPR:
8257 case PREDECREMENT_EXPR:
8258 non_constant_p = unary_operator == PREINCREMENT_EXPR
8259 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8260 /* Fall through. */
8261 case NEGATE_EXPR:
8262 /* Immediately fold negation of a constant, unless the constant is 0
8263 (since -0 == 0) or it would overflow. */
8264 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8265 && CONSTANT_CLASS_P (cast_expression)
8266 && !integer_zerop (cast_expression)
8267 && !TREE_OVERFLOW (cast_expression))
8269 tree folded = fold_build1 (unary_operator,
8270 TREE_TYPE (cast_expression),
8271 cast_expression);
8272 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8274 expression = cp_expr (folded, loc);
8275 break;
8278 /* Fall through. */
8279 case UNARY_PLUS_EXPR:
8280 case TRUTH_NOT_EXPR:
8281 expression = finish_unary_op_expr (loc, unary_operator,
8282 cast_expression, complain);
8283 break;
8285 default:
8286 gcc_unreachable ();
8289 if (non_constant_p != NIC_NONE
8290 && cp_parser_non_integral_constant_expression (parser,
8291 non_constant_p))
8292 expression = error_mark_node;
8294 return expression;
8297 return cp_parser_postfix_expression (parser, address_p, cast_p,
8298 /*member_access_only_p=*/false,
8299 decltype_p,
8300 pidk);
8303 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8304 unary-operator, the corresponding tree code is returned. */
8306 static enum tree_code
8307 cp_parser_unary_operator (cp_token* token)
8309 switch (token->type)
8311 case CPP_MULT:
8312 return INDIRECT_REF;
8314 case CPP_AND:
8315 return ADDR_EXPR;
8317 case CPP_PLUS:
8318 return UNARY_PLUS_EXPR;
8320 case CPP_MINUS:
8321 return NEGATE_EXPR;
8323 case CPP_NOT:
8324 return TRUTH_NOT_EXPR;
8326 case CPP_COMPL:
8327 return BIT_NOT_EXPR;
8329 default:
8330 return ERROR_MARK;
8334 /* Parse a new-expression.
8336 new-expression:
8337 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8338 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8340 Returns a representation of the expression. */
8342 static tree
8343 cp_parser_new_expression (cp_parser* parser)
8345 bool global_scope_p;
8346 vec<tree, va_gc> *placement;
8347 tree type;
8348 vec<tree, va_gc> *initializer;
8349 tree nelts = NULL_TREE;
8350 tree ret;
8352 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8354 /* Look for the optional `::' operator. */
8355 global_scope_p
8356 = (cp_parser_global_scope_opt (parser,
8357 /*current_scope_valid_p=*/false)
8358 != NULL_TREE);
8359 /* Look for the `new' operator. */
8360 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8361 /* There's no easy way to tell a new-placement from the
8362 `( type-id )' construct. */
8363 cp_parser_parse_tentatively (parser);
8364 /* Look for a new-placement. */
8365 placement = cp_parser_new_placement (parser);
8366 /* If that didn't work out, there's no new-placement. */
8367 if (!cp_parser_parse_definitely (parser))
8369 if (placement != NULL)
8370 release_tree_vector (placement);
8371 placement = NULL;
8374 /* If the next token is a `(', then we have a parenthesized
8375 type-id. */
8376 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8378 cp_token *token;
8379 const char *saved_message = parser->type_definition_forbidden_message;
8381 /* Consume the `('. */
8382 matching_parens parens;
8383 parens.consume_open (parser);
8385 /* Parse the type-id. */
8386 parser->type_definition_forbidden_message
8387 = G_("types may not be defined in a new-expression");
8389 type_id_in_expr_sentinel s (parser);
8390 type = cp_parser_type_id (parser);
8392 parser->type_definition_forbidden_message = saved_message;
8394 /* Look for the closing `)'. */
8395 parens.require_close (parser);
8396 token = cp_lexer_peek_token (parser->lexer);
8397 /* There should not be a direct-new-declarator in this production,
8398 but GCC used to allowed this, so we check and emit a sensible error
8399 message for this case. */
8400 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8402 error_at (token->location,
8403 "array bound forbidden after parenthesized type-id");
8404 inform (token->location,
8405 "try removing the parentheses around the type-id");
8406 cp_parser_direct_new_declarator (parser);
8409 /* Otherwise, there must be a new-type-id. */
8410 else
8411 type = cp_parser_new_type_id (parser, &nelts);
8413 /* If the next token is a `(' or '{', then we have a new-initializer. */
8414 cp_token *token = cp_lexer_peek_token (parser->lexer);
8415 if (token->type == CPP_OPEN_PAREN
8416 || token->type == CPP_OPEN_BRACE)
8417 initializer = cp_parser_new_initializer (parser);
8418 else
8419 initializer = NULL;
8421 /* A new-expression may not appear in an integral constant
8422 expression. */
8423 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8424 ret = error_mark_node;
8425 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8426 of a new-type-id or type-id of a new-expression, the new-expression shall
8427 contain a new-initializer of the form ( assignment-expression )".
8428 Additionally, consistently with the spirit of DR 1467, we want to accept
8429 'new auto { 2 }' too. */
8430 else if ((ret = type_uses_auto (type))
8431 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8432 && (vec_safe_length (initializer) != 1
8433 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8434 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8436 error_at (token->location,
8437 "initialization of new-expression for type %<auto%> "
8438 "requires exactly one element");
8439 ret = error_mark_node;
8441 else
8443 /* Construct a location e.g.:
8444 ptr = new int[100]
8445 ^~~~~~~~~~~~
8446 with caret == start at the start of the "new" token, and the end
8447 at the end of the final token we consumed. */
8448 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8449 location_t end_loc = get_finish (end_tok->location);
8450 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8452 /* Create a representation of the new-expression. */
8453 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8454 tf_warning_or_error);
8455 protected_set_expr_location (ret, combined_loc);
8458 if (placement != NULL)
8459 release_tree_vector (placement);
8460 if (initializer != NULL)
8461 release_tree_vector (initializer);
8463 return ret;
8466 /* Parse a new-placement.
8468 new-placement:
8469 ( expression-list )
8471 Returns the same representation as for an expression-list. */
8473 static vec<tree, va_gc> *
8474 cp_parser_new_placement (cp_parser* parser)
8476 vec<tree, va_gc> *expression_list;
8478 /* Parse the expression-list. */
8479 expression_list = (cp_parser_parenthesized_expression_list
8480 (parser, non_attr, /*cast_p=*/false,
8481 /*allow_expansion_p=*/true,
8482 /*non_constant_p=*/NULL));
8484 if (expression_list && expression_list->is_empty ())
8485 error ("expected expression-list or type-id");
8487 return expression_list;
8490 /* Parse a new-type-id.
8492 new-type-id:
8493 type-specifier-seq new-declarator [opt]
8495 Returns the TYPE allocated. If the new-type-id indicates an array
8496 type, *NELTS is set to the number of elements in the last array
8497 bound; the TYPE will not include the last array bound. */
8499 static tree
8500 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8502 cp_decl_specifier_seq type_specifier_seq;
8503 cp_declarator *new_declarator;
8504 cp_declarator *declarator;
8505 cp_declarator *outer_declarator;
8506 const char *saved_message;
8508 /* The type-specifier sequence must not contain type definitions.
8509 (It cannot contain declarations of new types either, but if they
8510 are not definitions we will catch that because they are not
8511 complete.) */
8512 saved_message = parser->type_definition_forbidden_message;
8513 parser->type_definition_forbidden_message
8514 = G_("types may not be defined in a new-type-id");
8515 /* Parse the type-specifier-seq. */
8516 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8517 /*is_trailing_return=*/false,
8518 &type_specifier_seq);
8519 /* Restore the old message. */
8520 parser->type_definition_forbidden_message = saved_message;
8522 if (type_specifier_seq.type == error_mark_node)
8523 return error_mark_node;
8525 /* Parse the new-declarator. */
8526 new_declarator = cp_parser_new_declarator_opt (parser);
8528 /* Determine the number of elements in the last array dimension, if
8529 any. */
8530 *nelts = NULL_TREE;
8531 /* Skip down to the last array dimension. */
8532 declarator = new_declarator;
8533 outer_declarator = NULL;
8534 while (declarator && (declarator->kind == cdk_pointer
8535 || declarator->kind == cdk_ptrmem))
8537 outer_declarator = declarator;
8538 declarator = declarator->declarator;
8540 while (declarator
8541 && declarator->kind == cdk_array
8542 && declarator->declarator
8543 && declarator->declarator->kind == cdk_array)
8545 outer_declarator = declarator;
8546 declarator = declarator->declarator;
8549 if (declarator && declarator->kind == cdk_array)
8551 *nelts = declarator->u.array.bounds;
8552 if (*nelts == error_mark_node)
8553 *nelts = integer_one_node;
8555 if (outer_declarator)
8556 outer_declarator->declarator = declarator->declarator;
8557 else
8558 new_declarator = NULL;
8561 return groktypename (&type_specifier_seq, new_declarator, false);
8564 /* Parse an (optional) new-declarator.
8566 new-declarator:
8567 ptr-operator new-declarator [opt]
8568 direct-new-declarator
8570 Returns the declarator. */
8572 static cp_declarator *
8573 cp_parser_new_declarator_opt (cp_parser* parser)
8575 enum tree_code code;
8576 tree type, std_attributes = NULL_TREE;
8577 cp_cv_quals cv_quals;
8579 /* We don't know if there's a ptr-operator next, or not. */
8580 cp_parser_parse_tentatively (parser);
8581 /* Look for a ptr-operator. */
8582 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8583 /* If that worked, look for more new-declarators. */
8584 if (cp_parser_parse_definitely (parser))
8586 cp_declarator *declarator;
8588 /* Parse another optional declarator. */
8589 declarator = cp_parser_new_declarator_opt (parser);
8591 declarator = cp_parser_make_indirect_declarator
8592 (code, type, cv_quals, declarator, std_attributes);
8594 return declarator;
8597 /* If the next token is a `[', there is a direct-new-declarator. */
8598 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8599 return cp_parser_direct_new_declarator (parser);
8601 return NULL;
8604 /* Parse a direct-new-declarator.
8606 direct-new-declarator:
8607 [ expression ]
8608 direct-new-declarator [constant-expression]
8612 static cp_declarator *
8613 cp_parser_direct_new_declarator (cp_parser* parser)
8615 cp_declarator *declarator = NULL;
8617 while (true)
8619 tree expression;
8620 cp_token *token;
8622 /* Look for the opening `['. */
8623 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8625 token = cp_lexer_peek_token (parser->lexer);
8626 expression = cp_parser_expression (parser);
8627 /* The standard requires that the expression have integral
8628 type. DR 74 adds enumeration types. We believe that the
8629 real intent is that these expressions be handled like the
8630 expression in a `switch' condition, which also allows
8631 classes with a single conversion to integral or
8632 enumeration type. */
8633 if (!processing_template_decl)
8635 expression
8636 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8637 expression,
8638 /*complain=*/true);
8639 if (!expression)
8641 error_at (token->location,
8642 "expression in new-declarator must have integral "
8643 "or enumeration type");
8644 expression = error_mark_node;
8648 /* Look for the closing `]'. */
8649 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8651 /* Add this bound to the declarator. */
8652 declarator = make_array_declarator (declarator, expression);
8654 /* If the next token is not a `[', then there are no more
8655 bounds. */
8656 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8657 break;
8660 return declarator;
8663 /* Parse a new-initializer.
8665 new-initializer:
8666 ( expression-list [opt] )
8667 braced-init-list
8669 Returns a representation of the expression-list. */
8671 static vec<tree, va_gc> *
8672 cp_parser_new_initializer (cp_parser* parser)
8674 vec<tree, va_gc> *expression_list;
8676 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8678 tree t;
8679 bool expr_non_constant_p;
8680 cp_lexer_set_source_position (parser->lexer);
8681 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8682 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8683 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8684 expression_list = make_tree_vector_single (t);
8686 else
8687 expression_list = (cp_parser_parenthesized_expression_list
8688 (parser, non_attr, /*cast_p=*/false,
8689 /*allow_expansion_p=*/true,
8690 /*non_constant_p=*/NULL));
8692 return expression_list;
8695 /* Parse a delete-expression.
8697 delete-expression:
8698 :: [opt] delete cast-expression
8699 :: [opt] delete [ ] cast-expression
8701 Returns a representation of the expression. */
8703 static tree
8704 cp_parser_delete_expression (cp_parser* parser)
8706 bool global_scope_p;
8707 bool array_p;
8708 tree expression;
8710 /* Look for the optional `::' operator. */
8711 global_scope_p
8712 = (cp_parser_global_scope_opt (parser,
8713 /*current_scope_valid_p=*/false)
8714 != NULL_TREE);
8715 /* Look for the `delete' keyword. */
8716 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8717 /* See if the array syntax is in use. */
8718 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8720 /* Consume the `[' token. */
8721 cp_lexer_consume_token (parser->lexer);
8722 /* Look for the `]' token. */
8723 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8724 /* Remember that this is the `[]' construct. */
8725 array_p = true;
8727 else
8728 array_p = false;
8730 /* Parse the cast-expression. */
8731 expression = cp_parser_simple_cast_expression (parser);
8733 /* A delete-expression may not appear in an integral constant
8734 expression. */
8735 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8736 return error_mark_node;
8738 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8739 tf_warning_or_error);
8742 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8743 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8744 0 otherwise. */
8746 static int
8747 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8749 cp_token *token = cp_lexer_peek_token (parser->lexer);
8750 switch (token->type)
8752 case CPP_COMMA:
8753 case CPP_SEMICOLON:
8754 case CPP_QUERY:
8755 case CPP_COLON:
8756 case CPP_CLOSE_SQUARE:
8757 case CPP_CLOSE_PAREN:
8758 case CPP_CLOSE_BRACE:
8759 case CPP_OPEN_BRACE:
8760 case CPP_DOT:
8761 case CPP_DOT_STAR:
8762 case CPP_DEREF:
8763 case CPP_DEREF_STAR:
8764 case CPP_DIV:
8765 case CPP_MOD:
8766 case CPP_LSHIFT:
8767 case CPP_RSHIFT:
8768 case CPP_LESS:
8769 case CPP_GREATER:
8770 case CPP_LESS_EQ:
8771 case CPP_GREATER_EQ:
8772 case CPP_EQ_EQ:
8773 case CPP_NOT_EQ:
8774 case CPP_EQ:
8775 case CPP_MULT_EQ:
8776 case CPP_DIV_EQ:
8777 case CPP_MOD_EQ:
8778 case CPP_PLUS_EQ:
8779 case CPP_MINUS_EQ:
8780 case CPP_RSHIFT_EQ:
8781 case CPP_LSHIFT_EQ:
8782 case CPP_AND_EQ:
8783 case CPP_XOR_EQ:
8784 case CPP_OR_EQ:
8785 case CPP_XOR:
8786 case CPP_OR:
8787 case CPP_OR_OR:
8788 case CPP_EOF:
8789 case CPP_ELLIPSIS:
8790 return 0;
8792 case CPP_OPEN_PAREN:
8793 /* In ((type ()) () the last () isn't a valid cast-expression,
8794 so the whole must be parsed as postfix-expression. */
8795 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8796 != CPP_CLOSE_PAREN;
8798 case CPP_OPEN_SQUARE:
8799 /* '[' may start a primary-expression in obj-c++ and in C++11,
8800 as a lambda-expression, eg, '(void)[]{}'. */
8801 if (cxx_dialect >= cxx11)
8802 return -1;
8803 return c_dialect_objc ();
8805 case CPP_PLUS_PLUS:
8806 case CPP_MINUS_MINUS:
8807 /* '++' and '--' may or may not start a cast-expression:
8809 struct T { void operator++(int); };
8810 void f() { (T())++; }
8814 int a;
8815 (int)++a; */
8816 return -1;
8818 default:
8819 return 1;
8823 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8824 in the order: const_cast, static_cast, reinterpret_cast.
8826 Don't suggest dynamic_cast.
8828 Return the first legal cast kind found, or NULL otherwise. */
8830 static const char *
8831 get_cast_suggestion (tree dst_type, tree orig_expr)
8833 tree trial;
8835 /* Reuse the parser logic by attempting to build the various kinds of
8836 cast, with "complain" disabled.
8837 Identify the first such cast that is valid. */
8839 /* Don't attempt to run such logic within template processing. */
8840 if (processing_template_decl)
8841 return NULL;
8843 /* First try const_cast. */
8844 trial = build_const_cast (dst_type, orig_expr, tf_none);
8845 if (trial != error_mark_node)
8846 return "const_cast";
8848 /* If that fails, try static_cast. */
8849 trial = build_static_cast (dst_type, orig_expr, tf_none);
8850 if (trial != error_mark_node)
8851 return "static_cast";
8853 /* Finally, try reinterpret_cast. */
8854 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8855 if (trial != error_mark_node)
8856 return "reinterpret_cast";
8858 /* No such cast possible. */
8859 return NULL;
8862 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8863 suggesting how to convert a C-style cast of the form:
8865 (DST_TYPE)ORIG_EXPR
8867 to a C++-style cast.
8869 The primary range of RICHLOC is asssumed to be that of the original
8870 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8871 of the parens in the C-style cast. */
8873 static void
8874 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8875 location_t close_paren_loc, tree orig_expr,
8876 tree dst_type)
8878 /* This function is non-trivial, so bail out now if the warning isn't
8879 going to be emitted. */
8880 if (!warn_old_style_cast)
8881 return;
8883 /* Try to find a legal C++ cast, trying them in order:
8884 const_cast, static_cast, reinterpret_cast. */
8885 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8886 if (!cast_suggestion)
8887 return;
8889 /* Replace the open paren with "CAST_SUGGESTION<". */
8890 pretty_printer pp;
8891 pp_printf (&pp, "%s<", cast_suggestion);
8892 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8894 /* Replace the close paren with "> (". */
8895 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8897 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8898 rich_loc->add_fixit_insert_after (")");
8902 /* Parse a cast-expression.
8904 cast-expression:
8905 unary-expression
8906 ( type-id ) cast-expression
8908 ADDRESS_P is true iff the unary-expression is appearing as the
8909 operand of the `&' operator. CAST_P is true if this expression is
8910 the target of a cast.
8912 Returns a representation of the expression. */
8914 static cp_expr
8915 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8916 bool decltype_p, cp_id_kind * pidk)
8918 /* If it's a `(', then we might be looking at a cast. */
8919 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8921 tree type = NULL_TREE;
8922 cp_expr expr (NULL_TREE);
8923 int cast_expression = 0;
8924 const char *saved_message;
8926 /* There's no way to know yet whether or not this is a cast.
8927 For example, `(int (3))' is a unary-expression, while `(int)
8928 3' is a cast. So, we resort to parsing tentatively. */
8929 cp_parser_parse_tentatively (parser);
8930 /* Types may not be defined in a cast. */
8931 saved_message = parser->type_definition_forbidden_message;
8932 parser->type_definition_forbidden_message
8933 = G_("types may not be defined in casts");
8934 /* Consume the `('. */
8935 matching_parens parens;
8936 cp_token *open_paren = parens.consume_open (parser);
8937 location_t open_paren_loc = open_paren->location;
8938 location_t close_paren_loc = UNKNOWN_LOCATION;
8940 /* A very tricky bit is that `(struct S) { 3 }' is a
8941 compound-literal (which we permit in C++ as an extension).
8942 But, that construct is not a cast-expression -- it is a
8943 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8944 is legal; if the compound-literal were a cast-expression,
8945 you'd need an extra set of parentheses.) But, if we parse
8946 the type-id, and it happens to be a class-specifier, then we
8947 will commit to the parse at that point, because we cannot
8948 undo the action that is done when creating a new class. So,
8949 then we cannot back up and do a postfix-expression.
8951 Another tricky case is the following (c++/29234):
8953 struct S { void operator () (); };
8955 void foo ()
8957 ( S()() );
8960 As a type-id we parse the parenthesized S()() as a function
8961 returning a function, groktypename complains and we cannot
8962 back up in this case either.
8964 Therefore, we scan ahead to the closing `)', and check to see
8965 if the tokens after the `)' can start a cast-expression. Otherwise
8966 we are dealing with an unary-expression, a postfix-expression
8967 or something else.
8969 Yet another tricky case, in C++11, is the following (c++/54891):
8971 (void)[]{};
8973 The issue is that usually, besides the case of lambda-expressions,
8974 the parenthesized type-id cannot be followed by '[', and, eg, we
8975 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8976 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8977 we don't commit, we try a cast-expression, then an unary-expression.
8979 Save tokens so that we can put them back. */
8980 cp_lexer_save_tokens (parser->lexer);
8982 /* We may be looking at a cast-expression. */
8983 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8984 /*consume_paren=*/true))
8985 cast_expression
8986 = cp_parser_tokens_start_cast_expression (parser);
8988 /* Roll back the tokens we skipped. */
8989 cp_lexer_rollback_tokens (parser->lexer);
8990 /* If we aren't looking at a cast-expression, simulate an error so
8991 that the call to cp_parser_error_occurred below returns true. */
8992 if (!cast_expression)
8993 cp_parser_simulate_error (parser);
8994 else
8996 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8997 parser->in_type_id_in_expr_p = true;
8998 /* Look for the type-id. */
8999 type = cp_parser_type_id (parser);
9000 /* Look for the closing `)'. */
9001 cp_token *close_paren = parens.require_close (parser);
9002 if (close_paren)
9003 close_paren_loc = close_paren->location;
9004 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9007 /* Restore the saved message. */
9008 parser->type_definition_forbidden_message = saved_message;
9010 /* At this point this can only be either a cast or a
9011 parenthesized ctor such as `(T ())' that looks like a cast to
9012 function returning T. */
9013 if (!cp_parser_error_occurred (parser))
9015 /* Only commit if the cast-expression doesn't start with
9016 '++', '--', or '[' in C++11. */
9017 if (cast_expression > 0)
9018 cp_parser_commit_to_topmost_tentative_parse (parser);
9020 expr = cp_parser_cast_expression (parser,
9021 /*address_p=*/false,
9022 /*cast_p=*/true,
9023 /*decltype_p=*/false,
9024 pidk);
9026 if (cp_parser_parse_definitely (parser))
9028 /* Warn about old-style casts, if so requested. */
9029 if (warn_old_style_cast
9030 && !in_system_header_at (input_location)
9031 && !VOID_TYPE_P (type)
9032 && current_lang_name != lang_name_c)
9034 gcc_rich_location rich_loc (input_location);
9035 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9036 expr, type);
9037 warning_at (&rich_loc, OPT_Wold_style_cast,
9038 "use of old-style cast to %q#T", type);
9041 /* Only type conversions to integral or enumeration types
9042 can be used in constant-expressions. */
9043 if (!cast_valid_in_integral_constant_expression_p (type)
9044 && cp_parser_non_integral_constant_expression (parser,
9045 NIC_CAST))
9046 return error_mark_node;
9048 /* Perform the cast. */
9049 /* Make a location:
9050 (TYPE) EXPR
9051 ^~~~~~~~~~~
9052 with start==caret at the open paren, extending to the
9053 end of "expr". */
9054 location_t cast_loc = make_location (open_paren_loc,
9055 open_paren_loc,
9056 expr.get_finish ());
9057 expr = build_c_cast (cast_loc, type, expr);
9058 return expr;
9061 else
9062 cp_parser_abort_tentative_parse (parser);
9065 /* If we get here, then it's not a cast, so it must be a
9066 unary-expression. */
9067 return cp_parser_unary_expression (parser, pidk, address_p,
9068 cast_p, decltype_p);
9071 /* Parse a binary expression of the general form:
9073 pm-expression:
9074 cast-expression
9075 pm-expression .* cast-expression
9076 pm-expression ->* cast-expression
9078 multiplicative-expression:
9079 pm-expression
9080 multiplicative-expression * pm-expression
9081 multiplicative-expression / pm-expression
9082 multiplicative-expression % pm-expression
9084 additive-expression:
9085 multiplicative-expression
9086 additive-expression + multiplicative-expression
9087 additive-expression - multiplicative-expression
9089 shift-expression:
9090 additive-expression
9091 shift-expression << additive-expression
9092 shift-expression >> additive-expression
9094 relational-expression:
9095 shift-expression
9096 relational-expression < shift-expression
9097 relational-expression > shift-expression
9098 relational-expression <= shift-expression
9099 relational-expression >= shift-expression
9101 GNU Extension:
9103 relational-expression:
9104 relational-expression <? shift-expression
9105 relational-expression >? shift-expression
9107 equality-expression:
9108 relational-expression
9109 equality-expression == relational-expression
9110 equality-expression != relational-expression
9112 and-expression:
9113 equality-expression
9114 and-expression & equality-expression
9116 exclusive-or-expression:
9117 and-expression
9118 exclusive-or-expression ^ and-expression
9120 inclusive-or-expression:
9121 exclusive-or-expression
9122 inclusive-or-expression | exclusive-or-expression
9124 logical-and-expression:
9125 inclusive-or-expression
9126 logical-and-expression && inclusive-or-expression
9128 logical-or-expression:
9129 logical-and-expression
9130 logical-or-expression || logical-and-expression
9132 All these are implemented with a single function like:
9134 binary-expression:
9135 simple-cast-expression
9136 binary-expression <token> binary-expression
9138 CAST_P is true if this expression is the target of a cast.
9140 The binops_by_token map is used to get the tree codes for each <token> type.
9141 binary-expressions are associated according to a precedence table. */
9143 #define TOKEN_PRECEDENCE(token) \
9144 (((token->type == CPP_GREATER \
9145 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9146 && !parser->greater_than_is_operator_p) \
9147 ? PREC_NOT_OPERATOR \
9148 : binops_by_token[token->type].prec)
9150 static cp_expr
9151 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9152 bool no_toplevel_fold_p,
9153 bool decltype_p,
9154 enum cp_parser_prec prec,
9155 cp_id_kind * pidk)
9157 cp_parser_expression_stack stack;
9158 cp_parser_expression_stack_entry *sp = &stack[0];
9159 cp_parser_expression_stack_entry current;
9160 cp_expr rhs;
9161 cp_token *token;
9162 enum tree_code rhs_type;
9163 enum cp_parser_prec new_prec, lookahead_prec;
9164 tree overload;
9166 /* Parse the first expression. */
9167 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9168 ? TRUTH_NOT_EXPR : ERROR_MARK);
9169 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9170 cast_p, decltype_p, pidk);
9171 current.prec = prec;
9173 if (cp_parser_error_occurred (parser))
9174 return error_mark_node;
9176 for (;;)
9178 /* Get an operator token. */
9179 token = cp_lexer_peek_token (parser->lexer);
9181 if (warn_cxx11_compat
9182 && token->type == CPP_RSHIFT
9183 && !parser->greater_than_is_operator_p)
9185 if (warning_at (token->location, OPT_Wc__11_compat,
9186 "%<>>%> operator is treated"
9187 " as two right angle brackets in C++11"))
9188 inform (token->location,
9189 "suggest parentheses around %<>>%> expression");
9192 new_prec = TOKEN_PRECEDENCE (token);
9193 if (new_prec != PREC_NOT_OPERATOR
9194 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9195 /* This is a fold-expression; handle it later. */
9196 new_prec = PREC_NOT_OPERATOR;
9198 /* Popping an entry off the stack means we completed a subexpression:
9199 - either we found a token which is not an operator (`>' where it is not
9200 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9201 will happen repeatedly;
9202 - or, we found an operator which has lower priority. This is the case
9203 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9204 parsing `3 * 4'. */
9205 if (new_prec <= current.prec)
9207 if (sp == stack)
9208 break;
9209 else
9210 goto pop;
9213 get_rhs:
9214 current.tree_type = binops_by_token[token->type].tree_type;
9215 current.loc = token->location;
9217 /* We used the operator token. */
9218 cp_lexer_consume_token (parser->lexer);
9220 /* For "false && x" or "true || x", x will never be executed;
9221 disable warnings while evaluating it. */
9222 if (current.tree_type == TRUTH_ANDIF_EXPR)
9223 c_inhibit_evaluation_warnings +=
9224 cp_fully_fold (current.lhs) == truthvalue_false_node;
9225 else if (current.tree_type == TRUTH_ORIF_EXPR)
9226 c_inhibit_evaluation_warnings +=
9227 cp_fully_fold (current.lhs) == truthvalue_true_node;
9229 /* Extract another operand. It may be the RHS of this expression
9230 or the LHS of a new, higher priority expression. */
9231 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9232 ? TRUTH_NOT_EXPR : ERROR_MARK);
9233 rhs = cp_parser_simple_cast_expression (parser);
9235 /* Get another operator token. Look up its precedence to avoid
9236 building a useless (immediately popped) stack entry for common
9237 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9238 token = cp_lexer_peek_token (parser->lexer);
9239 lookahead_prec = TOKEN_PRECEDENCE (token);
9240 if (lookahead_prec != PREC_NOT_OPERATOR
9241 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9242 lookahead_prec = PREC_NOT_OPERATOR;
9243 if (lookahead_prec > new_prec)
9245 /* ... and prepare to parse the RHS of the new, higher priority
9246 expression. Since precedence levels on the stack are
9247 monotonically increasing, we do not have to care about
9248 stack overflows. */
9249 *sp = current;
9250 ++sp;
9251 current.lhs = rhs;
9252 current.lhs_type = rhs_type;
9253 current.prec = new_prec;
9254 new_prec = lookahead_prec;
9255 goto get_rhs;
9257 pop:
9258 lookahead_prec = new_prec;
9259 /* If the stack is not empty, we have parsed into LHS the right side
9260 (`4' in the example above) of an expression we had suspended.
9261 We can use the information on the stack to recover the LHS (`3')
9262 from the stack together with the tree code (`MULT_EXPR'), and
9263 the precedence of the higher level subexpression
9264 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9265 which will be used to actually build the additive expression. */
9266 rhs = current.lhs;
9267 rhs_type = current.lhs_type;
9268 --sp;
9269 current = *sp;
9272 /* Undo the disabling of warnings done above. */
9273 if (current.tree_type == TRUTH_ANDIF_EXPR)
9274 c_inhibit_evaluation_warnings -=
9275 cp_fully_fold (current.lhs) == truthvalue_false_node;
9276 else if (current.tree_type == TRUTH_ORIF_EXPR)
9277 c_inhibit_evaluation_warnings -=
9278 cp_fully_fold (current.lhs) == truthvalue_true_node;
9280 if (warn_logical_not_paren
9281 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9282 && current.lhs_type == TRUTH_NOT_EXPR
9283 /* Avoid warning for !!x == y. */
9284 && (TREE_CODE (current.lhs) != NE_EXPR
9285 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9286 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9287 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9288 /* Avoid warning for !b == y where b is boolean. */
9289 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9290 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9291 != BOOLEAN_TYPE))))
9292 /* Avoid warning for !!b == y where b is boolean. */
9293 && (!DECL_P (current.lhs)
9294 || TREE_TYPE (current.lhs) == NULL_TREE
9295 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9296 warn_logical_not_parentheses (current.loc, current.tree_type,
9297 current.lhs, maybe_constant_value (rhs));
9299 overload = NULL;
9301 location_t combined_loc = make_location (current.loc,
9302 current.lhs.get_start (),
9303 rhs.get_finish ());
9305 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9306 ERROR_MARK for everything that is not a binary expression.
9307 This makes warn_about_parentheses miss some warnings that
9308 involve unary operators. For unary expressions we should
9309 pass the correct tree_code unless the unary expression was
9310 surrounded by parentheses.
9312 if (no_toplevel_fold_p
9313 && lookahead_prec <= current.prec
9314 && sp == stack)
9316 if (current.lhs == error_mark_node || rhs == error_mark_node)
9317 current.lhs = error_mark_node;
9318 else
9320 current.lhs
9321 = build_min (current.tree_type,
9322 TREE_CODE_CLASS (current.tree_type)
9323 == tcc_comparison
9324 ? boolean_type_node : TREE_TYPE (current.lhs),
9325 current.lhs.get_value (), rhs.get_value ());
9326 SET_EXPR_LOCATION (current.lhs, combined_loc);
9329 else
9331 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9332 current.lhs, current.lhs_type,
9333 rhs, rhs_type, &overload,
9334 complain_flags (decltype_p));
9335 /* TODO: build_x_binary_op doesn't always honor the location. */
9336 current.lhs.set_location (combined_loc);
9338 current.lhs_type = current.tree_type;
9340 /* If the binary operator required the use of an overloaded operator,
9341 then this expression cannot be an integral constant-expression.
9342 An overloaded operator can be used even if both operands are
9343 otherwise permissible in an integral constant-expression if at
9344 least one of the operands is of enumeration type. */
9346 if (overload
9347 && cp_parser_non_integral_constant_expression (parser,
9348 NIC_OVERLOADED))
9349 return error_mark_node;
9352 return current.lhs;
9355 static cp_expr
9356 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9357 bool no_toplevel_fold_p,
9358 enum cp_parser_prec prec,
9359 cp_id_kind * pidk)
9361 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9362 /*decltype*/false, prec, pidk);
9365 /* Parse the `? expression : assignment-expression' part of a
9366 conditional-expression. The LOGICAL_OR_EXPR is the
9367 logical-or-expression that started the conditional-expression.
9368 Returns a representation of the entire conditional-expression.
9370 This routine is used by cp_parser_assignment_expression.
9372 ? expression : assignment-expression
9374 GNU Extensions:
9376 ? : assignment-expression */
9378 static tree
9379 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9381 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9382 cp_expr assignment_expr;
9383 struct cp_token *token;
9384 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9386 /* Consume the `?' token. */
9387 cp_lexer_consume_token (parser->lexer);
9388 token = cp_lexer_peek_token (parser->lexer);
9389 if (cp_parser_allow_gnu_extensions_p (parser)
9390 && token->type == CPP_COLON)
9392 pedwarn (token->location, OPT_Wpedantic,
9393 "ISO C++ does not allow ?: with omitted middle operand");
9394 /* Implicit true clause. */
9395 expr = NULL_TREE;
9396 c_inhibit_evaluation_warnings +=
9397 folded_logical_or_expr == truthvalue_true_node;
9398 warn_for_omitted_condop (token->location, logical_or_expr);
9400 else
9402 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9403 parser->colon_corrects_to_scope_p = false;
9404 /* Parse the expression. */
9405 c_inhibit_evaluation_warnings +=
9406 folded_logical_or_expr == truthvalue_false_node;
9407 expr = cp_parser_expression (parser);
9408 c_inhibit_evaluation_warnings +=
9409 ((folded_logical_or_expr == truthvalue_true_node)
9410 - (folded_logical_or_expr == truthvalue_false_node));
9411 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9414 /* The next token should be a `:'. */
9415 cp_parser_require (parser, CPP_COLON, RT_COLON);
9416 /* Parse the assignment-expression. */
9417 assignment_expr = cp_parser_assignment_expression (parser);
9418 c_inhibit_evaluation_warnings -=
9419 folded_logical_or_expr == truthvalue_true_node;
9421 /* Make a location:
9422 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9423 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9424 with the caret at the "?", ranging from the start of
9425 the logical_or_expr to the end of the assignment_expr. */
9426 loc = make_location (loc,
9427 logical_or_expr.get_start (),
9428 assignment_expr.get_finish ());
9430 /* Build the conditional-expression. */
9431 return build_x_conditional_expr (loc, logical_or_expr,
9432 expr,
9433 assignment_expr,
9434 tf_warning_or_error);
9437 /* Parse an assignment-expression.
9439 assignment-expression:
9440 conditional-expression
9441 logical-or-expression assignment-operator assignment_expression
9442 throw-expression
9444 CAST_P is true if this expression is the target of a cast.
9445 DECLTYPE_P is true if this expression is the operand of decltype.
9447 Returns a representation for the expression. */
9449 static cp_expr
9450 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9451 bool cast_p, bool decltype_p)
9453 cp_expr expr;
9455 /* If the next token is the `throw' keyword, then we're looking at
9456 a throw-expression. */
9457 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9458 expr = cp_parser_throw_expression (parser);
9459 /* Otherwise, it must be that we are looking at a
9460 logical-or-expression. */
9461 else
9463 /* Parse the binary expressions (logical-or-expression). */
9464 expr = cp_parser_binary_expression (parser, cast_p, false,
9465 decltype_p,
9466 PREC_NOT_OPERATOR, pidk);
9467 /* If the next token is a `?' then we're actually looking at a
9468 conditional-expression. */
9469 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9470 return cp_parser_question_colon_clause (parser, expr);
9471 else
9473 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9475 /* If it's an assignment-operator, we're using the second
9476 production. */
9477 enum tree_code assignment_operator
9478 = cp_parser_assignment_operator_opt (parser);
9479 if (assignment_operator != ERROR_MARK)
9481 bool non_constant_p;
9483 /* Parse the right-hand side of the assignment. */
9484 cp_expr rhs = cp_parser_initializer_clause (parser,
9485 &non_constant_p);
9487 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9488 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9490 /* An assignment may not appear in a
9491 constant-expression. */
9492 if (cp_parser_non_integral_constant_expression (parser,
9493 NIC_ASSIGNMENT))
9494 return error_mark_node;
9495 /* Build the assignment expression. Its default
9496 location:
9497 LHS = RHS
9498 ~~~~^~~~~
9499 is the location of the '=' token as the
9500 caret, ranging from the start of the lhs to the
9501 end of the rhs. */
9502 loc = make_location (loc,
9503 expr.get_start (),
9504 rhs.get_finish ());
9505 expr = build_x_modify_expr (loc, expr,
9506 assignment_operator,
9507 rhs,
9508 complain_flags (decltype_p));
9509 /* TODO: build_x_modify_expr doesn't honor the location,
9510 so we must set it here. */
9511 expr.set_location (loc);
9516 return expr;
9519 /* Parse an (optional) assignment-operator.
9521 assignment-operator: one of
9522 = *= /= %= += -= >>= <<= &= ^= |=
9524 GNU Extension:
9526 assignment-operator: one of
9527 <?= >?=
9529 If the next token is an assignment operator, the corresponding tree
9530 code is returned, and the token is consumed. For example, for
9531 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9532 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9533 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9534 operator, ERROR_MARK is returned. */
9536 static enum tree_code
9537 cp_parser_assignment_operator_opt (cp_parser* parser)
9539 enum tree_code op;
9540 cp_token *token;
9542 /* Peek at the next token. */
9543 token = cp_lexer_peek_token (parser->lexer);
9545 switch (token->type)
9547 case CPP_EQ:
9548 op = NOP_EXPR;
9549 break;
9551 case CPP_MULT_EQ:
9552 op = MULT_EXPR;
9553 break;
9555 case CPP_DIV_EQ:
9556 op = TRUNC_DIV_EXPR;
9557 break;
9559 case CPP_MOD_EQ:
9560 op = TRUNC_MOD_EXPR;
9561 break;
9563 case CPP_PLUS_EQ:
9564 op = PLUS_EXPR;
9565 break;
9567 case CPP_MINUS_EQ:
9568 op = MINUS_EXPR;
9569 break;
9571 case CPP_RSHIFT_EQ:
9572 op = RSHIFT_EXPR;
9573 break;
9575 case CPP_LSHIFT_EQ:
9576 op = LSHIFT_EXPR;
9577 break;
9579 case CPP_AND_EQ:
9580 op = BIT_AND_EXPR;
9581 break;
9583 case CPP_XOR_EQ:
9584 op = BIT_XOR_EXPR;
9585 break;
9587 case CPP_OR_EQ:
9588 op = BIT_IOR_EXPR;
9589 break;
9591 default:
9592 /* Nothing else is an assignment operator. */
9593 op = ERROR_MARK;
9596 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9597 if (op != ERROR_MARK
9598 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9599 op = ERROR_MARK;
9601 /* If it was an assignment operator, consume it. */
9602 if (op != ERROR_MARK)
9603 cp_lexer_consume_token (parser->lexer);
9605 return op;
9608 /* Parse an expression.
9610 expression:
9611 assignment-expression
9612 expression , assignment-expression
9614 CAST_P is true if this expression is the target of a cast.
9615 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9616 except possibly parenthesized or on the RHS of a comma (N3276).
9618 Returns a representation of the expression. */
9620 static cp_expr
9621 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9622 bool cast_p, bool decltype_p)
9624 cp_expr expression = NULL_TREE;
9625 location_t loc = UNKNOWN_LOCATION;
9627 while (true)
9629 cp_expr assignment_expression;
9631 /* Parse the next assignment-expression. */
9632 assignment_expression
9633 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9635 /* We don't create a temporary for a call that is the immediate operand
9636 of decltype or on the RHS of a comma. But when we see a comma, we
9637 need to create a temporary for a call on the LHS. */
9638 if (decltype_p && !processing_template_decl
9639 && TREE_CODE (assignment_expression) == CALL_EXPR
9640 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9641 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9642 assignment_expression
9643 = build_cplus_new (TREE_TYPE (assignment_expression),
9644 assignment_expression, tf_warning_or_error);
9646 /* If this is the first assignment-expression, we can just
9647 save it away. */
9648 if (!expression)
9649 expression = assignment_expression;
9650 else
9652 /* Create a location with caret at the comma, ranging
9653 from the start of the LHS to the end of the RHS. */
9654 loc = make_location (loc,
9655 expression.get_start (),
9656 assignment_expression.get_finish ());
9657 expression = build_x_compound_expr (loc, expression,
9658 assignment_expression,
9659 complain_flags (decltype_p));
9660 expression.set_location (loc);
9662 /* If the next token is not a comma, or we're in a fold-expression, then
9663 we are done with the expression. */
9664 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9665 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9666 break;
9667 /* Consume the `,'. */
9668 loc = cp_lexer_peek_token (parser->lexer)->location;
9669 cp_lexer_consume_token (parser->lexer);
9670 /* A comma operator cannot appear in a constant-expression. */
9671 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9672 expression = error_mark_node;
9675 return expression;
9678 /* Parse a constant-expression.
9680 constant-expression:
9681 conditional-expression
9683 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9684 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9685 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9686 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9687 only parse a conditional-expression, otherwise parse an
9688 assignment-expression. See below for rationale. */
9690 static cp_expr
9691 cp_parser_constant_expression (cp_parser* parser,
9692 bool allow_non_constant_p,
9693 bool *non_constant_p,
9694 bool strict_p)
9696 bool saved_integral_constant_expression_p;
9697 bool saved_allow_non_integral_constant_expression_p;
9698 bool saved_non_integral_constant_expression_p;
9699 cp_expr expression;
9701 /* It might seem that we could simply parse the
9702 conditional-expression, and then check to see if it were
9703 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9704 one that the compiler can figure out is constant, possibly after
9705 doing some simplifications or optimizations. The standard has a
9706 precise definition of constant-expression, and we must honor
9707 that, even though it is somewhat more restrictive.
9709 For example:
9711 int i[(2, 3)];
9713 is not a legal declaration, because `(2, 3)' is not a
9714 constant-expression. The `,' operator is forbidden in a
9715 constant-expression. However, GCC's constant-folding machinery
9716 will fold this operation to an INTEGER_CST for `3'. */
9718 /* Save the old settings. */
9719 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9720 saved_allow_non_integral_constant_expression_p
9721 = parser->allow_non_integral_constant_expression_p;
9722 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9723 /* We are now parsing a constant-expression. */
9724 parser->integral_constant_expression_p = true;
9725 parser->allow_non_integral_constant_expression_p
9726 = (allow_non_constant_p || cxx_dialect >= cxx11);
9727 parser->non_integral_constant_expression_p = false;
9728 /* Although the grammar says "conditional-expression", when not STRICT_P,
9729 we parse an "assignment-expression", which also permits
9730 "throw-expression" and the use of assignment operators. In the case
9731 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9732 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9733 actually essential that we look for an assignment-expression.
9734 For example, cp_parser_initializer_clauses uses this function to
9735 determine whether a particular assignment-expression is in fact
9736 constant. */
9737 if (strict_p)
9739 /* Parse the binary expressions (logical-or-expression). */
9740 expression = cp_parser_binary_expression (parser, false, false, false,
9741 PREC_NOT_OPERATOR, NULL);
9742 /* If the next token is a `?' then we're actually looking at
9743 a conditional-expression; otherwise we're done. */
9744 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9745 expression = cp_parser_question_colon_clause (parser, expression);
9747 else
9748 expression = cp_parser_assignment_expression (parser);
9749 /* Restore the old settings. */
9750 parser->integral_constant_expression_p
9751 = saved_integral_constant_expression_p;
9752 parser->allow_non_integral_constant_expression_p
9753 = saved_allow_non_integral_constant_expression_p;
9754 if (cxx_dialect >= cxx11)
9756 /* Require an rvalue constant expression here; that's what our
9757 callers expect. Reference constant expressions are handled
9758 separately in e.g. cp_parser_template_argument. */
9759 tree decay = expression;
9760 if (TREE_TYPE (expression)
9761 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9762 decay = build_address (expression);
9763 bool is_const = potential_rvalue_constant_expression (decay);
9764 parser->non_integral_constant_expression_p = !is_const;
9765 if (!is_const && !allow_non_constant_p)
9766 require_potential_rvalue_constant_expression (decay);
9768 if (allow_non_constant_p)
9769 *non_constant_p = parser->non_integral_constant_expression_p;
9770 parser->non_integral_constant_expression_p
9771 = saved_non_integral_constant_expression_p;
9773 return expression;
9776 /* Parse __builtin_offsetof.
9778 offsetof-expression:
9779 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9781 offsetof-member-designator:
9782 id-expression
9783 | offsetof-member-designator "." id-expression
9784 | offsetof-member-designator "[" expression "]"
9785 | offsetof-member-designator "->" id-expression */
9787 static cp_expr
9788 cp_parser_builtin_offsetof (cp_parser *parser)
9790 int save_ice_p, save_non_ice_p;
9791 tree type;
9792 cp_expr expr;
9793 cp_id_kind dummy;
9794 cp_token *token;
9795 location_t finish_loc;
9797 /* We're about to accept non-integral-constant things, but will
9798 definitely yield an integral constant expression. Save and
9799 restore these values around our local parsing. */
9800 save_ice_p = parser->integral_constant_expression_p;
9801 save_non_ice_p = parser->non_integral_constant_expression_p;
9803 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9805 /* Consume the "__builtin_offsetof" token. */
9806 cp_lexer_consume_token (parser->lexer);
9807 /* Consume the opening `('. */
9808 matching_parens parens;
9809 parens.require_open (parser);
9810 /* Parse the type-id. */
9811 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9813 const char *saved_message = parser->type_definition_forbidden_message;
9814 parser->type_definition_forbidden_message
9815 = G_("types may not be defined within __builtin_offsetof");
9816 type = cp_parser_type_id (parser);
9817 parser->type_definition_forbidden_message = saved_message;
9819 /* Look for the `,'. */
9820 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9821 token = cp_lexer_peek_token (parser->lexer);
9823 /* Build the (type *)null that begins the traditional offsetof macro. */
9824 tree object_ptr
9825 = build_static_cast (build_pointer_type (type), null_pointer_node,
9826 tf_warning_or_error);
9828 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9829 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9830 true, &dummy, token->location);
9831 while (true)
9833 token = cp_lexer_peek_token (parser->lexer);
9834 switch (token->type)
9836 case CPP_OPEN_SQUARE:
9837 /* offsetof-member-designator "[" expression "]" */
9838 expr = cp_parser_postfix_open_square_expression (parser, expr,
9839 true, false);
9840 break;
9842 case CPP_DEREF:
9843 /* offsetof-member-designator "->" identifier */
9844 expr = grok_array_decl (token->location, expr,
9845 integer_zero_node, false);
9846 /* FALLTHRU */
9848 case CPP_DOT:
9849 /* offsetof-member-designator "." identifier */
9850 cp_lexer_consume_token (parser->lexer);
9851 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9852 expr, true, &dummy,
9853 token->location);
9854 break;
9856 case CPP_CLOSE_PAREN:
9857 /* Consume the ")" token. */
9858 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9859 cp_lexer_consume_token (parser->lexer);
9860 goto success;
9862 default:
9863 /* Error. We know the following require will fail, but
9864 that gives the proper error message. */
9865 parens.require_close (parser);
9866 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9867 expr = error_mark_node;
9868 goto failure;
9872 success:
9873 /* Make a location of the form:
9874 __builtin_offsetof (struct s, f)
9875 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9876 with caret at the type-id, ranging from the start of the
9877 "_builtin_offsetof" token to the close paren. */
9878 loc = make_location (loc, start_loc, finish_loc);
9879 /* The result will be an INTEGER_CST, so we need to explicitly
9880 preserve the location. */
9881 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9883 failure:
9884 parser->integral_constant_expression_p = save_ice_p;
9885 parser->non_integral_constant_expression_p = save_non_ice_p;
9887 expr = expr.maybe_add_location_wrapper ();
9888 return expr;
9891 /* Parse a trait expression.
9893 Returns a representation of the expression, the underlying type
9894 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9896 static cp_expr
9897 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9899 cp_trait_kind kind;
9900 tree type1, type2 = NULL_TREE;
9901 bool binary = false;
9902 bool variadic = false;
9904 switch (keyword)
9906 case RID_HAS_NOTHROW_ASSIGN:
9907 kind = CPTK_HAS_NOTHROW_ASSIGN;
9908 break;
9909 case RID_HAS_NOTHROW_CONSTRUCTOR:
9910 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9911 break;
9912 case RID_HAS_NOTHROW_COPY:
9913 kind = CPTK_HAS_NOTHROW_COPY;
9914 break;
9915 case RID_HAS_TRIVIAL_ASSIGN:
9916 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9917 break;
9918 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9919 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9920 break;
9921 case RID_HAS_TRIVIAL_COPY:
9922 kind = CPTK_HAS_TRIVIAL_COPY;
9923 break;
9924 case RID_HAS_TRIVIAL_DESTRUCTOR:
9925 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9926 break;
9927 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9928 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9929 break;
9930 case RID_HAS_VIRTUAL_DESTRUCTOR:
9931 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9932 break;
9933 case RID_IS_ABSTRACT:
9934 kind = CPTK_IS_ABSTRACT;
9935 break;
9936 case RID_IS_AGGREGATE:
9937 kind = CPTK_IS_AGGREGATE;
9938 break;
9939 case RID_IS_BASE_OF:
9940 kind = CPTK_IS_BASE_OF;
9941 binary = true;
9942 break;
9943 case RID_IS_CLASS:
9944 kind = CPTK_IS_CLASS;
9945 break;
9946 case RID_IS_EMPTY:
9947 kind = CPTK_IS_EMPTY;
9948 break;
9949 case RID_IS_ENUM:
9950 kind = CPTK_IS_ENUM;
9951 break;
9952 case RID_IS_FINAL:
9953 kind = CPTK_IS_FINAL;
9954 break;
9955 case RID_IS_LITERAL_TYPE:
9956 kind = CPTK_IS_LITERAL_TYPE;
9957 break;
9958 case RID_IS_POD:
9959 kind = CPTK_IS_POD;
9960 break;
9961 case RID_IS_POLYMORPHIC:
9962 kind = CPTK_IS_POLYMORPHIC;
9963 break;
9964 case RID_IS_SAME_AS:
9965 kind = CPTK_IS_SAME_AS;
9966 binary = true;
9967 break;
9968 case RID_IS_STD_LAYOUT:
9969 kind = CPTK_IS_STD_LAYOUT;
9970 break;
9971 case RID_IS_TRIVIAL:
9972 kind = CPTK_IS_TRIVIAL;
9973 break;
9974 case RID_IS_TRIVIALLY_ASSIGNABLE:
9975 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9976 binary = true;
9977 break;
9978 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9979 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9980 variadic = true;
9981 break;
9982 case RID_IS_TRIVIALLY_COPYABLE:
9983 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9984 break;
9985 case RID_IS_UNION:
9986 kind = CPTK_IS_UNION;
9987 break;
9988 case RID_UNDERLYING_TYPE:
9989 kind = CPTK_UNDERLYING_TYPE;
9990 break;
9991 case RID_BASES:
9992 kind = CPTK_BASES;
9993 break;
9994 case RID_DIRECT_BASES:
9995 kind = CPTK_DIRECT_BASES;
9996 break;
9997 case RID_IS_ASSIGNABLE:
9998 kind = CPTK_IS_ASSIGNABLE;
9999 binary = true;
10000 break;
10001 case RID_IS_CONSTRUCTIBLE:
10002 kind = CPTK_IS_CONSTRUCTIBLE;
10003 variadic = true;
10004 break;
10005 default:
10006 gcc_unreachable ();
10009 /* Get location of initial token. */
10010 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10012 /* Consume the token. */
10013 cp_lexer_consume_token (parser->lexer);
10015 matching_parens parens;
10016 parens.require_open (parser);
10019 type_id_in_expr_sentinel s (parser);
10020 type1 = cp_parser_type_id (parser);
10023 if (type1 == error_mark_node)
10024 return error_mark_node;
10026 if (binary)
10028 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10031 type_id_in_expr_sentinel s (parser);
10032 type2 = cp_parser_type_id (parser);
10035 if (type2 == error_mark_node)
10036 return error_mark_node;
10038 else if (variadic)
10040 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10042 cp_lexer_consume_token (parser->lexer);
10043 tree elt = cp_parser_type_id (parser);
10044 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10046 cp_lexer_consume_token (parser->lexer);
10047 elt = make_pack_expansion (elt);
10049 if (elt == error_mark_node)
10050 return error_mark_node;
10051 type2 = tree_cons (NULL_TREE, elt, type2);
10055 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10056 parens.require_close (parser);
10058 /* Construct a location of the form:
10059 __is_trivially_copyable(_Tp)
10060 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10061 with start == caret, finishing at the close-paren. */
10062 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10064 /* Complete the trait expression, which may mean either processing
10065 the trait expr now or saving it for template instantiation. */
10066 switch (kind)
10068 case CPTK_UNDERLYING_TYPE:
10069 return cp_expr (finish_underlying_type (type1), trait_loc);
10070 case CPTK_BASES:
10071 return cp_expr (finish_bases (type1, false), trait_loc);
10072 case CPTK_DIRECT_BASES:
10073 return cp_expr (finish_bases (type1, true), trait_loc);
10074 default:
10075 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10079 /* Parse a lambda expression.
10081 lambda-expression:
10082 lambda-introducer lambda-declarator [opt] compound-statement
10084 Returns a representation of the expression. */
10086 static cp_expr
10087 cp_parser_lambda_expression (cp_parser* parser)
10089 tree lambda_expr = build_lambda_expr ();
10090 tree type;
10091 bool ok = true;
10092 cp_token *token = cp_lexer_peek_token (parser->lexer);
10093 cp_token_position start = 0;
10095 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10097 if (cp_unevaluated_operand)
10099 if (!token->error_reported)
10101 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10102 "lambda-expression in unevaluated context");
10103 token->error_reported = true;
10105 ok = false;
10107 else if (parser->in_template_argument_list_p)
10109 if (!token->error_reported)
10111 error_at (token->location, "lambda-expression in template-argument");
10112 token->error_reported = true;
10114 ok = false;
10117 /* We may be in the middle of deferred access check. Disable
10118 it now. */
10119 push_deferring_access_checks (dk_no_deferred);
10121 cp_parser_lambda_introducer (parser, lambda_expr);
10123 type = begin_lambda_type (lambda_expr);
10124 if (type == error_mark_node)
10125 return error_mark_node;
10127 record_lambda_scope (lambda_expr);
10129 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10130 determine_visibility (TYPE_NAME (type));
10132 /* Now that we've started the type, add the capture fields for any
10133 explicit captures. */
10134 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10137 /* Inside the class, surrounding template-parameter-lists do not apply. */
10138 unsigned int saved_num_template_parameter_lists
10139 = parser->num_template_parameter_lists;
10140 unsigned char in_statement = parser->in_statement;
10141 bool in_switch_statement_p = parser->in_switch_statement_p;
10142 bool fully_implicit_function_template_p
10143 = parser->fully_implicit_function_template_p;
10144 tree implicit_template_parms = parser->implicit_template_parms;
10145 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10146 bool auto_is_implicit_function_template_parm_p
10147 = parser->auto_is_implicit_function_template_parm_p;
10149 parser->num_template_parameter_lists = 0;
10150 parser->in_statement = 0;
10151 parser->in_switch_statement_p = false;
10152 parser->fully_implicit_function_template_p = false;
10153 parser->implicit_template_parms = 0;
10154 parser->implicit_template_scope = 0;
10155 parser->auto_is_implicit_function_template_parm_p = false;
10157 /* By virtue of defining a local class, a lambda expression has access to
10158 the private variables of enclosing classes. */
10160 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10162 if (ok && cp_parser_error_occurred (parser))
10163 ok = false;
10165 if (ok)
10167 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10168 && cp_parser_start_tentative_firewall (parser))
10169 start = token;
10170 cp_parser_lambda_body (parser, lambda_expr);
10172 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10174 if (cp_parser_skip_to_closing_brace (parser))
10175 cp_lexer_consume_token (parser->lexer);
10178 /* The capture list was built up in reverse order; fix that now. */
10179 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10180 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10182 if (ok)
10183 maybe_add_lambda_conv_op (type);
10185 type = finish_struct (type, /*attributes=*/NULL_TREE);
10187 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10188 parser->in_statement = in_statement;
10189 parser->in_switch_statement_p = in_switch_statement_p;
10190 parser->fully_implicit_function_template_p
10191 = fully_implicit_function_template_p;
10192 parser->implicit_template_parms = implicit_template_parms;
10193 parser->implicit_template_scope = implicit_template_scope;
10194 parser->auto_is_implicit_function_template_parm_p
10195 = auto_is_implicit_function_template_parm_p;
10198 /* This field is only used during parsing of the lambda. */
10199 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10201 /* This lambda shouldn't have any proxies left at this point. */
10202 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10203 /* And now that we're done, push proxies for an enclosing lambda. */
10204 insert_pending_capture_proxies ();
10206 if (ok)
10207 lambda_expr = build_lambda_object (lambda_expr);
10208 else
10209 lambda_expr = error_mark_node;
10211 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10213 pop_deferring_access_checks ();
10215 return lambda_expr;
10218 /* Parse the beginning of a lambda expression.
10220 lambda-introducer:
10221 [ lambda-capture [opt] ]
10223 LAMBDA_EXPR is the current representation of the lambda expression. */
10225 static void
10226 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10228 /* Need commas after the first capture. */
10229 bool first = true;
10231 /* Eat the leading `['. */
10232 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10234 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10235 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10236 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10237 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10238 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10239 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10241 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10243 cp_lexer_consume_token (parser->lexer);
10244 first = false;
10247 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10249 cp_token* capture_token;
10250 tree capture_id;
10251 tree capture_init_expr;
10252 cp_id_kind idk = CP_ID_KIND_NONE;
10253 bool explicit_init_p = false;
10255 enum capture_kind_type
10257 BY_COPY,
10258 BY_REFERENCE
10260 enum capture_kind_type capture_kind = BY_COPY;
10262 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10264 error ("expected end of capture-list");
10265 return;
10268 if (first)
10269 first = false;
10270 else
10271 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10273 /* Possibly capture `this'. */
10274 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10276 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10277 if (cxx_dialect < cxx2a
10278 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10279 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10280 "with by-copy capture default");
10281 cp_lexer_consume_token (parser->lexer);
10282 add_capture (lambda_expr,
10283 /*id=*/this_identifier,
10284 /*initializer=*/finish_this_expr (),
10285 /*by_reference_p=*/true,
10286 explicit_init_p);
10287 continue;
10290 /* Possibly capture `*this'. */
10291 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10292 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10294 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10295 if (cxx_dialect < cxx17)
10296 pedwarn (loc, 0, "%<*this%> capture only available with "
10297 "-std=c++17 or -std=gnu++17");
10298 cp_lexer_consume_token (parser->lexer);
10299 cp_lexer_consume_token (parser->lexer);
10300 add_capture (lambda_expr,
10301 /*id=*/this_identifier,
10302 /*initializer=*/finish_this_expr (),
10303 /*by_reference_p=*/false,
10304 explicit_init_p);
10305 continue;
10308 /* Remember whether we want to capture as a reference or not. */
10309 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10311 capture_kind = BY_REFERENCE;
10312 cp_lexer_consume_token (parser->lexer);
10315 /* Get the identifier. */
10316 capture_token = cp_lexer_peek_token (parser->lexer);
10317 capture_id = cp_parser_identifier (parser);
10319 if (capture_id == error_mark_node)
10320 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10321 delimiters, but I modified this to stop on unnested ']' as well. It
10322 was already changed to stop on unnested '}', so the
10323 "closing_parenthesis" name is no more misleading with my change. */
10325 cp_parser_skip_to_closing_parenthesis (parser,
10326 /*recovering=*/true,
10327 /*or_comma=*/true,
10328 /*consume_paren=*/true);
10329 break;
10332 /* Find the initializer for this capture. */
10333 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10334 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10335 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10337 bool direct, non_constant;
10338 /* An explicit initializer exists. */
10339 if (cxx_dialect < cxx14)
10340 pedwarn (input_location, 0,
10341 "lambda capture initializers "
10342 "only available with -std=c++14 or -std=gnu++14");
10343 capture_init_expr = cp_parser_initializer (parser, &direct,
10344 &non_constant, true);
10345 explicit_init_p = true;
10346 if (capture_init_expr == NULL_TREE)
10348 error ("empty initializer for lambda init-capture");
10349 capture_init_expr = error_mark_node;
10352 else
10354 const char* error_msg;
10356 /* Turn the identifier into an id-expression. */
10357 capture_init_expr
10358 = cp_parser_lookup_name_simple (parser, capture_id,
10359 capture_token->location);
10361 if (capture_init_expr == error_mark_node)
10363 unqualified_name_lookup_error (capture_id);
10364 continue;
10366 else if (!VAR_P (capture_init_expr)
10367 && TREE_CODE (capture_init_expr) != PARM_DECL)
10369 error_at (capture_token->location,
10370 "capture of non-variable %qE",
10371 capture_init_expr);
10372 if (DECL_P (capture_init_expr))
10373 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10374 "%q#D declared here", capture_init_expr);
10375 continue;
10377 if (VAR_P (capture_init_expr)
10378 && decl_storage_duration (capture_init_expr) != dk_auto)
10380 if (pedwarn (capture_token->location, 0, "capture of variable "
10381 "%qD with non-automatic storage duration",
10382 capture_init_expr))
10383 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10384 "%q#D declared here", capture_init_expr);
10385 continue;
10388 capture_init_expr
10389 = finish_id_expression
10390 (capture_id,
10391 capture_init_expr,
10392 parser->scope,
10393 &idk,
10394 /*integral_constant_expression_p=*/false,
10395 /*allow_non_integral_constant_expression_p=*/false,
10396 /*non_integral_constant_expression_p=*/NULL,
10397 /*template_p=*/false,
10398 /*done=*/true,
10399 /*address_p=*/false,
10400 /*template_arg_p=*/false,
10401 &error_msg,
10402 capture_token->location);
10404 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10406 cp_lexer_consume_token (parser->lexer);
10407 capture_init_expr = make_pack_expansion (capture_init_expr);
10411 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10412 && !explicit_init_p)
10414 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10415 && capture_kind == BY_COPY)
10416 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10417 "of %qD redundant with by-copy capture default",
10418 capture_id);
10419 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10420 && capture_kind == BY_REFERENCE)
10421 pedwarn (capture_token->location, 0, "explicit by-reference "
10422 "capture of %qD redundant with by-reference capture "
10423 "default", capture_id);
10426 add_capture (lambda_expr,
10427 capture_id,
10428 capture_init_expr,
10429 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10430 explicit_init_p);
10432 /* If there is any qualification still in effect, clear it
10433 now; we will be starting fresh with the next capture. */
10434 parser->scope = NULL_TREE;
10435 parser->qualifying_scope = NULL_TREE;
10436 parser->object_scope = NULL_TREE;
10439 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10442 /* Parse the (optional) middle of a lambda expression.
10444 lambda-declarator:
10445 < template-parameter-list [opt] >
10446 ( parameter-declaration-clause [opt] )
10447 attribute-specifier [opt]
10448 decl-specifier-seq [opt]
10449 exception-specification [opt]
10450 lambda-return-type-clause [opt]
10452 LAMBDA_EXPR is the current representation of the lambda expression. */
10454 static bool
10455 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10457 /* 5.1.1.4 of the standard says:
10458 If a lambda-expression does not include a lambda-declarator, it is as if
10459 the lambda-declarator were ().
10460 This means an empty parameter list, no attributes, and no exception
10461 specification. */
10462 tree param_list = void_list_node;
10463 tree attributes = NULL_TREE;
10464 tree exception_spec = NULL_TREE;
10465 tree template_param_list = NULL_TREE;
10466 tree tx_qual = NULL_TREE;
10467 tree return_type = NULL_TREE;
10468 cp_decl_specifier_seq lambda_specs;
10469 clear_decl_specs (&lambda_specs);
10471 /* The template-parameter-list is optional, but must begin with
10472 an opening angle if present. */
10473 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10475 if (cxx_dialect < cxx14)
10476 pedwarn (parser->lexer->next_token->location, 0,
10477 "lambda templates are only available with "
10478 "-std=c++14 or -std=gnu++14");
10479 else if (cxx_dialect < cxx2a)
10480 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10481 "lambda templates are only available with "
10482 "-std=c++2a or -std=gnu++2a");
10484 cp_lexer_consume_token (parser->lexer);
10486 template_param_list = cp_parser_template_parameter_list (parser);
10488 cp_parser_skip_to_end_of_template_parameter_list (parser);
10490 /* We just processed one more parameter list. */
10491 ++parser->num_template_parameter_lists;
10494 /* The parameter-declaration-clause is optional (unless
10495 template-parameter-list was given), but must begin with an
10496 opening parenthesis if present. */
10497 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10499 matching_parens parens;
10500 parens.consume_open (parser);
10502 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10504 /* Parse parameters. */
10505 param_list = cp_parser_parameter_declaration_clause (parser);
10507 /* Default arguments shall not be specified in the
10508 parameter-declaration-clause of a lambda-declarator. */
10509 if (cxx_dialect < cxx14)
10510 for (tree t = param_list; t; t = TREE_CHAIN (t))
10511 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10512 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10513 "default argument specified for lambda parameter");
10515 parens.require_close (parser);
10517 attributes = cp_parser_attributes_opt (parser);
10519 /* In the decl-specifier-seq of the lambda-declarator, each
10520 decl-specifier shall either be mutable or constexpr. */
10521 int declares_class_or_enum;
10522 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10523 cp_parser_decl_specifier_seq (parser,
10524 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10525 &lambda_specs, &declares_class_or_enum);
10526 if (lambda_specs.storage_class == sc_mutable)
10528 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10529 if (lambda_specs.conflicting_specifiers_p)
10530 error_at (lambda_specs.locations[ds_storage_class],
10531 "duplicate %<mutable%>");
10534 tx_qual = cp_parser_tx_qualifier_opt (parser);
10536 /* Parse optional exception specification. */
10537 exception_spec = cp_parser_exception_specification_opt (parser);
10539 /* Parse optional trailing return type. */
10540 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10542 cp_lexer_consume_token (parser->lexer);
10543 return_type = cp_parser_trailing_type_id (parser);
10546 /* The function parameters must be in scope all the way until after the
10547 trailing-return-type in case of decltype. */
10548 pop_bindings_and_leave_scope ();
10550 else if (template_param_list != NULL_TREE) // generate diagnostic
10551 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10553 /* Create the function call operator.
10555 Messing with declarators like this is no uglier than building up the
10556 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10557 other code. */
10559 cp_decl_specifier_seq return_type_specs;
10560 cp_declarator* declarator;
10561 tree fco;
10562 int quals;
10563 void *p;
10565 clear_decl_specs (&return_type_specs);
10566 return_type_specs.type = make_auto ();
10568 if (lambda_specs.locations[ds_constexpr])
10570 if (cxx_dialect >= cxx17)
10571 return_type_specs.locations[ds_constexpr]
10572 = lambda_specs.locations[ds_constexpr];
10573 else
10574 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10575 "lambda only available with -std=c++17 or -std=gnu++17");
10578 p = obstack_alloc (&declarator_obstack, 0);
10580 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10582 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10583 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10584 declarator = make_call_declarator (declarator, param_list, quals,
10585 VIRT_SPEC_UNSPECIFIED,
10586 REF_QUAL_NONE,
10587 tx_qual,
10588 exception_spec,
10589 /*late_return_type=*/NULL_TREE,
10590 /*requires_clause*/NULL_TREE);
10591 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10592 if (return_type)
10593 declarator->u.function.late_return_type = return_type;
10595 fco = grokmethod (&return_type_specs,
10596 declarator,
10597 attributes);
10598 if (fco != error_mark_node)
10600 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10601 DECL_ARTIFICIAL (fco) = 1;
10602 /* Give the object parameter a different name. */
10603 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10605 if (template_param_list)
10607 fco = finish_member_template_decl (fco);
10608 finish_template_decl (template_param_list);
10609 --parser->num_template_parameter_lists;
10611 else if (parser->fully_implicit_function_template_p)
10612 fco = finish_fully_implicit_template (parser, fco);
10614 finish_member_declaration (fco);
10616 obstack_free (&declarator_obstack, p);
10618 return (fco != error_mark_node);
10622 /* Parse the body of a lambda expression, which is simply
10624 compound-statement
10626 but which requires special handling.
10627 LAMBDA_EXPR is the current representation of the lambda expression. */
10629 static void
10630 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10632 bool nested = (current_function_decl != NULL_TREE);
10633 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10634 bool in_function_body = parser->in_function_body;
10636 if (nested)
10637 push_function_context ();
10638 else
10639 /* Still increment function_depth so that we don't GC in the
10640 middle of an expression. */
10641 ++function_depth;
10643 vec<tree> omp_privatization_save;
10644 save_omp_privatization_clauses (omp_privatization_save);
10645 /* Clear this in case we're in the middle of a default argument. */
10646 parser->local_variables_forbidden_p = false;
10647 parser->in_function_body = true;
10650 local_specialization_stack s (lss_copy);
10651 tree fco = lambda_function (lambda_expr);
10652 tree body = start_lambda_function (fco, lambda_expr);
10653 matching_braces braces;
10655 if (braces.require_open (parser))
10657 tree compound_stmt = begin_compound_stmt (0);
10659 /* Originally C++11 required us to peek for 'return expr'; and
10660 process it specially here to deduce the return type. N3638
10661 removed the need for that. */
10663 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10664 cp_parser_label_declaration (parser);
10665 cp_parser_statement_seq_opt (parser, NULL_TREE);
10666 braces.require_close (parser);
10668 finish_compound_stmt (compound_stmt);
10671 finish_lambda_function (body);
10674 restore_omp_privatization_clauses (omp_privatization_save);
10675 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10676 parser->in_function_body = in_function_body;
10677 if (nested)
10678 pop_function_context();
10679 else
10680 --function_depth;
10683 /* Statements [gram.stmt.stmt] */
10685 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10687 static void
10688 add_debug_begin_stmt (location_t loc)
10690 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10691 return;
10692 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10693 /* A concept is never expanded normally. */
10694 return;
10696 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10697 SET_EXPR_LOCATION (stmt, loc);
10698 add_stmt (stmt);
10701 /* Parse a statement.
10703 statement:
10704 labeled-statement
10705 expression-statement
10706 compound-statement
10707 selection-statement
10708 iteration-statement
10709 jump-statement
10710 declaration-statement
10711 try-block
10713 C++11:
10715 statement:
10716 labeled-statement
10717 attribute-specifier-seq (opt) expression-statement
10718 attribute-specifier-seq (opt) compound-statement
10719 attribute-specifier-seq (opt) selection-statement
10720 attribute-specifier-seq (opt) iteration-statement
10721 attribute-specifier-seq (opt) jump-statement
10722 declaration-statement
10723 attribute-specifier-seq (opt) try-block
10725 init-statement:
10726 expression-statement
10727 simple-declaration
10729 TM Extension:
10731 statement:
10732 atomic-statement
10734 IN_COMPOUND is true when the statement is nested inside a
10735 cp_parser_compound_statement; this matters for certain pragmas.
10737 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10738 is a (possibly labeled) if statement which is not enclosed in braces
10739 and has an else clause. This is used to implement -Wparentheses.
10741 CHAIN is a vector of if-else-if conditions. */
10743 static void
10744 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10745 bool in_compound, bool *if_p, vec<tree> *chain,
10746 location_t *loc_after_labels)
10748 tree statement, std_attrs = NULL_TREE;
10749 cp_token *token;
10750 location_t statement_location, attrs_location;
10752 restart:
10753 if (if_p != NULL)
10754 *if_p = false;
10755 /* There is no statement yet. */
10756 statement = NULL_TREE;
10758 saved_token_sentinel saved_tokens (parser->lexer);
10759 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10760 if (c_dialect_objc ())
10761 /* In obj-c++, seeing '[[' might be the either the beginning of
10762 c++11 attributes, or a nested objc-message-expression. So
10763 let's parse the c++11 attributes tentatively. */
10764 cp_parser_parse_tentatively (parser);
10765 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10766 if (c_dialect_objc ())
10768 if (!cp_parser_parse_definitely (parser))
10769 std_attrs = NULL_TREE;
10772 /* Peek at the next token. */
10773 token = cp_lexer_peek_token (parser->lexer);
10774 /* Remember the location of the first token in the statement. */
10775 statement_location = token->location;
10776 add_debug_begin_stmt (statement_location);
10777 /* If this is a keyword, then that will often determine what kind of
10778 statement we have. */
10779 if (token->type == CPP_KEYWORD)
10781 enum rid keyword = token->keyword;
10783 switch (keyword)
10785 case RID_CASE:
10786 case RID_DEFAULT:
10787 /* Looks like a labeled-statement with a case label.
10788 Parse the label, and then use tail recursion to parse
10789 the statement. */
10790 cp_parser_label_for_labeled_statement (parser, std_attrs);
10791 in_compound = false;
10792 goto restart;
10794 case RID_IF:
10795 case RID_SWITCH:
10796 statement = cp_parser_selection_statement (parser, if_p, chain);
10797 break;
10799 case RID_WHILE:
10800 case RID_DO:
10801 case RID_FOR:
10802 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10803 break;
10805 case RID_BREAK:
10806 case RID_CONTINUE:
10807 case RID_RETURN:
10808 case RID_GOTO:
10809 statement = cp_parser_jump_statement (parser);
10810 break;
10812 /* Objective-C++ exception-handling constructs. */
10813 case RID_AT_TRY:
10814 case RID_AT_CATCH:
10815 case RID_AT_FINALLY:
10816 case RID_AT_SYNCHRONIZED:
10817 case RID_AT_THROW:
10818 statement = cp_parser_objc_statement (parser);
10819 break;
10821 case RID_TRY:
10822 statement = cp_parser_try_block (parser);
10823 break;
10825 case RID_NAMESPACE:
10826 /* This must be a namespace alias definition. */
10827 cp_parser_declaration_statement (parser);
10828 return;
10830 case RID_TRANSACTION_ATOMIC:
10831 case RID_TRANSACTION_RELAXED:
10832 case RID_SYNCHRONIZED:
10833 case RID_ATOMIC_NOEXCEPT:
10834 case RID_ATOMIC_CANCEL:
10835 statement = cp_parser_transaction (parser, token);
10836 break;
10837 case RID_TRANSACTION_CANCEL:
10838 statement = cp_parser_transaction_cancel (parser);
10839 break;
10841 default:
10842 /* It might be a keyword like `int' that can start a
10843 declaration-statement. */
10844 break;
10847 else if (token->type == CPP_NAME)
10849 /* If the next token is a `:', then we are looking at a
10850 labeled-statement. */
10851 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10852 if (token->type == CPP_COLON)
10854 /* Looks like a labeled-statement with an ordinary label.
10855 Parse the label, and then use tail recursion to parse
10856 the statement. */
10858 cp_parser_label_for_labeled_statement (parser, std_attrs);
10859 in_compound = false;
10860 goto restart;
10863 /* Anything that starts with a `{' must be a compound-statement. */
10864 else if (token->type == CPP_OPEN_BRACE)
10865 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10866 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10867 a statement all its own. */
10868 else if (token->type == CPP_PRAGMA)
10870 /* Only certain OpenMP pragmas are attached to statements, and thus
10871 are considered statements themselves. All others are not. In
10872 the context of a compound, accept the pragma as a "statement" and
10873 return so that we can check for a close brace. Otherwise we
10874 require a real statement and must go back and read one. */
10875 if (in_compound)
10876 cp_parser_pragma (parser, pragma_compound, if_p);
10877 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10878 goto restart;
10879 return;
10881 else if (token->type == CPP_EOF)
10883 cp_parser_error (parser, "expected statement");
10884 return;
10887 /* Everything else must be a declaration-statement or an
10888 expression-statement. Try for the declaration-statement
10889 first, unless we are looking at a `;', in which case we know that
10890 we have an expression-statement. */
10891 if (!statement)
10893 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10895 if (std_attrs != NULL_TREE)
10897 /* Attributes should be parsed as part of the the
10898 declaration, so let's un-parse them. */
10899 saved_tokens.rollback();
10900 std_attrs = NULL_TREE;
10903 cp_parser_parse_tentatively (parser);
10904 /* Try to parse the declaration-statement. */
10905 cp_parser_declaration_statement (parser);
10906 /* If that worked, we're done. */
10907 if (cp_parser_parse_definitely (parser))
10908 return;
10910 /* All preceding labels have been parsed at this point. */
10911 if (loc_after_labels != NULL)
10912 *loc_after_labels = statement_location;
10914 /* Look for an expression-statement instead. */
10915 statement = cp_parser_expression_statement (parser, in_statement_expr);
10917 /* Handle [[fallthrough]];. */
10918 if (attribute_fallthrough_p (std_attrs))
10920 /* The next token after the fallthrough attribute is ';'. */
10921 if (statement == NULL_TREE)
10923 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10924 statement = build_call_expr_internal_loc (statement_location,
10925 IFN_FALLTHROUGH,
10926 void_type_node, 0);
10927 finish_expr_stmt (statement);
10929 else
10930 warning_at (statement_location, OPT_Wattributes,
10931 "%<fallthrough%> attribute not followed by %<;%>");
10932 std_attrs = NULL_TREE;
10936 /* Set the line number for the statement. */
10937 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10938 SET_EXPR_LOCATION (statement, statement_location);
10940 /* Allow "[[fallthrough]];", but warn otherwise. */
10941 if (std_attrs != NULL_TREE)
10942 warning_at (attrs_location,
10943 OPT_Wattributes,
10944 "attributes at the beginning of statement are ignored");
10947 /* Append ATTR to attribute list ATTRS. */
10949 static tree
10950 attr_chainon (tree attrs, tree attr)
10952 if (attrs == error_mark_node)
10953 return error_mark_node;
10954 if (attr == error_mark_node)
10955 return error_mark_node;
10956 return chainon (attrs, attr);
10959 /* Parse the label for a labeled-statement, i.e.
10961 identifier :
10962 case constant-expression :
10963 default :
10965 GNU Extension:
10966 case constant-expression ... constant-expression : statement
10968 When a label is parsed without errors, the label is added to the
10969 parse tree by the finish_* functions, so this function doesn't
10970 have to return the label. */
10972 static void
10973 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10975 cp_token *token;
10976 tree label = NULL_TREE;
10977 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10979 /* The next token should be an identifier. */
10980 token = cp_lexer_peek_token (parser->lexer);
10981 if (token->type != CPP_NAME
10982 && token->type != CPP_KEYWORD)
10984 cp_parser_error (parser, "expected labeled-statement");
10985 return;
10988 /* Remember whether this case or a user-defined label is allowed to fall
10989 through to. */
10990 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
10992 parser->colon_corrects_to_scope_p = false;
10993 switch (token->keyword)
10995 case RID_CASE:
10997 tree expr, expr_hi;
10998 cp_token *ellipsis;
11000 /* Consume the `case' token. */
11001 cp_lexer_consume_token (parser->lexer);
11002 /* Parse the constant-expression. */
11003 expr = cp_parser_constant_expression (parser);
11004 if (check_for_bare_parameter_packs (expr))
11005 expr = error_mark_node;
11007 ellipsis = cp_lexer_peek_token (parser->lexer);
11008 if (ellipsis->type == CPP_ELLIPSIS)
11010 /* Consume the `...' token. */
11011 cp_lexer_consume_token (parser->lexer);
11012 expr_hi = cp_parser_constant_expression (parser);
11013 if (check_for_bare_parameter_packs (expr_hi))
11014 expr_hi = error_mark_node;
11016 /* We don't need to emit warnings here, as the common code
11017 will do this for us. */
11019 else
11020 expr_hi = NULL_TREE;
11022 if (parser->in_switch_statement_p)
11024 tree l = finish_case_label (token->location, expr, expr_hi);
11025 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11026 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11028 else
11029 error_at (token->location,
11030 "case label %qE not within a switch statement",
11031 expr);
11033 break;
11035 case RID_DEFAULT:
11036 /* Consume the `default' token. */
11037 cp_lexer_consume_token (parser->lexer);
11039 if (parser->in_switch_statement_p)
11041 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11042 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11043 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11045 else
11046 error_at (token->location, "case label not within a switch statement");
11047 break;
11049 default:
11050 /* Anything else must be an ordinary label. */
11051 label = finish_label_stmt (cp_parser_identifier (parser));
11052 if (label && TREE_CODE (label) == LABEL_DECL)
11053 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11054 break;
11057 /* Require the `:' token. */
11058 cp_parser_require (parser, CPP_COLON, RT_COLON);
11060 /* An ordinary label may optionally be followed by attributes.
11061 However, this is only permitted if the attributes are then
11062 followed by a semicolon. This is because, for backward
11063 compatibility, when parsing
11064 lab: __attribute__ ((unused)) int i;
11065 we want the attribute to attach to "i", not "lab". */
11066 if (label != NULL_TREE
11067 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11069 tree attrs;
11070 cp_parser_parse_tentatively (parser);
11071 attrs = cp_parser_gnu_attributes_opt (parser);
11072 if (attrs == NULL_TREE
11073 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11074 cp_parser_abort_tentative_parse (parser);
11075 else if (!cp_parser_parse_definitely (parser))
11077 else
11078 attributes = attr_chainon (attributes, attrs);
11081 if (attributes != NULL_TREE)
11082 cplus_decl_attributes (&label, attributes, 0);
11084 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11087 /* Parse an expression-statement.
11089 expression-statement:
11090 expression [opt] ;
11092 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11093 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11094 indicates whether this expression-statement is part of an
11095 expression statement. */
11097 static tree
11098 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11100 tree statement = NULL_TREE;
11101 cp_token *token = cp_lexer_peek_token (parser->lexer);
11102 location_t loc = token->location;
11104 /* There might be attribute fallthrough. */
11105 tree attr = cp_parser_gnu_attributes_opt (parser);
11107 /* If the next token is a ';', then there is no expression
11108 statement. */
11109 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11111 statement = cp_parser_expression (parser);
11112 if (statement == error_mark_node
11113 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11115 cp_parser_skip_to_end_of_block_or_statement (parser);
11116 return error_mark_node;
11120 /* Handle [[fallthrough]];. */
11121 if (attribute_fallthrough_p (attr))
11123 /* The next token after the fallthrough attribute is ';'. */
11124 if (statement == NULL_TREE)
11125 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11126 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11127 void_type_node, 0);
11128 else
11129 warning_at (loc, OPT_Wattributes,
11130 "%<fallthrough%> attribute not followed by %<;%>");
11131 attr = NULL_TREE;
11134 /* Allow "[[fallthrough]];", but warn otherwise. */
11135 if (attr != NULL_TREE)
11136 warning_at (loc, OPT_Wattributes,
11137 "attributes at the beginning of statement are ignored");
11139 /* Give a helpful message for "A<T>::type t;" and the like. */
11140 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11141 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11143 if (TREE_CODE (statement) == SCOPE_REF)
11144 error_at (token->location, "need %<typename%> before %qE because "
11145 "%qT is a dependent scope",
11146 statement, TREE_OPERAND (statement, 0));
11147 else if (is_overloaded_fn (statement)
11148 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11150 /* A::A a; */
11151 tree fn = get_first_fn (statement);
11152 error_at (token->location,
11153 "%<%T::%D%> names the constructor, not the type",
11154 DECL_CONTEXT (fn), DECL_NAME (fn));
11158 /* Consume the final `;'. */
11159 cp_parser_consume_semicolon_at_end_of_statement (parser);
11161 if (in_statement_expr
11162 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11163 /* This is the final expression statement of a statement
11164 expression. */
11165 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11166 else if (statement)
11167 statement = finish_expr_stmt (statement);
11169 return statement;
11172 /* Parse a compound-statement.
11174 compound-statement:
11175 { statement-seq [opt] }
11177 GNU extension:
11179 compound-statement:
11180 { label-declaration-seq [opt] statement-seq [opt] }
11182 label-declaration-seq:
11183 label-declaration
11184 label-declaration-seq label-declaration
11186 Returns a tree representing the statement. */
11188 static tree
11189 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11190 int bcs_flags, bool function_body)
11192 tree compound_stmt;
11193 matching_braces braces;
11195 /* Consume the `{'. */
11196 if (!braces.require_open (parser))
11197 return error_mark_node;
11198 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11199 && !function_body && cxx_dialect < cxx14)
11200 pedwarn (input_location, OPT_Wpedantic,
11201 "compound-statement in %<constexpr%> function");
11202 /* Begin the compound-statement. */
11203 compound_stmt = begin_compound_stmt (bcs_flags);
11204 /* If the next keyword is `__label__' we have a label declaration. */
11205 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11206 cp_parser_label_declaration (parser);
11207 /* Parse an (optional) statement-seq. */
11208 cp_parser_statement_seq_opt (parser, in_statement_expr);
11209 /* Finish the compound-statement. */
11210 finish_compound_stmt (compound_stmt);
11211 /* Consume the `}'. */
11212 braces.require_close (parser);
11214 return compound_stmt;
11217 /* Parse an (optional) statement-seq.
11219 statement-seq:
11220 statement
11221 statement-seq [opt] statement */
11223 static void
11224 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11226 /* Scan statements until there aren't any more. */
11227 while (true)
11229 cp_token *token = cp_lexer_peek_token (parser->lexer);
11231 /* If we are looking at a `}', then we have run out of
11232 statements; the same is true if we have reached the end
11233 of file, or have stumbled upon a stray '@end'. */
11234 if (token->type == CPP_CLOSE_BRACE
11235 || token->type == CPP_EOF
11236 || token->type == CPP_PRAGMA_EOL
11237 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11238 break;
11240 /* If we are in a compound statement and find 'else' then
11241 something went wrong. */
11242 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11244 if (parser->in_statement & IN_IF_STMT)
11245 break;
11246 else
11248 token = cp_lexer_consume_token (parser->lexer);
11249 error_at (token->location, "%<else%> without a previous %<if%>");
11253 /* Parse the statement. */
11254 cp_parser_statement (parser, in_statement_expr, true, NULL);
11258 /* Return true if we're looking at (init; cond), false otherwise. */
11260 static bool
11261 cp_parser_init_statement_p (cp_parser *parser)
11263 /* Save tokens so that we can put them back. */
11264 cp_lexer_save_tokens (parser->lexer);
11266 /* Look for ';' that is not nested in () or {}. */
11267 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11268 /*recovering=*/false,
11269 CPP_SEMICOLON,
11270 /*consume_paren=*/false);
11272 /* Roll back the tokens we skipped. */
11273 cp_lexer_rollback_tokens (parser->lexer);
11275 return ret == -1;
11278 /* Parse a selection-statement.
11280 selection-statement:
11281 if ( init-statement [opt] condition ) statement
11282 if ( init-statement [opt] condition ) statement else statement
11283 switch ( init-statement [opt] condition ) statement
11285 Returns the new IF_STMT or SWITCH_STMT.
11287 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11288 is a (possibly labeled) if statement which is not enclosed in
11289 braces and has an else clause. This is used to implement
11290 -Wparentheses.
11292 CHAIN is a vector of if-else-if conditions. This is used to implement
11293 -Wduplicated-cond. */
11295 static tree
11296 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11297 vec<tree> *chain)
11299 cp_token *token;
11300 enum rid keyword;
11301 token_indent_info guard_tinfo;
11303 if (if_p != NULL)
11304 *if_p = false;
11306 /* Peek at the next token. */
11307 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11308 guard_tinfo = get_token_indent_info (token);
11310 /* See what kind of keyword it is. */
11311 keyword = token->keyword;
11312 switch (keyword)
11314 case RID_IF:
11315 case RID_SWITCH:
11317 tree statement;
11318 tree condition;
11320 bool cx = false;
11321 if (keyword == RID_IF
11322 && cp_lexer_next_token_is_keyword (parser->lexer,
11323 RID_CONSTEXPR))
11325 cx = true;
11326 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11327 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11328 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11329 "with -std=c++17 or -std=gnu++17");
11332 /* Look for the `('. */
11333 matching_parens parens;
11334 if (!parens.require_open (parser))
11336 cp_parser_skip_to_end_of_statement (parser);
11337 return error_mark_node;
11340 /* Begin the selection-statement. */
11341 if (keyword == RID_IF)
11343 statement = begin_if_stmt ();
11344 IF_STMT_CONSTEXPR_P (statement) = cx;
11346 else
11347 statement = begin_switch_stmt ();
11349 /* Parse the optional init-statement. */
11350 if (cp_parser_init_statement_p (parser))
11352 tree decl;
11353 if (cxx_dialect < cxx17)
11354 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11355 "init-statement in selection statements only available "
11356 "with -std=c++17 or -std=gnu++17");
11357 cp_parser_init_statement (parser, &decl);
11360 /* Parse the condition. */
11361 condition = cp_parser_condition (parser);
11362 /* Look for the `)'. */
11363 if (!parens.require_close (parser))
11364 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11365 /*consume_paren=*/true);
11367 if (keyword == RID_IF)
11369 bool nested_if;
11370 unsigned char in_statement;
11372 /* Add the condition. */
11373 condition = finish_if_stmt_cond (condition, statement);
11375 if (warn_duplicated_cond)
11376 warn_duplicated_cond_add_or_warn (token->location, condition,
11377 &chain);
11379 /* Parse the then-clause. */
11380 in_statement = parser->in_statement;
11381 parser->in_statement |= IN_IF_STMT;
11383 /* Outside a template, the non-selected branch of a constexpr
11384 if is a 'discarded statement', i.e. unevaluated. */
11385 bool was_discarded = in_discarded_stmt;
11386 bool discard_then = (cx && !processing_template_decl
11387 && integer_zerop (condition));
11388 if (discard_then)
11390 in_discarded_stmt = true;
11391 ++c_inhibit_evaluation_warnings;
11394 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11395 guard_tinfo);
11397 parser->in_statement = in_statement;
11399 finish_then_clause (statement);
11401 if (discard_then)
11403 THEN_CLAUSE (statement) = NULL_TREE;
11404 in_discarded_stmt = was_discarded;
11405 --c_inhibit_evaluation_warnings;
11408 /* If the next token is `else', parse the else-clause. */
11409 if (cp_lexer_next_token_is_keyword (parser->lexer,
11410 RID_ELSE))
11412 bool discard_else = (cx && !processing_template_decl
11413 && integer_nonzerop (condition));
11414 if (discard_else)
11416 in_discarded_stmt = true;
11417 ++c_inhibit_evaluation_warnings;
11420 guard_tinfo
11421 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11422 /* Consume the `else' keyword. */
11423 cp_lexer_consume_token (parser->lexer);
11424 if (warn_duplicated_cond)
11426 if (cp_lexer_next_token_is_keyword (parser->lexer,
11427 RID_IF)
11428 && chain == NULL)
11430 /* We've got "if (COND) else if (COND2)". Start
11431 the condition chain and add COND as the first
11432 element. */
11433 chain = new vec<tree> ();
11434 if (!CONSTANT_CLASS_P (condition)
11435 && !TREE_SIDE_EFFECTS (condition))
11437 /* Wrap it in a NOP_EXPR so that we can set the
11438 location of the condition. */
11439 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11440 condition);
11441 SET_EXPR_LOCATION (e, token->location);
11442 chain->safe_push (e);
11445 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11446 RID_IF))
11448 /* This is if-else without subsequent if. Zap the
11449 condition chain; we would have already warned at
11450 this point. */
11451 delete chain;
11452 chain = NULL;
11455 begin_else_clause (statement);
11456 /* Parse the else-clause. */
11457 cp_parser_implicitly_scoped_statement (parser, NULL,
11458 guard_tinfo, chain);
11460 finish_else_clause (statement);
11462 /* If we are currently parsing a then-clause, then
11463 IF_P will not be NULL. We set it to true to
11464 indicate that this if statement has an else clause.
11465 This may trigger the Wparentheses warning below
11466 when we get back up to the parent if statement. */
11467 if (if_p != NULL)
11468 *if_p = true;
11470 if (discard_else)
11472 ELSE_CLAUSE (statement) = NULL_TREE;
11473 in_discarded_stmt = was_discarded;
11474 --c_inhibit_evaluation_warnings;
11477 else
11479 /* This if statement does not have an else clause. If
11480 NESTED_IF is true, then the then-clause has an if
11481 statement which does have an else clause. We warn
11482 about the potential ambiguity. */
11483 if (nested_if)
11484 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11485 "suggest explicit braces to avoid ambiguous"
11486 " %<else%>");
11487 if (warn_duplicated_cond)
11489 /* We don't need the condition chain anymore. */
11490 delete chain;
11491 chain = NULL;
11495 /* Now we're all done with the if-statement. */
11496 finish_if_stmt (statement);
11498 else
11500 bool in_switch_statement_p;
11501 unsigned char in_statement;
11503 /* Add the condition. */
11504 finish_switch_cond (condition, statement);
11506 /* Parse the body of the switch-statement. */
11507 in_switch_statement_p = parser->in_switch_statement_p;
11508 in_statement = parser->in_statement;
11509 parser->in_switch_statement_p = true;
11510 parser->in_statement |= IN_SWITCH_STMT;
11511 cp_parser_implicitly_scoped_statement (parser, if_p,
11512 guard_tinfo);
11513 parser->in_switch_statement_p = in_switch_statement_p;
11514 parser->in_statement = in_statement;
11516 /* Now we're all done with the switch-statement. */
11517 finish_switch_stmt (statement);
11520 return statement;
11522 break;
11524 default:
11525 cp_parser_error (parser, "expected selection-statement");
11526 return error_mark_node;
11530 /* Parse a condition.
11532 condition:
11533 expression
11534 type-specifier-seq declarator = initializer-clause
11535 type-specifier-seq declarator braced-init-list
11537 GNU Extension:
11539 condition:
11540 type-specifier-seq declarator asm-specification [opt]
11541 attributes [opt] = assignment-expression
11543 Returns the expression that should be tested. */
11545 static tree
11546 cp_parser_condition (cp_parser* parser)
11548 cp_decl_specifier_seq type_specifiers;
11549 const char *saved_message;
11550 int declares_class_or_enum;
11552 /* Try the declaration first. */
11553 cp_parser_parse_tentatively (parser);
11554 /* New types are not allowed in the type-specifier-seq for a
11555 condition. */
11556 saved_message = parser->type_definition_forbidden_message;
11557 parser->type_definition_forbidden_message
11558 = G_("types may not be defined in conditions");
11559 /* Parse the type-specifier-seq. */
11560 cp_parser_decl_specifier_seq (parser,
11561 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11562 &type_specifiers,
11563 &declares_class_or_enum);
11564 /* Restore the saved message. */
11565 parser->type_definition_forbidden_message = saved_message;
11566 /* If all is well, we might be looking at a declaration. */
11567 if (!cp_parser_error_occurred (parser))
11569 tree decl;
11570 tree asm_specification;
11571 tree attributes;
11572 cp_declarator *declarator;
11573 tree initializer = NULL_TREE;
11575 /* Parse the declarator. */
11576 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11577 /*ctor_dtor_or_conv_p=*/NULL,
11578 /*parenthesized_p=*/NULL,
11579 /*member_p=*/false,
11580 /*friend_p=*/false);
11581 /* Parse the attributes. */
11582 attributes = cp_parser_attributes_opt (parser);
11583 /* Parse the asm-specification. */
11584 asm_specification = cp_parser_asm_specification_opt (parser);
11585 /* If the next token is not an `=' or '{', then we might still be
11586 looking at an expression. For example:
11588 if (A(a).x)
11590 looks like a decl-specifier-seq and a declarator -- but then
11591 there is no `=', so this is an expression. */
11592 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11593 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11594 cp_parser_simulate_error (parser);
11596 /* If we did see an `=' or '{', then we are looking at a declaration
11597 for sure. */
11598 if (cp_parser_parse_definitely (parser))
11600 tree pushed_scope;
11601 bool non_constant_p;
11602 int flags = LOOKUP_ONLYCONVERTING;
11604 /* Create the declaration. */
11605 decl = start_decl (declarator, &type_specifiers,
11606 /*initialized_p=*/true,
11607 attributes, /*prefix_attributes=*/NULL_TREE,
11608 &pushed_scope);
11610 /* Parse the initializer. */
11611 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11613 initializer = cp_parser_braced_list (parser, &non_constant_p);
11614 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11615 flags = 0;
11617 else
11619 /* Consume the `='. */
11620 cp_parser_require (parser, CPP_EQ, RT_EQ);
11621 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11623 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11624 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11626 /* Process the initializer. */
11627 cp_finish_decl (decl,
11628 initializer, !non_constant_p,
11629 asm_specification,
11630 flags);
11632 if (pushed_scope)
11633 pop_scope (pushed_scope);
11635 return convert_from_reference (decl);
11638 /* If we didn't even get past the declarator successfully, we are
11639 definitely not looking at a declaration. */
11640 else
11641 cp_parser_abort_tentative_parse (parser);
11643 /* Otherwise, we are looking at an expression. */
11644 return cp_parser_expression (parser);
11647 /* Parses a for-statement or range-for-statement until the closing ')',
11648 not included. */
11650 static tree
11651 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11653 tree init, scope, decl;
11654 bool is_range_for;
11656 /* Begin the for-statement. */
11657 scope = begin_for_scope (&init);
11659 /* Parse the initialization. */
11660 is_range_for = cp_parser_init_statement (parser, &decl);
11662 if (is_range_for)
11663 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll);
11664 else
11665 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11668 static tree
11669 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11670 unsigned short unroll)
11672 /* Normal for loop */
11673 tree condition = NULL_TREE;
11674 tree expression = NULL_TREE;
11675 tree stmt;
11677 stmt = begin_for_stmt (scope, init);
11678 /* The init-statement has already been parsed in
11679 cp_parser_init_statement, so no work is needed here. */
11680 finish_init_stmt (stmt);
11682 /* If there's a condition, process it. */
11683 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11684 condition = cp_parser_condition (parser);
11685 else if (ivdep)
11687 cp_parser_error (parser, "missing loop condition in loop with "
11688 "%<GCC ivdep%> pragma");
11689 condition = error_mark_node;
11691 else if (unroll)
11693 cp_parser_error (parser, "missing loop condition in loop with "
11694 "%<GCC unroll%> pragma");
11695 condition = error_mark_node;
11697 finish_for_cond (condition, stmt, ivdep, unroll);
11698 /* Look for the `;'. */
11699 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11701 /* If there's an expression, process it. */
11702 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11703 expression = cp_parser_expression (parser);
11704 finish_for_expr (expression, stmt);
11706 return stmt;
11709 /* Tries to parse a range-based for-statement:
11711 range-based-for:
11712 decl-specifier-seq declarator : expression
11714 The decl-specifier-seq declarator and the `:' are already parsed by
11715 cp_parser_init_statement. If processing_template_decl it returns a
11716 newly created RANGE_FOR_STMT; if not, it is converted to a
11717 regular FOR_STMT. */
11719 static tree
11720 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11721 bool ivdep, unsigned short unroll)
11723 tree stmt, range_expr;
11724 auto_vec <cxx_binding *, 16> bindings;
11725 auto_vec <tree, 16> names;
11726 tree decomp_first_name = NULL_TREE;
11727 unsigned int decomp_cnt = 0;
11729 /* Get the range declaration momentarily out of the way so that
11730 the range expression doesn't clash with it. */
11731 if (range_decl != error_mark_node)
11733 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11735 tree v = DECL_VALUE_EXPR (range_decl);
11736 /* For decomposition declaration get all of the corresponding
11737 declarations out of the way. */
11738 if (TREE_CODE (v) == ARRAY_REF
11739 && VAR_P (TREE_OPERAND (v, 0))
11740 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11742 tree d = range_decl;
11743 range_decl = TREE_OPERAND (v, 0);
11744 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11745 decomp_first_name = d;
11746 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11748 tree name = DECL_NAME (d);
11749 names.safe_push (name);
11750 bindings.safe_push (IDENTIFIER_BINDING (name));
11751 IDENTIFIER_BINDING (name)
11752 = IDENTIFIER_BINDING (name)->previous;
11756 if (names.is_empty ())
11758 tree name = DECL_NAME (range_decl);
11759 names.safe_push (name);
11760 bindings.safe_push (IDENTIFIER_BINDING (name));
11761 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11765 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11767 bool expr_non_constant_p;
11768 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11770 else
11771 range_expr = cp_parser_expression (parser);
11773 /* Put the range declaration(s) back into scope. */
11774 for (unsigned int i = 0; i < names.length (); i++)
11776 cxx_binding *binding = bindings[i];
11777 binding->previous = IDENTIFIER_BINDING (names[i]);
11778 IDENTIFIER_BINDING (names[i]) = binding;
11781 /* If in template, STMT is converted to a normal for-statement
11782 at instantiation. If not, it is done just ahead. */
11783 if (processing_template_decl)
11785 if (check_for_bare_parameter_packs (range_expr))
11786 range_expr = error_mark_node;
11787 stmt = begin_range_for_stmt (scope, init);
11788 if (ivdep)
11789 RANGE_FOR_IVDEP (stmt) = 1;
11790 if (unroll)
11791 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
11792 finish_range_for_decl (stmt, range_decl, range_expr);
11793 if (!type_dependent_expression_p (range_expr)
11794 /* do_auto_deduction doesn't mess with template init-lists. */
11795 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11796 do_range_for_auto_deduction (range_decl, range_expr);
11798 else
11800 stmt = begin_for_stmt (scope, init);
11801 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11802 decomp_first_name, decomp_cnt, ivdep,
11803 unroll);
11805 return stmt;
11808 /* Subroutine of cp_convert_range_for: given the initializer expression,
11809 builds up the range temporary. */
11811 static tree
11812 build_range_temp (tree range_expr)
11814 tree range_type, range_temp;
11816 /* Find out the type deduced by the declaration
11817 `auto &&__range = range_expr'. */
11818 range_type = cp_build_reference_type (make_auto (), true);
11819 range_type = do_auto_deduction (range_type, range_expr,
11820 type_uses_auto (range_type));
11822 /* Create the __range variable. */
11823 range_temp = build_decl (input_location, VAR_DECL,
11824 get_identifier ("__for_range"), range_type);
11825 TREE_USED (range_temp) = 1;
11826 DECL_ARTIFICIAL (range_temp) = 1;
11828 return range_temp;
11831 /* Used by cp_parser_range_for in template context: we aren't going to
11832 do a full conversion yet, but we still need to resolve auto in the
11833 type of the for-range-declaration if present. This is basically
11834 a shortcut version of cp_convert_range_for. */
11836 static void
11837 do_range_for_auto_deduction (tree decl, tree range_expr)
11839 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11840 if (auto_node)
11842 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11843 range_temp = convert_from_reference (build_range_temp (range_expr));
11844 iter_type = (cp_parser_perform_range_for_lookup
11845 (range_temp, &begin_dummy, &end_dummy));
11846 if (iter_type)
11848 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11849 iter_type);
11850 iter_decl = build_x_indirect_ref (input_location, iter_decl,
11851 RO_UNARY_STAR,
11852 tf_warning_or_error);
11853 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11854 iter_decl, auto_node);
11859 /* Converts a range-based for-statement into a normal
11860 for-statement, as per the definition.
11862 for (RANGE_DECL : RANGE_EXPR)
11863 BLOCK
11865 should be equivalent to:
11868 auto &&__range = RANGE_EXPR;
11869 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11870 __begin != __end;
11871 ++__begin)
11873 RANGE_DECL = *__begin;
11874 BLOCK
11878 If RANGE_EXPR is an array:
11879 BEGIN_EXPR = __range
11880 END_EXPR = __range + ARRAY_SIZE(__range)
11881 Else if RANGE_EXPR has a member 'begin' or 'end':
11882 BEGIN_EXPR = __range.begin()
11883 END_EXPR = __range.end()
11884 Else:
11885 BEGIN_EXPR = begin(__range)
11886 END_EXPR = end(__range);
11888 If __range has a member 'begin' but not 'end', or vice versa, we must
11889 still use the second alternative (it will surely fail, however).
11890 When calling begin()/end() in the third alternative we must use
11891 argument dependent lookup, but always considering 'std' as an associated
11892 namespace. */
11894 tree
11895 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11896 tree decomp_first_name, unsigned int decomp_cnt,
11897 bool ivdep, unsigned short unroll)
11899 tree begin, end;
11900 tree iter_type, begin_expr, end_expr;
11901 tree condition, expression;
11903 range_expr = mark_lvalue_use (range_expr);
11905 if (range_decl == error_mark_node || range_expr == error_mark_node)
11906 /* If an error happened previously do nothing or else a lot of
11907 unhelpful errors would be issued. */
11908 begin_expr = end_expr = iter_type = error_mark_node;
11909 else
11911 tree range_temp;
11913 if (VAR_P (range_expr)
11914 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11915 /* Can't bind a reference to an array of runtime bound. */
11916 range_temp = range_expr;
11917 else
11919 range_temp = build_range_temp (range_expr);
11920 pushdecl (range_temp);
11921 cp_finish_decl (range_temp, range_expr,
11922 /*is_constant_init*/false, NULL_TREE,
11923 LOOKUP_ONLYCONVERTING);
11924 range_temp = convert_from_reference (range_temp);
11926 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11927 &begin_expr, &end_expr);
11930 /* The new for initialization statement. */
11931 begin = build_decl (input_location, VAR_DECL,
11932 get_identifier ("__for_begin"), iter_type);
11933 TREE_USED (begin) = 1;
11934 DECL_ARTIFICIAL (begin) = 1;
11935 pushdecl (begin);
11936 cp_finish_decl (begin, begin_expr,
11937 /*is_constant_init*/false, NULL_TREE,
11938 LOOKUP_ONLYCONVERTING);
11940 if (cxx_dialect >= cxx17)
11941 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11942 end = build_decl (input_location, VAR_DECL,
11943 get_identifier ("__for_end"), iter_type);
11944 TREE_USED (end) = 1;
11945 DECL_ARTIFICIAL (end) = 1;
11946 pushdecl (end);
11947 cp_finish_decl (end, end_expr,
11948 /*is_constant_init*/false, NULL_TREE,
11949 LOOKUP_ONLYCONVERTING);
11951 finish_init_stmt (statement);
11953 /* The new for condition. */
11954 condition = build_x_binary_op (input_location, NE_EXPR,
11955 begin, ERROR_MARK,
11956 end, ERROR_MARK,
11957 NULL, tf_warning_or_error);
11958 finish_for_cond (condition, statement, ivdep, unroll);
11960 /* The new increment expression. */
11961 expression = finish_unary_op_expr (input_location,
11962 PREINCREMENT_EXPR, begin,
11963 tf_warning_or_error);
11964 finish_for_expr (expression, statement);
11966 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11967 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
11969 /* The declaration is initialized with *__begin inside the loop body. */
11970 cp_finish_decl (range_decl,
11971 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
11972 tf_warning_or_error),
11973 /*is_constant_init*/false, NULL_TREE,
11974 LOOKUP_ONLYCONVERTING);
11975 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11976 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
11978 return statement;
11981 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11982 We need to solve both at the same time because the method used
11983 depends on the existence of members begin or end.
11984 Returns the type deduced for the iterator expression. */
11986 static tree
11987 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11989 if (error_operand_p (range))
11991 *begin = *end = error_mark_node;
11992 return error_mark_node;
11995 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11997 error ("range-based %<for%> expression of type %qT "
11998 "has incomplete type", TREE_TYPE (range));
11999 *begin = *end = error_mark_node;
12000 return error_mark_node;
12002 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12004 /* If RANGE is an array, we will use pointer arithmetic. */
12005 *begin = decay_conversion (range, tf_warning_or_error);
12006 *end = build_binary_op (input_location, PLUS_EXPR,
12007 range,
12008 array_type_nelts_top (TREE_TYPE (range)),
12009 false);
12010 return TREE_TYPE (*begin);
12012 else
12014 /* If it is not an array, we must do a bit of magic. */
12015 tree id_begin, id_end;
12016 tree member_begin, member_end;
12018 *begin = *end = error_mark_node;
12020 id_begin = get_identifier ("begin");
12021 id_end = get_identifier ("end");
12022 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12023 /*protect=*/2, /*want_type=*/false,
12024 tf_warning_or_error);
12025 member_end = lookup_member (TREE_TYPE (range), id_end,
12026 /*protect=*/2, /*want_type=*/false,
12027 tf_warning_or_error);
12029 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12031 /* Use the member functions. */
12032 *begin = cp_parser_range_for_member_function (range, id_begin);
12033 *end = cp_parser_range_for_member_function (range, id_end);
12035 else
12037 /* Use global functions with ADL. */
12038 vec<tree, va_gc> *vec;
12039 vec = make_tree_vector ();
12041 vec_safe_push (vec, range);
12043 member_begin = perform_koenig_lookup (id_begin, vec,
12044 tf_warning_or_error);
12045 *begin = finish_call_expr (member_begin, &vec, false, true,
12046 tf_warning_or_error);
12047 member_end = perform_koenig_lookup (id_end, vec,
12048 tf_warning_or_error);
12049 *end = finish_call_expr (member_end, &vec, false, true,
12050 tf_warning_or_error);
12052 release_tree_vector (vec);
12055 /* Last common checks. */
12056 if (*begin == error_mark_node || *end == error_mark_node)
12058 /* If one of the expressions is an error do no more checks. */
12059 *begin = *end = error_mark_node;
12060 return error_mark_node;
12062 else if (type_dependent_expression_p (*begin)
12063 || type_dependent_expression_p (*end))
12064 /* Can happen, when, eg, in a template context, Koenig lookup
12065 can't resolve begin/end (c++/58503). */
12066 return NULL_TREE;
12067 else
12069 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12070 /* The unqualified type of the __begin and __end temporaries should
12071 be the same, as required by the multiple auto declaration. */
12072 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12074 if (cxx_dialect >= cxx17
12075 && (build_x_binary_op (input_location, NE_EXPR,
12076 *begin, ERROR_MARK,
12077 *end, ERROR_MARK,
12078 NULL, tf_none)
12079 != error_mark_node))
12080 /* P0184R0 allows __begin and __end to have different types,
12081 but make sure they are comparable so we can give a better
12082 diagnostic. */;
12083 else
12084 error ("inconsistent begin/end types in range-based %<for%> "
12085 "statement: %qT and %qT",
12086 TREE_TYPE (*begin), TREE_TYPE (*end));
12088 return iter_type;
12093 /* Helper function for cp_parser_perform_range_for_lookup.
12094 Builds a tree for RANGE.IDENTIFIER(). */
12096 static tree
12097 cp_parser_range_for_member_function (tree range, tree identifier)
12099 tree member, res;
12100 vec<tree, va_gc> *vec;
12102 member = finish_class_member_access_expr (range, identifier,
12103 false, tf_warning_or_error);
12104 if (member == error_mark_node)
12105 return error_mark_node;
12107 vec = make_tree_vector ();
12108 res = finish_call_expr (member, &vec,
12109 /*disallow_virtual=*/false,
12110 /*koenig_p=*/false,
12111 tf_warning_or_error);
12112 release_tree_vector (vec);
12113 return res;
12116 /* Parse an iteration-statement.
12118 iteration-statement:
12119 while ( condition ) statement
12120 do statement while ( expression ) ;
12121 for ( init-statement condition [opt] ; expression [opt] )
12122 statement
12124 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12126 static tree
12127 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12128 unsigned short unroll)
12130 cp_token *token;
12131 enum rid keyword;
12132 tree statement;
12133 unsigned char in_statement;
12134 token_indent_info guard_tinfo;
12136 /* Peek at the next token. */
12137 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12138 if (!token)
12139 return error_mark_node;
12141 guard_tinfo = get_token_indent_info (token);
12143 /* Remember whether or not we are already within an iteration
12144 statement. */
12145 in_statement = parser->in_statement;
12147 /* See what kind of keyword it is. */
12148 keyword = token->keyword;
12149 switch (keyword)
12151 case RID_WHILE:
12153 tree condition;
12155 /* Begin the while-statement. */
12156 statement = begin_while_stmt ();
12157 /* Look for the `('. */
12158 matching_parens parens;
12159 parens.require_open (parser);
12160 /* Parse the condition. */
12161 condition = cp_parser_condition (parser);
12162 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12163 /* Look for the `)'. */
12164 parens.require_close (parser);
12165 /* Parse the dependent statement. */
12166 parser->in_statement = IN_ITERATION_STMT;
12167 bool prev = note_iteration_stmt_body_start ();
12168 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12169 note_iteration_stmt_body_end (prev);
12170 parser->in_statement = in_statement;
12171 /* We're done with the while-statement. */
12172 finish_while_stmt (statement);
12174 break;
12176 case RID_DO:
12178 tree expression;
12180 /* Begin the do-statement. */
12181 statement = begin_do_stmt ();
12182 /* Parse the body of the do-statement. */
12183 parser->in_statement = IN_ITERATION_STMT;
12184 bool prev = note_iteration_stmt_body_start ();
12185 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12186 note_iteration_stmt_body_end (prev);
12187 parser->in_statement = in_statement;
12188 finish_do_body (statement);
12189 /* Look for the `while' keyword. */
12190 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12191 /* Look for the `('. */
12192 matching_parens parens;
12193 parens.require_open (parser);
12194 /* Parse the expression. */
12195 expression = cp_parser_expression (parser);
12196 /* We're done with the do-statement. */
12197 finish_do_stmt (expression, statement, ivdep, unroll);
12198 /* Look for the `)'. */
12199 parens.require_close (parser);
12200 /* Look for the `;'. */
12201 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12203 break;
12205 case RID_FOR:
12207 /* Look for the `('. */
12208 matching_parens parens;
12209 parens.require_open (parser);
12211 statement = cp_parser_for (parser, ivdep, unroll);
12213 /* Look for the `)'. */
12214 parens.require_close (parser);
12216 /* Parse the body of the for-statement. */
12217 parser->in_statement = IN_ITERATION_STMT;
12218 bool prev = note_iteration_stmt_body_start ();
12219 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12220 note_iteration_stmt_body_end (prev);
12221 parser->in_statement = in_statement;
12223 /* We're done with the for-statement. */
12224 finish_for_stmt (statement);
12226 break;
12228 default:
12229 cp_parser_error (parser, "expected iteration-statement");
12230 statement = error_mark_node;
12231 break;
12234 return statement;
12237 /* Parse a init-statement or the declarator of a range-based-for.
12238 Returns true if a range-based-for declaration is seen.
12240 init-statement:
12241 expression-statement
12242 simple-declaration */
12244 static bool
12245 cp_parser_init_statement (cp_parser* parser, tree *decl)
12247 /* If the next token is a `;', then we have an empty
12248 expression-statement. Grammatically, this is also a
12249 simple-declaration, but an invalid one, because it does not
12250 declare anything. Therefore, if we did not handle this case
12251 specially, we would issue an error message about an invalid
12252 declaration. */
12253 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12255 bool is_range_for = false;
12256 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12258 /* A colon is used in range-based for. */
12259 parser->colon_corrects_to_scope_p = false;
12261 /* We're going to speculatively look for a declaration, falling back
12262 to an expression, if necessary. */
12263 cp_parser_parse_tentatively (parser);
12264 /* Parse the declaration. */
12265 cp_parser_simple_declaration (parser,
12266 /*function_definition_allowed_p=*/false,
12267 decl);
12268 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12269 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12271 /* It is a range-for, consume the ':' */
12272 cp_lexer_consume_token (parser->lexer);
12273 is_range_for = true;
12274 if (cxx_dialect < cxx11)
12276 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12277 "range-based %<for%> loops only available with "
12278 "-std=c++11 or -std=gnu++11");
12279 *decl = error_mark_node;
12282 else
12283 /* The ';' is not consumed yet because we told
12284 cp_parser_simple_declaration not to. */
12285 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12287 if (cp_parser_parse_definitely (parser))
12288 return is_range_for;
12289 /* If the tentative parse failed, then we shall need to look for an
12290 expression-statement. */
12292 /* If we are here, it is an expression-statement. */
12293 cp_parser_expression_statement (parser, NULL_TREE);
12294 return false;
12297 /* Parse a jump-statement.
12299 jump-statement:
12300 break ;
12301 continue ;
12302 return expression [opt] ;
12303 return braced-init-list ;
12304 goto identifier ;
12306 GNU extension:
12308 jump-statement:
12309 goto * expression ;
12311 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12313 static tree
12314 cp_parser_jump_statement (cp_parser* parser)
12316 tree statement = error_mark_node;
12317 cp_token *token;
12318 enum rid keyword;
12319 unsigned char in_statement;
12321 /* Peek at the next token. */
12322 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12323 if (!token)
12324 return error_mark_node;
12326 /* See what kind of keyword it is. */
12327 keyword = token->keyword;
12328 switch (keyword)
12330 case RID_BREAK:
12331 in_statement = parser->in_statement & ~IN_IF_STMT;
12332 switch (in_statement)
12334 case 0:
12335 error_at (token->location, "break statement not within loop or switch");
12336 break;
12337 default:
12338 gcc_assert ((in_statement & IN_SWITCH_STMT)
12339 || in_statement == IN_ITERATION_STMT);
12340 statement = finish_break_stmt ();
12341 if (in_statement == IN_ITERATION_STMT)
12342 break_maybe_infinite_loop ();
12343 break;
12344 case IN_OMP_BLOCK:
12345 error_at (token->location, "invalid exit from OpenMP structured block");
12346 break;
12347 case IN_OMP_FOR:
12348 error_at (token->location, "break statement used with OpenMP for loop");
12349 break;
12351 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12352 break;
12354 case RID_CONTINUE:
12355 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12357 case 0:
12358 error_at (token->location, "continue statement not within a loop");
12359 break;
12360 /* Fall through. */
12361 case IN_ITERATION_STMT:
12362 case IN_OMP_FOR:
12363 statement = finish_continue_stmt ();
12364 break;
12365 case IN_OMP_BLOCK:
12366 error_at (token->location, "invalid exit from OpenMP structured block");
12367 break;
12368 default:
12369 gcc_unreachable ();
12371 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12372 break;
12374 case RID_RETURN:
12376 tree expr;
12377 bool expr_non_constant_p;
12379 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12381 cp_lexer_set_source_position (parser->lexer);
12382 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12383 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12385 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12386 expr = cp_parser_expression (parser);
12387 else
12388 /* If the next token is a `;', then there is no
12389 expression. */
12390 expr = NULL_TREE;
12391 /* Build the return-statement. */
12392 if (current_function_auto_return_pattern && in_discarded_stmt)
12393 /* Don't deduce from a discarded return statement. */;
12394 else
12395 statement = finish_return_stmt (expr);
12396 /* Look for the final `;'. */
12397 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12399 break;
12401 case RID_GOTO:
12402 if (parser->in_function_body
12403 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12405 error ("%<goto%> in %<constexpr%> function");
12406 cp_function_chain->invalid_constexpr = true;
12409 /* Create the goto-statement. */
12410 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12412 /* Issue a warning about this use of a GNU extension. */
12413 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12414 /* Consume the '*' token. */
12415 cp_lexer_consume_token (parser->lexer);
12416 /* Parse the dependent expression. */
12417 finish_goto_stmt (cp_parser_expression (parser));
12419 else
12420 finish_goto_stmt (cp_parser_identifier (parser));
12421 /* Look for the final `;'. */
12422 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12423 break;
12425 default:
12426 cp_parser_error (parser, "expected jump-statement");
12427 break;
12430 return statement;
12433 /* Parse a declaration-statement.
12435 declaration-statement:
12436 block-declaration */
12438 static void
12439 cp_parser_declaration_statement (cp_parser* parser)
12441 void *p;
12443 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12444 p = obstack_alloc (&declarator_obstack, 0);
12446 /* Parse the block-declaration. */
12447 cp_parser_block_declaration (parser, /*statement_p=*/true);
12449 /* Free any declarators allocated. */
12450 obstack_free (&declarator_obstack, p);
12453 /* Some dependent statements (like `if (cond) statement'), are
12454 implicitly in their own scope. In other words, if the statement is
12455 a single statement (as opposed to a compound-statement), it is
12456 none-the-less treated as if it were enclosed in braces. Any
12457 declarations appearing in the dependent statement are out of scope
12458 after control passes that point. This function parses a statement,
12459 but ensures that is in its own scope, even if it is not a
12460 compound-statement.
12462 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12463 is a (possibly labeled) if statement which is not enclosed in
12464 braces and has an else clause. This is used to implement
12465 -Wparentheses.
12467 CHAIN is a vector of if-else-if conditions. This is used to implement
12468 -Wduplicated-cond.
12470 Returns the new statement. */
12472 static tree
12473 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12474 const token_indent_info &guard_tinfo,
12475 vec<tree> *chain)
12477 tree statement;
12478 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12479 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12480 token_indent_info body_tinfo
12481 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12483 if (if_p != NULL)
12484 *if_p = false;
12486 /* Mark if () ; with a special NOP_EXPR. */
12487 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12489 cp_lexer_consume_token (parser->lexer);
12490 statement = add_stmt (build_empty_stmt (body_loc));
12492 if (guard_tinfo.keyword == RID_IF
12493 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12494 warning_at (body_loc, OPT_Wempty_body,
12495 "suggest braces around empty body in an %<if%> statement");
12496 else if (guard_tinfo.keyword == RID_ELSE)
12497 warning_at (body_loc, OPT_Wempty_body,
12498 "suggest braces around empty body in an %<else%> statement");
12500 /* if a compound is opened, we simply parse the statement directly. */
12501 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12502 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12503 /* If the token is not a `{', then we must take special action. */
12504 else
12506 /* Create a compound-statement. */
12507 statement = begin_compound_stmt (0);
12508 /* Parse the dependent-statement. */
12509 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12510 &body_loc_after_labels);
12511 /* Finish the dummy compound-statement. */
12512 finish_compound_stmt (statement);
12515 token_indent_info next_tinfo
12516 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12517 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12519 if (body_loc_after_labels != UNKNOWN_LOCATION
12520 && next_tinfo.type != CPP_SEMICOLON)
12521 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12522 guard_tinfo.location, guard_tinfo.keyword);
12524 /* Return the statement. */
12525 return statement;
12528 /* For some dependent statements (like `while (cond) statement'), we
12529 have already created a scope. Therefore, even if the dependent
12530 statement is a compound-statement, we do not want to create another
12531 scope. */
12533 static void
12534 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12535 const token_indent_info &guard_tinfo)
12537 /* If the token is a `{', then we must take special action. */
12538 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12540 token_indent_info body_tinfo
12541 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12542 location_t loc_after_labels = UNKNOWN_LOCATION;
12544 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12545 &loc_after_labels);
12546 token_indent_info next_tinfo
12547 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12548 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12550 if (loc_after_labels != UNKNOWN_LOCATION
12551 && next_tinfo.type != CPP_SEMICOLON)
12552 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12553 guard_tinfo.location,
12554 guard_tinfo.keyword);
12556 else
12558 /* Avoid calling cp_parser_compound_statement, so that we
12559 don't create a new scope. Do everything else by hand. */
12560 matching_braces braces;
12561 braces.require_open (parser);
12562 /* If the next keyword is `__label__' we have a label declaration. */
12563 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12564 cp_parser_label_declaration (parser);
12565 /* Parse an (optional) statement-seq. */
12566 cp_parser_statement_seq_opt (parser, NULL_TREE);
12567 braces.require_close (parser);
12571 /* Declarations [gram.dcl.dcl] */
12573 /* Parse an optional declaration-sequence.
12575 declaration-seq:
12576 declaration
12577 declaration-seq declaration */
12579 static void
12580 cp_parser_declaration_seq_opt (cp_parser* parser)
12582 while (true)
12584 cp_token *token;
12586 token = cp_lexer_peek_token (parser->lexer);
12588 if (token->type == CPP_CLOSE_BRACE
12589 || token->type == CPP_EOF
12590 || token->type == CPP_PRAGMA_EOL)
12591 break;
12593 if (token->type == CPP_SEMICOLON)
12595 /* A declaration consisting of a single semicolon is
12596 invalid. Allow it unless we're being pedantic. */
12597 cp_lexer_consume_token (parser->lexer);
12598 if (!in_system_header_at (input_location))
12599 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12600 continue;
12603 /* If we're entering or exiting a region that's implicitly
12604 extern "C", modify the lang context appropriately. */
12605 if (!parser->implicit_extern_c && token->implicit_extern_c)
12607 push_lang_context (lang_name_c);
12608 parser->implicit_extern_c = true;
12610 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12612 pop_lang_context ();
12613 parser->implicit_extern_c = false;
12616 if (token->type == CPP_PRAGMA)
12618 /* A top-level declaration can consist solely of a #pragma.
12619 A nested declaration cannot, so this is done here and not
12620 in cp_parser_declaration. (A #pragma at block scope is
12621 handled in cp_parser_statement.) */
12622 cp_parser_pragma (parser, pragma_external, NULL);
12623 continue;
12626 /* Parse the declaration itself. */
12627 cp_parser_declaration (parser);
12631 /* Parse a declaration.
12633 declaration:
12634 block-declaration
12635 function-definition
12636 template-declaration
12637 explicit-instantiation
12638 explicit-specialization
12639 linkage-specification
12640 namespace-definition
12642 C++17:
12643 deduction-guide
12645 GNU extension:
12647 declaration:
12648 __extension__ declaration */
12650 static void
12651 cp_parser_declaration (cp_parser* parser)
12653 cp_token token1;
12654 cp_token token2;
12655 int saved_pedantic;
12656 void *p;
12657 tree attributes = NULL_TREE;
12659 /* Check for the `__extension__' keyword. */
12660 if (cp_parser_extension_opt (parser, &saved_pedantic))
12662 /* Parse the qualified declaration. */
12663 cp_parser_declaration (parser);
12664 /* Restore the PEDANTIC flag. */
12665 pedantic = saved_pedantic;
12667 return;
12670 /* Try to figure out what kind of declaration is present. */
12671 token1 = *cp_lexer_peek_token (parser->lexer);
12673 if (token1.type != CPP_EOF)
12674 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12675 else
12677 token2.type = CPP_EOF;
12678 token2.keyword = RID_MAX;
12681 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12682 p = obstack_alloc (&declarator_obstack, 0);
12684 /* If the next token is `extern' and the following token is a string
12685 literal, then we have a linkage specification. */
12686 if (token1.keyword == RID_EXTERN
12687 && cp_parser_is_pure_string_literal (&token2))
12688 cp_parser_linkage_specification (parser);
12689 /* If the next token is `template', then we have either a template
12690 declaration, an explicit instantiation, or an explicit
12691 specialization. */
12692 else if (token1.keyword == RID_TEMPLATE)
12694 /* `template <>' indicates a template specialization. */
12695 if (token2.type == CPP_LESS
12696 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12697 cp_parser_explicit_specialization (parser);
12698 /* `template <' indicates a template declaration. */
12699 else if (token2.type == CPP_LESS)
12700 cp_parser_template_declaration (parser, /*member_p=*/false);
12701 /* Anything else must be an explicit instantiation. */
12702 else
12703 cp_parser_explicit_instantiation (parser);
12705 /* If the next token is `export', then we have a template
12706 declaration. */
12707 else if (token1.keyword == RID_EXPORT)
12708 cp_parser_template_declaration (parser, /*member_p=*/false);
12709 /* If the next token is `extern', 'static' or 'inline' and the one
12710 after that is `template', we have a GNU extended explicit
12711 instantiation directive. */
12712 else if (cp_parser_allow_gnu_extensions_p (parser)
12713 && (token1.keyword == RID_EXTERN
12714 || token1.keyword == RID_STATIC
12715 || token1.keyword == RID_INLINE)
12716 && token2.keyword == RID_TEMPLATE)
12717 cp_parser_explicit_instantiation (parser);
12718 /* If the next token is `namespace', check for a named or unnamed
12719 namespace definition. */
12720 else if (token1.keyword == RID_NAMESPACE
12721 && (/* A named namespace definition. */
12722 (token2.type == CPP_NAME
12723 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12724 != CPP_EQ))
12725 || (token2.type == CPP_OPEN_SQUARE
12726 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12727 == CPP_OPEN_SQUARE)
12728 /* An unnamed namespace definition. */
12729 || token2.type == CPP_OPEN_BRACE
12730 || token2.keyword == RID_ATTRIBUTE))
12731 cp_parser_namespace_definition (parser);
12732 /* An inline (associated) namespace definition. */
12733 else if (token1.keyword == RID_INLINE
12734 && token2.keyword == RID_NAMESPACE)
12735 cp_parser_namespace_definition (parser);
12736 /* Objective-C++ declaration/definition. */
12737 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12738 cp_parser_objc_declaration (parser, NULL_TREE);
12739 else if (c_dialect_objc ()
12740 && token1.keyword == RID_ATTRIBUTE
12741 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12742 cp_parser_objc_declaration (parser, attributes);
12743 /* At this point we may have a template declared by a concept
12744 introduction. */
12745 else if (flag_concepts
12746 && cp_parser_template_declaration_after_export (parser,
12747 /*member_p=*/false))
12748 /* We did. */;
12749 else
12750 /* Try to parse a block-declaration, or a function-definition. */
12751 cp_parser_block_declaration (parser, /*statement_p=*/false);
12753 /* Free any declarators allocated. */
12754 obstack_free (&declarator_obstack, p);
12757 /* Parse a block-declaration.
12759 block-declaration:
12760 simple-declaration
12761 asm-definition
12762 namespace-alias-definition
12763 using-declaration
12764 using-directive
12766 GNU Extension:
12768 block-declaration:
12769 __extension__ block-declaration
12771 C++0x Extension:
12773 block-declaration:
12774 static_assert-declaration
12776 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12777 part of a declaration-statement. */
12779 static void
12780 cp_parser_block_declaration (cp_parser *parser,
12781 bool statement_p)
12783 cp_token *token1;
12784 int saved_pedantic;
12786 /* Check for the `__extension__' keyword. */
12787 if (cp_parser_extension_opt (parser, &saved_pedantic))
12789 /* Parse the qualified declaration. */
12790 cp_parser_block_declaration (parser, statement_p);
12791 /* Restore the PEDANTIC flag. */
12792 pedantic = saved_pedantic;
12794 return;
12797 /* Peek at the next token to figure out which kind of declaration is
12798 present. */
12799 token1 = cp_lexer_peek_token (parser->lexer);
12801 /* If the next keyword is `asm', we have an asm-definition. */
12802 if (token1->keyword == RID_ASM)
12804 if (statement_p)
12805 cp_parser_commit_to_tentative_parse (parser);
12806 cp_parser_asm_definition (parser);
12808 /* If the next keyword is `namespace', we have a
12809 namespace-alias-definition. */
12810 else if (token1->keyword == RID_NAMESPACE)
12811 cp_parser_namespace_alias_definition (parser);
12812 /* If the next keyword is `using', we have a
12813 using-declaration, a using-directive, or an alias-declaration. */
12814 else if (token1->keyword == RID_USING)
12816 cp_token *token2;
12818 if (statement_p)
12819 cp_parser_commit_to_tentative_parse (parser);
12820 /* If the token after `using' is `namespace', then we have a
12821 using-directive. */
12822 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12823 if (token2->keyword == RID_NAMESPACE)
12824 cp_parser_using_directive (parser);
12825 /* If the second token after 'using' is '=', then we have an
12826 alias-declaration. */
12827 else if (cxx_dialect >= cxx11
12828 && token2->type == CPP_NAME
12829 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12830 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12831 cp_parser_alias_declaration (parser);
12832 /* Otherwise, it's a using-declaration. */
12833 else
12834 cp_parser_using_declaration (parser,
12835 /*access_declaration_p=*/false);
12837 /* If the next keyword is `__label__' we have a misplaced label
12838 declaration. */
12839 else if (token1->keyword == RID_LABEL)
12841 cp_lexer_consume_token (parser->lexer);
12842 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12843 cp_parser_skip_to_end_of_statement (parser);
12844 /* If the next token is now a `;', consume it. */
12845 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12846 cp_lexer_consume_token (parser->lexer);
12848 /* If the next token is `static_assert' we have a static assertion. */
12849 else if (token1->keyword == RID_STATIC_ASSERT)
12850 cp_parser_static_assert (parser, /*member_p=*/false);
12851 /* Anything else must be a simple-declaration. */
12852 else
12853 cp_parser_simple_declaration (parser, !statement_p,
12854 /*maybe_range_for_decl*/NULL);
12857 /* Parse a simple-declaration.
12859 simple-declaration:
12860 decl-specifier-seq [opt] init-declarator-list [opt] ;
12861 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12862 brace-or-equal-initializer ;
12864 init-declarator-list:
12865 init-declarator
12866 init-declarator-list , init-declarator
12868 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12869 function-definition as a simple-declaration.
12871 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12872 parsed declaration if it is an uninitialized single declarator not followed
12873 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12874 if present, will not be consumed. */
12876 static void
12877 cp_parser_simple_declaration (cp_parser* parser,
12878 bool function_definition_allowed_p,
12879 tree *maybe_range_for_decl)
12881 cp_decl_specifier_seq decl_specifiers;
12882 int declares_class_or_enum;
12883 bool saw_declarator;
12884 location_t comma_loc = UNKNOWN_LOCATION;
12885 location_t init_loc = UNKNOWN_LOCATION;
12887 if (maybe_range_for_decl)
12888 *maybe_range_for_decl = NULL_TREE;
12890 /* Defer access checks until we know what is being declared; the
12891 checks for names appearing in the decl-specifier-seq should be
12892 done as if we were in the scope of the thing being declared. */
12893 push_deferring_access_checks (dk_deferred);
12895 /* Parse the decl-specifier-seq. We have to keep track of whether
12896 or not the decl-specifier-seq declares a named class or
12897 enumeration type, since that is the only case in which the
12898 init-declarator-list is allowed to be empty.
12900 [dcl.dcl]
12902 In a simple-declaration, the optional init-declarator-list can be
12903 omitted only when declaring a class or enumeration, that is when
12904 the decl-specifier-seq contains either a class-specifier, an
12905 elaborated-type-specifier, or an enum-specifier. */
12906 cp_parser_decl_specifier_seq (parser,
12907 CP_PARSER_FLAGS_OPTIONAL,
12908 &decl_specifiers,
12909 &declares_class_or_enum);
12910 /* We no longer need to defer access checks. */
12911 stop_deferring_access_checks ();
12913 /* In a block scope, a valid declaration must always have a
12914 decl-specifier-seq. By not trying to parse declarators, we can
12915 resolve the declaration/expression ambiguity more quickly. */
12916 if (!function_definition_allowed_p
12917 && !decl_specifiers.any_specifiers_p)
12919 cp_parser_error (parser, "expected declaration");
12920 goto done;
12923 /* If the next two tokens are both identifiers, the code is
12924 erroneous. The usual cause of this situation is code like:
12926 T t;
12928 where "T" should name a type -- but does not. */
12929 if (!decl_specifiers.any_type_specifiers_p
12930 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12932 /* If parsing tentatively, we should commit; we really are
12933 looking at a declaration. */
12934 cp_parser_commit_to_tentative_parse (parser);
12935 /* Give up. */
12936 goto done;
12939 /* If we have seen at least one decl-specifier, and the next token
12940 is not a parenthesis, then we must be looking at a declaration.
12941 (After "int (" we might be looking at a functional cast.) */
12942 if (decl_specifiers.any_specifiers_p
12943 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12944 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12945 && !cp_parser_error_occurred (parser))
12946 cp_parser_commit_to_tentative_parse (parser);
12948 /* Look for C++17 decomposition declaration. */
12949 for (size_t n = 1; ; n++)
12950 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12951 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12952 continue;
12953 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12954 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12955 && decl_specifiers.any_specifiers_p)
12957 tree decl
12958 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12959 maybe_range_for_decl,
12960 &init_loc);
12962 /* The next token should be either a `,' or a `;'. */
12963 cp_token *token = cp_lexer_peek_token (parser->lexer);
12964 /* If it's a `;', we are done. */
12965 if (token->type == CPP_SEMICOLON)
12966 goto finish;
12967 else if (maybe_range_for_decl)
12969 if (*maybe_range_for_decl == NULL_TREE)
12970 *maybe_range_for_decl = error_mark_node;
12971 goto finish;
12973 /* Anything else is an error. */
12974 else
12976 /* If we have already issued an error message we don't need
12977 to issue another one. */
12978 if ((decl != error_mark_node
12979 && DECL_INITIAL (decl) != error_mark_node)
12980 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12981 cp_parser_error (parser, "expected %<,%> or %<;%>");
12982 /* Skip tokens until we reach the end of the statement. */
12983 cp_parser_skip_to_end_of_statement (parser);
12984 /* If the next token is now a `;', consume it. */
12985 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12986 cp_lexer_consume_token (parser->lexer);
12987 goto done;
12990 else
12991 break;
12993 tree last_type;
12994 bool auto_specifier_p;
12995 /* NULL_TREE if both variable and function declaration are allowed,
12996 error_mark_node if function declaration are not allowed and
12997 a FUNCTION_DECL that should be diagnosed if it is followed by
12998 variable declarations. */
12999 tree auto_function_declaration;
13001 last_type = NULL_TREE;
13002 auto_specifier_p
13003 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13004 auto_function_declaration = NULL_TREE;
13006 /* Keep going until we hit the `;' at the end of the simple
13007 declaration. */
13008 saw_declarator = false;
13009 while (cp_lexer_next_token_is_not (parser->lexer,
13010 CPP_SEMICOLON))
13012 cp_token *token;
13013 bool function_definition_p;
13014 tree decl;
13015 tree auto_result = NULL_TREE;
13017 if (saw_declarator)
13019 /* If we are processing next declarator, comma is expected */
13020 token = cp_lexer_peek_token (parser->lexer);
13021 gcc_assert (token->type == CPP_COMMA);
13022 cp_lexer_consume_token (parser->lexer);
13023 if (maybe_range_for_decl)
13025 *maybe_range_for_decl = error_mark_node;
13026 if (comma_loc == UNKNOWN_LOCATION)
13027 comma_loc = token->location;
13030 else
13031 saw_declarator = true;
13033 /* Parse the init-declarator. */
13034 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13035 /*checks=*/NULL,
13036 function_definition_allowed_p,
13037 /*member_p=*/false,
13038 declares_class_or_enum,
13039 &function_definition_p,
13040 maybe_range_for_decl,
13041 &init_loc,
13042 &auto_result);
13043 /* If an error occurred while parsing tentatively, exit quickly.
13044 (That usually happens when in the body of a function; each
13045 statement is treated as a declaration-statement until proven
13046 otherwise.) */
13047 if (cp_parser_error_occurred (parser))
13048 goto done;
13050 if (auto_specifier_p && cxx_dialect >= cxx14)
13052 /* If the init-declarator-list contains more than one
13053 init-declarator, they shall all form declarations of
13054 variables. */
13055 if (auto_function_declaration == NULL_TREE)
13056 auto_function_declaration
13057 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13058 else if (TREE_CODE (decl) == FUNCTION_DECL
13059 || auto_function_declaration != error_mark_node)
13061 error_at (decl_specifiers.locations[ds_type_spec],
13062 "non-variable %qD in declaration with more than one "
13063 "declarator with placeholder type",
13064 TREE_CODE (decl) == FUNCTION_DECL
13065 ? decl : auto_function_declaration);
13066 auto_function_declaration = error_mark_node;
13070 if (auto_result
13071 && (!processing_template_decl || !type_uses_auto (auto_result)))
13073 if (last_type
13074 && last_type != error_mark_node
13075 && !same_type_p (auto_result, last_type))
13077 /* If the list of declarators contains more than one declarator,
13078 the type of each declared variable is determined as described
13079 above. If the type deduced for the template parameter U is not
13080 the same in each deduction, the program is ill-formed. */
13081 error_at (decl_specifiers.locations[ds_type_spec],
13082 "inconsistent deduction for %qT: %qT and then %qT",
13083 decl_specifiers.type, last_type, auto_result);
13084 last_type = error_mark_node;
13086 else
13087 last_type = auto_result;
13090 /* Handle function definitions specially. */
13091 if (function_definition_p)
13093 /* If the next token is a `,', then we are probably
13094 processing something like:
13096 void f() {}, *p;
13098 which is erroneous. */
13099 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13101 cp_token *token = cp_lexer_peek_token (parser->lexer);
13102 error_at (token->location,
13103 "mixing"
13104 " declarations and function-definitions is forbidden");
13106 /* Otherwise, we're done with the list of declarators. */
13107 else
13109 pop_deferring_access_checks ();
13110 return;
13113 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13114 *maybe_range_for_decl = decl;
13115 /* The next token should be either a `,' or a `;'. */
13116 token = cp_lexer_peek_token (parser->lexer);
13117 /* If it's a `,', there are more declarators to come. */
13118 if (token->type == CPP_COMMA)
13119 /* will be consumed next time around */;
13120 /* If it's a `;', we are done. */
13121 else if (token->type == CPP_SEMICOLON)
13122 break;
13123 else if (maybe_range_for_decl)
13125 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13126 permerror (decl_specifiers.locations[ds_type_spec],
13127 "types may not be defined in a for-range-declaration");
13128 break;
13130 /* Anything else is an error. */
13131 else
13133 /* If we have already issued an error message we don't need
13134 to issue another one. */
13135 if ((decl != error_mark_node
13136 && DECL_INITIAL (decl) != error_mark_node)
13137 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13138 cp_parser_error (parser, "expected %<,%> or %<;%>");
13139 /* Skip tokens until we reach the end of the statement. */
13140 cp_parser_skip_to_end_of_statement (parser);
13141 /* If the next token is now a `;', consume it. */
13142 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13143 cp_lexer_consume_token (parser->lexer);
13144 goto done;
13146 /* After the first time around, a function-definition is not
13147 allowed -- even if it was OK at first. For example:
13149 int i, f() {}
13151 is not valid. */
13152 function_definition_allowed_p = false;
13155 /* Issue an error message if no declarators are present, and the
13156 decl-specifier-seq does not itself declare a class or
13157 enumeration: [dcl.dcl]/3. */
13158 if (!saw_declarator)
13160 if (cp_parser_declares_only_class_p (parser))
13162 if (!declares_class_or_enum
13163 && decl_specifiers.type
13164 && OVERLOAD_TYPE_P (decl_specifiers.type))
13165 /* Ensure an error is issued anyway when finish_decltype_type,
13166 called via cp_parser_decl_specifier_seq, returns a class or
13167 an enumeration (c++/51786). */
13168 decl_specifiers.type = NULL_TREE;
13169 shadow_tag (&decl_specifiers);
13171 /* Perform any deferred access checks. */
13172 perform_deferred_access_checks (tf_warning_or_error);
13175 /* Consume the `;'. */
13176 finish:
13177 if (!maybe_range_for_decl)
13178 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13179 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13181 if (init_loc != UNKNOWN_LOCATION)
13182 error_at (init_loc, "initializer in range-based %<for%> loop");
13183 if (comma_loc != UNKNOWN_LOCATION)
13184 error_at (comma_loc,
13185 "multiple declarations in range-based %<for%> loop");
13188 done:
13189 pop_deferring_access_checks ();
13192 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13193 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13194 initializer ; */
13196 static tree
13197 cp_parser_decomposition_declaration (cp_parser *parser,
13198 cp_decl_specifier_seq *decl_specifiers,
13199 tree *maybe_range_for_decl,
13200 location_t *init_loc)
13202 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13203 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13204 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13206 /* Parse the identifier-list. */
13207 auto_vec<cp_expr, 10> v;
13208 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13209 while (true)
13211 cp_expr e = cp_parser_identifier (parser);
13212 if (e.get_value () == error_mark_node)
13213 break;
13214 v.safe_push (e);
13215 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13216 break;
13217 cp_lexer_consume_token (parser->lexer);
13220 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13221 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13223 end_loc = UNKNOWN_LOCATION;
13224 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13225 false);
13226 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13227 cp_lexer_consume_token (parser->lexer);
13228 else
13230 cp_parser_skip_to_end_of_statement (parser);
13231 return error_mark_node;
13235 if (cxx_dialect < cxx17)
13236 pedwarn (loc, 0, "structured bindings only available with "
13237 "-std=c++17 or -std=gnu++17");
13239 tree pushed_scope;
13240 cp_declarator *declarator = make_declarator (cdk_decomp);
13241 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13242 declarator->id_loc = loc;
13243 if (ref_qual != REF_QUAL_NONE)
13244 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13245 ref_qual == REF_QUAL_RVALUE,
13246 NULL_TREE);
13247 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13248 NULL_TREE, decl_specifiers->attributes,
13249 &pushed_scope);
13250 tree orig_decl = decl;
13252 unsigned int i;
13253 cp_expr e;
13254 cp_decl_specifier_seq decl_specs;
13255 clear_decl_specs (&decl_specs);
13256 decl_specs.type = make_auto ();
13257 tree prev = decl;
13258 FOR_EACH_VEC_ELT (v, i, e)
13260 if (i == 0)
13261 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13262 else
13263 declarator->u.id.unqualified_name = e.get_value ();
13264 declarator->id_loc = e.get_location ();
13265 tree elt_pushed_scope;
13266 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13267 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13268 if (decl2 == error_mark_node)
13269 decl = error_mark_node;
13270 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13272 /* Ensure we've diagnosed redeclaration if we aren't creating
13273 a new VAR_DECL. */
13274 gcc_assert (errorcount);
13275 decl = error_mark_node;
13277 else
13278 prev = decl2;
13279 if (elt_pushed_scope)
13280 pop_scope (elt_pushed_scope);
13283 if (v.is_empty ())
13285 error_at (loc, "empty structured binding declaration");
13286 decl = error_mark_node;
13289 if (maybe_range_for_decl == NULL
13290 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13292 bool non_constant_p = false, is_direct_init = false;
13293 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13294 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13295 &non_constant_p);
13296 if (initializer == NULL_TREE
13297 || (TREE_CODE (initializer) == TREE_LIST
13298 && TREE_CHAIN (initializer))
13299 || (is_direct_init
13300 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13301 && CONSTRUCTOR_NELTS (initializer) != 1))
13303 error_at (loc, "invalid initializer for structured binding "
13304 "declaration");
13305 initializer = error_mark_node;
13308 if (decl != error_mark_node)
13310 cp_maybe_mangle_decomp (decl, prev, v.length ());
13311 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13312 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13313 cp_finish_decomp (decl, prev, v.length ());
13316 else if (decl != error_mark_node)
13318 *maybe_range_for_decl = prev;
13319 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13320 the underlying DECL. */
13321 cp_finish_decomp (decl, prev, v.length ());
13324 if (pushed_scope)
13325 pop_scope (pushed_scope);
13327 if (decl == error_mark_node && DECL_P (orig_decl))
13329 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13330 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13333 return decl;
13336 /* Parse a decl-specifier-seq.
13338 decl-specifier-seq:
13339 decl-specifier-seq [opt] decl-specifier
13340 decl-specifier attribute-specifier-seq [opt] (C++11)
13342 decl-specifier:
13343 storage-class-specifier
13344 type-specifier
13345 function-specifier
13346 friend
13347 typedef
13349 GNU Extension:
13351 decl-specifier:
13352 attributes
13354 Concepts Extension:
13356 decl-specifier:
13357 concept
13359 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13361 The parser flags FLAGS is used to control type-specifier parsing.
13363 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13364 flags:
13366 1: one of the decl-specifiers is an elaborated-type-specifier
13367 (i.e., a type declaration)
13368 2: one of the decl-specifiers is an enum-specifier or a
13369 class-specifier (i.e., a type definition)
13373 static void
13374 cp_parser_decl_specifier_seq (cp_parser* parser,
13375 cp_parser_flags flags,
13376 cp_decl_specifier_seq *decl_specs,
13377 int* declares_class_or_enum)
13379 bool constructor_possible_p = !parser->in_declarator_p;
13380 bool found_decl_spec = false;
13381 cp_token *start_token = NULL;
13382 cp_decl_spec ds;
13384 /* Clear DECL_SPECS. */
13385 clear_decl_specs (decl_specs);
13387 /* Assume no class or enumeration type is declared. */
13388 *declares_class_or_enum = 0;
13390 /* Keep reading specifiers until there are no more to read. */
13391 while (true)
13393 bool constructor_p;
13394 cp_token *token;
13395 ds = ds_last;
13397 /* Peek at the next token. */
13398 token = cp_lexer_peek_token (parser->lexer);
13400 /* Save the first token of the decl spec list for error
13401 reporting. */
13402 if (!start_token)
13403 start_token = token;
13404 /* Handle attributes. */
13405 if (cp_next_tokens_can_be_attribute_p (parser))
13407 /* Parse the attributes. */
13408 tree attrs = cp_parser_attributes_opt (parser);
13410 /* In a sequence of declaration specifiers, c++11 attributes
13411 appertain to the type that precede them. In that case
13412 [dcl.spec]/1 says:
13414 The attribute-specifier-seq affects the type only for
13415 the declaration it appears in, not other declarations
13416 involving the same type.
13418 But for now let's force the user to position the
13419 attribute either at the beginning of the declaration or
13420 after the declarator-id, which would clearly mean that it
13421 applies to the declarator. */
13422 if (cxx11_attribute_p (attrs))
13424 if (!found_decl_spec)
13425 /* The c++11 attribute is at the beginning of the
13426 declaration. It appertains to the entity being
13427 declared. */;
13428 else
13430 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13432 /* This is an attribute following a
13433 class-specifier. */
13434 if (decl_specs->type_definition_p)
13435 warn_misplaced_attr_for_class_type (token->location,
13436 decl_specs->type);
13437 attrs = NULL_TREE;
13439 else
13441 decl_specs->std_attributes
13442 = attr_chainon (decl_specs->std_attributes, attrs);
13443 if (decl_specs->locations[ds_std_attribute] == 0)
13444 decl_specs->locations[ds_std_attribute] = token->location;
13446 continue;
13450 decl_specs->attributes
13451 = attr_chainon (decl_specs->attributes, attrs);
13452 if (decl_specs->locations[ds_attribute] == 0)
13453 decl_specs->locations[ds_attribute] = token->location;
13454 continue;
13456 /* Assume we will find a decl-specifier keyword. */
13457 found_decl_spec = true;
13458 /* If the next token is an appropriate keyword, we can simply
13459 add it to the list. */
13460 switch (token->keyword)
13462 /* decl-specifier:
13463 friend
13464 constexpr */
13465 case RID_FRIEND:
13466 if (!at_class_scope_p ())
13468 gcc_rich_location richloc (token->location);
13469 richloc.add_fixit_remove ();
13470 error_at (&richloc, "%<friend%> used outside of class");
13471 cp_lexer_purge_token (parser->lexer);
13473 else
13475 ds = ds_friend;
13476 /* Consume the token. */
13477 cp_lexer_consume_token (parser->lexer);
13479 break;
13481 case RID_CONSTEXPR:
13482 ds = ds_constexpr;
13483 cp_lexer_consume_token (parser->lexer);
13484 break;
13486 case RID_CONCEPT:
13487 ds = ds_concept;
13488 cp_lexer_consume_token (parser->lexer);
13489 break;
13491 /* function-specifier:
13492 inline
13493 virtual
13494 explicit */
13495 case RID_INLINE:
13496 case RID_VIRTUAL:
13497 case RID_EXPLICIT:
13498 cp_parser_function_specifier_opt (parser, decl_specs);
13499 break;
13501 /* decl-specifier:
13502 typedef */
13503 case RID_TYPEDEF:
13504 ds = ds_typedef;
13505 /* Consume the token. */
13506 cp_lexer_consume_token (parser->lexer);
13507 /* A constructor declarator cannot appear in a typedef. */
13508 constructor_possible_p = false;
13509 /* The "typedef" keyword can only occur in a declaration; we
13510 may as well commit at this point. */
13511 cp_parser_commit_to_tentative_parse (parser);
13513 if (decl_specs->storage_class != sc_none)
13514 decl_specs->conflicting_specifiers_p = true;
13515 break;
13517 /* storage-class-specifier:
13518 auto
13519 register
13520 static
13521 extern
13522 mutable
13524 GNU Extension:
13525 thread */
13526 case RID_AUTO:
13527 if (cxx_dialect == cxx98)
13529 /* Consume the token. */
13530 cp_lexer_consume_token (parser->lexer);
13532 /* Complain about `auto' as a storage specifier, if
13533 we're complaining about C++0x compatibility. */
13534 gcc_rich_location richloc (token->location);
13535 richloc.add_fixit_remove ();
13536 warning_at (&richloc, OPT_Wc__11_compat,
13537 "%<auto%> changes meaning in C++11; "
13538 "please remove it");
13540 /* Set the storage class anyway. */
13541 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13542 token);
13544 else
13545 /* C++0x auto type-specifier. */
13546 found_decl_spec = false;
13547 break;
13549 case RID_REGISTER:
13550 case RID_STATIC:
13551 case RID_EXTERN:
13552 case RID_MUTABLE:
13553 /* Consume the token. */
13554 cp_lexer_consume_token (parser->lexer);
13555 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13556 token);
13557 break;
13558 case RID_THREAD:
13559 /* Consume the token. */
13560 ds = ds_thread;
13561 cp_lexer_consume_token (parser->lexer);
13562 break;
13564 default:
13565 /* We did not yet find a decl-specifier yet. */
13566 found_decl_spec = false;
13567 break;
13570 if (found_decl_spec
13571 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13572 && token->keyword != RID_CONSTEXPR)
13573 error ("decl-specifier invalid in condition");
13575 if (found_decl_spec
13576 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13577 && token->keyword != RID_MUTABLE
13578 && token->keyword != RID_CONSTEXPR)
13579 error_at (token->location, "%qD invalid in lambda",
13580 ridpointers[token->keyword]);
13582 if (ds != ds_last)
13583 set_and_check_decl_spec_loc (decl_specs, ds, token);
13585 /* Constructors are a special case. The `S' in `S()' is not a
13586 decl-specifier; it is the beginning of the declarator. */
13587 constructor_p
13588 = (!found_decl_spec
13589 && constructor_possible_p
13590 && (cp_parser_constructor_declarator_p
13591 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13593 /* If we don't have a DECL_SPEC yet, then we must be looking at
13594 a type-specifier. */
13595 if (!found_decl_spec && !constructor_p)
13597 int decl_spec_declares_class_or_enum;
13598 bool is_cv_qualifier;
13599 tree type_spec;
13601 type_spec
13602 = cp_parser_type_specifier (parser, flags,
13603 decl_specs,
13604 /*is_declaration=*/true,
13605 &decl_spec_declares_class_or_enum,
13606 &is_cv_qualifier);
13607 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13609 /* If this type-specifier referenced a user-defined type
13610 (a typedef, class-name, etc.), then we can't allow any
13611 more such type-specifiers henceforth.
13613 [dcl.spec]
13615 The longest sequence of decl-specifiers that could
13616 possibly be a type name is taken as the
13617 decl-specifier-seq of a declaration. The sequence shall
13618 be self-consistent as described below.
13620 [dcl.type]
13622 As a general rule, at most one type-specifier is allowed
13623 in the complete decl-specifier-seq of a declaration. The
13624 only exceptions are the following:
13626 -- const or volatile can be combined with any other
13627 type-specifier.
13629 -- signed or unsigned can be combined with char, long,
13630 short, or int.
13632 -- ..
13634 Example:
13636 typedef char* Pc;
13637 void g (const int Pc);
13639 Here, Pc is *not* part of the decl-specifier seq; it's
13640 the declarator. Therefore, once we see a type-specifier
13641 (other than a cv-qualifier), we forbid any additional
13642 user-defined types. We *do* still allow things like `int
13643 int' to be considered a decl-specifier-seq, and issue the
13644 error message later. */
13645 if (type_spec && !is_cv_qualifier)
13646 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13647 /* A constructor declarator cannot follow a type-specifier. */
13648 if (type_spec)
13650 constructor_possible_p = false;
13651 found_decl_spec = true;
13652 if (!is_cv_qualifier)
13653 decl_specs->any_type_specifiers_p = true;
13657 /* If we still do not have a DECL_SPEC, then there are no more
13658 decl-specifiers. */
13659 if (!found_decl_spec)
13660 break;
13662 decl_specs->any_specifiers_p = true;
13663 /* After we see one decl-specifier, further decl-specifiers are
13664 always optional. */
13665 flags |= CP_PARSER_FLAGS_OPTIONAL;
13668 /* Don't allow a friend specifier with a class definition. */
13669 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13670 && (*declares_class_or_enum & 2))
13671 error_at (decl_specs->locations[ds_friend],
13672 "class definition may not be declared a friend");
13675 /* Parse an (optional) storage-class-specifier.
13677 storage-class-specifier:
13678 auto
13679 register
13680 static
13681 extern
13682 mutable
13684 GNU Extension:
13686 storage-class-specifier:
13687 thread
13689 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13691 static tree
13692 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13694 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13696 case RID_AUTO:
13697 if (cxx_dialect != cxx98)
13698 return NULL_TREE;
13699 /* Fall through for C++98. */
13700 gcc_fallthrough ();
13702 case RID_REGISTER:
13703 case RID_STATIC:
13704 case RID_EXTERN:
13705 case RID_MUTABLE:
13706 case RID_THREAD:
13707 /* Consume the token. */
13708 return cp_lexer_consume_token (parser->lexer)->u.value;
13710 default:
13711 return NULL_TREE;
13715 /* Parse an (optional) function-specifier.
13717 function-specifier:
13718 inline
13719 virtual
13720 explicit
13722 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13723 Updates DECL_SPECS, if it is non-NULL. */
13725 static tree
13726 cp_parser_function_specifier_opt (cp_parser* parser,
13727 cp_decl_specifier_seq *decl_specs)
13729 cp_token *token = cp_lexer_peek_token (parser->lexer);
13730 switch (token->keyword)
13732 case RID_INLINE:
13733 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13734 break;
13736 case RID_VIRTUAL:
13737 /* 14.5.2.3 [temp.mem]
13739 A member function template shall not be virtual. */
13740 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13741 && current_class_type)
13742 error_at (token->location, "templates may not be %<virtual%>");
13743 else
13744 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13745 break;
13747 case RID_EXPLICIT:
13748 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13749 break;
13751 default:
13752 return NULL_TREE;
13755 /* Consume the token. */
13756 return cp_lexer_consume_token (parser->lexer)->u.value;
13759 /* Parse a linkage-specification.
13761 linkage-specification:
13762 extern string-literal { declaration-seq [opt] }
13763 extern string-literal declaration */
13765 static void
13766 cp_parser_linkage_specification (cp_parser* parser)
13768 tree linkage;
13770 /* Look for the `extern' keyword. */
13771 cp_token *extern_token
13772 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13774 /* Look for the string-literal. */
13775 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13776 linkage = cp_parser_string_literal (parser, false, false);
13778 /* Transform the literal into an identifier. If the literal is a
13779 wide-character string, or contains embedded NULs, then we can't
13780 handle it as the user wants. */
13781 if (strlen (TREE_STRING_POINTER (linkage))
13782 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13784 cp_parser_error (parser, "invalid linkage-specification");
13785 /* Assume C++ linkage. */
13786 linkage = lang_name_cplusplus;
13788 else
13789 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13791 /* We're now using the new linkage. */
13792 push_lang_context (linkage);
13794 /* Preserve the location of the the innermost linkage specification,
13795 tracking the locations of nested specifications via a local. */
13796 location_t saved_location
13797 = parser->innermost_linkage_specification_location;
13798 /* Construct a location ranging from the start of the "extern" to
13799 the end of the string-literal, with the caret at the start, e.g.:
13800 extern "C" {
13801 ^~~~~~~~~~
13803 parser->innermost_linkage_specification_location
13804 = make_location (extern_token->location,
13805 extern_token->location,
13806 get_finish (string_token->location));
13808 /* If the next token is a `{', then we're using the first
13809 production. */
13810 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13812 cp_ensure_no_omp_declare_simd (parser);
13813 cp_ensure_no_oacc_routine (parser);
13815 /* Consume the `{' token. */
13816 matching_braces braces;
13817 braces.consume_open (parser)->location;
13818 /* Parse the declarations. */
13819 cp_parser_declaration_seq_opt (parser);
13820 /* Look for the closing `}'. */
13821 braces.require_close (parser);
13823 /* Otherwise, there's just one declaration. */
13824 else
13826 bool saved_in_unbraced_linkage_specification_p;
13828 saved_in_unbraced_linkage_specification_p
13829 = parser->in_unbraced_linkage_specification_p;
13830 parser->in_unbraced_linkage_specification_p = true;
13831 cp_parser_declaration (parser);
13832 parser->in_unbraced_linkage_specification_p
13833 = saved_in_unbraced_linkage_specification_p;
13836 /* We're done with the linkage-specification. */
13837 pop_lang_context ();
13839 /* Restore location of parent linkage specification, if any. */
13840 parser->innermost_linkage_specification_location = saved_location;
13843 /* Parse a static_assert-declaration.
13845 static_assert-declaration:
13846 static_assert ( constant-expression , string-literal ) ;
13847 static_assert ( constant-expression ) ; (C++17)
13849 If MEMBER_P, this static_assert is a class member. */
13851 static void
13852 cp_parser_static_assert(cp_parser *parser, bool member_p)
13854 cp_expr condition;
13855 location_t token_loc;
13856 tree message;
13857 bool dummy;
13859 /* Peek at the `static_assert' token so we can keep track of exactly
13860 where the static assertion started. */
13861 token_loc = cp_lexer_peek_token (parser->lexer)->location;
13863 /* Look for the `static_assert' keyword. */
13864 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13865 RT_STATIC_ASSERT))
13866 return;
13868 /* We know we are in a static assertion; commit to any tentative
13869 parse. */
13870 if (cp_parser_parsing_tentatively (parser))
13871 cp_parser_commit_to_tentative_parse (parser);
13873 /* Parse the `(' starting the static assertion condition. */
13874 matching_parens parens;
13875 parens.require_open (parser);
13877 /* Parse the constant-expression. Allow a non-constant expression
13878 here in order to give better diagnostics in finish_static_assert. */
13879 condition =
13880 cp_parser_constant_expression (parser,
13881 /*allow_non_constant_p=*/true,
13882 /*non_constant_p=*/&dummy);
13884 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13886 if (cxx_dialect < cxx17)
13887 pedwarn (input_location, OPT_Wpedantic,
13888 "static_assert without a message "
13889 "only available with -std=c++17 or -std=gnu++17");
13890 /* Eat the ')' */
13891 cp_lexer_consume_token (parser->lexer);
13892 message = build_string (1, "");
13893 TREE_TYPE (message) = char_array_type_node;
13894 fix_string_type (message);
13896 else
13898 /* Parse the separating `,'. */
13899 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13901 /* Parse the string-literal message. */
13902 message = cp_parser_string_literal (parser,
13903 /*translate=*/false,
13904 /*wide_ok=*/true);
13906 /* A `)' completes the static assertion. */
13907 if (!parens.require_close (parser))
13908 cp_parser_skip_to_closing_parenthesis (parser,
13909 /*recovering=*/true,
13910 /*or_comma=*/false,
13911 /*consume_paren=*/true);
13914 /* A semicolon terminates the declaration. */
13915 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13917 /* Get the location for the static assertion. Use that of the
13918 condition if available, otherwise, use that of the "static_assert"
13919 token. */
13920 location_t assert_loc = condition.get_location ();
13921 if (assert_loc == UNKNOWN_LOCATION)
13922 assert_loc = token_loc;
13924 /* Complete the static assertion, which may mean either processing
13925 the static assert now or saving it for template instantiation. */
13926 finish_static_assert (condition, message, assert_loc, member_p);
13929 /* Parse the expression in decltype ( expression ). */
13931 static tree
13932 cp_parser_decltype_expr (cp_parser *parser,
13933 bool &id_expression_or_member_access_p)
13935 cp_token *id_expr_start_token;
13936 tree expr;
13938 /* Since we're going to preserve any side-effects from this parse, set up a
13939 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13940 in the expression. */
13941 tentative_firewall firewall (parser);
13943 /* First, try parsing an id-expression. */
13944 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13945 cp_parser_parse_tentatively (parser);
13946 expr = cp_parser_id_expression (parser,
13947 /*template_keyword_p=*/false,
13948 /*check_dependency_p=*/true,
13949 /*template_p=*/NULL,
13950 /*declarator_p=*/false,
13951 /*optional_p=*/false);
13953 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13955 bool non_integral_constant_expression_p = false;
13956 tree id_expression = expr;
13957 cp_id_kind idk;
13958 const char *error_msg;
13960 if (identifier_p (expr))
13961 /* Lookup the name we got back from the id-expression. */
13962 expr = cp_parser_lookup_name_simple (parser, expr,
13963 id_expr_start_token->location);
13965 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
13966 /* A template without args is not a complete id-expression. */
13967 expr = error_mark_node;
13969 if (expr
13970 && expr != error_mark_node
13971 && TREE_CODE (expr) != TYPE_DECL
13972 && (TREE_CODE (expr) != BIT_NOT_EXPR
13973 || !TYPE_P (TREE_OPERAND (expr, 0)))
13974 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13976 /* Complete lookup of the id-expression. */
13977 expr = (finish_id_expression
13978 (id_expression, expr, parser->scope, &idk,
13979 /*integral_constant_expression_p=*/false,
13980 /*allow_non_integral_constant_expression_p=*/true,
13981 &non_integral_constant_expression_p,
13982 /*template_p=*/false,
13983 /*done=*/true,
13984 /*address_p=*/false,
13985 /*template_arg_p=*/false,
13986 &error_msg,
13987 id_expr_start_token->location));
13989 if (expr == error_mark_node)
13990 /* We found an id-expression, but it was something that we
13991 should not have found. This is an error, not something
13992 we can recover from, so note that we found an
13993 id-expression and we'll recover as gracefully as
13994 possible. */
13995 id_expression_or_member_access_p = true;
13998 if (expr
13999 && expr != error_mark_node
14000 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14001 /* We have an id-expression. */
14002 id_expression_or_member_access_p = true;
14005 if (!id_expression_or_member_access_p)
14007 /* Abort the id-expression parse. */
14008 cp_parser_abort_tentative_parse (parser);
14010 /* Parsing tentatively, again. */
14011 cp_parser_parse_tentatively (parser);
14013 /* Parse a class member access. */
14014 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14015 /*cast_p=*/false, /*decltype*/true,
14016 /*member_access_only_p=*/true, NULL);
14018 if (expr
14019 && expr != error_mark_node
14020 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14021 /* We have an id-expression. */
14022 id_expression_or_member_access_p = true;
14025 if (id_expression_or_member_access_p)
14026 /* We have parsed the complete id-expression or member access. */
14027 cp_parser_parse_definitely (parser);
14028 else
14030 /* Abort our attempt to parse an id-expression or member access
14031 expression. */
14032 cp_parser_abort_tentative_parse (parser);
14034 /* Commit to the tentative_firewall so we get syntax errors. */
14035 cp_parser_commit_to_tentative_parse (parser);
14037 /* Parse a full expression. */
14038 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14039 /*decltype_p=*/true);
14042 return expr;
14045 /* Parse a `decltype' type. Returns the type.
14047 simple-type-specifier:
14048 decltype ( expression )
14049 C++14 proposal:
14050 decltype ( auto ) */
14052 static tree
14053 cp_parser_decltype (cp_parser *parser)
14055 bool id_expression_or_member_access_p = false;
14056 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14058 if (start_token->type == CPP_DECLTYPE)
14060 /* Already parsed. */
14061 cp_lexer_consume_token (parser->lexer);
14062 return saved_checks_value (start_token->u.tree_check_value);
14065 /* Look for the `decltype' token. */
14066 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14067 return error_mark_node;
14069 /* Parse the opening `('. */
14070 matching_parens parens;
14071 if (!parens.require_open (parser))
14072 return error_mark_node;
14074 push_deferring_access_checks (dk_deferred);
14076 tree expr = NULL_TREE;
14078 if (cxx_dialect >= cxx14
14079 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14080 /* decltype (auto) */
14081 cp_lexer_consume_token (parser->lexer);
14082 else
14084 /* decltype (expression) */
14086 /* Types cannot be defined in a `decltype' expression. Save away the
14087 old message and set the new one. */
14088 const char *saved_message = parser->type_definition_forbidden_message;
14089 parser->type_definition_forbidden_message
14090 = G_("types may not be defined in %<decltype%> expressions");
14092 /* The restrictions on constant-expressions do not apply inside
14093 decltype expressions. */
14094 bool saved_integral_constant_expression_p
14095 = parser->integral_constant_expression_p;
14096 bool saved_non_integral_constant_expression_p
14097 = parser->non_integral_constant_expression_p;
14098 parser->integral_constant_expression_p = false;
14100 /* Within a parenthesized expression, a `>' token is always
14101 the greater-than operator. */
14102 bool saved_greater_than_is_operator_p
14103 = parser->greater_than_is_operator_p;
14104 parser->greater_than_is_operator_p = true;
14106 /* Do not actually evaluate the expression. */
14107 ++cp_unevaluated_operand;
14109 /* Do not warn about problems with the expression. */
14110 ++c_inhibit_evaluation_warnings;
14112 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14114 /* Go back to evaluating expressions. */
14115 --cp_unevaluated_operand;
14116 --c_inhibit_evaluation_warnings;
14118 /* The `>' token might be the end of a template-id or
14119 template-parameter-list now. */
14120 parser->greater_than_is_operator_p
14121 = saved_greater_than_is_operator_p;
14123 /* Restore the old message and the integral constant expression
14124 flags. */
14125 parser->type_definition_forbidden_message = saved_message;
14126 parser->integral_constant_expression_p
14127 = saved_integral_constant_expression_p;
14128 parser->non_integral_constant_expression_p
14129 = saved_non_integral_constant_expression_p;
14132 /* Parse to the closing `)'. */
14133 if (!parens.require_close (parser))
14135 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14136 /*consume_paren=*/true);
14137 pop_deferring_access_checks ();
14138 return error_mark_node;
14141 if (!expr)
14143 /* Build auto. */
14144 expr = make_decltype_auto ();
14145 AUTO_IS_DECLTYPE (expr) = true;
14147 else
14148 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14149 tf_warning_or_error);
14151 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14152 it again. */
14153 start_token->type = CPP_DECLTYPE;
14154 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14155 start_token->u.tree_check_value->value = expr;
14156 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14157 start_token->keyword = RID_MAX;
14158 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14160 pop_to_parent_deferring_access_checks ();
14162 return expr;
14165 /* Special member functions [gram.special] */
14167 /* Parse a conversion-function-id.
14169 conversion-function-id:
14170 operator conversion-type-id
14172 Returns an IDENTIFIER_NODE representing the operator. */
14174 static tree
14175 cp_parser_conversion_function_id (cp_parser* parser)
14177 tree type;
14178 tree saved_scope;
14179 tree saved_qualifying_scope;
14180 tree saved_object_scope;
14181 tree pushed_scope = NULL_TREE;
14183 /* Look for the `operator' token. */
14184 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14185 return error_mark_node;
14186 /* When we parse the conversion-type-id, the current scope will be
14187 reset. However, we need that information in able to look up the
14188 conversion function later, so we save it here. */
14189 saved_scope = parser->scope;
14190 saved_qualifying_scope = parser->qualifying_scope;
14191 saved_object_scope = parser->object_scope;
14192 /* We must enter the scope of the class so that the names of
14193 entities declared within the class are available in the
14194 conversion-type-id. For example, consider:
14196 struct S {
14197 typedef int I;
14198 operator I();
14201 S::operator I() { ... }
14203 In order to see that `I' is a type-name in the definition, we
14204 must be in the scope of `S'. */
14205 if (saved_scope)
14206 pushed_scope = push_scope (saved_scope);
14207 /* Parse the conversion-type-id. */
14208 type = cp_parser_conversion_type_id (parser);
14209 /* Leave the scope of the class, if any. */
14210 if (pushed_scope)
14211 pop_scope (pushed_scope);
14212 /* Restore the saved scope. */
14213 parser->scope = saved_scope;
14214 parser->qualifying_scope = saved_qualifying_scope;
14215 parser->object_scope = saved_object_scope;
14216 /* If the TYPE is invalid, indicate failure. */
14217 if (type == error_mark_node)
14218 return error_mark_node;
14219 return make_conv_op_name (type);
14222 /* Parse a conversion-type-id:
14224 conversion-type-id:
14225 type-specifier-seq conversion-declarator [opt]
14227 Returns the TYPE specified. */
14229 static tree
14230 cp_parser_conversion_type_id (cp_parser* parser)
14232 tree attributes;
14233 cp_decl_specifier_seq type_specifiers;
14234 cp_declarator *declarator;
14235 tree type_specified;
14236 const char *saved_message;
14238 /* Parse the attributes. */
14239 attributes = cp_parser_attributes_opt (parser);
14241 saved_message = parser->type_definition_forbidden_message;
14242 parser->type_definition_forbidden_message
14243 = G_("types may not be defined in a conversion-type-id");
14245 /* Parse the type-specifiers. */
14246 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14247 /*is_trailing_return=*/false,
14248 &type_specifiers);
14250 parser->type_definition_forbidden_message = saved_message;
14252 /* If that didn't work, stop. */
14253 if (type_specifiers.type == error_mark_node)
14254 return error_mark_node;
14255 /* Parse the conversion-declarator. */
14256 declarator = cp_parser_conversion_declarator_opt (parser);
14258 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14259 /*initialized=*/0, &attributes);
14260 if (attributes)
14261 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14263 /* Don't give this error when parsing tentatively. This happens to
14264 work because we always parse this definitively once. */
14265 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14266 && type_uses_auto (type_specified))
14268 if (cxx_dialect < cxx14)
14270 error ("invalid use of %<auto%> in conversion operator");
14271 return error_mark_node;
14273 else if (template_parm_scope_p ())
14274 warning (0, "use of %<auto%> in member template "
14275 "conversion operator can never be deduced");
14278 return type_specified;
14281 /* Parse an (optional) conversion-declarator.
14283 conversion-declarator:
14284 ptr-operator conversion-declarator [opt]
14288 static cp_declarator *
14289 cp_parser_conversion_declarator_opt (cp_parser* parser)
14291 enum tree_code code;
14292 tree class_type, std_attributes = NULL_TREE;
14293 cp_cv_quals cv_quals;
14295 /* We don't know if there's a ptr-operator next, or not. */
14296 cp_parser_parse_tentatively (parser);
14297 /* Try the ptr-operator. */
14298 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14299 &std_attributes);
14300 /* If it worked, look for more conversion-declarators. */
14301 if (cp_parser_parse_definitely (parser))
14303 cp_declarator *declarator;
14305 /* Parse another optional declarator. */
14306 declarator = cp_parser_conversion_declarator_opt (parser);
14308 declarator = cp_parser_make_indirect_declarator
14309 (code, class_type, cv_quals, declarator, std_attributes);
14311 return declarator;
14314 return NULL;
14317 /* Parse an (optional) ctor-initializer.
14319 ctor-initializer:
14320 : mem-initializer-list */
14322 static void
14323 cp_parser_ctor_initializer_opt (cp_parser* parser)
14325 /* If the next token is not a `:', then there is no
14326 ctor-initializer. */
14327 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14329 /* Do default initialization of any bases and members. */
14330 if (DECL_CONSTRUCTOR_P (current_function_decl))
14331 finish_mem_initializers (NULL_TREE);
14332 return;
14335 /* Consume the `:' token. */
14336 cp_lexer_consume_token (parser->lexer);
14337 /* And the mem-initializer-list. */
14338 cp_parser_mem_initializer_list (parser);
14341 /* Parse a mem-initializer-list.
14343 mem-initializer-list:
14344 mem-initializer ... [opt]
14345 mem-initializer ... [opt] , mem-initializer-list */
14347 static void
14348 cp_parser_mem_initializer_list (cp_parser* parser)
14350 tree mem_initializer_list = NULL_TREE;
14351 tree target_ctor = error_mark_node;
14352 cp_token *token = cp_lexer_peek_token (parser->lexer);
14354 /* Let the semantic analysis code know that we are starting the
14355 mem-initializer-list. */
14356 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14357 error_at (token->location,
14358 "only constructors take member initializers");
14360 /* Loop through the list. */
14361 while (true)
14363 tree mem_initializer;
14365 token = cp_lexer_peek_token (parser->lexer);
14366 /* Parse the mem-initializer. */
14367 mem_initializer = cp_parser_mem_initializer (parser);
14368 /* If the next token is a `...', we're expanding member initializers. */
14369 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14370 if (ellipsis
14371 || (mem_initializer != error_mark_node
14372 && check_for_bare_parameter_packs (TREE_PURPOSE
14373 (mem_initializer))))
14375 /* Consume the `...'. */
14376 if (ellipsis)
14377 cp_lexer_consume_token (parser->lexer);
14379 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14380 can be expanded but members cannot. */
14381 if (mem_initializer != error_mark_node
14382 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14384 error_at (token->location,
14385 "cannot expand initializer for member %qD",
14386 TREE_PURPOSE (mem_initializer));
14387 mem_initializer = error_mark_node;
14390 /* Construct the pack expansion type. */
14391 if (mem_initializer != error_mark_node)
14392 mem_initializer = make_pack_expansion (mem_initializer);
14394 if (target_ctor != error_mark_node
14395 && mem_initializer != error_mark_node)
14397 error ("mem-initializer for %qD follows constructor delegation",
14398 TREE_PURPOSE (mem_initializer));
14399 mem_initializer = error_mark_node;
14401 /* Look for a target constructor. */
14402 if (mem_initializer != error_mark_node
14403 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14404 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14406 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14407 if (mem_initializer_list)
14409 error ("constructor delegation follows mem-initializer for %qD",
14410 TREE_PURPOSE (mem_initializer_list));
14411 mem_initializer = error_mark_node;
14413 target_ctor = mem_initializer;
14415 /* Add it to the list, unless it was erroneous. */
14416 if (mem_initializer != error_mark_node)
14418 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14419 mem_initializer_list = mem_initializer;
14421 /* If the next token is not a `,', we're done. */
14422 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14423 break;
14424 /* Consume the `,' token. */
14425 cp_lexer_consume_token (parser->lexer);
14428 /* Perform semantic analysis. */
14429 if (DECL_CONSTRUCTOR_P (current_function_decl))
14430 finish_mem_initializers (mem_initializer_list);
14433 /* Parse a mem-initializer.
14435 mem-initializer:
14436 mem-initializer-id ( expression-list [opt] )
14437 mem-initializer-id braced-init-list
14439 GNU extension:
14441 mem-initializer:
14442 ( expression-list [opt] )
14444 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14445 class) or FIELD_DECL (for a non-static data member) to initialize;
14446 the TREE_VALUE is the expression-list. An empty initialization
14447 list is represented by void_list_node. */
14449 static tree
14450 cp_parser_mem_initializer (cp_parser* parser)
14452 tree mem_initializer_id;
14453 tree expression_list;
14454 tree member;
14455 cp_token *token = cp_lexer_peek_token (parser->lexer);
14457 /* Find out what is being initialized. */
14458 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14460 permerror (token->location,
14461 "anachronistic old-style base class initializer");
14462 mem_initializer_id = NULL_TREE;
14464 else
14466 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14467 if (mem_initializer_id == error_mark_node)
14468 return mem_initializer_id;
14470 member = expand_member_init (mem_initializer_id);
14471 if (member && !DECL_P (member))
14472 in_base_initializer = 1;
14474 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14476 bool expr_non_constant_p;
14477 cp_lexer_set_source_position (parser->lexer);
14478 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14479 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14480 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14481 expression_list = build_tree_list (NULL_TREE, expression_list);
14483 else
14485 vec<tree, va_gc> *vec;
14486 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14487 /*cast_p=*/false,
14488 /*allow_expansion_p=*/true,
14489 /*non_constant_p=*/NULL);
14490 if (vec == NULL)
14491 return error_mark_node;
14492 expression_list = build_tree_list_vec (vec);
14493 release_tree_vector (vec);
14496 if (expression_list == error_mark_node)
14497 return error_mark_node;
14498 if (!expression_list)
14499 expression_list = void_type_node;
14501 in_base_initializer = 0;
14503 return member ? build_tree_list (member, expression_list) : error_mark_node;
14506 /* Parse a mem-initializer-id.
14508 mem-initializer-id:
14509 :: [opt] nested-name-specifier [opt] class-name
14510 decltype-specifier (C++11)
14511 identifier
14513 Returns a TYPE indicating the class to be initialized for the first
14514 production (and the second in C++11). Returns an IDENTIFIER_NODE
14515 indicating the data member to be initialized for the last production. */
14517 static tree
14518 cp_parser_mem_initializer_id (cp_parser* parser)
14520 bool global_scope_p;
14521 bool nested_name_specifier_p;
14522 bool template_p = false;
14523 tree id;
14525 cp_token *token = cp_lexer_peek_token (parser->lexer);
14527 /* `typename' is not allowed in this context ([temp.res]). */
14528 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14530 error_at (token->location,
14531 "keyword %<typename%> not allowed in this context (a qualified "
14532 "member initializer is implicitly a type)");
14533 cp_lexer_consume_token (parser->lexer);
14535 /* Look for the optional `::' operator. */
14536 global_scope_p
14537 = (cp_parser_global_scope_opt (parser,
14538 /*current_scope_valid_p=*/false)
14539 != NULL_TREE);
14540 /* Look for the optional nested-name-specifier. The simplest way to
14541 implement:
14543 [temp.res]
14545 The keyword `typename' is not permitted in a base-specifier or
14546 mem-initializer; in these contexts a qualified name that
14547 depends on a template-parameter is implicitly assumed to be a
14548 type name.
14550 is to assume that we have seen the `typename' keyword at this
14551 point. */
14552 nested_name_specifier_p
14553 = (cp_parser_nested_name_specifier_opt (parser,
14554 /*typename_keyword_p=*/true,
14555 /*check_dependency_p=*/true,
14556 /*type_p=*/true,
14557 /*is_declaration=*/true)
14558 != NULL_TREE);
14559 if (nested_name_specifier_p)
14560 template_p = cp_parser_optional_template_keyword (parser);
14561 /* If there is a `::' operator or a nested-name-specifier, then we
14562 are definitely looking for a class-name. */
14563 if (global_scope_p || nested_name_specifier_p)
14564 return cp_parser_class_name (parser,
14565 /*typename_keyword_p=*/true,
14566 /*template_keyword_p=*/template_p,
14567 typename_type,
14568 /*check_dependency_p=*/true,
14569 /*class_head_p=*/false,
14570 /*is_declaration=*/true);
14571 /* Otherwise, we could also be looking for an ordinary identifier. */
14572 cp_parser_parse_tentatively (parser);
14573 if (cp_lexer_next_token_is_decltype (parser->lexer))
14574 /* Try a decltype-specifier. */
14575 id = cp_parser_decltype (parser);
14576 else
14577 /* Otherwise, try a class-name. */
14578 id = cp_parser_class_name (parser,
14579 /*typename_keyword_p=*/true,
14580 /*template_keyword_p=*/false,
14581 none_type,
14582 /*check_dependency_p=*/true,
14583 /*class_head_p=*/false,
14584 /*is_declaration=*/true);
14585 /* If we found one, we're done. */
14586 if (cp_parser_parse_definitely (parser))
14587 return id;
14588 /* Otherwise, look for an ordinary identifier. */
14589 return cp_parser_identifier (parser);
14592 /* Overloading [gram.over] */
14594 /* Parse an operator-function-id.
14596 operator-function-id:
14597 operator operator
14599 Returns an IDENTIFIER_NODE for the operator which is a
14600 human-readable spelling of the identifier, e.g., `operator +'. */
14602 static cp_expr
14603 cp_parser_operator_function_id (cp_parser* parser)
14605 /* Look for the `operator' keyword. */
14606 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14607 return error_mark_node;
14608 /* And then the name of the operator itself. */
14609 return cp_parser_operator (parser);
14612 /* Return an identifier node for a user-defined literal operator.
14613 The suffix identifier is chained to the operator name identifier. */
14615 tree
14616 cp_literal_operator_id (const char* name)
14618 tree identifier;
14619 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14620 + strlen (name) + 10);
14621 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14622 identifier = get_identifier (buffer);
14624 return identifier;
14627 /* Parse an operator.
14629 operator:
14630 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14631 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14632 || ++ -- , ->* -> () []
14634 GNU Extensions:
14636 operator:
14637 <? >? <?= >?=
14639 Returns an IDENTIFIER_NODE for the operator which is a
14640 human-readable spelling of the identifier, e.g., `operator +'. */
14642 static cp_expr
14643 cp_parser_operator (cp_parser* parser)
14645 tree id = NULL_TREE;
14646 cp_token *token;
14647 bool utf8 = false;
14649 /* Peek at the next token. */
14650 token = cp_lexer_peek_token (parser->lexer);
14652 location_t start_loc = token->location;
14654 /* Figure out which operator we have. */
14655 enum tree_code op = ERROR_MARK;
14656 bool assop = false;
14657 bool consumed = false;
14658 switch (token->type)
14660 case CPP_KEYWORD:
14662 /* The keyword should be either `new' or `delete'. */
14663 if (token->keyword == RID_NEW)
14664 op = NEW_EXPR;
14665 else if (token->keyword == RID_DELETE)
14666 op = DELETE_EXPR;
14667 else
14668 break;
14670 /* Consume the `new' or `delete' token. */
14671 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14673 /* Peek at the next token. */
14674 token = cp_lexer_peek_token (parser->lexer);
14675 /* If it's a `[' token then this is the array variant of the
14676 operator. */
14677 if (token->type == CPP_OPEN_SQUARE)
14679 /* Consume the `[' token. */
14680 cp_lexer_consume_token (parser->lexer);
14681 /* Look for the `]' token. */
14682 if (cp_token *close_token
14683 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14684 end_loc = close_token->location;
14685 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14687 start_loc = make_location (start_loc, start_loc, end_loc);
14688 consumed = true;
14689 break;
14692 case CPP_PLUS:
14693 op = PLUS_EXPR;
14694 break;
14696 case CPP_MINUS:
14697 op = MINUS_EXPR;
14698 break;
14700 case CPP_MULT:
14701 op = MULT_EXPR;
14702 break;
14704 case CPP_DIV:
14705 op = TRUNC_DIV_EXPR;
14706 break;
14708 case CPP_MOD:
14709 op = TRUNC_MOD_EXPR;
14710 break;
14712 case CPP_XOR:
14713 op = BIT_XOR_EXPR;
14714 break;
14716 case CPP_AND:
14717 op = BIT_AND_EXPR;
14718 break;
14720 case CPP_OR:
14721 op = BIT_IOR_EXPR;
14722 break;
14724 case CPP_COMPL:
14725 op = BIT_NOT_EXPR;
14726 break;
14728 case CPP_NOT:
14729 op = TRUTH_NOT_EXPR;
14730 break;
14732 case CPP_EQ:
14733 assop = true;
14734 op = NOP_EXPR;
14735 break;
14737 case CPP_LESS:
14738 op = LT_EXPR;
14739 break;
14741 case CPP_GREATER:
14742 op = GT_EXPR;
14743 break;
14745 case CPP_PLUS_EQ:
14746 assop = true;
14747 op = PLUS_EXPR;
14748 break;
14750 case CPP_MINUS_EQ:
14751 assop = true;
14752 op = MINUS_EXPR;
14753 break;
14755 case CPP_MULT_EQ:
14756 assop = true;
14757 op = MULT_EXPR;
14758 break;
14760 case CPP_DIV_EQ:
14761 assop = true;
14762 op = TRUNC_DIV_EXPR;
14763 break;
14765 case CPP_MOD_EQ:
14766 assop = true;
14767 op = TRUNC_MOD_EXPR;
14768 break;
14770 case CPP_XOR_EQ:
14771 assop = true;
14772 op = BIT_XOR_EXPR;
14773 break;
14775 case CPP_AND_EQ:
14776 assop = true;
14777 op = BIT_AND_EXPR;
14778 break;
14780 case CPP_OR_EQ:
14781 assop = true;
14782 op = BIT_IOR_EXPR;
14783 break;
14785 case CPP_LSHIFT:
14786 op = LSHIFT_EXPR;
14787 break;
14789 case CPP_RSHIFT:
14790 op = RSHIFT_EXPR;
14791 break;
14793 case CPP_LSHIFT_EQ:
14794 assop = true;
14795 op = LSHIFT_EXPR;
14796 break;
14798 case CPP_RSHIFT_EQ:
14799 assop = true;
14800 op = RSHIFT_EXPR;
14801 break;
14803 case CPP_EQ_EQ:
14804 op = EQ_EXPR;
14805 break;
14807 case CPP_NOT_EQ:
14808 op = NE_EXPR;
14809 break;
14811 case CPP_LESS_EQ:
14812 op = LE_EXPR;
14813 break;
14815 case CPP_GREATER_EQ:
14816 op = GE_EXPR;
14817 break;
14819 case CPP_AND_AND:
14820 op = TRUTH_ANDIF_EXPR;
14821 break;
14823 case CPP_OR_OR:
14824 op = TRUTH_ORIF_EXPR;
14825 break;
14827 case CPP_PLUS_PLUS:
14828 op = POSTINCREMENT_EXPR;
14829 break;
14831 case CPP_MINUS_MINUS:
14832 op = PREDECREMENT_EXPR;
14833 break;
14835 case CPP_COMMA:
14836 op = COMPOUND_EXPR;
14837 break;
14839 case CPP_DEREF_STAR:
14840 op = MEMBER_REF;
14841 break;
14843 case CPP_DEREF:
14844 op = COMPONENT_REF;
14845 break;
14847 case CPP_OPEN_PAREN:
14849 /* Consume the `('. */
14850 matching_parens parens;
14851 parens.consume_open (parser);
14852 /* Look for the matching `)'. */
14853 parens.require_close (parser);
14854 op = CALL_EXPR;
14855 consumed = true;
14856 break;
14859 case CPP_OPEN_SQUARE:
14860 /* Consume the `['. */
14861 cp_lexer_consume_token (parser->lexer);
14862 /* Look for the matching `]'. */
14863 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14864 op = ARRAY_REF;
14865 consumed = true;
14866 break;
14868 case CPP_UTF8STRING:
14869 case CPP_UTF8STRING_USERDEF:
14870 utf8 = true;
14871 /* FALLTHRU */
14872 case CPP_STRING:
14873 case CPP_WSTRING:
14874 case CPP_STRING16:
14875 case CPP_STRING32:
14876 case CPP_STRING_USERDEF:
14877 case CPP_WSTRING_USERDEF:
14878 case CPP_STRING16_USERDEF:
14879 case CPP_STRING32_USERDEF:
14881 tree str, string_tree;
14882 int sz, len;
14884 if (cxx_dialect == cxx98)
14885 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14887 /* Consume the string. */
14888 str = cp_parser_string_literal (parser, /*translate=*/true,
14889 /*wide_ok=*/true, /*lookup_udlit=*/false);
14890 if (str == error_mark_node)
14891 return error_mark_node;
14892 else if (TREE_CODE (str) == USERDEF_LITERAL)
14894 string_tree = USERDEF_LITERAL_VALUE (str);
14895 id = USERDEF_LITERAL_SUFFIX_ID (str);
14897 else
14899 string_tree = str;
14900 /* Look for the suffix identifier. */
14901 token = cp_lexer_peek_token (parser->lexer);
14902 if (token->type == CPP_NAME)
14903 id = cp_parser_identifier (parser);
14904 else if (token->type == CPP_KEYWORD)
14906 error ("unexpected keyword;"
14907 " remove space between quotes and suffix identifier");
14908 return error_mark_node;
14910 else
14912 error ("expected suffix identifier");
14913 return error_mark_node;
14916 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14917 (TREE_TYPE (TREE_TYPE (string_tree))));
14918 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14919 if (len != 0)
14921 error ("expected empty string after %<operator%> keyword");
14922 return error_mark_node;
14924 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14925 != char_type_node)
14927 error ("invalid encoding prefix in literal operator");
14928 return error_mark_node;
14930 if (id != error_mark_node)
14932 const char *name = IDENTIFIER_POINTER (id);
14933 id = cp_literal_operator_id (name);
14935 return id;
14938 default:
14939 /* Anything else is an error. */
14940 break;
14943 /* If we have selected an identifier, we need to consume the
14944 operator token. */
14945 if (op != ERROR_MARK)
14947 id = ovl_op_identifier (assop, op);
14948 if (!consumed)
14949 cp_lexer_consume_token (parser->lexer);
14951 /* Otherwise, no valid operator name was present. */
14952 else
14954 cp_parser_error (parser, "expected operator");
14955 id = error_mark_node;
14958 return cp_expr (id, start_loc);
14961 /* Parse a template-declaration.
14963 template-declaration:
14964 export [opt] template < template-parameter-list > declaration
14966 If MEMBER_P is TRUE, this template-declaration occurs within a
14967 class-specifier.
14969 The grammar rule given by the standard isn't correct. What
14970 is really meant is:
14972 template-declaration:
14973 export [opt] template-parameter-list-seq
14974 decl-specifier-seq [opt] init-declarator [opt] ;
14975 export [opt] template-parameter-list-seq
14976 function-definition
14978 template-parameter-list-seq:
14979 template-parameter-list-seq [opt]
14980 template < template-parameter-list >
14982 Concept Extensions:
14984 template-parameter-list-seq:
14985 template < template-parameter-list > requires-clause [opt]
14987 requires-clause:
14988 requires logical-or-expression */
14990 static void
14991 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14993 /* Check for `export'. */
14994 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14996 /* Consume the `export' token. */
14997 cp_lexer_consume_token (parser->lexer);
14998 /* Warn that we do not support `export'. */
14999 warning (0, "keyword %<export%> not implemented, and will be ignored");
15002 cp_parser_template_declaration_after_export (parser, member_p);
15005 /* Parse a template-parameter-list.
15007 template-parameter-list:
15008 template-parameter
15009 template-parameter-list , template-parameter
15011 Returns a TREE_LIST. Each node represents a template parameter.
15012 The nodes are connected via their TREE_CHAINs. */
15014 static tree
15015 cp_parser_template_parameter_list (cp_parser* parser)
15017 tree parameter_list = NULL_TREE;
15019 begin_template_parm_list ();
15021 /* The loop below parses the template parms. We first need to know
15022 the total number of template parms to be able to compute proper
15023 canonical types of each dependent type. So after the loop, when
15024 we know the total number of template parms,
15025 end_template_parm_list computes the proper canonical types and
15026 fixes up the dependent types accordingly. */
15027 while (true)
15029 tree parameter;
15030 bool is_non_type;
15031 bool is_parameter_pack;
15032 location_t parm_loc;
15034 /* Parse the template-parameter. */
15035 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15036 parameter = cp_parser_template_parameter (parser,
15037 &is_non_type,
15038 &is_parameter_pack);
15039 /* Add it to the list. */
15040 if (parameter != error_mark_node)
15041 parameter_list = process_template_parm (parameter_list,
15042 parm_loc,
15043 parameter,
15044 is_non_type,
15045 is_parameter_pack);
15046 else
15048 tree err_parm = build_tree_list (parameter, parameter);
15049 parameter_list = chainon (parameter_list, err_parm);
15052 /* If the next token is not a `,', we're done. */
15053 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15054 break;
15055 /* Otherwise, consume the `,' token. */
15056 cp_lexer_consume_token (parser->lexer);
15059 return end_template_parm_list (parameter_list);
15062 /* Parse a introduction-list.
15064 introduction-list:
15065 introduced-parameter
15066 introduction-list , introduced-parameter
15068 introduced-parameter:
15069 ...[opt] identifier
15071 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15072 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15073 WILDCARD_DECL will also have DECL_NAME set and token location in
15074 DECL_SOURCE_LOCATION. */
15076 static tree
15077 cp_parser_introduction_list (cp_parser *parser)
15079 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15081 while (true)
15083 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15084 if (is_pack)
15085 cp_lexer_consume_token (parser->lexer);
15087 /* Build placeholder. */
15088 tree parm = build_nt (WILDCARD_DECL);
15089 DECL_SOURCE_LOCATION (parm)
15090 = cp_lexer_peek_token (parser->lexer)->location;
15091 DECL_NAME (parm) = cp_parser_identifier (parser);
15092 WILDCARD_PACK_P (parm) = is_pack;
15093 vec_safe_push (introduction_vec, parm);
15095 /* If the next token is not a `,', we're done. */
15096 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15097 break;
15098 /* Otherwise, consume the `,' token. */
15099 cp_lexer_consume_token (parser->lexer);
15102 /* Convert the vec into a TREE_VEC. */
15103 tree introduction_list = make_tree_vec (introduction_vec->length ());
15104 unsigned int n;
15105 tree parm;
15106 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15107 TREE_VEC_ELT (introduction_list, n) = parm;
15109 release_tree_vector (introduction_vec);
15110 return introduction_list;
15113 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15114 is an abstract declarator. */
15116 static inline cp_declarator*
15117 get_id_declarator (cp_declarator *declarator)
15119 cp_declarator *d = declarator;
15120 while (d && d->kind != cdk_id)
15121 d = d->declarator;
15122 return d;
15125 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15126 is an abstract declarator. */
15128 static inline tree
15129 get_unqualified_id (cp_declarator *declarator)
15131 declarator = get_id_declarator (declarator);
15132 if (declarator)
15133 return declarator->u.id.unqualified_name;
15134 else
15135 return NULL_TREE;
15138 /* Returns true if DECL represents a constrained-parameter. */
15140 static inline bool
15141 is_constrained_parameter (tree decl)
15143 return (decl
15144 && TREE_CODE (decl) == TYPE_DECL
15145 && CONSTRAINED_PARM_CONCEPT (decl)
15146 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15149 /* Returns true if PARM declares a constrained-parameter. */
15151 static inline bool
15152 is_constrained_parameter (cp_parameter_declarator *parm)
15154 return is_constrained_parameter (parm->decl_specifiers.type);
15157 /* Check that the type parameter is only a declarator-id, and that its
15158 type is not cv-qualified. */
15160 bool
15161 cp_parser_check_constrained_type_parm (cp_parser *parser,
15162 cp_parameter_declarator *parm)
15164 if (!parm->declarator)
15165 return true;
15167 if (parm->declarator->kind != cdk_id)
15169 cp_parser_error (parser, "invalid constrained type parameter");
15170 return false;
15173 /* Don't allow cv-qualified type parameters. */
15174 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15175 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15177 cp_parser_error (parser, "cv-qualified type parameter");
15178 return false;
15181 return true;
15184 /* Finish parsing/processing a template type parameter and checking
15185 various restrictions. */
15187 static inline tree
15188 cp_parser_constrained_type_template_parm (cp_parser *parser,
15189 tree id,
15190 cp_parameter_declarator* parmdecl)
15192 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15193 return finish_template_type_parm (class_type_node, id);
15194 else
15195 return error_mark_node;
15198 static tree
15199 finish_constrained_template_template_parm (tree proto, tree id)
15201 /* FIXME: This should probably be copied, and we may need to adjust
15202 the template parameter depths. */
15203 tree saved_parms = current_template_parms;
15204 begin_template_parm_list ();
15205 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15206 end_template_parm_list ();
15208 tree parm = finish_template_template_parm (class_type_node, id);
15209 current_template_parms = saved_parms;
15211 return parm;
15214 /* Finish parsing/processing a template template parameter by borrowing
15215 the template parameter list from the prototype parameter. */
15217 static tree
15218 cp_parser_constrained_template_template_parm (cp_parser *parser,
15219 tree proto,
15220 tree id,
15221 cp_parameter_declarator *parmdecl)
15223 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15224 return error_mark_node;
15225 return finish_constrained_template_template_parm (proto, id);
15228 /* Create a new non-type template parameter from the given PARM
15229 declarator. */
15231 static tree
15232 constrained_non_type_template_parm (bool *is_non_type,
15233 cp_parameter_declarator *parm)
15235 *is_non_type = true;
15236 cp_declarator *decl = parm->declarator;
15237 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15238 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15239 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15242 /* Build a constrained template parameter based on the PARMDECL
15243 declarator. The type of PARMDECL is the constrained type, which
15244 refers to the prototype template parameter that ultimately
15245 specifies the type of the declared parameter. */
15247 static tree
15248 finish_constrained_parameter (cp_parser *parser,
15249 cp_parameter_declarator *parmdecl,
15250 bool *is_non_type,
15251 bool *is_parameter_pack)
15253 tree decl = parmdecl->decl_specifiers.type;
15254 tree id = get_unqualified_id (parmdecl->declarator);
15255 tree def = parmdecl->default_argument;
15256 tree proto = DECL_INITIAL (decl);
15258 /* A template parameter constrained by a variadic concept shall also
15259 be declared as a template parameter pack. */
15260 bool is_variadic = template_parameter_pack_p (proto);
15261 if (is_variadic && !*is_parameter_pack)
15262 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15264 /* Build the parameter. Return an error if the declarator was invalid. */
15265 tree parm;
15266 if (TREE_CODE (proto) == TYPE_DECL)
15267 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15268 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15269 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15270 parmdecl);
15271 else
15272 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15273 if (parm == error_mark_node)
15274 return error_mark_node;
15276 /* Finish the parameter decl and create a node attaching the
15277 default argument and constraint. */
15278 parm = build_tree_list (def, parm);
15279 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15281 return parm;
15284 /* Returns true if the parsed type actually represents the declaration
15285 of a type template-parameter. */
15287 static inline bool
15288 declares_constrained_type_template_parameter (tree type)
15290 return (is_constrained_parameter (type)
15291 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15295 /* Returns true if the parsed type actually represents the declaration of
15296 a template template-parameter. */
15298 static bool
15299 declares_constrained_template_template_parameter (tree type)
15301 return (is_constrained_parameter (type)
15302 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15305 /* Parse a default argument for a type template-parameter.
15306 Note that diagnostics are handled in cp_parser_template_parameter. */
15308 static tree
15309 cp_parser_default_type_template_argument (cp_parser *parser)
15311 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15313 /* Consume the `=' token. */
15314 cp_lexer_consume_token (parser->lexer);
15316 cp_token *token = cp_lexer_peek_token (parser->lexer);
15318 /* Parse the default-argument. */
15319 push_deferring_access_checks (dk_no_deferred);
15320 tree default_argument = cp_parser_type_id (parser);
15321 pop_deferring_access_checks ();
15323 if (flag_concepts && type_uses_auto (default_argument))
15325 error_at (token->location,
15326 "invalid use of %<auto%> in default template argument");
15327 return error_mark_node;
15330 return default_argument;
15333 /* Parse a default argument for a template template-parameter. */
15335 static tree
15336 cp_parser_default_template_template_argument (cp_parser *parser)
15338 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15340 bool is_template;
15342 /* Consume the `='. */
15343 cp_lexer_consume_token (parser->lexer);
15344 /* Parse the id-expression. */
15345 push_deferring_access_checks (dk_no_deferred);
15346 /* save token before parsing the id-expression, for error
15347 reporting */
15348 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15349 tree default_argument
15350 = cp_parser_id_expression (parser,
15351 /*template_keyword_p=*/false,
15352 /*check_dependency_p=*/true,
15353 /*template_p=*/&is_template,
15354 /*declarator_p=*/false,
15355 /*optional_p=*/false);
15356 if (TREE_CODE (default_argument) == TYPE_DECL)
15357 /* If the id-expression was a template-id that refers to
15358 a template-class, we already have the declaration here,
15359 so no further lookup is needed. */
15361 else
15362 /* Look up the name. */
15363 default_argument
15364 = cp_parser_lookup_name (parser, default_argument,
15365 none_type,
15366 /*is_template=*/is_template,
15367 /*is_namespace=*/false,
15368 /*check_dependency=*/true,
15369 /*ambiguous_decls=*/NULL,
15370 token->location);
15371 /* See if the default argument is valid. */
15372 default_argument = check_template_template_default_arg (default_argument);
15373 pop_deferring_access_checks ();
15374 return default_argument;
15377 /* Parse a template-parameter.
15379 template-parameter:
15380 type-parameter
15381 parameter-declaration
15383 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15384 the parameter. The TREE_PURPOSE is the default value, if any.
15385 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15386 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15387 set to true iff this parameter is a parameter pack. */
15389 static tree
15390 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15391 bool *is_parameter_pack)
15393 cp_token *token;
15394 cp_parameter_declarator *parameter_declarator;
15395 tree parm;
15397 /* Assume it is a type parameter or a template parameter. */
15398 *is_non_type = false;
15399 /* Assume it not a parameter pack. */
15400 *is_parameter_pack = false;
15401 /* Peek at the next token. */
15402 token = cp_lexer_peek_token (parser->lexer);
15403 /* If it is `template', we have a type-parameter. */
15404 if (token->keyword == RID_TEMPLATE)
15405 return cp_parser_type_parameter (parser, is_parameter_pack);
15406 /* If it is `class' or `typename' we do not know yet whether it is a
15407 type parameter or a non-type parameter. Consider:
15409 template <typename T, typename T::X X> ...
15413 template <class C, class D*> ...
15415 Here, the first parameter is a type parameter, and the second is
15416 a non-type parameter. We can tell by looking at the token after
15417 the identifier -- if it is a `,', `=', or `>' then we have a type
15418 parameter. */
15419 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15421 /* Peek at the token after `class' or `typename'. */
15422 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15423 /* If it's an ellipsis, we have a template type parameter
15424 pack. */
15425 if (token->type == CPP_ELLIPSIS)
15426 return cp_parser_type_parameter (parser, is_parameter_pack);
15427 /* If it's an identifier, skip it. */
15428 if (token->type == CPP_NAME)
15429 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15430 /* Now, see if the token looks like the end of a template
15431 parameter. */
15432 if (token->type == CPP_COMMA
15433 || token->type == CPP_EQ
15434 || token->type == CPP_GREATER)
15435 return cp_parser_type_parameter (parser, is_parameter_pack);
15438 /* Otherwise, it is a non-type parameter or a constrained parameter.
15440 [temp.param]
15442 When parsing a default template-argument for a non-type
15443 template-parameter, the first non-nested `>' is taken as the end
15444 of the template parameter-list rather than a greater-than
15445 operator. */
15446 parameter_declarator
15447 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15448 /*parenthesized_p=*/NULL);
15450 if (!parameter_declarator)
15451 return error_mark_node;
15453 /* If the parameter declaration is marked as a parameter pack, set
15454 *IS_PARAMETER_PACK to notify the caller. */
15455 if (parameter_declarator->template_parameter_pack_p)
15456 *is_parameter_pack = true;
15458 if (parameter_declarator->default_argument)
15460 /* Can happen in some cases of erroneous input (c++/34892). */
15461 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15462 /* Consume the `...' for better error recovery. */
15463 cp_lexer_consume_token (parser->lexer);
15466 // The parameter may have been constrained.
15467 if (is_constrained_parameter (parameter_declarator))
15468 return finish_constrained_parameter (parser,
15469 parameter_declarator,
15470 is_non_type,
15471 is_parameter_pack);
15473 // Now we're sure that the parameter is a non-type parameter.
15474 *is_non_type = true;
15476 parm = grokdeclarator (parameter_declarator->declarator,
15477 &parameter_declarator->decl_specifiers,
15478 TPARM, /*initialized=*/0,
15479 /*attrlist=*/NULL);
15480 if (parm == error_mark_node)
15481 return error_mark_node;
15483 return build_tree_list (parameter_declarator->default_argument, parm);
15486 /* Parse a type-parameter.
15488 type-parameter:
15489 class identifier [opt]
15490 class identifier [opt] = type-id
15491 typename identifier [opt]
15492 typename identifier [opt] = type-id
15493 template < template-parameter-list > class identifier [opt]
15494 template < template-parameter-list > class identifier [opt]
15495 = id-expression
15497 GNU Extension (variadic templates):
15499 type-parameter:
15500 class ... identifier [opt]
15501 typename ... identifier [opt]
15503 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15504 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15505 the declaration of the parameter.
15507 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15509 static tree
15510 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15512 cp_token *token;
15513 tree parameter;
15515 /* Look for a keyword to tell us what kind of parameter this is. */
15516 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15517 if (!token)
15518 return error_mark_node;
15520 switch (token->keyword)
15522 case RID_CLASS:
15523 case RID_TYPENAME:
15525 tree identifier;
15526 tree default_argument;
15528 /* If the next token is an ellipsis, we have a template
15529 argument pack. */
15530 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15532 /* Consume the `...' token. */
15533 cp_lexer_consume_token (parser->lexer);
15534 maybe_warn_variadic_templates ();
15536 *is_parameter_pack = true;
15539 /* If the next token is an identifier, then it names the
15540 parameter. */
15541 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15542 identifier = cp_parser_identifier (parser);
15543 else
15544 identifier = NULL_TREE;
15546 /* Create the parameter. */
15547 parameter = finish_template_type_parm (class_type_node, identifier);
15549 /* If the next token is an `=', we have a default argument. */
15550 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15552 default_argument
15553 = cp_parser_default_type_template_argument (parser);
15555 /* Template parameter packs cannot have default
15556 arguments. */
15557 if (*is_parameter_pack)
15559 if (identifier)
15560 error_at (token->location,
15561 "template parameter pack %qD cannot have a "
15562 "default argument", identifier);
15563 else
15564 error_at (token->location,
15565 "template parameter packs cannot have "
15566 "default arguments");
15567 default_argument = NULL_TREE;
15569 else if (check_for_bare_parameter_packs (default_argument))
15570 default_argument = error_mark_node;
15572 else
15573 default_argument = NULL_TREE;
15575 /* Create the combined representation of the parameter and the
15576 default argument. */
15577 parameter = build_tree_list (default_argument, parameter);
15579 break;
15581 case RID_TEMPLATE:
15583 tree identifier;
15584 tree default_argument;
15586 /* Look for the `<'. */
15587 cp_parser_require (parser, CPP_LESS, RT_LESS);
15588 /* Parse the template-parameter-list. */
15589 cp_parser_template_parameter_list (parser);
15590 /* Look for the `>'. */
15591 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15593 // If template requirements are present, parse them.
15594 if (flag_concepts)
15596 tree reqs = get_shorthand_constraints (current_template_parms);
15597 if (tree r = cp_parser_requires_clause_opt (parser))
15598 reqs = conjoin_constraints (reqs, normalize_expression (r));
15599 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15602 /* Look for the `class' or 'typename' keywords. */
15603 cp_parser_type_parameter_key (parser);
15604 /* If the next token is an ellipsis, we have a template
15605 argument pack. */
15606 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15608 /* Consume the `...' token. */
15609 cp_lexer_consume_token (parser->lexer);
15610 maybe_warn_variadic_templates ();
15612 *is_parameter_pack = true;
15614 /* If the next token is an `=', then there is a
15615 default-argument. If the next token is a `>', we are at
15616 the end of the parameter-list. If the next token is a `,',
15617 then we are at the end of this parameter. */
15618 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15619 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15620 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15622 identifier = cp_parser_identifier (parser);
15623 /* Treat invalid names as if the parameter were nameless. */
15624 if (identifier == error_mark_node)
15625 identifier = NULL_TREE;
15627 else
15628 identifier = NULL_TREE;
15630 /* Create the template parameter. */
15631 parameter = finish_template_template_parm (class_type_node,
15632 identifier);
15634 /* If the next token is an `=', then there is a
15635 default-argument. */
15636 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15638 default_argument
15639 = cp_parser_default_template_template_argument (parser);
15641 /* Template parameter packs cannot have default
15642 arguments. */
15643 if (*is_parameter_pack)
15645 if (identifier)
15646 error_at (token->location,
15647 "template parameter pack %qD cannot "
15648 "have a default argument",
15649 identifier);
15650 else
15651 error_at (token->location, "template parameter packs cannot "
15652 "have default arguments");
15653 default_argument = NULL_TREE;
15656 else
15657 default_argument = NULL_TREE;
15659 /* Create the combined representation of the parameter and the
15660 default argument. */
15661 parameter = build_tree_list (default_argument, parameter);
15663 break;
15665 default:
15666 gcc_unreachable ();
15667 break;
15670 return parameter;
15673 /* Parse a template-id.
15675 template-id:
15676 template-name < template-argument-list [opt] >
15678 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15679 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15680 returned. Otherwise, if the template-name names a function, or set
15681 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15682 names a class, returns a TYPE_DECL for the specialization.
15684 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15685 uninstantiated templates. */
15687 static tree
15688 cp_parser_template_id (cp_parser *parser,
15689 bool template_keyword_p,
15690 bool check_dependency_p,
15691 enum tag_types tag_type,
15692 bool is_declaration)
15694 tree templ;
15695 tree arguments;
15696 tree template_id;
15697 cp_token_position start_of_id = 0;
15698 cp_token *next_token = NULL, *next_token_2 = NULL;
15699 bool is_identifier;
15701 /* If the next token corresponds to a template-id, there is no need
15702 to reparse it. */
15703 cp_token *token = cp_lexer_peek_token (parser->lexer);
15704 if (token->type == CPP_TEMPLATE_ID)
15706 cp_lexer_consume_token (parser->lexer);
15707 return saved_checks_value (token->u.tree_check_value);
15710 /* Avoid performing name lookup if there is no possibility of
15711 finding a template-id. */
15712 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15713 || (token->type == CPP_NAME
15714 && !cp_parser_nth_token_starts_template_argument_list_p
15715 (parser, 2)))
15717 cp_parser_error (parser, "expected template-id");
15718 return error_mark_node;
15721 /* Remember where the template-id starts. */
15722 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15723 start_of_id = cp_lexer_token_position (parser->lexer, false);
15725 push_deferring_access_checks (dk_deferred);
15727 /* Parse the template-name. */
15728 is_identifier = false;
15729 templ = cp_parser_template_name (parser, template_keyword_p,
15730 check_dependency_p,
15731 is_declaration,
15732 tag_type,
15733 &is_identifier);
15734 if (templ == error_mark_node || is_identifier)
15736 pop_deferring_access_checks ();
15737 return templ;
15740 /* Since we're going to preserve any side-effects from this parse, set up a
15741 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15742 in the template arguments. */
15743 tentative_firewall firewall (parser);
15745 /* If we find the sequence `[:' after a template-name, it's probably
15746 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15747 parse correctly the argument list. */
15748 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15749 == CPP_OPEN_SQUARE)
15750 && next_token->flags & DIGRAPH
15751 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15752 == CPP_COLON)
15753 && !(next_token_2->flags & PREV_WHITE))
15755 cp_parser_parse_tentatively (parser);
15756 /* Change `:' into `::'. */
15757 next_token_2->type = CPP_SCOPE;
15758 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15759 CPP_LESS. */
15760 cp_lexer_consume_token (parser->lexer);
15762 /* Parse the arguments. */
15763 arguments = cp_parser_enclosed_template_argument_list (parser);
15764 if (!cp_parser_parse_definitely (parser))
15766 /* If we couldn't parse an argument list, then we revert our changes
15767 and return simply an error. Maybe this is not a template-id
15768 after all. */
15769 next_token_2->type = CPP_COLON;
15770 cp_parser_error (parser, "expected %<<%>");
15771 pop_deferring_access_checks ();
15772 return error_mark_node;
15774 /* Otherwise, emit an error about the invalid digraph, but continue
15775 parsing because we got our argument list. */
15776 if (permerror (next_token->location,
15777 "%<<::%> cannot begin a template-argument list"))
15779 static bool hint = false;
15780 inform (next_token->location,
15781 "%<<:%> is an alternate spelling for %<[%>."
15782 " Insert whitespace between %<<%> and %<::%>");
15783 if (!hint && !flag_permissive)
15785 inform (next_token->location, "(if you use %<-fpermissive%> "
15786 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15787 "accept your code)");
15788 hint = true;
15792 else
15794 /* Look for the `<' that starts the template-argument-list. */
15795 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15797 pop_deferring_access_checks ();
15798 return error_mark_node;
15800 /* Parse the arguments. */
15801 arguments = cp_parser_enclosed_template_argument_list (parser);
15804 /* Set the location to be of the form:
15805 template-name < template-argument-list [opt] >
15806 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15807 with caret == start at the start of the template-name,
15808 ranging until the closing '>'. */
15809 location_t finish_loc
15810 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15811 location_t combined_loc
15812 = make_location (token->location, token->location, finish_loc);
15814 /* Check for concepts autos where they don't belong. We could
15815 identify types in some cases of idnetifier TEMPL, looking ahead
15816 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
15817 types. We reject them in functions, but if what we have is an
15818 identifier, even with none_type we can't conclude it's NOT a
15819 type, we have to wait for template substitution. */
15820 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
15821 template_id = error_mark_node;
15822 /* Build a representation of the specialization. */
15823 else if (identifier_p (templ))
15824 template_id = build_min_nt_loc (combined_loc,
15825 TEMPLATE_ID_EXPR,
15826 templ, arguments);
15827 else if (DECL_TYPE_TEMPLATE_P (templ)
15828 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15830 bool entering_scope;
15831 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15832 template (rather than some instantiation thereof) only if
15833 is not nested within some other construct. For example, in
15834 "template <typename T> void f(T) { A<T>::", A<T> is just an
15835 instantiation of A. */
15836 entering_scope = (template_parm_scope_p ()
15837 && cp_lexer_next_token_is (parser->lexer,
15838 CPP_SCOPE));
15839 template_id
15840 = finish_template_type (templ, arguments, entering_scope);
15842 /* A template-like identifier may be a partial concept id. */
15843 else if (flag_concepts
15844 && (template_id = (cp_parser_maybe_partial_concept_id
15845 (parser, templ, arguments))))
15846 return template_id;
15847 else if (variable_template_p (templ))
15849 template_id = lookup_template_variable (templ, arguments);
15850 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15851 SET_EXPR_LOCATION (template_id, combined_loc);
15853 else
15855 /* If it's not a class-template or a template-template, it should be
15856 a function-template. */
15857 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15858 || TREE_CODE (templ) == OVERLOAD
15859 || BASELINK_P (templ)));
15861 template_id = lookup_template_function (templ, arguments);
15862 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15863 SET_EXPR_LOCATION (template_id, combined_loc);
15866 /* If parsing tentatively, replace the sequence of tokens that makes
15867 up the template-id with a CPP_TEMPLATE_ID token. That way,
15868 should we re-parse the token stream, we will not have to repeat
15869 the effort required to do the parse, nor will we issue duplicate
15870 error messages about problems during instantiation of the
15871 template. */
15872 if (start_of_id
15873 /* Don't do this if we had a parse error in a declarator; re-parsing
15874 might succeed if a name changes meaning (60361). */
15875 && !(cp_parser_error_occurred (parser)
15876 && cp_parser_parsing_tentatively (parser)
15877 && parser->in_declarator_p))
15879 /* Reset the contents of the START_OF_ID token. */
15880 token->type = CPP_TEMPLATE_ID;
15881 token->location = combined_loc;
15883 /* We must mark the lookup as kept, so we don't throw it away on
15884 the first parse. */
15885 if (is_overloaded_fn (template_id))
15886 lookup_keep (get_fns (template_id), true);
15888 /* Retrieve any deferred checks. Do not pop this access checks yet
15889 so the memory will not be reclaimed during token replacing below. */
15890 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15891 token->u.tree_check_value->value = template_id;
15892 token->u.tree_check_value->checks = get_deferred_access_checks ();
15893 token->keyword = RID_MAX;
15895 /* Purge all subsequent tokens. */
15896 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15898 /* ??? Can we actually assume that, if template_id ==
15899 error_mark_node, we will have issued a diagnostic to the
15900 user, as opposed to simply marking the tentative parse as
15901 failed? */
15902 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15903 error_at (token->location, "parse error in template argument list");
15906 pop_to_parent_deferring_access_checks ();
15907 return template_id;
15910 /* Parse a template-name.
15912 template-name:
15913 identifier
15915 The standard should actually say:
15917 template-name:
15918 identifier
15919 operator-function-id
15921 A defect report has been filed about this issue.
15923 A conversion-function-id cannot be a template name because they cannot
15924 be part of a template-id. In fact, looking at this code:
15926 a.operator K<int>()
15928 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15929 It is impossible to call a templated conversion-function-id with an
15930 explicit argument list, since the only allowed template parameter is
15931 the type to which it is converting.
15933 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15934 `template' keyword, in a construction like:
15936 T::template f<3>()
15938 In that case `f' is taken to be a template-name, even though there
15939 is no way of knowing for sure.
15941 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15942 name refers to a set of overloaded functions, at least one of which
15943 is a template, or an IDENTIFIER_NODE with the name of the template,
15944 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15945 names are looked up inside uninstantiated templates. */
15947 static tree
15948 cp_parser_template_name (cp_parser* parser,
15949 bool template_keyword_p,
15950 bool check_dependency_p,
15951 bool is_declaration,
15952 enum tag_types tag_type,
15953 bool *is_identifier)
15955 tree identifier;
15956 tree decl;
15957 cp_token *token = cp_lexer_peek_token (parser->lexer);
15959 /* If the next token is `operator', then we have either an
15960 operator-function-id or a conversion-function-id. */
15961 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15963 /* We don't know whether we're looking at an
15964 operator-function-id or a conversion-function-id. */
15965 cp_parser_parse_tentatively (parser);
15966 /* Try an operator-function-id. */
15967 identifier = cp_parser_operator_function_id (parser);
15968 /* If that didn't work, try a conversion-function-id. */
15969 if (!cp_parser_parse_definitely (parser))
15971 cp_parser_error (parser, "expected template-name");
15972 return error_mark_node;
15975 /* Look for the identifier. */
15976 else
15977 identifier = cp_parser_identifier (parser);
15979 /* If we didn't find an identifier, we don't have a template-id. */
15980 if (identifier == error_mark_node)
15981 return error_mark_node;
15983 /* If the name immediately followed the `template' keyword, then it
15984 is a template-name. However, if the next token is not `<', then
15985 we do not treat it as a template-name, since it is not being used
15986 as part of a template-id. This enables us to handle constructs
15987 like:
15989 template <typename T> struct S { S(); };
15990 template <typename T> S<T>::S();
15992 correctly. We would treat `S' as a template -- if it were `S<T>'
15993 -- but we do not if there is no `<'. */
15995 if (processing_template_decl
15996 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15998 /* In a declaration, in a dependent context, we pretend that the
15999 "template" keyword was present in order to improve error
16000 recovery. For example, given:
16002 template <typename T> void f(T::X<int>);
16004 we want to treat "X<int>" as a template-id. */
16005 if (is_declaration
16006 && !template_keyword_p
16007 && parser->scope && TYPE_P (parser->scope)
16008 && check_dependency_p
16009 && dependent_scope_p (parser->scope)
16010 /* Do not do this for dtors (or ctors), since they never
16011 need the template keyword before their name. */
16012 && !constructor_name_p (identifier, parser->scope))
16014 cp_token_position start = 0;
16016 /* Explain what went wrong. */
16017 error_at (token->location, "non-template %qD used as template",
16018 identifier);
16019 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16020 parser->scope, identifier);
16021 /* If parsing tentatively, find the location of the "<" token. */
16022 if (cp_parser_simulate_error (parser))
16023 start = cp_lexer_token_position (parser->lexer, true);
16024 /* Parse the template arguments so that we can issue error
16025 messages about them. */
16026 cp_lexer_consume_token (parser->lexer);
16027 cp_parser_enclosed_template_argument_list (parser);
16028 /* Skip tokens until we find a good place from which to
16029 continue parsing. */
16030 cp_parser_skip_to_closing_parenthesis (parser,
16031 /*recovering=*/true,
16032 /*or_comma=*/true,
16033 /*consume_paren=*/false);
16034 /* If parsing tentatively, permanently remove the
16035 template argument list. That will prevent duplicate
16036 error messages from being issued about the missing
16037 "template" keyword. */
16038 if (start)
16039 cp_lexer_purge_tokens_after (parser->lexer, start);
16040 if (is_identifier)
16041 *is_identifier = true;
16042 parser->context->object_type = NULL_TREE;
16043 return identifier;
16046 /* If the "template" keyword is present, then there is generally
16047 no point in doing name-lookup, so we just return IDENTIFIER.
16048 But, if the qualifying scope is non-dependent then we can
16049 (and must) do name-lookup normally. */
16050 if (template_keyword_p)
16052 tree scope = (parser->scope ? parser->scope
16053 : parser->context->object_type);
16054 if (scope && TYPE_P (scope)
16055 && (!CLASS_TYPE_P (scope)
16056 || (check_dependency_p && dependent_type_p (scope))))
16058 /* We're optimizing away the call to cp_parser_lookup_name, but
16059 we still need to do this. */
16060 parser->context->object_type = NULL_TREE;
16061 return identifier;
16066 /* Look up the name. */
16067 decl = cp_parser_lookup_name (parser, identifier,
16068 tag_type,
16069 /*is_template=*/true,
16070 /*is_namespace=*/false,
16071 check_dependency_p,
16072 /*ambiguous_decls=*/NULL,
16073 token->location);
16075 decl = strip_using_decl (decl);
16077 /* If DECL is a template, then the name was a template-name. */
16078 if (TREE_CODE (decl) == TEMPLATE_DECL)
16080 if (TREE_DEPRECATED (decl)
16081 && deprecated_state != DEPRECATED_SUPPRESS)
16082 warn_deprecated_use (decl, NULL_TREE);
16084 else
16086 /* The standard does not explicitly indicate whether a name that
16087 names a set of overloaded declarations, some of which are
16088 templates, is a template-name. However, such a name should
16089 be a template-name; otherwise, there is no way to form a
16090 template-id for the overloaded templates. */
16091 bool found = false;
16093 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16094 !found && iter; ++iter)
16095 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16096 found = true;
16098 if (!found)
16100 /* The name does not name a template. */
16101 cp_parser_error (parser, "expected template-name");
16102 return error_mark_node;
16106 /* If DECL is dependent, and refers to a function, then just return
16107 its name; we will look it up again during template instantiation. */
16108 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16110 tree scope = ovl_scope (decl);
16111 if (TYPE_P (scope) && dependent_type_p (scope))
16112 return identifier;
16115 return decl;
16118 /* Parse a template-argument-list.
16120 template-argument-list:
16121 template-argument ... [opt]
16122 template-argument-list , template-argument ... [opt]
16124 Returns a TREE_VEC containing the arguments. */
16126 static tree
16127 cp_parser_template_argument_list (cp_parser* parser)
16129 tree fixed_args[10];
16130 unsigned n_args = 0;
16131 unsigned alloced = 10;
16132 tree *arg_ary = fixed_args;
16133 tree vec;
16134 bool saved_in_template_argument_list_p;
16135 bool saved_ice_p;
16136 bool saved_non_ice_p;
16138 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16139 parser->in_template_argument_list_p = true;
16140 /* Even if the template-id appears in an integral
16141 constant-expression, the contents of the argument list do
16142 not. */
16143 saved_ice_p = parser->integral_constant_expression_p;
16144 parser->integral_constant_expression_p = false;
16145 saved_non_ice_p = parser->non_integral_constant_expression_p;
16146 parser->non_integral_constant_expression_p = false;
16148 /* Parse the arguments. */
16151 tree argument;
16153 if (n_args)
16154 /* Consume the comma. */
16155 cp_lexer_consume_token (parser->lexer);
16157 /* Parse the template-argument. */
16158 argument = cp_parser_template_argument (parser);
16160 /* If the next token is an ellipsis, we're expanding a template
16161 argument pack. */
16162 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16164 if (argument == error_mark_node)
16166 cp_token *token = cp_lexer_peek_token (parser->lexer);
16167 error_at (token->location,
16168 "expected parameter pack before %<...%>");
16170 /* Consume the `...' token. */
16171 cp_lexer_consume_token (parser->lexer);
16173 /* Make the argument into a TYPE_PACK_EXPANSION or
16174 EXPR_PACK_EXPANSION. */
16175 argument = make_pack_expansion (argument);
16178 if (n_args == alloced)
16180 alloced *= 2;
16182 if (arg_ary == fixed_args)
16184 arg_ary = XNEWVEC (tree, alloced);
16185 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16187 else
16188 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16190 arg_ary[n_args++] = argument;
16192 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16194 vec = make_tree_vec (n_args);
16196 while (n_args--)
16197 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16199 if (arg_ary != fixed_args)
16200 free (arg_ary);
16201 parser->non_integral_constant_expression_p = saved_non_ice_p;
16202 parser->integral_constant_expression_p = saved_ice_p;
16203 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16204 if (CHECKING_P)
16205 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16206 return vec;
16209 /* Parse a template-argument.
16211 template-argument:
16212 assignment-expression
16213 type-id
16214 id-expression
16216 The representation is that of an assignment-expression, type-id, or
16217 id-expression -- except that the qualified id-expression is
16218 evaluated, so that the value returned is either a DECL or an
16219 OVERLOAD.
16221 Although the standard says "assignment-expression", it forbids
16222 throw-expressions or assignments in the template argument.
16223 Therefore, we use "conditional-expression" instead. */
16225 static tree
16226 cp_parser_template_argument (cp_parser* parser)
16228 tree argument;
16229 bool template_p;
16230 bool address_p;
16231 bool maybe_type_id = false;
16232 cp_token *token = NULL, *argument_start_token = NULL;
16233 location_t loc = 0;
16234 cp_id_kind idk;
16236 /* There's really no way to know what we're looking at, so we just
16237 try each alternative in order.
16239 [temp.arg]
16241 In a template-argument, an ambiguity between a type-id and an
16242 expression is resolved to a type-id, regardless of the form of
16243 the corresponding template-parameter.
16245 Therefore, we try a type-id first. */
16246 cp_parser_parse_tentatively (parser);
16247 argument = cp_parser_template_type_arg (parser);
16248 /* If there was no error parsing the type-id but the next token is a
16249 '>>', our behavior depends on which dialect of C++ we're
16250 parsing. In C++98, we probably found a typo for '> >'. But there
16251 are type-id which are also valid expressions. For instance:
16253 struct X { int operator >> (int); };
16254 template <int V> struct Foo {};
16255 Foo<X () >> 5> r;
16257 Here 'X()' is a valid type-id of a function type, but the user just
16258 wanted to write the expression "X() >> 5". Thus, we remember that we
16259 found a valid type-id, but we still try to parse the argument as an
16260 expression to see what happens.
16262 In C++0x, the '>>' will be considered two separate '>'
16263 tokens. */
16264 if (!cp_parser_error_occurred (parser)
16265 && cxx_dialect == cxx98
16266 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16268 maybe_type_id = true;
16269 cp_parser_abort_tentative_parse (parser);
16271 else
16273 /* If the next token isn't a `,' or a `>', then this argument wasn't
16274 really finished. This means that the argument is not a valid
16275 type-id. */
16276 if (!cp_parser_next_token_ends_template_argument_p (parser))
16277 cp_parser_error (parser, "expected template-argument");
16278 /* If that worked, we're done. */
16279 if (cp_parser_parse_definitely (parser))
16280 return argument;
16282 /* We're still not sure what the argument will be. */
16283 cp_parser_parse_tentatively (parser);
16284 /* Try a template. */
16285 argument_start_token = cp_lexer_peek_token (parser->lexer);
16286 argument = cp_parser_id_expression (parser,
16287 /*template_keyword_p=*/false,
16288 /*check_dependency_p=*/true,
16289 &template_p,
16290 /*declarator_p=*/false,
16291 /*optional_p=*/false);
16292 /* If the next token isn't a `,' or a `>', then this argument wasn't
16293 really finished. */
16294 if (!cp_parser_next_token_ends_template_argument_p (parser))
16295 cp_parser_error (parser, "expected template-argument");
16296 if (!cp_parser_error_occurred (parser))
16298 /* Figure out what is being referred to. If the id-expression
16299 was for a class template specialization, then we will have a
16300 TYPE_DECL at this point. There is no need to do name lookup
16301 at this point in that case. */
16302 if (TREE_CODE (argument) != TYPE_DECL)
16303 argument = cp_parser_lookup_name (parser, argument,
16304 none_type,
16305 /*is_template=*/template_p,
16306 /*is_namespace=*/false,
16307 /*check_dependency=*/true,
16308 /*ambiguous_decls=*/NULL,
16309 argument_start_token->location);
16310 /* Handle a constrained-type-specifier for a non-type template
16311 parameter. */
16312 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16313 argument = decl;
16314 else if (TREE_CODE (argument) != TEMPLATE_DECL
16315 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16316 cp_parser_error (parser, "expected template-name");
16318 if (cp_parser_parse_definitely (parser))
16320 if (TREE_DEPRECATED (argument))
16321 warn_deprecated_use (argument, NULL_TREE);
16322 return argument;
16324 /* It must be a non-type argument. In C++17 any constant-expression is
16325 allowed. */
16326 if (cxx_dialect > cxx14)
16327 goto general_expr;
16329 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16331 -- an integral constant-expression of integral or enumeration
16332 type; or
16334 -- the name of a non-type template-parameter; or
16336 -- the name of an object or function with external linkage...
16338 -- the address of an object or function with external linkage...
16340 -- a pointer to member... */
16341 /* Look for a non-type template parameter. */
16342 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16344 cp_parser_parse_tentatively (parser);
16345 argument = cp_parser_primary_expression (parser,
16346 /*address_p=*/false,
16347 /*cast_p=*/false,
16348 /*template_arg_p=*/true,
16349 &idk);
16350 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16351 || !cp_parser_next_token_ends_template_argument_p (parser))
16352 cp_parser_simulate_error (parser);
16353 if (cp_parser_parse_definitely (parser))
16354 return argument;
16357 /* If the next token is "&", the argument must be the address of an
16358 object or function with external linkage. */
16359 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16360 if (address_p)
16362 loc = cp_lexer_peek_token (parser->lexer)->location;
16363 cp_lexer_consume_token (parser->lexer);
16365 /* See if we might have an id-expression. */
16366 token = cp_lexer_peek_token (parser->lexer);
16367 if (token->type == CPP_NAME
16368 || token->keyword == RID_OPERATOR
16369 || token->type == CPP_SCOPE
16370 || token->type == CPP_TEMPLATE_ID
16371 || token->type == CPP_NESTED_NAME_SPECIFIER)
16373 cp_parser_parse_tentatively (parser);
16374 argument = cp_parser_primary_expression (parser,
16375 address_p,
16376 /*cast_p=*/false,
16377 /*template_arg_p=*/true,
16378 &idk);
16379 if (cp_parser_error_occurred (parser)
16380 || !cp_parser_next_token_ends_template_argument_p (parser))
16381 cp_parser_abort_tentative_parse (parser);
16382 else
16384 tree probe;
16386 if (INDIRECT_REF_P (argument))
16388 /* Strip the dereference temporarily. */
16389 gcc_assert (REFERENCE_REF_P (argument));
16390 argument = TREE_OPERAND (argument, 0);
16393 /* If we're in a template, we represent a qualified-id referring
16394 to a static data member as a SCOPE_REF even if the scope isn't
16395 dependent so that we can check access control later. */
16396 probe = argument;
16397 if (TREE_CODE (probe) == SCOPE_REF)
16398 probe = TREE_OPERAND (probe, 1);
16399 if (VAR_P (probe))
16401 /* A variable without external linkage might still be a
16402 valid constant-expression, so no error is issued here
16403 if the external-linkage check fails. */
16404 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16405 cp_parser_simulate_error (parser);
16407 else if (is_overloaded_fn (argument))
16408 /* All overloaded functions are allowed; if the external
16409 linkage test does not pass, an error will be issued
16410 later. */
16412 else if (address_p
16413 && (TREE_CODE (argument) == OFFSET_REF
16414 || TREE_CODE (argument) == SCOPE_REF))
16415 /* A pointer-to-member. */
16417 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16419 else
16420 cp_parser_simulate_error (parser);
16422 if (cp_parser_parse_definitely (parser))
16424 if (address_p)
16425 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16426 tf_warning_or_error);
16427 else
16428 argument = convert_from_reference (argument);
16429 return argument;
16433 /* If the argument started with "&", there are no other valid
16434 alternatives at this point. */
16435 if (address_p)
16437 cp_parser_error (parser, "invalid non-type template argument");
16438 return error_mark_node;
16441 general_expr:
16442 /* If the argument wasn't successfully parsed as a type-id followed
16443 by '>>', the argument can only be a constant expression now.
16444 Otherwise, we try parsing the constant-expression tentatively,
16445 because the argument could really be a type-id. */
16446 if (maybe_type_id)
16447 cp_parser_parse_tentatively (parser);
16449 if (cxx_dialect <= cxx14)
16450 argument = cp_parser_constant_expression (parser);
16451 else
16453 /* With C++17 generalized non-type template arguments we need to handle
16454 lvalue constant expressions, too. */
16455 argument = cp_parser_assignment_expression (parser);
16456 require_potential_constant_expression (argument);
16459 if (!maybe_type_id)
16460 return argument;
16461 if (!cp_parser_next_token_ends_template_argument_p (parser))
16462 cp_parser_error (parser, "expected template-argument");
16463 if (cp_parser_parse_definitely (parser))
16464 return argument;
16465 /* We did our best to parse the argument as a non type-id, but that
16466 was the only alternative that matched (albeit with a '>' after
16467 it). We can assume it's just a typo from the user, and a
16468 diagnostic will then be issued. */
16469 return cp_parser_template_type_arg (parser);
16472 /* Parse an explicit-instantiation.
16474 explicit-instantiation:
16475 template declaration
16477 Although the standard says `declaration', what it really means is:
16479 explicit-instantiation:
16480 template decl-specifier-seq [opt] declarator [opt] ;
16482 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16483 supposed to be allowed. A defect report has been filed about this
16484 issue.
16486 GNU Extension:
16488 explicit-instantiation:
16489 storage-class-specifier template
16490 decl-specifier-seq [opt] declarator [opt] ;
16491 function-specifier template
16492 decl-specifier-seq [opt] declarator [opt] ; */
16494 static void
16495 cp_parser_explicit_instantiation (cp_parser* parser)
16497 int declares_class_or_enum;
16498 cp_decl_specifier_seq decl_specifiers;
16499 tree extension_specifier = NULL_TREE;
16501 timevar_push (TV_TEMPLATE_INST);
16503 /* Look for an (optional) storage-class-specifier or
16504 function-specifier. */
16505 if (cp_parser_allow_gnu_extensions_p (parser))
16507 extension_specifier
16508 = cp_parser_storage_class_specifier_opt (parser);
16509 if (!extension_specifier)
16510 extension_specifier
16511 = cp_parser_function_specifier_opt (parser,
16512 /*decl_specs=*/NULL);
16515 /* Look for the `template' keyword. */
16516 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16517 /* Let the front end know that we are processing an explicit
16518 instantiation. */
16519 begin_explicit_instantiation ();
16520 /* [temp.explicit] says that we are supposed to ignore access
16521 control while processing explicit instantiation directives. */
16522 push_deferring_access_checks (dk_no_check);
16523 /* Parse a decl-specifier-seq. */
16524 cp_parser_decl_specifier_seq (parser,
16525 CP_PARSER_FLAGS_OPTIONAL,
16526 &decl_specifiers,
16527 &declares_class_or_enum);
16528 /* If there was exactly one decl-specifier, and it declared a class,
16529 and there's no declarator, then we have an explicit type
16530 instantiation. */
16531 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16533 tree type;
16535 type = check_tag_decl (&decl_specifiers,
16536 /*explicit_type_instantiation_p=*/true);
16537 /* Turn access control back on for names used during
16538 template instantiation. */
16539 pop_deferring_access_checks ();
16540 if (type)
16541 do_type_instantiation (type, extension_specifier,
16542 /*complain=*/tf_error);
16544 else
16546 cp_declarator *declarator;
16547 tree decl;
16549 /* Parse the declarator. */
16550 declarator
16551 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16552 /*ctor_dtor_or_conv_p=*/NULL,
16553 /*parenthesized_p=*/NULL,
16554 /*member_p=*/false,
16555 /*friend_p=*/false);
16556 if (declares_class_or_enum & 2)
16557 cp_parser_check_for_definition_in_return_type (declarator,
16558 decl_specifiers.type,
16559 decl_specifiers.locations[ds_type_spec]);
16560 if (declarator != cp_error_declarator)
16562 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16563 permerror (decl_specifiers.locations[ds_inline],
16564 "explicit instantiation shall not use"
16565 " %<inline%> specifier");
16566 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16567 permerror (decl_specifiers.locations[ds_constexpr],
16568 "explicit instantiation shall not use"
16569 " %<constexpr%> specifier");
16571 decl = grokdeclarator (declarator, &decl_specifiers,
16572 NORMAL, 0, &decl_specifiers.attributes);
16573 /* Turn access control back on for names used during
16574 template instantiation. */
16575 pop_deferring_access_checks ();
16576 /* Do the explicit instantiation. */
16577 do_decl_instantiation (decl, extension_specifier);
16579 else
16581 pop_deferring_access_checks ();
16582 /* Skip the body of the explicit instantiation. */
16583 cp_parser_skip_to_end_of_statement (parser);
16586 /* We're done with the instantiation. */
16587 end_explicit_instantiation ();
16589 cp_parser_consume_semicolon_at_end_of_statement (parser);
16591 timevar_pop (TV_TEMPLATE_INST);
16594 /* Parse an explicit-specialization.
16596 explicit-specialization:
16597 template < > declaration
16599 Although the standard says `declaration', what it really means is:
16601 explicit-specialization:
16602 template <> decl-specifier [opt] init-declarator [opt] ;
16603 template <> function-definition
16604 template <> explicit-specialization
16605 template <> template-declaration */
16607 static void
16608 cp_parser_explicit_specialization (cp_parser* parser)
16610 bool need_lang_pop;
16611 cp_token *token = cp_lexer_peek_token (parser->lexer);
16613 /* Look for the `template' keyword. */
16614 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16615 /* Look for the `<'. */
16616 cp_parser_require (parser, CPP_LESS, RT_LESS);
16617 /* Look for the `>'. */
16618 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16619 /* We have processed another parameter list. */
16620 ++parser->num_template_parameter_lists;
16621 /* [temp]
16623 A template ... explicit specialization ... shall not have C
16624 linkage. */
16625 if (current_lang_name == lang_name_c)
16627 error_at (token->location, "template specialization with C linkage");
16628 maybe_show_extern_c_location ();
16629 /* Give it C++ linkage to avoid confusing other parts of the
16630 front end. */
16631 push_lang_context (lang_name_cplusplus);
16632 need_lang_pop = true;
16634 else
16635 need_lang_pop = false;
16636 /* Let the front end know that we are beginning a specialization. */
16637 if (!begin_specialization ())
16639 end_specialization ();
16640 return;
16643 /* If the next keyword is `template', we need to figure out whether
16644 or not we're looking a template-declaration. */
16645 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16647 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16648 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16649 cp_parser_template_declaration_after_export (parser,
16650 /*member_p=*/false);
16651 else
16652 cp_parser_explicit_specialization (parser);
16654 else
16655 /* Parse the dependent declaration. */
16656 cp_parser_single_declaration (parser,
16657 /*checks=*/NULL,
16658 /*member_p=*/false,
16659 /*explicit_specialization_p=*/true,
16660 /*friend_p=*/NULL);
16661 /* We're done with the specialization. */
16662 end_specialization ();
16663 /* For the erroneous case of a template with C linkage, we pushed an
16664 implicit C++ linkage scope; exit that scope now. */
16665 if (need_lang_pop)
16666 pop_lang_context ();
16667 /* We're done with this parameter list. */
16668 --parser->num_template_parameter_lists;
16671 /* Parse a type-specifier.
16673 type-specifier:
16674 simple-type-specifier
16675 class-specifier
16676 enum-specifier
16677 elaborated-type-specifier
16678 cv-qualifier
16680 GNU Extension:
16682 type-specifier:
16683 __complex__
16685 Returns a representation of the type-specifier. For a
16686 class-specifier, enum-specifier, or elaborated-type-specifier, a
16687 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16689 The parser flags FLAGS is used to control type-specifier parsing.
16691 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16692 in a decl-specifier-seq.
16694 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16695 class-specifier, enum-specifier, or elaborated-type-specifier, then
16696 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16697 if a type is declared; 2 if it is defined. Otherwise, it is set to
16698 zero.
16700 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16701 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16702 is set to FALSE. */
16704 static tree
16705 cp_parser_type_specifier (cp_parser* parser,
16706 cp_parser_flags flags,
16707 cp_decl_specifier_seq *decl_specs,
16708 bool is_declaration,
16709 int* declares_class_or_enum,
16710 bool* is_cv_qualifier)
16712 tree type_spec = NULL_TREE;
16713 cp_token *token;
16714 enum rid keyword;
16715 cp_decl_spec ds = ds_last;
16717 /* Assume this type-specifier does not declare a new type. */
16718 if (declares_class_or_enum)
16719 *declares_class_or_enum = 0;
16720 /* And that it does not specify a cv-qualifier. */
16721 if (is_cv_qualifier)
16722 *is_cv_qualifier = false;
16723 /* Peek at the next token. */
16724 token = cp_lexer_peek_token (parser->lexer);
16726 /* If we're looking at a keyword, we can use that to guide the
16727 production we choose. */
16728 keyword = token->keyword;
16729 switch (keyword)
16731 case RID_ENUM:
16732 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16733 goto elaborated_type_specifier;
16735 /* Look for the enum-specifier. */
16736 type_spec = cp_parser_enum_specifier (parser);
16737 /* If that worked, we're done. */
16738 if (type_spec)
16740 if (declares_class_or_enum)
16741 *declares_class_or_enum = 2;
16742 if (decl_specs)
16743 cp_parser_set_decl_spec_type (decl_specs,
16744 type_spec,
16745 token,
16746 /*type_definition_p=*/true);
16747 return type_spec;
16749 else
16750 goto elaborated_type_specifier;
16752 /* Any of these indicate either a class-specifier, or an
16753 elaborated-type-specifier. */
16754 case RID_CLASS:
16755 case RID_STRUCT:
16756 case RID_UNION:
16757 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16758 goto elaborated_type_specifier;
16760 /* Parse tentatively so that we can back up if we don't find a
16761 class-specifier. */
16762 cp_parser_parse_tentatively (parser);
16763 /* Look for the class-specifier. */
16764 type_spec = cp_parser_class_specifier (parser);
16765 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16766 /* If that worked, we're done. */
16767 if (cp_parser_parse_definitely (parser))
16769 if (declares_class_or_enum)
16770 *declares_class_or_enum = 2;
16771 if (decl_specs)
16772 cp_parser_set_decl_spec_type (decl_specs,
16773 type_spec,
16774 token,
16775 /*type_definition_p=*/true);
16776 return type_spec;
16779 /* Fall through. */
16780 elaborated_type_specifier:
16781 /* We're declaring (not defining) a class or enum. */
16782 if (declares_class_or_enum)
16783 *declares_class_or_enum = 1;
16785 /* Fall through. */
16786 case RID_TYPENAME:
16787 /* Look for an elaborated-type-specifier. */
16788 type_spec
16789 = (cp_parser_elaborated_type_specifier
16790 (parser,
16791 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16792 is_declaration));
16793 if (decl_specs)
16794 cp_parser_set_decl_spec_type (decl_specs,
16795 type_spec,
16796 token,
16797 /*type_definition_p=*/false);
16798 return type_spec;
16800 case RID_CONST:
16801 ds = ds_const;
16802 if (is_cv_qualifier)
16803 *is_cv_qualifier = true;
16804 break;
16806 case RID_VOLATILE:
16807 ds = ds_volatile;
16808 if (is_cv_qualifier)
16809 *is_cv_qualifier = true;
16810 break;
16812 case RID_RESTRICT:
16813 ds = ds_restrict;
16814 if (is_cv_qualifier)
16815 *is_cv_qualifier = true;
16816 break;
16818 case RID_COMPLEX:
16819 /* The `__complex__' keyword is a GNU extension. */
16820 ds = ds_complex;
16821 break;
16823 default:
16824 break;
16827 /* Handle simple keywords. */
16828 if (ds != ds_last)
16830 if (decl_specs)
16832 set_and_check_decl_spec_loc (decl_specs, ds, token);
16833 decl_specs->any_specifiers_p = true;
16835 return cp_lexer_consume_token (parser->lexer)->u.value;
16838 /* If we do not already have a type-specifier, assume we are looking
16839 at a simple-type-specifier. */
16840 type_spec = cp_parser_simple_type_specifier (parser,
16841 decl_specs,
16842 flags);
16844 /* If we didn't find a type-specifier, and a type-specifier was not
16845 optional in this context, issue an error message. */
16846 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16848 cp_parser_error (parser, "expected type specifier");
16849 return error_mark_node;
16852 return type_spec;
16855 /* Parse a simple-type-specifier.
16857 simple-type-specifier:
16858 :: [opt] nested-name-specifier [opt] type-name
16859 :: [opt] nested-name-specifier template template-id
16860 char
16861 wchar_t
16862 bool
16863 short
16865 long
16866 signed
16867 unsigned
16868 float
16869 double
16870 void
16872 C++11 Extension:
16874 simple-type-specifier:
16875 auto
16876 decltype ( expression )
16877 char16_t
16878 char32_t
16879 __underlying_type ( type-id )
16881 C++17 extension:
16883 nested-name-specifier(opt) template-name
16885 GNU Extension:
16887 simple-type-specifier:
16888 __int128
16889 __typeof__ unary-expression
16890 __typeof__ ( type-id )
16891 __typeof__ ( type-id ) { initializer-list , [opt] }
16893 Concepts Extension:
16895 simple-type-specifier:
16896 constrained-type-specifier
16898 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16899 appropriately updated. */
16901 static tree
16902 cp_parser_simple_type_specifier (cp_parser* parser,
16903 cp_decl_specifier_seq *decl_specs,
16904 cp_parser_flags flags)
16906 tree type = NULL_TREE;
16907 cp_token *token;
16908 int idx;
16910 /* Peek at the next token. */
16911 token = cp_lexer_peek_token (parser->lexer);
16913 /* If we're looking at a keyword, things are easy. */
16914 switch (token->keyword)
16916 case RID_CHAR:
16917 if (decl_specs)
16918 decl_specs->explicit_char_p = true;
16919 type = char_type_node;
16920 break;
16921 case RID_CHAR16:
16922 type = char16_type_node;
16923 break;
16924 case RID_CHAR32:
16925 type = char32_type_node;
16926 break;
16927 case RID_WCHAR:
16928 type = wchar_type_node;
16929 break;
16930 case RID_BOOL:
16931 type = boolean_type_node;
16932 break;
16933 case RID_SHORT:
16934 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16935 type = short_integer_type_node;
16936 break;
16937 case RID_INT:
16938 if (decl_specs)
16939 decl_specs->explicit_int_p = true;
16940 type = integer_type_node;
16941 break;
16942 case RID_INT_N_0:
16943 case RID_INT_N_1:
16944 case RID_INT_N_2:
16945 case RID_INT_N_3:
16946 idx = token->keyword - RID_INT_N_0;
16947 if (! int_n_enabled_p [idx])
16948 break;
16949 if (decl_specs)
16951 decl_specs->explicit_intN_p = true;
16952 decl_specs->int_n_idx = idx;
16954 type = int_n_trees [idx].signed_type;
16955 break;
16956 case RID_LONG:
16957 if (decl_specs)
16958 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16959 type = long_integer_type_node;
16960 break;
16961 case RID_SIGNED:
16962 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16963 type = integer_type_node;
16964 break;
16965 case RID_UNSIGNED:
16966 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16967 type = unsigned_type_node;
16968 break;
16969 case RID_FLOAT:
16970 type = float_type_node;
16971 break;
16972 case RID_DOUBLE:
16973 type = double_type_node;
16974 break;
16975 case RID_VOID:
16976 type = void_type_node;
16977 break;
16979 case RID_AUTO:
16980 maybe_warn_cpp0x (CPP0X_AUTO);
16981 if (parser->auto_is_implicit_function_template_parm_p)
16983 /* The 'auto' might be the placeholder return type for a function decl
16984 with trailing return type. */
16985 bool have_trailing_return_fn_decl = false;
16987 cp_parser_parse_tentatively (parser);
16988 cp_lexer_consume_token (parser->lexer);
16989 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16990 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16991 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16992 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16994 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16996 cp_lexer_consume_token (parser->lexer);
16997 cp_parser_skip_to_closing_parenthesis (parser,
16998 /*recovering*/false,
16999 /*or_comma*/false,
17000 /*consume_paren*/true);
17001 continue;
17004 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17006 have_trailing_return_fn_decl = true;
17007 break;
17010 cp_lexer_consume_token (parser->lexer);
17012 cp_parser_abort_tentative_parse (parser);
17014 if (have_trailing_return_fn_decl)
17016 type = make_auto ();
17017 break;
17020 if (cxx_dialect >= cxx14)
17022 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17023 type = TREE_TYPE (type);
17025 else
17026 type = error_mark_node;
17028 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17030 if (cxx_dialect < cxx14)
17031 error_at (token->location,
17032 "use of %<auto%> in lambda parameter declaration "
17033 "only available with "
17034 "-std=c++14 or -std=gnu++14");
17036 else if (cxx_dialect < cxx14)
17037 error_at (token->location,
17038 "use of %<auto%> in parameter declaration "
17039 "only available with "
17040 "-std=c++14 or -std=gnu++14");
17041 else if (!flag_concepts)
17042 pedwarn (token->location, 0,
17043 "use of %<auto%> in parameter declaration "
17044 "only available with -fconcepts");
17046 else
17047 type = make_auto ();
17048 break;
17050 case RID_DECLTYPE:
17051 /* Since DR 743, decltype can either be a simple-type-specifier by
17052 itself or begin a nested-name-specifier. Parsing it will replace
17053 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17054 handling below decide what to do. */
17055 cp_parser_decltype (parser);
17056 cp_lexer_set_token_position (parser->lexer, token);
17057 break;
17059 case RID_TYPEOF:
17060 /* Consume the `typeof' token. */
17061 cp_lexer_consume_token (parser->lexer);
17062 /* Parse the operand to `typeof'. */
17063 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17064 /* If it is not already a TYPE, take its type. */
17065 if (!TYPE_P (type))
17066 type = finish_typeof (type);
17068 if (decl_specs)
17069 cp_parser_set_decl_spec_type (decl_specs, type,
17070 token,
17071 /*type_definition_p=*/false);
17073 return type;
17075 case RID_UNDERLYING_TYPE:
17076 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17077 if (decl_specs)
17078 cp_parser_set_decl_spec_type (decl_specs, type,
17079 token,
17080 /*type_definition_p=*/false);
17082 return type;
17084 case RID_BASES:
17085 case RID_DIRECT_BASES:
17086 type = cp_parser_trait_expr (parser, token->keyword);
17087 if (decl_specs)
17088 cp_parser_set_decl_spec_type (decl_specs, type,
17089 token,
17090 /*type_definition_p=*/false);
17091 return type;
17092 default:
17093 break;
17096 /* If token is an already-parsed decltype not followed by ::,
17097 it's a simple-type-specifier. */
17098 if (token->type == CPP_DECLTYPE
17099 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17101 type = saved_checks_value (token->u.tree_check_value);
17102 if (decl_specs)
17104 cp_parser_set_decl_spec_type (decl_specs, type,
17105 token,
17106 /*type_definition_p=*/false);
17107 /* Remember that we are handling a decltype in order to
17108 implement the resolution of DR 1510 when the argument
17109 isn't instantiation dependent. */
17110 decl_specs->decltype_p = true;
17112 cp_lexer_consume_token (parser->lexer);
17113 return type;
17116 /* If the type-specifier was for a built-in type, we're done. */
17117 if (type)
17119 /* Record the type. */
17120 if (decl_specs
17121 && (token->keyword != RID_SIGNED
17122 && token->keyword != RID_UNSIGNED
17123 && token->keyword != RID_SHORT
17124 && token->keyword != RID_LONG))
17125 cp_parser_set_decl_spec_type (decl_specs,
17126 type,
17127 token,
17128 /*type_definition_p=*/false);
17129 if (decl_specs)
17130 decl_specs->any_specifiers_p = true;
17132 /* Consume the token. */
17133 cp_lexer_consume_token (parser->lexer);
17135 if (type == error_mark_node)
17136 return error_mark_node;
17138 /* There is no valid C++ program where a non-template type is
17139 followed by a "<". That usually indicates that the user thought
17140 that the type was a template. */
17141 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17142 token->location);
17144 return TYPE_NAME (type);
17147 /* The type-specifier must be a user-defined type. */
17148 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17150 bool qualified_p;
17151 bool global_p;
17153 /* Don't gobble tokens or issue error messages if this is an
17154 optional type-specifier. */
17155 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17156 cp_parser_parse_tentatively (parser);
17158 token = cp_lexer_peek_token (parser->lexer);
17160 /* Look for the optional `::' operator. */
17161 global_p
17162 = (cp_parser_global_scope_opt (parser,
17163 /*current_scope_valid_p=*/false)
17164 != NULL_TREE);
17165 /* Look for the nested-name specifier. */
17166 qualified_p
17167 = (cp_parser_nested_name_specifier_opt (parser,
17168 /*typename_keyword_p=*/false,
17169 /*check_dependency_p=*/true,
17170 /*type_p=*/false,
17171 /*is_declaration=*/false)
17172 != NULL_TREE);
17173 /* If we have seen a nested-name-specifier, and the next token
17174 is `template', then we are using the template-id production. */
17175 if (parser->scope
17176 && cp_parser_optional_template_keyword (parser))
17178 /* Look for the template-id. */
17179 type = cp_parser_template_id (parser,
17180 /*template_keyword_p=*/true,
17181 /*check_dependency_p=*/true,
17182 none_type,
17183 /*is_declaration=*/false);
17184 /* If the template-id did not name a type, we are out of
17185 luck. */
17186 if (TREE_CODE (type) != TYPE_DECL)
17188 cp_parser_error (parser, "expected template-id for type");
17189 type = NULL_TREE;
17192 /* Otherwise, look for a type-name. */
17193 else
17194 type = cp_parser_type_name (parser);
17195 /* Keep track of all name-lookups performed in class scopes. */
17196 if (type
17197 && !global_p
17198 && !qualified_p
17199 && TREE_CODE (type) == TYPE_DECL
17200 && identifier_p (DECL_NAME (type)))
17201 maybe_note_name_used_in_class (DECL_NAME (type), type);
17202 /* If it didn't work out, we don't have a TYPE. */
17203 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17204 && !cp_parser_parse_definitely (parser))
17205 type = NULL_TREE;
17206 if (!type && cxx_dialect >= cxx17)
17208 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17209 cp_parser_parse_tentatively (parser);
17211 cp_parser_global_scope_opt (parser,
17212 /*current_scope_valid_p=*/false);
17213 cp_parser_nested_name_specifier_opt (parser,
17214 /*typename_keyword_p=*/false,
17215 /*check_dependency_p=*/true,
17216 /*type_p=*/false,
17217 /*is_declaration=*/false);
17218 tree name = cp_parser_identifier (parser);
17219 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17220 && parser->scope != error_mark_node)
17222 tree tmpl = cp_parser_lookup_name (parser, name,
17223 none_type,
17224 /*is_template=*/false,
17225 /*is_namespace=*/false,
17226 /*check_dependency=*/true,
17227 /*ambiguous_decls=*/NULL,
17228 token->location);
17229 if (tmpl && tmpl != error_mark_node
17230 && (DECL_CLASS_TEMPLATE_P (tmpl)
17231 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17232 type = make_template_placeholder (tmpl);
17233 else
17235 type = error_mark_node;
17236 if (!cp_parser_simulate_error (parser))
17237 cp_parser_name_lookup_error (parser, name, tmpl,
17238 NLE_TYPE, token->location);
17241 else
17242 type = error_mark_node;
17244 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17245 && !cp_parser_parse_definitely (parser))
17246 type = NULL_TREE;
17248 if (type && decl_specs)
17249 cp_parser_set_decl_spec_type (decl_specs, type,
17250 token,
17251 /*type_definition_p=*/false);
17254 /* If we didn't get a type-name, issue an error message. */
17255 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17257 cp_parser_error (parser, "expected type-name");
17258 return error_mark_node;
17261 if (type && type != error_mark_node)
17263 /* See if TYPE is an Objective-C type, and if so, parse and
17264 accept any protocol references following it. Do this before
17265 the cp_parser_check_for_invalid_template_id() call, because
17266 Objective-C types can be followed by '<...>' which would
17267 enclose protocol names rather than template arguments, and so
17268 everything is fine. */
17269 if (c_dialect_objc () && !parser->scope
17270 && (objc_is_id (type) || objc_is_class_name (type)))
17272 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17273 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17275 /* Clobber the "unqualified" type previously entered into
17276 DECL_SPECS with the new, improved protocol-qualified version. */
17277 if (decl_specs)
17278 decl_specs->type = qual_type;
17280 return qual_type;
17283 /* There is no valid C++ program where a non-template type is
17284 followed by a "<". That usually indicates that the user
17285 thought that the type was a template. */
17286 cp_parser_check_for_invalid_template_id (parser, type,
17287 none_type,
17288 token->location);
17291 return type;
17294 /* Parse a type-name.
17296 type-name:
17297 class-name
17298 enum-name
17299 typedef-name
17300 simple-template-id [in c++0x]
17302 enum-name:
17303 identifier
17305 typedef-name:
17306 identifier
17308 Concepts:
17310 type-name:
17311 concept-name
17312 partial-concept-id
17314 concept-name:
17315 identifier
17317 Returns a TYPE_DECL for the type. */
17319 static tree
17320 cp_parser_type_name (cp_parser* parser)
17322 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17325 /* See above. */
17326 static tree
17327 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17329 tree type_decl;
17331 /* We can't know yet whether it is a class-name or not. */
17332 cp_parser_parse_tentatively (parser);
17333 /* Try a class-name. */
17334 type_decl = cp_parser_class_name (parser,
17335 typename_keyword_p,
17336 /*template_keyword_p=*/false,
17337 none_type,
17338 /*check_dependency_p=*/true,
17339 /*class_head_p=*/false,
17340 /*is_declaration=*/false);
17341 /* If it's not a class-name, keep looking. */
17342 if (!cp_parser_parse_definitely (parser))
17344 if (cxx_dialect < cxx11)
17345 /* It must be a typedef-name or an enum-name. */
17346 return cp_parser_nonclass_name (parser);
17348 cp_parser_parse_tentatively (parser);
17349 /* It is either a simple-template-id representing an
17350 instantiation of an alias template... */
17351 type_decl = cp_parser_template_id (parser,
17352 /*template_keyword_p=*/false,
17353 /*check_dependency_p=*/true,
17354 none_type,
17355 /*is_declaration=*/false);
17356 /* Note that this must be an instantiation of an alias template
17357 because [temp.names]/6 says:
17359 A template-id that names an alias template specialization
17360 is a type-name.
17362 Whereas [temp.names]/7 says:
17364 A simple-template-id that names a class template
17365 specialization is a class-name.
17367 With concepts, this could also be a partial-concept-id that
17368 declares a non-type template parameter. */
17369 if (type_decl != NULL_TREE
17370 && TREE_CODE (type_decl) == TYPE_DECL
17371 && TYPE_DECL_ALIAS_P (type_decl))
17372 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17373 else if (is_constrained_parameter (type_decl))
17374 /* Don't do anything. */ ;
17375 else
17376 cp_parser_simulate_error (parser);
17378 if (!cp_parser_parse_definitely (parser))
17379 /* ... Or a typedef-name or an enum-name. */
17380 return cp_parser_nonclass_name (parser);
17383 return type_decl;
17386 /* Check if DECL and ARGS can form a constrained-type-specifier.
17387 If ARGS is non-null, we try to form a concept check of the
17388 form DECL<?, ARGS> where ? is a wildcard that matches any
17389 kind of template argument. If ARGS is NULL, then we try to
17390 form a concept check of the form DECL<?>. */
17392 static tree
17393 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17394 tree decl, tree args)
17396 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17398 /* If we a constrained-type-specifier cannot be deduced. */
17399 if (parser->prevent_constrained_type_specifiers)
17400 return NULL_TREE;
17402 /* A constrained type specifier can only be found in an
17403 overload set or as a reference to a template declaration.
17405 FIXME: This might be masking a bug. It's possible that
17406 that the deduction below is causing template specializations
17407 to be formed with the wildcard as an argument. */
17408 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17409 return NULL_TREE;
17411 /* Try to build a call expression that evaluates the
17412 concept. This can fail if the overload set refers
17413 only to non-templates. */
17414 tree placeholder = build_nt (WILDCARD_DECL);
17415 tree check = build_concept_check (decl, placeholder, args);
17416 if (check == error_mark_node)
17417 return NULL_TREE;
17419 /* Deduce the checked constraint and the prototype parameter.
17421 FIXME: In certain cases, failure to deduce should be a
17422 diagnosable error. */
17423 tree conc;
17424 tree proto;
17425 if (!deduce_constrained_parameter (check, conc, proto))
17426 return NULL_TREE;
17428 /* In template parameter scope, this results in a constrained
17429 parameter. Return a descriptor of that parm. */
17430 if (processing_template_parmlist)
17431 return build_constrained_parameter (conc, proto, args);
17433 /* In a parameter-declaration-clause, constrained-type
17434 specifiers result in invented template parameters. */
17435 if (parser->auto_is_implicit_function_template_parm_p)
17437 tree x = build_constrained_parameter (conc, proto, args);
17438 return synthesize_implicit_template_parm (parser, x);
17440 else
17442 /* Otherwise, we're in a context where the constrained
17443 type name is deduced and the constraint applies
17444 after deduction. */
17445 return make_constrained_auto (conc, args);
17448 return NULL_TREE;
17451 /* If DECL refers to a concept, return a TYPE_DECL representing
17452 the result of using the constrained type specifier in the
17453 current context. DECL refers to a concept if
17455 - it is an overload set containing a function concept taking a single
17456 type argument, or
17458 - it is a variable concept taking a single type argument. */
17460 static tree
17461 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17463 if (flag_concepts
17464 && (TREE_CODE (decl) == OVERLOAD
17465 || BASELINK_P (decl)
17466 || variable_concept_p (decl)))
17467 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17468 else
17469 return NULL_TREE;
17472 /* Check if DECL and ARGS form a partial-concept-id. If so,
17473 assign ID to the resulting constrained placeholder.
17475 Returns true if the partial-concept-id designates a placeholder
17476 and false otherwise. Note that *id is set to NULL_TREE in
17477 this case. */
17479 static tree
17480 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17482 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17485 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17486 or a concept-name.
17488 enum-name:
17489 identifier
17491 typedef-name:
17492 identifier
17494 concept-name:
17495 identifier
17497 Returns a TYPE_DECL for the type. */
17499 static tree
17500 cp_parser_nonclass_name (cp_parser* parser)
17502 tree type_decl;
17503 tree identifier;
17505 cp_token *token = cp_lexer_peek_token (parser->lexer);
17506 identifier = cp_parser_identifier (parser);
17507 if (identifier == error_mark_node)
17508 return error_mark_node;
17510 /* Look up the type-name. */
17511 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17513 type_decl = strip_using_decl (type_decl);
17515 /* If we found an overload set, then it may refer to a concept-name. */
17516 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17517 type_decl = decl;
17519 if (TREE_CODE (type_decl) != TYPE_DECL
17520 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17522 /* See if this is an Objective-C type. */
17523 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17524 tree type = objc_get_protocol_qualified_type (identifier, protos);
17525 if (type)
17526 type_decl = TYPE_NAME (type);
17529 /* Issue an error if we did not find a type-name. */
17530 if (TREE_CODE (type_decl) != TYPE_DECL
17531 /* In Objective-C, we have the complication that class names are
17532 normally type names and start declarations (eg, the
17533 "NSObject" in "NSObject *object;"), but can be used in an
17534 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17535 is an expression. So, a classname followed by a dot is not a
17536 valid type-name. */
17537 || (objc_is_class_name (TREE_TYPE (type_decl))
17538 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17540 if (!cp_parser_simulate_error (parser))
17541 cp_parser_name_lookup_error (parser, identifier, type_decl,
17542 NLE_TYPE, token->location);
17543 return error_mark_node;
17545 /* Remember that the name was used in the definition of the
17546 current class so that we can check later to see if the
17547 meaning would have been different after the class was
17548 entirely defined. */
17549 else if (type_decl != error_mark_node
17550 && !parser->scope)
17551 maybe_note_name_used_in_class (identifier, type_decl);
17553 return type_decl;
17556 /* Parse an elaborated-type-specifier. Note that the grammar given
17557 here incorporates the resolution to DR68.
17559 elaborated-type-specifier:
17560 class-key :: [opt] nested-name-specifier [opt] identifier
17561 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17562 enum-key :: [opt] nested-name-specifier [opt] identifier
17563 typename :: [opt] nested-name-specifier identifier
17564 typename :: [opt] nested-name-specifier template [opt]
17565 template-id
17567 GNU extension:
17569 elaborated-type-specifier:
17570 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17571 class-key attributes :: [opt] nested-name-specifier [opt]
17572 template [opt] template-id
17573 enum attributes :: [opt] nested-name-specifier [opt] identifier
17575 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17576 declared `friend'. If IS_DECLARATION is TRUE, then this
17577 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17578 something is being declared.
17580 Returns the TYPE specified. */
17582 static tree
17583 cp_parser_elaborated_type_specifier (cp_parser* parser,
17584 bool is_friend,
17585 bool is_declaration)
17587 enum tag_types tag_type;
17588 tree identifier;
17589 tree type = NULL_TREE;
17590 tree attributes = NULL_TREE;
17591 tree globalscope;
17592 cp_token *token = NULL;
17594 /* See if we're looking at the `enum' keyword. */
17595 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17597 /* Consume the `enum' token. */
17598 cp_lexer_consume_token (parser->lexer);
17599 /* Remember that it's an enumeration type. */
17600 tag_type = enum_type;
17601 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17602 enums) is used here. */
17603 cp_token *token = cp_lexer_peek_token (parser->lexer);
17604 if (cp_parser_is_keyword (token, RID_CLASS)
17605 || cp_parser_is_keyword (token, RID_STRUCT))
17607 gcc_rich_location richloc (token->location);
17608 richloc.add_range (input_location, false);
17609 richloc.add_fixit_remove ();
17610 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17611 "a scoped enum must not use the %qD keyword",
17612 token->u.value);
17613 /* Consume the `struct' or `class' and parse it anyway. */
17614 cp_lexer_consume_token (parser->lexer);
17616 /* Parse the attributes. */
17617 attributes = cp_parser_attributes_opt (parser);
17619 /* Or, it might be `typename'. */
17620 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17621 RID_TYPENAME))
17623 /* Consume the `typename' token. */
17624 cp_lexer_consume_token (parser->lexer);
17625 /* Remember that it's a `typename' type. */
17626 tag_type = typename_type;
17628 /* Otherwise it must be a class-key. */
17629 else
17631 tag_type = cp_parser_class_key (parser);
17632 if (tag_type == none_type)
17633 return error_mark_node;
17634 /* Parse the attributes. */
17635 attributes = cp_parser_attributes_opt (parser);
17638 /* Look for the `::' operator. */
17639 globalscope = cp_parser_global_scope_opt (parser,
17640 /*current_scope_valid_p=*/false);
17641 /* Look for the nested-name-specifier. */
17642 tree nested_name_specifier;
17643 if (tag_type == typename_type && !globalscope)
17645 nested_name_specifier
17646 = cp_parser_nested_name_specifier (parser,
17647 /*typename_keyword_p=*/true,
17648 /*check_dependency_p=*/true,
17649 /*type_p=*/true,
17650 is_declaration);
17651 if (!nested_name_specifier)
17652 return error_mark_node;
17654 else
17655 /* Even though `typename' is not present, the proposed resolution
17656 to Core Issue 180 says that in `class A<T>::B', `B' should be
17657 considered a type-name, even if `A<T>' is dependent. */
17658 nested_name_specifier
17659 = cp_parser_nested_name_specifier_opt (parser,
17660 /*typename_keyword_p=*/true,
17661 /*check_dependency_p=*/true,
17662 /*type_p=*/true,
17663 is_declaration);
17664 /* For everything but enumeration types, consider a template-id.
17665 For an enumeration type, consider only a plain identifier. */
17666 if (tag_type != enum_type)
17668 bool template_p = false;
17669 tree decl;
17671 /* Allow the `template' keyword. */
17672 template_p = cp_parser_optional_template_keyword (parser);
17673 /* If we didn't see `template', we don't know if there's a
17674 template-id or not. */
17675 if (!template_p)
17676 cp_parser_parse_tentatively (parser);
17677 /* Parse the template-id. */
17678 token = cp_lexer_peek_token (parser->lexer);
17679 decl = cp_parser_template_id (parser, template_p,
17680 /*check_dependency_p=*/true,
17681 tag_type,
17682 is_declaration);
17683 /* If we didn't find a template-id, look for an ordinary
17684 identifier. */
17685 if (!template_p && !cp_parser_parse_definitely (parser))
17687 /* We can get here when cp_parser_template_id, called by
17688 cp_parser_class_name with tag_type == none_type, succeeds
17689 and caches a BASELINK. Then, when called again here,
17690 instead of failing and returning an error_mark_node
17691 returns it (see template/typename17.C in C++11).
17692 ??? Could we diagnose this earlier? */
17693 else if (tag_type == typename_type && BASELINK_P (decl))
17695 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17696 type = error_mark_node;
17698 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17699 in effect, then we must assume that, upon instantiation, the
17700 template will correspond to a class. */
17701 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17702 && tag_type == typename_type)
17703 type = make_typename_type (parser->scope, decl,
17704 typename_type,
17705 /*complain=*/tf_error);
17706 /* If the `typename' keyword is in effect and DECL is not a type
17707 decl, then type is non existent. */
17708 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17710 else if (TREE_CODE (decl) == TYPE_DECL)
17712 type = check_elaborated_type_specifier (tag_type, decl,
17713 /*allow_template_p=*/true);
17715 /* If the next token is a semicolon, this must be a specialization,
17716 instantiation, or friend declaration. Check the scope while we
17717 still know whether or not we had a nested-name-specifier. */
17718 if (type != error_mark_node
17719 && !nested_name_specifier && !is_friend
17720 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17721 check_unqualified_spec_or_inst (type, token->location);
17723 else if (decl == error_mark_node)
17724 type = error_mark_node;
17727 if (!type)
17729 token = cp_lexer_peek_token (parser->lexer);
17730 identifier = cp_parser_identifier (parser);
17732 if (identifier == error_mark_node)
17734 parser->scope = NULL_TREE;
17735 return error_mark_node;
17738 /* For a `typename', we needn't call xref_tag. */
17739 if (tag_type == typename_type
17740 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17741 return cp_parser_make_typename_type (parser, identifier,
17742 token->location);
17744 /* Template parameter lists apply only if we are not within a
17745 function parameter list. */
17746 bool template_parm_lists_apply
17747 = parser->num_template_parameter_lists;
17748 if (template_parm_lists_apply)
17749 for (cp_binding_level *s = current_binding_level;
17750 s && s->kind != sk_template_parms;
17751 s = s->level_chain)
17752 if (s->kind == sk_function_parms)
17753 template_parm_lists_apply = false;
17755 /* Look up a qualified name in the usual way. */
17756 if (parser->scope)
17758 tree decl;
17759 tree ambiguous_decls;
17761 decl = cp_parser_lookup_name (parser, identifier,
17762 tag_type,
17763 /*is_template=*/false,
17764 /*is_namespace=*/false,
17765 /*check_dependency=*/true,
17766 &ambiguous_decls,
17767 token->location);
17769 /* If the lookup was ambiguous, an error will already have been
17770 issued. */
17771 if (ambiguous_decls)
17772 return error_mark_node;
17774 /* If we are parsing friend declaration, DECL may be a
17775 TEMPLATE_DECL tree node here. However, we need to check
17776 whether this TEMPLATE_DECL results in valid code. Consider
17777 the following example:
17779 namespace N {
17780 template <class T> class C {};
17782 class X {
17783 template <class T> friend class N::C; // #1, valid code
17785 template <class T> class Y {
17786 friend class N::C; // #2, invalid code
17789 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17790 name lookup of `N::C'. We see that friend declaration must
17791 be template for the code to be valid. Note that
17792 processing_template_decl does not work here since it is
17793 always 1 for the above two cases. */
17795 decl = (cp_parser_maybe_treat_template_as_class
17796 (decl, /*tag_name_p=*/is_friend
17797 && template_parm_lists_apply));
17799 if (TREE_CODE (decl) != TYPE_DECL)
17801 cp_parser_diagnose_invalid_type_name (parser,
17802 identifier,
17803 token->location);
17804 return error_mark_node;
17807 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17809 bool allow_template = (template_parm_lists_apply
17810 || DECL_SELF_REFERENCE_P (decl));
17811 type = check_elaborated_type_specifier (tag_type, decl,
17812 allow_template);
17814 if (type == error_mark_node)
17815 return error_mark_node;
17818 /* Forward declarations of nested types, such as
17820 class C1::C2;
17821 class C1::C2::C3;
17823 are invalid unless all components preceding the final '::'
17824 are complete. If all enclosing types are complete, these
17825 declarations become merely pointless.
17827 Invalid forward declarations of nested types are errors
17828 caught elsewhere in parsing. Those that are pointless arrive
17829 here. */
17831 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17832 && !is_friend && !processing_explicit_instantiation)
17833 warning (0, "declaration %qD does not declare anything", decl);
17835 type = TREE_TYPE (decl);
17837 else
17839 /* An elaborated-type-specifier sometimes introduces a new type and
17840 sometimes names an existing type. Normally, the rule is that it
17841 introduces a new type only if there is not an existing type of
17842 the same name already in scope. For example, given:
17844 struct S {};
17845 void f() { struct S s; }
17847 the `struct S' in the body of `f' is the same `struct S' as in
17848 the global scope; the existing definition is used. However, if
17849 there were no global declaration, this would introduce a new
17850 local class named `S'.
17852 An exception to this rule applies to the following code:
17854 namespace N { struct S; }
17856 Here, the elaborated-type-specifier names a new type
17857 unconditionally; even if there is already an `S' in the
17858 containing scope this declaration names a new type.
17859 This exception only applies if the elaborated-type-specifier
17860 forms the complete declaration:
17862 [class.name]
17864 A declaration consisting solely of `class-key identifier ;' is
17865 either a redeclaration of the name in the current scope or a
17866 forward declaration of the identifier as a class name. It
17867 introduces the name into the current scope.
17869 We are in this situation precisely when the next token is a `;'.
17871 An exception to the exception is that a `friend' declaration does
17872 *not* name a new type; i.e., given:
17874 struct S { friend struct T; };
17876 `T' is not a new type in the scope of `S'.
17878 Also, `new struct S' or `sizeof (struct S)' never results in the
17879 definition of a new type; a new type can only be declared in a
17880 declaration context. */
17882 tag_scope ts;
17883 bool template_p;
17885 if (is_friend)
17886 /* Friends have special name lookup rules. */
17887 ts = ts_within_enclosing_non_class;
17888 else if (is_declaration
17889 && cp_lexer_next_token_is (parser->lexer,
17890 CPP_SEMICOLON))
17891 /* This is a `class-key identifier ;' */
17892 ts = ts_current;
17893 else
17894 ts = ts_global;
17896 template_p =
17897 (template_parm_lists_apply
17898 && (cp_parser_next_token_starts_class_definition_p (parser)
17899 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17900 /* An unqualified name was used to reference this type, so
17901 there were no qualifying templates. */
17902 if (template_parm_lists_apply
17903 && !cp_parser_check_template_parameters (parser,
17904 /*num_templates=*/0,
17905 /*template_id*/false,
17906 token->location,
17907 /*declarator=*/NULL))
17908 return error_mark_node;
17909 type = xref_tag (tag_type, identifier, ts, template_p);
17913 if (type == error_mark_node)
17914 return error_mark_node;
17916 /* Allow attributes on forward declarations of classes. */
17917 if (attributes)
17919 if (TREE_CODE (type) == TYPENAME_TYPE)
17920 warning (OPT_Wattributes,
17921 "attributes ignored on uninstantiated type");
17922 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17923 && ! processing_explicit_instantiation)
17924 warning (OPT_Wattributes,
17925 "attributes ignored on template instantiation");
17926 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17927 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17928 else
17929 warning (OPT_Wattributes,
17930 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17933 if (tag_type != enum_type)
17935 /* Indicate whether this class was declared as a `class' or as a
17936 `struct'. */
17937 if (CLASS_TYPE_P (type))
17938 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17939 cp_parser_check_class_key (tag_type, type);
17942 /* A "<" cannot follow an elaborated type specifier. If that
17943 happens, the user was probably trying to form a template-id. */
17944 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17945 token->location);
17947 return type;
17950 /* Parse an enum-specifier.
17952 enum-specifier:
17953 enum-head { enumerator-list [opt] }
17954 enum-head { enumerator-list , } [C++0x]
17956 enum-head:
17957 enum-key identifier [opt] enum-base [opt]
17958 enum-key nested-name-specifier identifier enum-base [opt]
17960 enum-key:
17961 enum
17962 enum class [C++0x]
17963 enum struct [C++0x]
17965 enum-base: [C++0x]
17966 : type-specifier-seq
17968 opaque-enum-specifier:
17969 enum-key identifier enum-base [opt] ;
17971 GNU Extensions:
17972 enum-key attributes[opt] identifier [opt] enum-base [opt]
17973 { enumerator-list [opt] }attributes[opt]
17974 enum-key attributes[opt] identifier [opt] enum-base [opt]
17975 { enumerator-list, }attributes[opt] [C++0x]
17977 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17978 if the token stream isn't an enum-specifier after all. */
17980 static tree
17981 cp_parser_enum_specifier (cp_parser* parser)
17983 tree identifier;
17984 tree type = NULL_TREE;
17985 tree prev_scope;
17986 tree nested_name_specifier = NULL_TREE;
17987 tree attributes;
17988 bool scoped_enum_p = false;
17989 bool has_underlying_type = false;
17990 bool nested_being_defined = false;
17991 bool new_value_list = false;
17992 bool is_new_type = false;
17993 bool is_unnamed = false;
17994 tree underlying_type = NULL_TREE;
17995 cp_token *type_start_token = NULL;
17996 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17998 parser->colon_corrects_to_scope_p = false;
18000 /* Parse tentatively so that we can back up if we don't find a
18001 enum-specifier. */
18002 cp_parser_parse_tentatively (parser);
18004 /* Caller guarantees that the current token is 'enum', an identifier
18005 possibly follows, and the token after that is an opening brace.
18006 If we don't have an identifier, fabricate an anonymous name for
18007 the enumeration being defined. */
18008 cp_lexer_consume_token (parser->lexer);
18010 /* Parse the "class" or "struct", which indicates a scoped
18011 enumeration type in C++0x. */
18012 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18013 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18015 if (cxx_dialect < cxx11)
18016 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18018 /* Consume the `struct' or `class' token. */
18019 cp_lexer_consume_token (parser->lexer);
18021 scoped_enum_p = true;
18024 attributes = cp_parser_attributes_opt (parser);
18026 /* Clear the qualification. */
18027 parser->scope = NULL_TREE;
18028 parser->qualifying_scope = NULL_TREE;
18029 parser->object_scope = NULL_TREE;
18031 /* Figure out in what scope the declaration is being placed. */
18032 prev_scope = current_scope ();
18034 type_start_token = cp_lexer_peek_token (parser->lexer);
18036 push_deferring_access_checks (dk_no_check);
18037 nested_name_specifier
18038 = cp_parser_nested_name_specifier_opt (parser,
18039 /*typename_keyword_p=*/true,
18040 /*check_dependency_p=*/false,
18041 /*type_p=*/false,
18042 /*is_declaration=*/false);
18044 if (nested_name_specifier)
18046 tree name;
18048 identifier = cp_parser_identifier (parser);
18049 name = cp_parser_lookup_name (parser, identifier,
18050 enum_type,
18051 /*is_template=*/false,
18052 /*is_namespace=*/false,
18053 /*check_dependency=*/true,
18054 /*ambiguous_decls=*/NULL,
18055 input_location);
18056 if (name && name != error_mark_node)
18058 type = TREE_TYPE (name);
18059 if (TREE_CODE (type) == TYPENAME_TYPE)
18061 /* Are template enums allowed in ISO? */
18062 if (template_parm_scope_p ())
18063 pedwarn (type_start_token->location, OPT_Wpedantic,
18064 "%qD is an enumeration template", name);
18065 /* ignore a typename reference, for it will be solved by name
18066 in start_enum. */
18067 type = NULL_TREE;
18070 else if (nested_name_specifier == error_mark_node)
18071 /* We already issued an error. */;
18072 else
18074 error_at (type_start_token->location,
18075 "%qD does not name an enumeration in %qT",
18076 identifier, nested_name_specifier);
18077 nested_name_specifier = error_mark_node;
18080 else
18082 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18083 identifier = cp_parser_identifier (parser);
18084 else
18086 identifier = make_anon_name ();
18087 is_unnamed = true;
18088 if (scoped_enum_p)
18089 error_at (type_start_token->location,
18090 "unnamed scoped enum is not allowed");
18093 pop_deferring_access_checks ();
18095 /* Check for the `:' that denotes a specified underlying type in C++0x.
18096 Note that a ':' could also indicate a bitfield width, however. */
18097 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18099 cp_decl_specifier_seq type_specifiers;
18101 /* Consume the `:'. */
18102 cp_lexer_consume_token (parser->lexer);
18104 /* Parse the type-specifier-seq. */
18105 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18106 /*is_trailing_return=*/false,
18107 &type_specifiers);
18109 /* At this point this is surely not elaborated type specifier. */
18110 if (!cp_parser_parse_definitely (parser))
18111 return NULL_TREE;
18113 if (cxx_dialect < cxx11)
18114 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18116 has_underlying_type = true;
18118 /* If that didn't work, stop. */
18119 if (type_specifiers.type != error_mark_node)
18121 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18122 /*initialized=*/0, NULL);
18123 if (underlying_type == error_mark_node
18124 || check_for_bare_parameter_packs (underlying_type))
18125 underlying_type = NULL_TREE;
18129 /* Look for the `{' but don't consume it yet. */
18130 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18132 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18134 cp_parser_error (parser, "expected %<{%>");
18135 if (has_underlying_type)
18137 type = NULL_TREE;
18138 goto out;
18141 /* An opaque-enum-specifier must have a ';' here. */
18142 if ((scoped_enum_p || underlying_type)
18143 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18145 cp_parser_error (parser, "expected %<;%> or %<{%>");
18146 if (has_underlying_type)
18148 type = NULL_TREE;
18149 goto out;
18154 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18155 return NULL_TREE;
18157 if (nested_name_specifier)
18159 if (CLASS_TYPE_P (nested_name_specifier))
18161 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18162 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18163 push_scope (nested_name_specifier);
18165 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18167 push_nested_namespace (nested_name_specifier);
18171 /* Issue an error message if type-definitions are forbidden here. */
18172 if (!cp_parser_check_type_definition (parser))
18173 type = error_mark_node;
18174 else
18175 /* Create the new type. We do this before consuming the opening
18176 brace so the enum will be recorded as being on the line of its
18177 tag (or the 'enum' keyword, if there is no tag). */
18178 type = start_enum (identifier, type, underlying_type,
18179 attributes, scoped_enum_p, &is_new_type);
18181 /* If the next token is not '{' it is an opaque-enum-specifier or an
18182 elaborated-type-specifier. */
18183 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18185 timevar_push (TV_PARSE_ENUM);
18186 if (nested_name_specifier
18187 && nested_name_specifier != error_mark_node)
18189 /* The following catches invalid code such as:
18190 enum class S<int>::E { A, B, C }; */
18191 if (!processing_specialization
18192 && CLASS_TYPE_P (nested_name_specifier)
18193 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18194 error_at (type_start_token->location, "cannot add an enumerator "
18195 "list to a template instantiation");
18197 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18199 error_at (type_start_token->location,
18200 "%<%T::%E%> has not been declared",
18201 TYPE_CONTEXT (nested_name_specifier),
18202 nested_name_specifier);
18203 type = error_mark_node;
18205 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18206 && !CLASS_TYPE_P (nested_name_specifier))
18208 error_at (type_start_token->location, "nested name specifier "
18209 "%qT for enum declaration does not name a class "
18210 "or namespace", nested_name_specifier);
18211 type = error_mark_node;
18213 /* If that scope does not contain the scope in which the
18214 class was originally declared, the program is invalid. */
18215 else if (prev_scope && !is_ancestor (prev_scope,
18216 nested_name_specifier))
18218 if (at_namespace_scope_p ())
18219 error_at (type_start_token->location,
18220 "declaration of %qD in namespace %qD which does not "
18221 "enclose %qD",
18222 type, prev_scope, nested_name_specifier);
18223 else
18224 error_at (type_start_token->location,
18225 "declaration of %qD in %qD which does not "
18226 "enclose %qD",
18227 type, prev_scope, nested_name_specifier);
18228 type = error_mark_node;
18230 /* If that scope is the scope where the declaration is being placed
18231 the program is invalid. */
18232 else if (CLASS_TYPE_P (nested_name_specifier)
18233 && CLASS_TYPE_P (prev_scope)
18234 && same_type_p (nested_name_specifier, prev_scope))
18236 permerror (type_start_token->location,
18237 "extra qualification not allowed");
18238 nested_name_specifier = NULL_TREE;
18242 if (scoped_enum_p)
18243 begin_scope (sk_scoped_enum, type);
18245 /* Consume the opening brace. */
18246 matching_braces braces;
18247 braces.consume_open (parser);
18249 if (type == error_mark_node)
18250 ; /* Nothing to add */
18251 else if (OPAQUE_ENUM_P (type)
18252 || (cxx_dialect > cxx98 && processing_specialization))
18254 new_value_list = true;
18255 SET_OPAQUE_ENUM_P (type, false);
18256 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18258 else
18260 error_at (type_start_token->location,
18261 "multiple definition of %q#T", type);
18262 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18263 "previous definition here");
18264 type = error_mark_node;
18267 if (type == error_mark_node)
18268 cp_parser_skip_to_end_of_block_or_statement (parser);
18269 /* If the next token is not '}', then there are some enumerators. */
18270 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18272 if (is_unnamed && !scoped_enum_p)
18273 pedwarn (type_start_token->location, OPT_Wpedantic,
18274 "ISO C++ forbids empty unnamed enum");
18276 else
18277 cp_parser_enumerator_list (parser, type);
18279 /* Consume the final '}'. */
18280 braces.require_close (parser);
18282 if (scoped_enum_p)
18283 finish_scope ();
18284 timevar_pop (TV_PARSE_ENUM);
18286 else
18288 /* If a ';' follows, then it is an opaque-enum-specifier
18289 and additional restrictions apply. */
18290 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18292 if (is_unnamed)
18293 error_at (type_start_token->location,
18294 "opaque-enum-specifier without name");
18295 else if (nested_name_specifier)
18296 error_at (type_start_token->location,
18297 "opaque-enum-specifier must use a simple identifier");
18301 /* Look for trailing attributes to apply to this enumeration, and
18302 apply them if appropriate. */
18303 if (cp_parser_allow_gnu_extensions_p (parser))
18305 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18306 cplus_decl_attributes (&type,
18307 trailing_attr,
18308 (int) ATTR_FLAG_TYPE_IN_PLACE);
18311 /* Finish up the enumeration. */
18312 if (type != error_mark_node)
18314 if (new_value_list)
18315 finish_enum_value_list (type);
18316 if (is_new_type)
18317 finish_enum (type);
18320 if (nested_name_specifier)
18322 if (CLASS_TYPE_P (nested_name_specifier))
18324 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18325 pop_scope (nested_name_specifier);
18327 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18329 pop_nested_namespace (nested_name_specifier);
18332 out:
18333 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18334 return type;
18337 /* Parse an enumerator-list. The enumerators all have the indicated
18338 TYPE.
18340 enumerator-list:
18341 enumerator-definition
18342 enumerator-list , enumerator-definition */
18344 static void
18345 cp_parser_enumerator_list (cp_parser* parser, tree type)
18347 while (true)
18349 /* Parse an enumerator-definition. */
18350 cp_parser_enumerator_definition (parser, type);
18352 /* If the next token is not a ',', we've reached the end of
18353 the list. */
18354 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18355 break;
18356 /* Otherwise, consume the `,' and keep going. */
18357 cp_lexer_consume_token (parser->lexer);
18358 /* If the next token is a `}', there is a trailing comma. */
18359 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18361 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18362 pedwarn (input_location, OPT_Wpedantic,
18363 "comma at end of enumerator list");
18364 break;
18369 /* Parse an enumerator-definition. The enumerator has the indicated
18370 TYPE.
18372 enumerator-definition:
18373 enumerator
18374 enumerator = constant-expression
18376 enumerator:
18377 identifier
18379 GNU Extensions:
18381 enumerator-definition:
18382 enumerator attributes [opt]
18383 enumerator attributes [opt] = constant-expression */
18385 static void
18386 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18388 tree identifier;
18389 tree value;
18390 location_t loc;
18392 /* Save the input location because we are interested in the location
18393 of the identifier and not the location of the explicit value. */
18394 loc = cp_lexer_peek_token (parser->lexer)->location;
18396 /* Look for the identifier. */
18397 identifier = cp_parser_identifier (parser);
18398 if (identifier == error_mark_node)
18399 return;
18401 /* Parse any specified attributes. */
18402 tree attrs = cp_parser_attributes_opt (parser);
18404 /* If the next token is an '=', then there is an explicit value. */
18405 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18407 /* Consume the `=' token. */
18408 cp_lexer_consume_token (parser->lexer);
18409 /* Parse the value. */
18410 value = cp_parser_constant_expression (parser);
18412 else
18413 value = NULL_TREE;
18415 /* If we are processing a template, make sure the initializer of the
18416 enumerator doesn't contain any bare template parameter pack. */
18417 if (check_for_bare_parameter_packs (value))
18418 value = error_mark_node;
18420 /* Create the enumerator. */
18421 build_enumerator (identifier, value, type, attrs, loc);
18424 /* Parse a namespace-name.
18426 namespace-name:
18427 original-namespace-name
18428 namespace-alias
18430 Returns the NAMESPACE_DECL for the namespace. */
18432 static tree
18433 cp_parser_namespace_name (cp_parser* parser)
18435 tree identifier;
18436 tree namespace_decl;
18438 cp_token *token = cp_lexer_peek_token (parser->lexer);
18440 /* Get the name of the namespace. */
18441 identifier = cp_parser_identifier (parser);
18442 if (identifier == error_mark_node)
18443 return error_mark_node;
18445 /* Look up the identifier in the currently active scope. Look only
18446 for namespaces, due to:
18448 [basic.lookup.udir]
18450 When looking up a namespace-name in a using-directive or alias
18451 definition, only namespace names are considered.
18453 And:
18455 [basic.lookup.qual]
18457 During the lookup of a name preceding the :: scope resolution
18458 operator, object, function, and enumerator names are ignored.
18460 (Note that cp_parser_qualifying_entity only calls this
18461 function if the token after the name is the scope resolution
18462 operator.) */
18463 namespace_decl = cp_parser_lookup_name (parser, identifier,
18464 none_type,
18465 /*is_template=*/false,
18466 /*is_namespace=*/true,
18467 /*check_dependency=*/true,
18468 /*ambiguous_decls=*/NULL,
18469 token->location);
18470 /* If it's not a namespace, issue an error. */
18471 if (namespace_decl == error_mark_node
18472 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18474 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18476 error_at (token->location, "%qD is not a namespace-name", identifier);
18477 if (namespace_decl == error_mark_node
18478 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18479 suggest_alternative_in_explicit_scope (token->location, identifier,
18480 parser->scope);
18482 cp_parser_error (parser, "expected namespace-name");
18483 namespace_decl = error_mark_node;
18486 return namespace_decl;
18489 /* Parse a namespace-definition.
18491 namespace-definition:
18492 named-namespace-definition
18493 unnamed-namespace-definition
18495 named-namespace-definition:
18496 original-namespace-definition
18497 extension-namespace-definition
18499 original-namespace-definition:
18500 namespace identifier { namespace-body }
18502 extension-namespace-definition:
18503 namespace original-namespace-name { namespace-body }
18505 unnamed-namespace-definition:
18506 namespace { namespace-body } */
18508 static void
18509 cp_parser_namespace_definition (cp_parser* parser)
18511 tree identifier;
18512 int nested_definition_count = 0;
18514 cp_ensure_no_omp_declare_simd (parser);
18515 cp_ensure_no_oacc_routine (parser);
18517 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18519 if (is_inline)
18521 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18522 cp_lexer_consume_token (parser->lexer);
18525 /* Look for the `namespace' keyword. */
18526 cp_token* token
18527 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18529 /* Parse any specified attributes before the identifier. */
18530 tree attribs = cp_parser_attributes_opt (parser);
18532 for (;;)
18534 identifier = NULL_TREE;
18536 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18538 identifier = cp_parser_identifier (parser);
18540 /* Parse any attributes specified after the identifier. */
18541 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18544 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18545 break;
18547 if (!nested_definition_count && cxx_dialect < cxx17)
18548 pedwarn (input_location, OPT_Wpedantic,
18549 "nested namespace definitions only available with "
18550 "-std=c++17 or -std=gnu++17");
18552 /* Nested namespace names can create new namespaces (unlike
18553 other qualified-ids). */
18554 if (int count = identifier ? push_namespace (identifier) : 0)
18555 nested_definition_count += count;
18556 else
18557 cp_parser_error (parser, "nested namespace name required");
18558 cp_lexer_consume_token (parser->lexer);
18561 if (nested_definition_count && !identifier)
18562 cp_parser_error (parser, "namespace name required");
18564 if (nested_definition_count && attribs)
18565 error_at (token->location,
18566 "a nested namespace definition cannot have attributes");
18567 if (nested_definition_count && is_inline)
18568 error_at (token->location,
18569 "a nested namespace definition cannot be inline");
18571 /* Start the namespace. */
18572 nested_definition_count += push_namespace (identifier, is_inline);
18574 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18576 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18578 /* Look for the `{' to validate starting the namespace. */
18579 matching_braces braces;
18580 if (braces.require_open (parser))
18582 /* Parse the body of the namespace. */
18583 cp_parser_namespace_body (parser);
18585 /* Look for the final `}'. */
18586 braces.require_close (parser);
18589 if (has_visibility)
18590 pop_visibility (1);
18592 /* Pop the nested namespace definitions. */
18593 while (nested_definition_count--)
18594 pop_namespace ();
18597 /* Parse a namespace-body.
18599 namespace-body:
18600 declaration-seq [opt] */
18602 static void
18603 cp_parser_namespace_body (cp_parser* parser)
18605 cp_parser_declaration_seq_opt (parser);
18608 /* Parse a namespace-alias-definition.
18610 namespace-alias-definition:
18611 namespace identifier = qualified-namespace-specifier ; */
18613 static void
18614 cp_parser_namespace_alias_definition (cp_parser* parser)
18616 tree identifier;
18617 tree namespace_specifier;
18619 cp_token *token = cp_lexer_peek_token (parser->lexer);
18621 /* Look for the `namespace' keyword. */
18622 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18623 /* Look for the identifier. */
18624 identifier = cp_parser_identifier (parser);
18625 if (identifier == error_mark_node)
18626 return;
18627 /* Look for the `=' token. */
18628 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18629 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18631 error_at (token->location, "%<namespace%> definition is not allowed here");
18632 /* Skip the definition. */
18633 cp_lexer_consume_token (parser->lexer);
18634 if (cp_parser_skip_to_closing_brace (parser))
18635 cp_lexer_consume_token (parser->lexer);
18636 return;
18638 cp_parser_require (parser, CPP_EQ, RT_EQ);
18639 /* Look for the qualified-namespace-specifier. */
18640 namespace_specifier
18641 = cp_parser_qualified_namespace_specifier (parser);
18642 /* Look for the `;' token. */
18643 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18645 /* Register the alias in the symbol table. */
18646 do_namespace_alias (identifier, namespace_specifier);
18649 /* Parse a qualified-namespace-specifier.
18651 qualified-namespace-specifier:
18652 :: [opt] nested-name-specifier [opt] namespace-name
18654 Returns a NAMESPACE_DECL corresponding to the specified
18655 namespace. */
18657 static tree
18658 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18660 /* Look for the optional `::'. */
18661 cp_parser_global_scope_opt (parser,
18662 /*current_scope_valid_p=*/false);
18664 /* Look for the optional nested-name-specifier. */
18665 cp_parser_nested_name_specifier_opt (parser,
18666 /*typename_keyword_p=*/false,
18667 /*check_dependency_p=*/true,
18668 /*type_p=*/false,
18669 /*is_declaration=*/true);
18671 return cp_parser_namespace_name (parser);
18674 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18675 access declaration.
18677 using-declaration:
18678 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18679 using :: unqualified-id ;
18681 access-declaration:
18682 qualified-id ;
18686 static bool
18687 cp_parser_using_declaration (cp_parser* parser,
18688 bool access_declaration_p)
18690 cp_token *token;
18691 bool typename_p = false;
18692 bool global_scope_p;
18693 tree decl;
18694 tree identifier;
18695 tree qscope;
18696 int oldcount = errorcount;
18697 cp_token *diag_token = NULL;
18699 if (access_declaration_p)
18701 diag_token = cp_lexer_peek_token (parser->lexer);
18702 cp_parser_parse_tentatively (parser);
18704 else
18706 /* Look for the `using' keyword. */
18707 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18709 again:
18710 /* Peek at the next token. */
18711 token = cp_lexer_peek_token (parser->lexer);
18712 /* See if it's `typename'. */
18713 if (token->keyword == RID_TYPENAME)
18715 /* Remember that we've seen it. */
18716 typename_p = true;
18717 /* Consume the `typename' token. */
18718 cp_lexer_consume_token (parser->lexer);
18722 /* Look for the optional global scope qualification. */
18723 global_scope_p
18724 = (cp_parser_global_scope_opt (parser,
18725 /*current_scope_valid_p=*/false)
18726 != NULL_TREE);
18728 /* If we saw `typename', or didn't see `::', then there must be a
18729 nested-name-specifier present. */
18730 if (typename_p || !global_scope_p)
18732 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18733 /*check_dependency_p=*/true,
18734 /*type_p=*/false,
18735 /*is_declaration=*/true);
18736 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18738 cp_parser_skip_to_end_of_block_or_statement (parser);
18739 return false;
18742 /* Otherwise, we could be in either of the two productions. In that
18743 case, treat the nested-name-specifier as optional. */
18744 else
18745 qscope = cp_parser_nested_name_specifier_opt (parser,
18746 /*typename_keyword_p=*/false,
18747 /*check_dependency_p=*/true,
18748 /*type_p=*/false,
18749 /*is_declaration=*/true);
18750 if (!qscope)
18751 qscope = global_namespace;
18752 else if (UNSCOPED_ENUM_P (qscope))
18753 qscope = CP_TYPE_CONTEXT (qscope);
18755 if (access_declaration_p && cp_parser_error_occurred (parser))
18756 /* Something has already gone wrong; there's no need to parse
18757 further. Since an error has occurred, the return value of
18758 cp_parser_parse_definitely will be false, as required. */
18759 return cp_parser_parse_definitely (parser);
18761 token = cp_lexer_peek_token (parser->lexer);
18762 /* Parse the unqualified-id. */
18763 identifier = cp_parser_unqualified_id (parser,
18764 /*template_keyword_p=*/false,
18765 /*check_dependency_p=*/true,
18766 /*declarator_p=*/true,
18767 /*optional_p=*/false);
18769 if (access_declaration_p)
18771 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18772 cp_parser_simulate_error (parser);
18773 if (!cp_parser_parse_definitely (parser))
18774 return false;
18776 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18778 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18779 if (cxx_dialect < cxx17
18780 && !in_system_header_at (ell->location))
18781 pedwarn (ell->location, 0,
18782 "pack expansion in using-declaration only available "
18783 "with -std=c++17 or -std=gnu++17");
18784 qscope = make_pack_expansion (qscope);
18787 /* The function we call to handle a using-declaration is different
18788 depending on what scope we are in. */
18789 if (qscope == error_mark_node || identifier == error_mark_node)
18791 else if (!identifier_p (identifier)
18792 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18793 /* [namespace.udecl]
18795 A using declaration shall not name a template-id. */
18796 error_at (token->location,
18797 "a template-id may not appear in a using-declaration");
18798 else
18800 if (at_class_scope_p ())
18802 /* Create the USING_DECL. */
18803 decl = do_class_using_decl (qscope, identifier);
18805 if (decl && typename_p)
18806 USING_DECL_TYPENAME_P (decl) = 1;
18808 if (check_for_bare_parameter_packs (decl))
18810 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18811 return false;
18813 else
18814 /* Add it to the list of members in this class. */
18815 finish_member_declaration (decl);
18817 else
18819 decl = cp_parser_lookup_name_simple (parser,
18820 identifier,
18821 token->location);
18822 if (decl == error_mark_node)
18823 cp_parser_name_lookup_error (parser, identifier,
18824 decl, NLE_NULL,
18825 token->location);
18826 else if (check_for_bare_parameter_packs (decl))
18828 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18829 return false;
18831 else if (!at_namespace_scope_p ())
18832 finish_local_using_decl (decl, qscope, identifier);
18833 else
18834 finish_namespace_using_decl (decl, qscope, identifier);
18838 if (!access_declaration_p
18839 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18841 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18842 if (cxx_dialect < cxx17)
18843 pedwarn (comma->location, 0,
18844 "comma-separated list in using-declaration only available "
18845 "with -std=c++17 or -std=gnu++17");
18846 goto again;
18849 /* Look for the final `;'. */
18850 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18852 if (access_declaration_p && errorcount == oldcount)
18853 warning_at (diag_token->location, OPT_Wdeprecated,
18854 "access declarations are deprecated "
18855 "in favour of using-declarations; "
18856 "suggestion: add the %<using%> keyword");
18858 return true;
18861 /* Parse an alias-declaration.
18863 alias-declaration:
18864 using identifier attribute-specifier-seq [opt] = type-id */
18866 static tree
18867 cp_parser_alias_declaration (cp_parser* parser)
18869 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18870 location_t id_location;
18871 cp_declarator *declarator;
18872 cp_decl_specifier_seq decl_specs;
18873 bool member_p;
18874 const char *saved_message = NULL;
18876 /* Look for the `using' keyword. */
18877 cp_token *using_token
18878 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18879 if (using_token == NULL)
18880 return error_mark_node;
18882 id_location = cp_lexer_peek_token (parser->lexer)->location;
18883 id = cp_parser_identifier (parser);
18884 if (id == error_mark_node)
18885 return error_mark_node;
18887 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18888 attributes = cp_parser_attributes_opt (parser);
18889 if (attributes == error_mark_node)
18890 return error_mark_node;
18892 cp_parser_require (parser, CPP_EQ, RT_EQ);
18894 if (cp_parser_error_occurred (parser))
18895 return error_mark_node;
18897 cp_parser_commit_to_tentative_parse (parser);
18899 /* Now we are going to parse the type-id of the declaration. */
18902 [dcl.type]/3 says:
18904 "A type-specifier-seq shall not define a class or enumeration
18905 unless it appears in the type-id of an alias-declaration (7.1.3) that
18906 is not the declaration of a template-declaration."
18908 In other words, if we currently are in an alias template, the
18909 type-id should not define a type.
18911 So let's set parser->type_definition_forbidden_message in that
18912 case; cp_parser_check_type_definition (called by
18913 cp_parser_class_specifier) will then emit an error if a type is
18914 defined in the type-id. */
18915 if (parser->num_template_parameter_lists)
18917 saved_message = parser->type_definition_forbidden_message;
18918 parser->type_definition_forbidden_message =
18919 G_("types may not be defined in alias template declarations");
18922 type = cp_parser_type_id (parser);
18924 /* Restore the error message if need be. */
18925 if (parser->num_template_parameter_lists)
18926 parser->type_definition_forbidden_message = saved_message;
18928 if (type == error_mark_node
18929 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18931 cp_parser_skip_to_end_of_block_or_statement (parser);
18932 return error_mark_node;
18935 /* A typedef-name can also be introduced by an alias-declaration. The
18936 identifier following the using keyword becomes a typedef-name. It has
18937 the same semantics as if it were introduced by the typedef
18938 specifier. In particular, it does not define a new type and it shall
18939 not appear in the type-id. */
18941 clear_decl_specs (&decl_specs);
18942 decl_specs.type = type;
18943 if (attributes != NULL_TREE)
18945 decl_specs.attributes = attributes;
18946 set_and_check_decl_spec_loc (&decl_specs,
18947 ds_attribute,
18948 attrs_token);
18950 set_and_check_decl_spec_loc (&decl_specs,
18951 ds_typedef,
18952 using_token);
18953 set_and_check_decl_spec_loc (&decl_specs,
18954 ds_alias,
18955 using_token);
18957 if (parser->num_template_parameter_lists
18958 && !cp_parser_check_template_parameters (parser,
18959 /*num_templates=*/0,
18960 /*template_id*/false,
18961 id_location,
18962 /*declarator=*/NULL))
18963 return error_mark_node;
18965 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18966 declarator->id_loc = id_location;
18968 member_p = at_class_scope_p ();
18969 if (member_p)
18970 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18971 NULL_TREE, attributes);
18972 else
18973 decl = start_decl (declarator, &decl_specs, 0,
18974 attributes, NULL_TREE, &pushed_scope);
18975 if (decl == error_mark_node)
18976 return decl;
18978 // Attach constraints to the alias declaration.
18979 if (flag_concepts && current_template_parms)
18981 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18982 tree constr = build_constraints (reqs, NULL_TREE);
18983 set_constraints (decl, constr);
18986 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18988 if (pushed_scope)
18989 pop_scope (pushed_scope);
18991 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18992 added into the symbol table; otherwise, return the TYPE_DECL. */
18993 if (DECL_LANG_SPECIFIC (decl)
18994 && DECL_TEMPLATE_INFO (decl)
18995 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18997 decl = DECL_TI_TEMPLATE (decl);
18998 if (member_p)
18999 check_member_template (decl);
19002 return decl;
19005 /* Parse a using-directive.
19007 using-directive:
19008 using namespace :: [opt] nested-name-specifier [opt]
19009 namespace-name ; */
19011 static void
19012 cp_parser_using_directive (cp_parser* parser)
19014 tree namespace_decl;
19015 tree attribs;
19017 /* Look for the `using' keyword. */
19018 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19019 /* And the `namespace' keyword. */
19020 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19021 /* Look for the optional `::' operator. */
19022 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19023 /* And the optional nested-name-specifier. */
19024 cp_parser_nested_name_specifier_opt (parser,
19025 /*typename_keyword_p=*/false,
19026 /*check_dependency_p=*/true,
19027 /*type_p=*/false,
19028 /*is_declaration=*/true);
19029 /* Get the namespace being used. */
19030 namespace_decl = cp_parser_namespace_name (parser);
19031 /* And any specified attributes. */
19032 attribs = cp_parser_attributes_opt (parser);
19034 /* Update the symbol table. */
19035 if (namespace_bindings_p ())
19036 finish_namespace_using_directive (namespace_decl, attribs);
19037 else
19038 finish_local_using_directive (namespace_decl, attribs);
19040 /* Look for the final `;'. */
19041 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19044 /* Parse an asm-definition.
19046 asm-definition:
19047 asm ( string-literal ) ;
19049 GNU Extension:
19051 asm-definition:
19052 asm volatile [opt] ( string-literal ) ;
19053 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19054 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19055 : asm-operand-list [opt] ) ;
19056 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19057 : asm-operand-list [opt]
19058 : asm-clobber-list [opt] ) ;
19059 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19060 : asm-clobber-list [opt]
19061 : asm-goto-list ) ; */
19063 static void
19064 cp_parser_asm_definition (cp_parser* parser)
19066 tree string;
19067 tree outputs = NULL_TREE;
19068 tree inputs = NULL_TREE;
19069 tree clobbers = NULL_TREE;
19070 tree labels = NULL_TREE;
19071 tree asm_stmt;
19072 bool volatile_p = false;
19073 bool extended_p = false;
19074 bool invalid_inputs_p = false;
19075 bool invalid_outputs_p = false;
19076 bool goto_p = false;
19077 required_token missing = RT_NONE;
19079 /* Look for the `asm' keyword. */
19080 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19082 if (parser->in_function_body
19083 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19085 error ("%<asm%> in %<constexpr%> function");
19086 cp_function_chain->invalid_constexpr = true;
19089 /* See if the next token is `volatile'. */
19090 if (cp_parser_allow_gnu_extensions_p (parser)
19091 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19093 /* Remember that we saw the `volatile' keyword. */
19094 volatile_p = true;
19095 /* Consume the token. */
19096 cp_lexer_consume_token (parser->lexer);
19098 if (cp_parser_allow_gnu_extensions_p (parser)
19099 && parser->in_function_body
19100 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19102 /* Remember that we saw the `goto' keyword. */
19103 goto_p = true;
19104 /* Consume the token. */
19105 cp_lexer_consume_token (parser->lexer);
19107 /* Look for the opening `('. */
19108 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19109 return;
19110 /* Look for the string. */
19111 string = cp_parser_string_literal (parser, false, false);
19112 if (string == error_mark_node)
19114 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19115 /*consume_paren=*/true);
19116 return;
19119 /* If we're allowing GNU extensions, check for the extended assembly
19120 syntax. Unfortunately, the `:' tokens need not be separated by
19121 a space in C, and so, for compatibility, we tolerate that here
19122 too. Doing that means that we have to treat the `::' operator as
19123 two `:' tokens. */
19124 if (cp_parser_allow_gnu_extensions_p (parser)
19125 && parser->in_function_body
19126 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19127 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19129 bool inputs_p = false;
19130 bool clobbers_p = false;
19131 bool labels_p = false;
19133 /* The extended syntax was used. */
19134 extended_p = true;
19136 /* Look for outputs. */
19137 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19139 /* Consume the `:'. */
19140 cp_lexer_consume_token (parser->lexer);
19141 /* Parse the output-operands. */
19142 if (cp_lexer_next_token_is_not (parser->lexer,
19143 CPP_COLON)
19144 && cp_lexer_next_token_is_not (parser->lexer,
19145 CPP_SCOPE)
19146 && cp_lexer_next_token_is_not (parser->lexer,
19147 CPP_CLOSE_PAREN)
19148 && !goto_p)
19150 outputs = cp_parser_asm_operand_list (parser);
19151 if (outputs == error_mark_node)
19152 invalid_outputs_p = true;
19155 /* If the next token is `::', there are no outputs, and the
19156 next token is the beginning of the inputs. */
19157 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19158 /* The inputs are coming next. */
19159 inputs_p = true;
19161 /* Look for inputs. */
19162 if (inputs_p
19163 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19165 /* Consume the `:' or `::'. */
19166 cp_lexer_consume_token (parser->lexer);
19167 /* Parse the output-operands. */
19168 if (cp_lexer_next_token_is_not (parser->lexer,
19169 CPP_COLON)
19170 && cp_lexer_next_token_is_not (parser->lexer,
19171 CPP_SCOPE)
19172 && cp_lexer_next_token_is_not (parser->lexer,
19173 CPP_CLOSE_PAREN))
19175 inputs = cp_parser_asm_operand_list (parser);
19176 if (inputs == error_mark_node)
19177 invalid_inputs_p = true;
19180 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19181 /* The clobbers are coming next. */
19182 clobbers_p = true;
19184 /* Look for clobbers. */
19185 if (clobbers_p
19186 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19188 clobbers_p = true;
19189 /* Consume the `:' or `::'. */
19190 cp_lexer_consume_token (parser->lexer);
19191 /* Parse the clobbers. */
19192 if (cp_lexer_next_token_is_not (parser->lexer,
19193 CPP_COLON)
19194 && cp_lexer_next_token_is_not (parser->lexer,
19195 CPP_CLOSE_PAREN))
19196 clobbers = cp_parser_asm_clobber_list (parser);
19198 else if (goto_p
19199 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19200 /* The labels are coming next. */
19201 labels_p = true;
19203 /* Look for labels. */
19204 if (labels_p
19205 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19207 labels_p = true;
19208 /* Consume the `:' or `::'. */
19209 cp_lexer_consume_token (parser->lexer);
19210 /* Parse the labels. */
19211 labels = cp_parser_asm_label_list (parser);
19214 if (goto_p && !labels_p)
19215 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19217 else if (goto_p)
19218 missing = RT_COLON_SCOPE;
19220 /* Look for the closing `)'. */
19221 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19222 missing ? missing : RT_CLOSE_PAREN))
19223 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19224 /*consume_paren=*/true);
19225 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19227 if (!invalid_inputs_p && !invalid_outputs_p)
19229 /* Create the ASM_EXPR. */
19230 if (parser->in_function_body)
19232 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19233 inputs, clobbers, labels);
19234 /* If the extended syntax was not used, mark the ASM_EXPR. */
19235 if (!extended_p)
19237 tree temp = asm_stmt;
19238 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19239 temp = TREE_OPERAND (temp, 0);
19241 ASM_INPUT_P (temp) = 1;
19244 else
19245 symtab->finalize_toplevel_asm (string);
19249 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19250 type that comes from the decl-specifier-seq. */
19252 static tree
19253 strip_declarator_types (tree type, cp_declarator *declarator)
19255 for (cp_declarator *d = declarator; d;)
19256 switch (d->kind)
19258 case cdk_id:
19259 case cdk_decomp:
19260 case cdk_error:
19261 d = NULL;
19262 break;
19264 default:
19265 if (TYPE_PTRMEMFUNC_P (type))
19266 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19267 type = TREE_TYPE (type);
19268 d = d->declarator;
19269 break;
19272 return type;
19275 /* Declarators [gram.dcl.decl] */
19277 /* Parse an init-declarator.
19279 init-declarator:
19280 declarator initializer [opt]
19282 GNU Extension:
19284 init-declarator:
19285 declarator asm-specification [opt] attributes [opt] initializer [opt]
19287 function-definition:
19288 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19289 function-body
19290 decl-specifier-seq [opt] declarator function-try-block
19292 GNU Extension:
19294 function-definition:
19295 __extension__ function-definition
19297 TM Extension:
19299 function-definition:
19300 decl-specifier-seq [opt] declarator function-transaction-block
19302 The DECL_SPECIFIERS apply to this declarator. Returns a
19303 representation of the entity declared. If MEMBER_P is TRUE, then
19304 this declarator appears in a class scope. The new DECL created by
19305 this declarator is returned.
19307 The CHECKS are access checks that should be performed once we know
19308 what entity is being declared (and, therefore, what classes have
19309 befriended it).
19311 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19312 for a function-definition here as well. If the declarator is a
19313 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19314 be TRUE upon return. By that point, the function-definition will
19315 have been completely parsed.
19317 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19318 is FALSE.
19320 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19321 parsed declaration if it is an uninitialized single declarator not followed
19322 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19323 if present, will not be consumed. If returned, this declarator will be
19324 created with SD_INITIALIZED but will not call cp_finish_decl.
19326 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19327 and there is an initializer, the pointed location_t is set to the
19328 location of the '=' or `(', or '{' in C++11 token introducing the
19329 initializer. */
19331 static tree
19332 cp_parser_init_declarator (cp_parser* parser,
19333 cp_decl_specifier_seq *decl_specifiers,
19334 vec<deferred_access_check, va_gc> *checks,
19335 bool function_definition_allowed_p,
19336 bool member_p,
19337 int declares_class_or_enum,
19338 bool* function_definition_p,
19339 tree* maybe_range_for_decl,
19340 location_t* init_loc,
19341 tree* auto_result)
19343 cp_token *token = NULL, *asm_spec_start_token = NULL,
19344 *attributes_start_token = NULL;
19345 cp_declarator *declarator;
19346 tree prefix_attributes;
19347 tree attributes = NULL;
19348 tree asm_specification;
19349 tree initializer;
19350 tree decl = NULL_TREE;
19351 tree scope;
19352 int is_initialized;
19353 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19354 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19355 "(...)". */
19356 enum cpp_ttype initialization_kind;
19357 bool is_direct_init = false;
19358 bool is_non_constant_init;
19359 int ctor_dtor_or_conv_p;
19360 bool friend_p = cp_parser_friend_p (decl_specifiers);
19361 tree pushed_scope = NULL_TREE;
19362 bool range_for_decl_p = false;
19363 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19364 location_t tmp_init_loc = UNKNOWN_LOCATION;
19366 /* Gather the attributes that were provided with the
19367 decl-specifiers. */
19368 prefix_attributes = decl_specifiers->attributes;
19370 /* Assume that this is not the declarator for a function
19371 definition. */
19372 if (function_definition_p)
19373 *function_definition_p = false;
19375 /* Default arguments are only permitted for function parameters. */
19376 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19377 parser->default_arg_ok_p = false;
19379 /* Defer access checks while parsing the declarator; we cannot know
19380 what names are accessible until we know what is being
19381 declared. */
19382 resume_deferring_access_checks ();
19384 token = cp_lexer_peek_token (parser->lexer);
19386 /* Parse the declarator. */
19387 declarator
19388 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19389 &ctor_dtor_or_conv_p,
19390 /*parenthesized_p=*/NULL,
19391 member_p, friend_p);
19392 /* Gather up the deferred checks. */
19393 stop_deferring_access_checks ();
19395 parser->default_arg_ok_p = saved_default_arg_ok_p;
19397 /* If the DECLARATOR was erroneous, there's no need to go
19398 further. */
19399 if (declarator == cp_error_declarator)
19400 return error_mark_node;
19402 /* Check that the number of template-parameter-lists is OK. */
19403 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19404 token->location))
19405 return error_mark_node;
19407 if (declares_class_or_enum & 2)
19408 cp_parser_check_for_definition_in_return_type (declarator,
19409 decl_specifiers->type,
19410 decl_specifiers->locations[ds_type_spec]);
19412 /* Figure out what scope the entity declared by the DECLARATOR is
19413 located in. `grokdeclarator' sometimes changes the scope, so
19414 we compute it now. */
19415 scope = get_scope_of_declarator (declarator);
19417 /* Perform any lookups in the declared type which were thought to be
19418 dependent, but are not in the scope of the declarator. */
19419 decl_specifiers->type
19420 = maybe_update_decl_type (decl_specifiers->type, scope);
19422 /* If we're allowing GNU extensions, look for an
19423 asm-specification. */
19424 if (cp_parser_allow_gnu_extensions_p (parser))
19426 /* Look for an asm-specification. */
19427 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19428 asm_specification = cp_parser_asm_specification_opt (parser);
19430 else
19431 asm_specification = NULL_TREE;
19433 /* Look for attributes. */
19434 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19435 attributes = cp_parser_attributes_opt (parser);
19437 /* Peek at the next token. */
19438 token = cp_lexer_peek_token (parser->lexer);
19440 bool bogus_implicit_tmpl = false;
19442 if (function_declarator_p (declarator))
19444 /* Handle C++17 deduction guides. */
19445 if (!decl_specifiers->type
19446 && ctor_dtor_or_conv_p <= 0
19447 && cxx_dialect >= cxx17)
19449 cp_declarator *id = get_id_declarator (declarator);
19450 tree name = id->u.id.unqualified_name;
19451 parser->scope = id->u.id.qualifying_scope;
19452 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19453 if (tmpl
19454 && (DECL_CLASS_TEMPLATE_P (tmpl)
19455 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19457 id->u.id.unqualified_name = dguide_name (tmpl);
19458 id->u.id.sfk = sfk_deduction_guide;
19459 ctor_dtor_or_conv_p = 1;
19463 /* Check to see if the token indicates the start of a
19464 function-definition. */
19465 if (cp_parser_token_starts_function_definition_p (token))
19467 if (!function_definition_allowed_p)
19469 /* If a function-definition should not appear here, issue an
19470 error message. */
19471 cp_parser_error (parser,
19472 "a function-definition is not allowed here");
19473 return error_mark_node;
19476 location_t func_brace_location
19477 = cp_lexer_peek_token (parser->lexer)->location;
19479 /* Neither attributes nor an asm-specification are allowed
19480 on a function-definition. */
19481 if (asm_specification)
19482 error_at (asm_spec_start_token->location,
19483 "an asm-specification is not allowed "
19484 "on a function-definition");
19485 if (attributes)
19486 error_at (attributes_start_token->location,
19487 "attributes are not allowed "
19488 "on a function-definition");
19489 /* This is a function-definition. */
19490 *function_definition_p = true;
19492 /* Parse the function definition. */
19493 if (member_p)
19494 decl = cp_parser_save_member_function_body (parser,
19495 decl_specifiers,
19496 declarator,
19497 prefix_attributes);
19498 else
19499 decl =
19500 (cp_parser_function_definition_from_specifiers_and_declarator
19501 (parser, decl_specifiers, prefix_attributes, declarator));
19503 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19505 /* This is where the prologue starts... */
19506 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19507 = func_brace_location;
19510 return decl;
19513 else if (parser->fully_implicit_function_template_p)
19515 /* A non-template declaration involving a function parameter list
19516 containing an implicit template parameter will be made into a
19517 template. If the resulting declaration is not going to be an
19518 actual function then finish the template scope here to prevent it.
19519 An error message will be issued once we have a decl to talk about.
19521 FIXME probably we should do type deduction rather than create an
19522 implicit template, but the standard currently doesn't allow it. */
19523 bogus_implicit_tmpl = true;
19524 finish_fully_implicit_template (parser, NULL_TREE);
19527 /* [dcl.dcl]
19529 Only in function declarations for constructors, destructors, type
19530 conversions, and deduction guides can the decl-specifier-seq be omitted.
19532 We explicitly postpone this check past the point where we handle
19533 function-definitions because we tolerate function-definitions
19534 that are missing their return types in some modes. */
19535 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19537 cp_parser_error (parser,
19538 "expected constructor, destructor, or type conversion");
19539 return error_mark_node;
19542 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19543 if (token->type == CPP_EQ
19544 || token->type == CPP_OPEN_PAREN
19545 || token->type == CPP_OPEN_BRACE)
19547 is_initialized = SD_INITIALIZED;
19548 initialization_kind = token->type;
19549 if (maybe_range_for_decl)
19550 *maybe_range_for_decl = error_mark_node;
19551 tmp_init_loc = token->location;
19552 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19553 *init_loc = tmp_init_loc;
19555 if (token->type == CPP_EQ
19556 && function_declarator_p (declarator))
19558 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19559 if (t2->keyword == RID_DEFAULT)
19560 is_initialized = SD_DEFAULTED;
19561 else if (t2->keyword == RID_DELETE)
19562 is_initialized = SD_DELETED;
19565 else
19567 /* If the init-declarator isn't initialized and isn't followed by a
19568 `,' or `;', it's not a valid init-declarator. */
19569 if (token->type != CPP_COMMA
19570 && token->type != CPP_SEMICOLON)
19572 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19573 range_for_decl_p = true;
19574 else
19576 if (!maybe_range_for_decl)
19577 cp_parser_error (parser, "expected initializer");
19578 return error_mark_node;
19581 is_initialized = SD_UNINITIALIZED;
19582 initialization_kind = CPP_EOF;
19585 /* Because start_decl has side-effects, we should only call it if we
19586 know we're going ahead. By this point, we know that we cannot
19587 possibly be looking at any other construct. */
19588 cp_parser_commit_to_tentative_parse (parser);
19590 /* Enter the newly declared entry in the symbol table. If we're
19591 processing a declaration in a class-specifier, we wait until
19592 after processing the initializer. */
19593 if (!member_p)
19595 if (parser->in_unbraced_linkage_specification_p)
19596 decl_specifiers->storage_class = sc_extern;
19597 decl = start_decl (declarator, decl_specifiers,
19598 range_for_decl_p? SD_INITIALIZED : is_initialized,
19599 attributes, prefix_attributes, &pushed_scope);
19600 cp_finalize_omp_declare_simd (parser, decl);
19601 cp_finalize_oacc_routine (parser, decl, false);
19602 /* Adjust location of decl if declarator->id_loc is more appropriate:
19603 set, and decl wasn't merged with another decl, in which case its
19604 location would be different from input_location, and more accurate. */
19605 if (DECL_P (decl)
19606 && declarator->id_loc != UNKNOWN_LOCATION
19607 && DECL_SOURCE_LOCATION (decl) == input_location)
19608 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19610 else if (scope)
19611 /* Enter the SCOPE. That way unqualified names appearing in the
19612 initializer will be looked up in SCOPE. */
19613 pushed_scope = push_scope (scope);
19615 /* Perform deferred access control checks, now that we know in which
19616 SCOPE the declared entity resides. */
19617 if (!member_p && decl)
19619 tree saved_current_function_decl = NULL_TREE;
19621 /* If the entity being declared is a function, pretend that we
19622 are in its scope. If it is a `friend', it may have access to
19623 things that would not otherwise be accessible. */
19624 if (TREE_CODE (decl) == FUNCTION_DECL)
19626 saved_current_function_decl = current_function_decl;
19627 current_function_decl = decl;
19630 /* Perform access checks for template parameters. */
19631 cp_parser_perform_template_parameter_access_checks (checks);
19633 /* Perform the access control checks for the declarator and the
19634 decl-specifiers. */
19635 perform_deferred_access_checks (tf_warning_or_error);
19637 /* Restore the saved value. */
19638 if (TREE_CODE (decl) == FUNCTION_DECL)
19639 current_function_decl = saved_current_function_decl;
19642 /* Parse the initializer. */
19643 initializer = NULL_TREE;
19644 is_direct_init = false;
19645 is_non_constant_init = true;
19646 if (is_initialized)
19648 if (function_declarator_p (declarator))
19650 if (initialization_kind == CPP_EQ)
19651 initializer = cp_parser_pure_specifier (parser);
19652 else
19654 /* If the declaration was erroneous, we don't really
19655 know what the user intended, so just silently
19656 consume the initializer. */
19657 if (decl != error_mark_node)
19658 error_at (tmp_init_loc, "initializer provided for function");
19659 cp_parser_skip_to_closing_parenthesis (parser,
19660 /*recovering=*/true,
19661 /*or_comma=*/false,
19662 /*consume_paren=*/true);
19665 else
19667 /* We want to record the extra mangling scope for in-class
19668 initializers of class members and initializers of static data
19669 member templates. The former involves deferring
19670 parsing of the initializer until end of class as with default
19671 arguments. So right here we only handle the latter. */
19672 if (!member_p && processing_template_decl && decl != error_mark_node)
19673 start_lambda_scope (decl);
19674 initializer = cp_parser_initializer (parser,
19675 &is_direct_init,
19676 &is_non_constant_init);
19677 if (!member_p && processing_template_decl && decl != error_mark_node)
19678 finish_lambda_scope ();
19679 if (initializer == error_mark_node)
19680 cp_parser_skip_to_end_of_statement (parser);
19684 /* The old parser allows attributes to appear after a parenthesized
19685 initializer. Mark Mitchell proposed removing this functionality
19686 on the GCC mailing lists on 2002-08-13. This parser accepts the
19687 attributes -- but ignores them. Made a permerror in GCC 8. */
19688 if (cp_parser_allow_gnu_extensions_p (parser)
19689 && initialization_kind == CPP_OPEN_PAREN
19690 && cp_parser_attributes_opt (parser)
19691 && permerror (input_location,
19692 "attributes after parenthesized initializer ignored"))
19694 static bool hint;
19695 if (flag_permissive && !hint)
19697 hint = true;
19698 inform (input_location,
19699 "this flexibility is deprecated and will be removed");
19703 /* And now complain about a non-function implicit template. */
19704 if (bogus_implicit_tmpl && decl != error_mark_node)
19705 error_at (DECL_SOURCE_LOCATION (decl),
19706 "non-function %qD declared as implicit template", decl);
19708 /* For an in-class declaration, use `grokfield' to create the
19709 declaration. */
19710 if (member_p)
19712 if (pushed_scope)
19714 pop_scope (pushed_scope);
19715 pushed_scope = NULL_TREE;
19717 decl = grokfield (declarator, decl_specifiers,
19718 initializer, !is_non_constant_init,
19719 /*asmspec=*/NULL_TREE,
19720 attr_chainon (attributes, prefix_attributes));
19721 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19722 cp_parser_save_default_args (parser, decl);
19723 cp_finalize_omp_declare_simd (parser, decl);
19724 cp_finalize_oacc_routine (parser, decl, false);
19727 /* Finish processing the declaration. But, skip member
19728 declarations. */
19729 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19731 cp_finish_decl (decl,
19732 initializer, !is_non_constant_init,
19733 asm_specification,
19734 /* If the initializer is in parentheses, then this is
19735 a direct-initialization, which means that an
19736 `explicit' constructor is OK. Otherwise, an
19737 `explicit' constructor cannot be used. */
19738 ((is_direct_init || !is_initialized)
19739 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19741 else if ((cxx_dialect != cxx98) && friend_p
19742 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19743 /* Core issue #226 (C++0x only): A default template-argument
19744 shall not be specified in a friend class template
19745 declaration. */
19746 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19747 /*is_partial=*/false, /*is_friend_decl=*/1);
19749 if (!friend_p && pushed_scope)
19750 pop_scope (pushed_scope);
19752 if (function_declarator_p (declarator)
19753 && parser->fully_implicit_function_template_p)
19755 if (member_p)
19756 decl = finish_fully_implicit_template (parser, decl);
19757 else
19758 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19761 if (auto_result && is_initialized && decl_specifiers->type
19762 && type_uses_auto (decl_specifiers->type))
19763 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19765 return decl;
19768 /* Parse a declarator.
19770 declarator:
19771 direct-declarator
19772 ptr-operator declarator
19774 abstract-declarator:
19775 ptr-operator abstract-declarator [opt]
19776 direct-abstract-declarator
19778 GNU Extensions:
19780 declarator:
19781 attributes [opt] direct-declarator
19782 attributes [opt] ptr-operator declarator
19784 abstract-declarator:
19785 attributes [opt] ptr-operator abstract-declarator [opt]
19786 attributes [opt] direct-abstract-declarator
19788 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19789 detect constructors, destructors, deduction guides, or conversion operators.
19790 It is set to -1 if the declarator is a name, and +1 if it is a
19791 function. Otherwise it is set to zero. Usually you just want to
19792 test for >0, but internally the negative value is used.
19794 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19795 a decl-specifier-seq unless it declares a constructor, destructor,
19796 or conversion. It might seem that we could check this condition in
19797 semantic analysis, rather than parsing, but that makes it difficult
19798 to handle something like `f()'. We want to notice that there are
19799 no decl-specifiers, and therefore realize that this is an
19800 expression, not a declaration.)
19802 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19803 the declarator is a direct-declarator of the form "(...)".
19805 MEMBER_P is true iff this declarator is a member-declarator.
19807 FRIEND_P is true iff this declarator is a friend. */
19809 static cp_declarator *
19810 cp_parser_declarator (cp_parser* parser,
19811 cp_parser_declarator_kind dcl_kind,
19812 int* ctor_dtor_or_conv_p,
19813 bool* parenthesized_p,
19814 bool member_p, bool friend_p)
19816 cp_declarator *declarator;
19817 enum tree_code code;
19818 cp_cv_quals cv_quals;
19819 tree class_type;
19820 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19822 /* Assume this is not a constructor, destructor, or type-conversion
19823 operator. */
19824 if (ctor_dtor_or_conv_p)
19825 *ctor_dtor_or_conv_p = 0;
19827 if (cp_parser_allow_gnu_extensions_p (parser))
19828 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19830 /* Check for the ptr-operator production. */
19831 cp_parser_parse_tentatively (parser);
19832 /* Parse the ptr-operator. */
19833 code = cp_parser_ptr_operator (parser,
19834 &class_type,
19835 &cv_quals,
19836 &std_attributes);
19838 /* If that worked, then we have a ptr-operator. */
19839 if (cp_parser_parse_definitely (parser))
19841 /* If a ptr-operator was found, then this declarator was not
19842 parenthesized. */
19843 if (parenthesized_p)
19844 *parenthesized_p = true;
19845 /* The dependent declarator is optional if we are parsing an
19846 abstract-declarator. */
19847 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19848 cp_parser_parse_tentatively (parser);
19850 /* Parse the dependent declarator. */
19851 declarator = cp_parser_declarator (parser, dcl_kind,
19852 /*ctor_dtor_or_conv_p=*/NULL,
19853 /*parenthesized_p=*/NULL,
19854 /*member_p=*/false,
19855 friend_p);
19857 /* If we are parsing an abstract-declarator, we must handle the
19858 case where the dependent declarator is absent. */
19859 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19860 && !cp_parser_parse_definitely (parser))
19861 declarator = NULL;
19863 declarator = cp_parser_make_indirect_declarator
19864 (code, class_type, cv_quals, declarator, std_attributes);
19866 /* Everything else is a direct-declarator. */
19867 else
19869 if (parenthesized_p)
19870 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19871 CPP_OPEN_PAREN);
19872 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19873 ctor_dtor_or_conv_p,
19874 member_p, friend_p);
19877 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19878 declarator->attributes = gnu_attributes;
19879 return declarator;
19882 /* Parse a direct-declarator or direct-abstract-declarator.
19884 direct-declarator:
19885 declarator-id
19886 direct-declarator ( parameter-declaration-clause )
19887 cv-qualifier-seq [opt]
19888 ref-qualifier [opt]
19889 exception-specification [opt]
19890 direct-declarator [ constant-expression [opt] ]
19891 ( declarator )
19893 direct-abstract-declarator:
19894 direct-abstract-declarator [opt]
19895 ( parameter-declaration-clause )
19896 cv-qualifier-seq [opt]
19897 ref-qualifier [opt]
19898 exception-specification [opt]
19899 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19900 ( abstract-declarator )
19902 Returns a representation of the declarator. DCL_KIND is
19903 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19904 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19905 we are parsing a direct-declarator. It is
19906 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19907 of ambiguity we prefer an abstract declarator, as per
19908 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19909 as for cp_parser_declarator. */
19911 static cp_declarator *
19912 cp_parser_direct_declarator (cp_parser* parser,
19913 cp_parser_declarator_kind dcl_kind,
19914 int* ctor_dtor_or_conv_p,
19915 bool member_p, bool friend_p)
19917 cp_token *token;
19918 cp_declarator *declarator = NULL;
19919 tree scope = NULL_TREE;
19920 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19921 bool saved_in_declarator_p = parser->in_declarator_p;
19922 bool first = true;
19923 tree pushed_scope = NULL_TREE;
19924 cp_token *open_paren = NULL, *close_paren = NULL;
19926 while (true)
19928 /* Peek at the next token. */
19929 token = cp_lexer_peek_token (parser->lexer);
19930 if (token->type == CPP_OPEN_PAREN)
19932 /* This is either a parameter-declaration-clause, or a
19933 parenthesized declarator. When we know we are parsing a
19934 named declarator, it must be a parenthesized declarator
19935 if FIRST is true. For instance, `(int)' is a
19936 parameter-declaration-clause, with an omitted
19937 direct-abstract-declarator. But `((*))', is a
19938 parenthesized abstract declarator. Finally, when T is a
19939 template parameter `(T)' is a
19940 parameter-declaration-clause, and not a parenthesized
19941 named declarator.
19943 We first try and parse a parameter-declaration-clause,
19944 and then try a nested declarator (if FIRST is true).
19946 It is not an error for it not to be a
19947 parameter-declaration-clause, even when FIRST is
19948 false. Consider,
19950 int i (int);
19951 int i (3);
19953 The first is the declaration of a function while the
19954 second is the definition of a variable, including its
19955 initializer.
19957 Having seen only the parenthesis, we cannot know which of
19958 these two alternatives should be selected. Even more
19959 complex are examples like:
19961 int i (int (a));
19962 int i (int (3));
19964 The former is a function-declaration; the latter is a
19965 variable initialization.
19967 Thus again, we try a parameter-declaration-clause, and if
19968 that fails, we back out and return. */
19970 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19972 tree params;
19973 bool is_declarator = false;
19975 open_paren = NULL;
19977 /* In a member-declarator, the only valid interpretation
19978 of a parenthesis is the start of a
19979 parameter-declaration-clause. (It is invalid to
19980 initialize a static data member with a parenthesized
19981 initializer; only the "=" form of initialization is
19982 permitted.) */
19983 if (!member_p)
19984 cp_parser_parse_tentatively (parser);
19986 /* Consume the `('. */
19987 matching_parens parens;
19988 parens.consume_open (parser);
19989 if (first)
19991 /* If this is going to be an abstract declarator, we're
19992 in a declarator and we can't have default args. */
19993 parser->default_arg_ok_p = false;
19994 parser->in_declarator_p = true;
19997 begin_scope (sk_function_parms, NULL_TREE);
19999 /* Parse the parameter-declaration-clause. */
20000 params = cp_parser_parameter_declaration_clause (parser);
20002 /* Consume the `)'. */
20003 parens.require_close (parser);
20005 /* If all went well, parse the cv-qualifier-seq,
20006 ref-qualifier and the exception-specification. */
20007 if (member_p || cp_parser_parse_definitely (parser))
20009 cp_cv_quals cv_quals;
20010 cp_virt_specifiers virt_specifiers;
20011 cp_ref_qualifier ref_qual;
20012 tree exception_specification;
20013 tree late_return;
20014 tree attrs;
20015 bool memfn = (member_p || (pushed_scope
20016 && CLASS_TYPE_P (pushed_scope)));
20018 is_declarator = true;
20020 if (ctor_dtor_or_conv_p)
20021 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20022 first = false;
20024 /* Parse the cv-qualifier-seq. */
20025 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20026 /* Parse the ref-qualifier. */
20027 ref_qual = cp_parser_ref_qualifier_opt (parser);
20028 /* Parse the tx-qualifier. */
20029 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20030 /* And the exception-specification. */
20031 exception_specification
20032 = cp_parser_exception_specification_opt (parser);
20034 attrs = cp_parser_std_attribute_spec_seq (parser);
20036 /* In here, we handle cases where attribute is used after
20037 the function declaration. For example:
20038 void func (int x) __attribute__((vector(..))); */
20039 tree gnu_attrs = NULL_TREE;
20040 tree requires_clause = NULL_TREE;
20041 late_return = (cp_parser_late_return_type_opt
20042 (parser, declarator, requires_clause,
20043 memfn ? cv_quals : -1));
20045 /* Parse the virt-specifier-seq. */
20046 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20048 /* Create the function-declarator. */
20049 declarator = make_call_declarator (declarator,
20050 params,
20051 cv_quals,
20052 virt_specifiers,
20053 ref_qual,
20054 tx_qual,
20055 exception_specification,
20056 late_return,
20057 requires_clause);
20058 declarator->std_attributes = attrs;
20059 declarator->attributes = gnu_attrs;
20060 /* Any subsequent parameter lists are to do with
20061 return type, so are not those of the declared
20062 function. */
20063 parser->default_arg_ok_p = false;
20066 /* Remove the function parms from scope. */
20067 pop_bindings_and_leave_scope ();
20069 if (is_declarator)
20070 /* Repeat the main loop. */
20071 continue;
20074 /* If this is the first, we can try a parenthesized
20075 declarator. */
20076 if (first)
20078 bool saved_in_type_id_in_expr_p;
20080 parser->default_arg_ok_p = saved_default_arg_ok_p;
20081 parser->in_declarator_p = saved_in_declarator_p;
20083 open_paren = token;
20084 /* Consume the `('. */
20085 matching_parens parens;
20086 parens.consume_open (parser);
20087 /* Parse the nested declarator. */
20088 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20089 parser->in_type_id_in_expr_p = true;
20090 declarator
20091 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20092 /*parenthesized_p=*/NULL,
20093 member_p, friend_p);
20094 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20095 first = false;
20096 /* Expect a `)'. */
20097 close_paren = cp_lexer_peek_token (parser->lexer);
20098 if (!parens.require_close (parser))
20099 declarator = cp_error_declarator;
20100 if (declarator == cp_error_declarator)
20101 break;
20103 goto handle_declarator;
20105 /* Otherwise, we must be done. */
20106 else
20107 break;
20109 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20110 && token->type == CPP_OPEN_SQUARE
20111 && !cp_next_tokens_can_be_attribute_p (parser))
20113 /* Parse an array-declarator. */
20114 tree bounds, attrs;
20116 if (ctor_dtor_or_conv_p)
20117 *ctor_dtor_or_conv_p = 0;
20119 open_paren = NULL;
20120 first = false;
20121 parser->default_arg_ok_p = false;
20122 parser->in_declarator_p = true;
20123 /* Consume the `['. */
20124 cp_lexer_consume_token (parser->lexer);
20125 /* Peek at the next token. */
20126 token = cp_lexer_peek_token (parser->lexer);
20127 /* If the next token is `]', then there is no
20128 constant-expression. */
20129 if (token->type != CPP_CLOSE_SQUARE)
20131 bool non_constant_p;
20132 bounds
20133 = cp_parser_constant_expression (parser,
20134 /*allow_non_constant=*/true,
20135 &non_constant_p);
20136 if (!non_constant_p)
20137 /* OK */;
20138 else if (error_operand_p (bounds))
20139 /* Already gave an error. */;
20140 else if (!parser->in_function_body
20141 || current_binding_level->kind == sk_function_parms)
20143 /* Normally, the array bound must be an integral constant
20144 expression. However, as an extension, we allow VLAs
20145 in function scopes as long as they aren't part of a
20146 parameter declaration. */
20147 cp_parser_error (parser,
20148 "array bound is not an integer constant");
20149 bounds = error_mark_node;
20151 else if (processing_template_decl
20152 && !type_dependent_expression_p (bounds))
20154 /* Remember this wasn't a constant-expression. */
20155 bounds = build_nop (TREE_TYPE (bounds), bounds);
20156 TREE_SIDE_EFFECTS (bounds) = 1;
20159 else
20160 bounds = NULL_TREE;
20161 /* Look for the closing `]'. */
20162 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20164 declarator = cp_error_declarator;
20165 break;
20168 attrs = cp_parser_std_attribute_spec_seq (parser);
20169 declarator = make_array_declarator (declarator, bounds);
20170 declarator->std_attributes = attrs;
20172 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20175 tree qualifying_scope;
20176 tree unqualified_name;
20177 tree attrs;
20178 special_function_kind sfk;
20179 bool abstract_ok;
20180 bool pack_expansion_p = false;
20181 cp_token *declarator_id_start_token;
20183 /* Parse a declarator-id */
20184 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20185 if (abstract_ok)
20187 cp_parser_parse_tentatively (parser);
20189 /* If we see an ellipsis, we should be looking at a
20190 parameter pack. */
20191 if (token->type == CPP_ELLIPSIS)
20193 /* Consume the `...' */
20194 cp_lexer_consume_token (parser->lexer);
20196 pack_expansion_p = true;
20200 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20201 unqualified_name
20202 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20203 qualifying_scope = parser->scope;
20204 if (abstract_ok)
20206 bool okay = false;
20208 if (!unqualified_name && pack_expansion_p)
20210 /* Check whether an error occurred. */
20211 okay = !cp_parser_error_occurred (parser);
20213 /* We already consumed the ellipsis to mark a
20214 parameter pack, but we have no way to report it,
20215 so abort the tentative parse. We will be exiting
20216 immediately anyway. */
20217 cp_parser_abort_tentative_parse (parser);
20219 else
20220 okay = cp_parser_parse_definitely (parser);
20222 if (!okay)
20223 unqualified_name = error_mark_node;
20224 else if (unqualified_name
20225 && (qualifying_scope
20226 || (!identifier_p (unqualified_name))))
20228 cp_parser_error (parser, "expected unqualified-id");
20229 unqualified_name = error_mark_node;
20233 if (!unqualified_name)
20234 return NULL;
20235 if (unqualified_name == error_mark_node)
20237 declarator = cp_error_declarator;
20238 pack_expansion_p = false;
20239 declarator->parameter_pack_p = false;
20240 break;
20243 attrs = cp_parser_std_attribute_spec_seq (parser);
20245 if (qualifying_scope && at_namespace_scope_p ()
20246 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20248 /* In the declaration of a member of a template class
20249 outside of the class itself, the SCOPE will sometimes
20250 be a TYPENAME_TYPE. For example, given:
20252 template <typename T>
20253 int S<T>::R::i = 3;
20255 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20256 this context, we must resolve S<T>::R to an ordinary
20257 type, rather than a typename type.
20259 The reason we normally avoid resolving TYPENAME_TYPEs
20260 is that a specialization of `S' might render
20261 `S<T>::R' not a type. However, if `S' is
20262 specialized, then this `i' will not be used, so there
20263 is no harm in resolving the types here. */
20264 tree type;
20266 /* Resolve the TYPENAME_TYPE. */
20267 type = resolve_typename_type (qualifying_scope,
20268 /*only_current_p=*/false);
20269 /* If that failed, the declarator is invalid. */
20270 if (TREE_CODE (type) == TYPENAME_TYPE)
20272 if (typedef_variant_p (type))
20273 error_at (declarator_id_start_token->location,
20274 "cannot define member of dependent typedef "
20275 "%qT", type);
20276 else
20277 error_at (declarator_id_start_token->location,
20278 "%<%T::%E%> is not a type",
20279 TYPE_CONTEXT (qualifying_scope),
20280 TYPE_IDENTIFIER (qualifying_scope));
20282 qualifying_scope = type;
20285 sfk = sfk_none;
20287 if (unqualified_name)
20289 tree class_type;
20291 if (qualifying_scope
20292 && CLASS_TYPE_P (qualifying_scope))
20293 class_type = qualifying_scope;
20294 else
20295 class_type = current_class_type;
20297 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20299 tree name_type = TREE_TYPE (unqualified_name);
20301 if (!class_type || !same_type_p (name_type, class_type))
20303 /* We do not attempt to print the declarator
20304 here because we do not have enough
20305 information about its original syntactic
20306 form. */
20307 cp_parser_error (parser, "invalid declarator");
20308 declarator = cp_error_declarator;
20309 break;
20311 else if (qualifying_scope
20312 && CLASSTYPE_USE_TEMPLATE (name_type))
20314 error_at (declarator_id_start_token->location,
20315 "invalid use of constructor as a template");
20316 inform (declarator_id_start_token->location,
20317 "use %<%T::%D%> instead of %<%T::%D%> to "
20318 "name the constructor in a qualified name",
20319 class_type,
20320 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20321 class_type, name_type);
20322 declarator = cp_error_declarator;
20323 break;
20325 unqualified_name = constructor_name (class_type);
20328 if (class_type)
20330 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20331 sfk = sfk_destructor;
20332 else if (identifier_p (unqualified_name)
20333 && IDENTIFIER_CONV_OP_P (unqualified_name))
20334 sfk = sfk_conversion;
20335 else if (/* There's no way to declare a constructor
20336 for an unnamed type, even if the type
20337 got a name for linkage purposes. */
20338 !TYPE_WAS_UNNAMED (class_type)
20339 /* Handle correctly (c++/19200):
20341 struct S {
20342 struct T{};
20343 friend void S(T);
20346 and also:
20348 namespace N {
20349 void S();
20352 struct S {
20353 friend void N::S();
20354 }; */
20355 && (!friend_p || class_type == qualifying_scope)
20356 && constructor_name_p (unqualified_name,
20357 class_type))
20358 sfk = sfk_constructor;
20359 else if (is_overloaded_fn (unqualified_name)
20360 && DECL_CONSTRUCTOR_P (get_first_fn
20361 (unqualified_name)))
20362 sfk = sfk_constructor;
20364 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20365 *ctor_dtor_or_conv_p = -1;
20368 declarator = make_id_declarator (qualifying_scope,
20369 unqualified_name,
20370 sfk);
20371 declarator->std_attributes = attrs;
20372 declarator->id_loc = token->location;
20373 declarator->parameter_pack_p = pack_expansion_p;
20375 if (pack_expansion_p)
20376 maybe_warn_variadic_templates ();
20379 handle_declarator:;
20380 scope = get_scope_of_declarator (declarator);
20381 if (scope)
20383 /* Any names that appear after the declarator-id for a
20384 member are looked up in the containing scope. */
20385 if (at_function_scope_p ())
20387 /* But declarations with qualified-ids can't appear in a
20388 function. */
20389 cp_parser_error (parser, "qualified-id in declaration");
20390 declarator = cp_error_declarator;
20391 break;
20393 pushed_scope = push_scope (scope);
20395 parser->in_declarator_p = true;
20396 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20397 || (declarator && declarator->kind == cdk_id))
20398 /* Default args are only allowed on function
20399 declarations. */
20400 parser->default_arg_ok_p = saved_default_arg_ok_p;
20401 else
20402 parser->default_arg_ok_p = false;
20404 first = false;
20406 /* We're done. */
20407 else
20408 break;
20411 /* For an abstract declarator, we might wind up with nothing at this
20412 point. That's an error; the declarator is not optional. */
20413 if (!declarator)
20414 cp_parser_error (parser, "expected declarator");
20415 else if (open_paren)
20417 /* Record overly parenthesized declarator so we can give a
20418 diagnostic about confusing decl/expr disambiguation. */
20419 if (declarator->kind == cdk_array)
20421 /* If the open and close parens are on different lines, this
20422 is probably a formatting thing, so ignore. */
20423 expanded_location open = expand_location (open_paren->location);
20424 expanded_location close = expand_location (close_paren->location);
20425 if (open.line != close.line || open.file != close.file)
20426 open_paren = NULL;
20428 if (open_paren)
20429 declarator->parenthesized = open_paren->location;
20432 /* If we entered a scope, we must exit it now. */
20433 if (pushed_scope)
20434 pop_scope (pushed_scope);
20436 parser->default_arg_ok_p = saved_default_arg_ok_p;
20437 parser->in_declarator_p = saved_in_declarator_p;
20439 return declarator;
20442 /* Parse a ptr-operator.
20444 ptr-operator:
20445 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20446 * cv-qualifier-seq [opt]
20448 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20449 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20451 GNU Extension:
20453 ptr-operator:
20454 & cv-qualifier-seq [opt]
20456 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20457 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20458 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20459 filled in with the TYPE containing the member. *CV_QUALS is
20460 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20461 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20462 Note that the tree codes returned by this function have nothing
20463 to do with the types of trees that will be eventually be created
20464 to represent the pointer or reference type being parsed. They are
20465 just constants with suggestive names. */
20466 static enum tree_code
20467 cp_parser_ptr_operator (cp_parser* parser,
20468 tree* type,
20469 cp_cv_quals *cv_quals,
20470 tree *attributes)
20472 enum tree_code code = ERROR_MARK;
20473 cp_token *token;
20474 tree attrs = NULL_TREE;
20476 /* Assume that it's not a pointer-to-member. */
20477 *type = NULL_TREE;
20478 /* And that there are no cv-qualifiers. */
20479 *cv_quals = TYPE_UNQUALIFIED;
20481 /* Peek at the next token. */
20482 token = cp_lexer_peek_token (parser->lexer);
20484 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20485 if (token->type == CPP_MULT)
20486 code = INDIRECT_REF;
20487 else if (token->type == CPP_AND)
20488 code = ADDR_EXPR;
20489 else if ((cxx_dialect != cxx98) &&
20490 token->type == CPP_AND_AND) /* C++0x only */
20491 code = NON_LVALUE_EXPR;
20493 if (code != ERROR_MARK)
20495 /* Consume the `*', `&' or `&&'. */
20496 cp_lexer_consume_token (parser->lexer);
20498 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20499 `&', if we are allowing GNU extensions. (The only qualifier
20500 that can legally appear after `&' is `restrict', but that is
20501 enforced during semantic analysis. */
20502 if (code == INDIRECT_REF
20503 || cp_parser_allow_gnu_extensions_p (parser))
20504 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20506 attrs = cp_parser_std_attribute_spec_seq (parser);
20507 if (attributes != NULL)
20508 *attributes = attrs;
20510 else
20512 /* Try the pointer-to-member case. */
20513 cp_parser_parse_tentatively (parser);
20514 /* Look for the optional `::' operator. */
20515 cp_parser_global_scope_opt (parser,
20516 /*current_scope_valid_p=*/false);
20517 /* Look for the nested-name specifier. */
20518 token = cp_lexer_peek_token (parser->lexer);
20519 cp_parser_nested_name_specifier (parser,
20520 /*typename_keyword_p=*/false,
20521 /*check_dependency_p=*/true,
20522 /*type_p=*/false,
20523 /*is_declaration=*/false);
20524 /* If we found it, and the next token is a `*', then we are
20525 indeed looking at a pointer-to-member operator. */
20526 if (!cp_parser_error_occurred (parser)
20527 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20529 /* Indicate that the `*' operator was used. */
20530 code = INDIRECT_REF;
20532 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20533 error_at (token->location, "%qD is a namespace", parser->scope);
20534 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20535 error_at (token->location, "cannot form pointer to member of "
20536 "non-class %q#T", parser->scope);
20537 else
20539 /* The type of which the member is a member is given by the
20540 current SCOPE. */
20541 *type = parser->scope;
20542 /* The next name will not be qualified. */
20543 parser->scope = NULL_TREE;
20544 parser->qualifying_scope = NULL_TREE;
20545 parser->object_scope = NULL_TREE;
20546 /* Look for optional c++11 attributes. */
20547 attrs = cp_parser_std_attribute_spec_seq (parser);
20548 if (attributes != NULL)
20549 *attributes = attrs;
20550 /* Look for the optional cv-qualifier-seq. */
20551 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20554 /* If that didn't work we don't have a ptr-operator. */
20555 if (!cp_parser_parse_definitely (parser))
20556 cp_parser_error (parser, "expected ptr-operator");
20559 return code;
20562 /* Parse an (optional) cv-qualifier-seq.
20564 cv-qualifier-seq:
20565 cv-qualifier cv-qualifier-seq [opt]
20567 cv-qualifier:
20568 const
20569 volatile
20571 GNU Extension:
20573 cv-qualifier:
20574 __restrict__
20576 Returns a bitmask representing the cv-qualifiers. */
20578 static cp_cv_quals
20579 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20581 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20583 while (true)
20585 cp_token *token;
20586 cp_cv_quals cv_qualifier;
20588 /* Peek at the next token. */
20589 token = cp_lexer_peek_token (parser->lexer);
20590 /* See if it's a cv-qualifier. */
20591 switch (token->keyword)
20593 case RID_CONST:
20594 cv_qualifier = TYPE_QUAL_CONST;
20595 break;
20597 case RID_VOLATILE:
20598 cv_qualifier = TYPE_QUAL_VOLATILE;
20599 break;
20601 case RID_RESTRICT:
20602 cv_qualifier = TYPE_QUAL_RESTRICT;
20603 break;
20605 default:
20606 cv_qualifier = TYPE_UNQUALIFIED;
20607 break;
20610 if (!cv_qualifier)
20611 break;
20613 if (cv_quals & cv_qualifier)
20615 gcc_rich_location richloc (token->location);
20616 richloc.add_fixit_remove ();
20617 error_at (&richloc, "duplicate cv-qualifier");
20618 cp_lexer_purge_token (parser->lexer);
20620 else
20622 cp_lexer_consume_token (parser->lexer);
20623 cv_quals |= cv_qualifier;
20627 return cv_quals;
20630 /* Parse an (optional) ref-qualifier
20632 ref-qualifier:
20636 Returns cp_ref_qualifier representing ref-qualifier. */
20638 static cp_ref_qualifier
20639 cp_parser_ref_qualifier_opt (cp_parser* parser)
20641 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20643 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20644 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20645 return ref_qual;
20647 while (true)
20649 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20650 cp_token *token = cp_lexer_peek_token (parser->lexer);
20652 switch (token->type)
20654 case CPP_AND:
20655 curr_ref_qual = REF_QUAL_LVALUE;
20656 break;
20658 case CPP_AND_AND:
20659 curr_ref_qual = REF_QUAL_RVALUE;
20660 break;
20662 default:
20663 curr_ref_qual = REF_QUAL_NONE;
20664 break;
20667 if (!curr_ref_qual)
20668 break;
20669 else if (ref_qual)
20671 error_at (token->location, "multiple ref-qualifiers");
20672 cp_lexer_purge_token (parser->lexer);
20674 else
20676 ref_qual = curr_ref_qual;
20677 cp_lexer_consume_token (parser->lexer);
20681 return ref_qual;
20684 /* Parse an optional tx-qualifier.
20686 tx-qualifier:
20687 transaction_safe
20688 transaction_safe_dynamic */
20690 static tree
20691 cp_parser_tx_qualifier_opt (cp_parser *parser)
20693 cp_token *token = cp_lexer_peek_token (parser->lexer);
20694 if (token->type == CPP_NAME)
20696 tree name = token->u.value;
20697 const char *p = IDENTIFIER_POINTER (name);
20698 const int len = strlen ("transaction_safe");
20699 if (!strncmp (p, "transaction_safe", len))
20701 p += len;
20702 if (*p == '\0'
20703 || !strcmp (p, "_dynamic"))
20705 cp_lexer_consume_token (parser->lexer);
20706 if (!flag_tm)
20708 error ("%qE requires %<-fgnu-tm%>", name);
20709 return NULL_TREE;
20711 else
20712 return name;
20716 return NULL_TREE;
20719 /* Parse an (optional) virt-specifier-seq.
20721 virt-specifier-seq:
20722 virt-specifier virt-specifier-seq [opt]
20724 virt-specifier:
20725 override
20726 final
20728 Returns a bitmask representing the virt-specifiers. */
20730 static cp_virt_specifiers
20731 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20733 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20735 while (true)
20737 cp_token *token;
20738 cp_virt_specifiers virt_specifier;
20740 /* Peek at the next token. */
20741 token = cp_lexer_peek_token (parser->lexer);
20742 /* See if it's a virt-specifier-qualifier. */
20743 if (token->type != CPP_NAME)
20744 break;
20745 if (id_equal (token->u.value, "override"))
20747 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20748 virt_specifier = VIRT_SPEC_OVERRIDE;
20750 else if (id_equal (token->u.value, "final"))
20752 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20753 virt_specifier = VIRT_SPEC_FINAL;
20755 else if (id_equal (token->u.value, "__final"))
20757 virt_specifier = VIRT_SPEC_FINAL;
20759 else
20760 break;
20762 if (virt_specifiers & virt_specifier)
20764 gcc_rich_location richloc (token->location);
20765 richloc.add_fixit_remove ();
20766 error_at (&richloc, "duplicate virt-specifier");
20767 cp_lexer_purge_token (parser->lexer);
20769 else
20771 cp_lexer_consume_token (parser->lexer);
20772 virt_specifiers |= virt_specifier;
20775 return virt_specifiers;
20778 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20779 is in scope even though it isn't real. */
20781 void
20782 inject_this_parameter (tree ctype, cp_cv_quals quals)
20784 tree this_parm;
20786 if (current_class_ptr)
20788 /* We don't clear this between NSDMIs. Is it already what we want? */
20789 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20790 if (DECL_P (current_class_ptr)
20791 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20792 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20793 && cp_type_quals (type) == quals)
20794 return;
20797 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20798 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20799 current_class_ptr = NULL_TREE;
20800 current_class_ref
20801 = cp_build_fold_indirect_ref (this_parm);
20802 current_class_ptr = this_parm;
20805 /* Return true iff our current scope is a non-static data member
20806 initializer. */
20808 bool
20809 parsing_nsdmi (void)
20811 /* We recognize NSDMI context by the context-less 'this' pointer set up
20812 by the function above. */
20813 if (current_class_ptr
20814 && TREE_CODE (current_class_ptr) == PARM_DECL
20815 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20816 return true;
20817 return false;
20820 /* Parse a late-specified return type, if any. This is not a separate
20821 non-terminal, but part of a function declarator, which looks like
20823 -> trailing-type-specifier-seq abstract-declarator(opt)
20825 Returns the type indicated by the type-id.
20827 In addition to this, parse any queued up #pragma omp declare simd
20828 clauses, and #pragma acc routine clauses.
20830 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20831 function. */
20833 static tree
20834 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20835 tree& requires_clause, cp_cv_quals quals)
20837 cp_token *token;
20838 tree type = NULL_TREE;
20839 bool declare_simd_p = (parser->omp_declare_simd
20840 && declarator
20841 && declarator->kind == cdk_id);
20843 bool oacc_routine_p = (parser->oacc_routine
20844 && declarator
20845 && declarator->kind == cdk_id);
20847 /* Peek at the next token. */
20848 token = cp_lexer_peek_token (parser->lexer);
20849 /* A late-specified return type is indicated by an initial '->'. */
20850 if (token->type != CPP_DEREF
20851 && token->keyword != RID_REQUIRES
20852 && !(token->type == CPP_NAME
20853 && token->u.value == ridpointers[RID_REQUIRES])
20854 && !(declare_simd_p || oacc_routine_p))
20855 return NULL_TREE;
20857 tree save_ccp = current_class_ptr;
20858 tree save_ccr = current_class_ref;
20859 if (quals >= 0)
20861 /* DR 1207: 'this' is in scope in the trailing return type. */
20862 inject_this_parameter (current_class_type, quals);
20865 if (token->type == CPP_DEREF)
20867 /* Consume the ->. */
20868 cp_lexer_consume_token (parser->lexer);
20870 type = cp_parser_trailing_type_id (parser);
20873 /* Function declarations may be followed by a trailing
20874 requires-clause. */
20875 requires_clause = cp_parser_requires_clause_opt (parser);
20877 if (declare_simd_p)
20878 declarator->attributes
20879 = cp_parser_late_parsing_omp_declare_simd (parser,
20880 declarator->attributes);
20881 if (oacc_routine_p)
20882 declarator->attributes
20883 = cp_parser_late_parsing_oacc_routine (parser,
20884 declarator->attributes);
20886 if (quals >= 0)
20888 current_class_ptr = save_ccp;
20889 current_class_ref = save_ccr;
20892 return type;
20895 /* Parse a declarator-id.
20897 declarator-id:
20898 id-expression
20899 :: [opt] nested-name-specifier [opt] type-name
20901 In the `id-expression' case, the value returned is as for
20902 cp_parser_id_expression if the id-expression was an unqualified-id.
20903 If the id-expression was a qualified-id, then a SCOPE_REF is
20904 returned. The first operand is the scope (either a NAMESPACE_DECL
20905 or TREE_TYPE), but the second is still just a representation of an
20906 unqualified-id. */
20908 static tree
20909 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20911 tree id;
20912 /* The expression must be an id-expression. Assume that qualified
20913 names are the names of types so that:
20915 template <class T>
20916 int S<T>::R::i = 3;
20918 will work; we must treat `S<T>::R' as the name of a type.
20919 Similarly, assume that qualified names are templates, where
20920 required, so that:
20922 template <class T>
20923 int S<T>::R<T>::i = 3;
20925 will work, too. */
20926 id = cp_parser_id_expression (parser,
20927 /*template_keyword_p=*/false,
20928 /*check_dependency_p=*/false,
20929 /*template_p=*/NULL,
20930 /*declarator_p=*/true,
20931 optional_p);
20932 if (id && BASELINK_P (id))
20933 id = BASELINK_FUNCTIONS (id);
20934 return id;
20937 /* Parse a type-id.
20939 type-id:
20940 type-specifier-seq abstract-declarator [opt]
20942 Returns the TYPE specified. */
20944 static tree
20945 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20946 bool is_trailing_return)
20948 cp_decl_specifier_seq type_specifier_seq;
20949 cp_declarator *abstract_declarator;
20951 /* Parse the type-specifier-seq. */
20952 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20953 is_trailing_return,
20954 &type_specifier_seq);
20955 if (is_template_arg && type_specifier_seq.type
20956 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20957 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20958 /* A bare template name as a template argument is a template template
20959 argument, not a placeholder, so fail parsing it as a type argument. */
20961 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20962 cp_parser_simulate_error (parser);
20963 return error_mark_node;
20965 if (type_specifier_seq.type == error_mark_node)
20966 return error_mark_node;
20968 /* There might or might not be an abstract declarator. */
20969 cp_parser_parse_tentatively (parser);
20970 /* Look for the declarator. */
20971 abstract_declarator
20972 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20973 /*parenthesized_p=*/NULL,
20974 /*member_p=*/false,
20975 /*friend_p=*/false);
20976 /* Check to see if there really was a declarator. */
20977 if (!cp_parser_parse_definitely (parser))
20978 abstract_declarator = NULL;
20980 if (type_specifier_seq.type
20981 /* The concepts TS allows 'auto' as a type-id. */
20982 && (!flag_concepts || parser->in_type_id_in_expr_p)
20983 /* None of the valid uses of 'auto' in C++14 involve the type-id
20984 nonterminal, but it is valid in a trailing-return-type. */
20985 && !(cxx_dialect >= cxx14 && is_trailing_return))
20986 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20988 /* A type-id with type 'auto' is only ok if the abstract declarator
20989 is a function declarator with a late-specified return type.
20991 A type-id with 'auto' is also valid in a trailing-return-type
20992 in a compound-requirement. */
20993 if (abstract_declarator
20994 && abstract_declarator->kind == cdk_function
20995 && abstract_declarator->u.function.late_return_type)
20996 /* OK */;
20997 else if (parser->in_result_type_constraint_p)
20998 /* OK */;
20999 else
21001 location_t loc = type_specifier_seq.locations[ds_type_spec];
21002 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21004 error_at (loc, "missing template arguments after %qT",
21005 auto_node);
21006 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21007 tmpl);
21009 else
21010 error_at (loc, "invalid use of %qT", auto_node);
21011 return error_mark_node;
21015 return groktypename (&type_specifier_seq, abstract_declarator,
21016 is_template_arg);
21019 static tree
21020 cp_parser_type_id (cp_parser *parser)
21022 return cp_parser_type_id_1 (parser, false, false);
21025 static tree
21026 cp_parser_template_type_arg (cp_parser *parser)
21028 tree r;
21029 const char *saved_message = parser->type_definition_forbidden_message;
21030 parser->type_definition_forbidden_message
21031 = G_("types may not be defined in template arguments");
21032 r = cp_parser_type_id_1 (parser, true, false);
21033 parser->type_definition_forbidden_message = saved_message;
21034 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21036 error ("invalid use of %<auto%> in template argument");
21037 r = error_mark_node;
21039 return r;
21042 static tree
21043 cp_parser_trailing_type_id (cp_parser *parser)
21045 return cp_parser_type_id_1 (parser, false, true);
21048 /* Parse a type-specifier-seq.
21050 type-specifier-seq:
21051 type-specifier type-specifier-seq [opt]
21053 GNU extension:
21055 type-specifier-seq:
21056 attributes type-specifier-seq [opt]
21058 If IS_DECLARATION is true, we are at the start of a "condition" or
21059 exception-declaration, so we might be followed by a declarator-id.
21061 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21062 i.e. we've just seen "->".
21064 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21066 static void
21067 cp_parser_type_specifier_seq (cp_parser* parser,
21068 bool is_declaration,
21069 bool is_trailing_return,
21070 cp_decl_specifier_seq *type_specifier_seq)
21072 bool seen_type_specifier = false;
21073 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21074 cp_token *start_token = NULL;
21076 /* Clear the TYPE_SPECIFIER_SEQ. */
21077 clear_decl_specs (type_specifier_seq);
21079 /* In the context of a trailing return type, enum E { } is an
21080 elaborated-type-specifier followed by a function-body, not an
21081 enum-specifier. */
21082 if (is_trailing_return)
21083 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21085 /* Parse the type-specifiers and attributes. */
21086 while (true)
21088 tree type_specifier;
21089 bool is_cv_qualifier;
21091 /* Check for attributes first. */
21092 if (cp_next_tokens_can_be_attribute_p (parser))
21094 type_specifier_seq->attributes
21095 = attr_chainon (type_specifier_seq->attributes,
21096 cp_parser_attributes_opt (parser));
21097 continue;
21100 /* record the token of the beginning of the type specifier seq,
21101 for error reporting purposes*/
21102 if (!start_token)
21103 start_token = cp_lexer_peek_token (parser->lexer);
21105 /* Look for the type-specifier. */
21106 type_specifier = cp_parser_type_specifier (parser,
21107 flags,
21108 type_specifier_seq,
21109 /*is_declaration=*/false,
21110 NULL,
21111 &is_cv_qualifier);
21112 if (!type_specifier)
21114 /* If the first type-specifier could not be found, this is not a
21115 type-specifier-seq at all. */
21116 if (!seen_type_specifier)
21118 /* Set in_declarator_p to avoid skipping to the semicolon. */
21119 int in_decl = parser->in_declarator_p;
21120 parser->in_declarator_p = true;
21122 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21123 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21124 cp_parser_error (parser, "expected type-specifier");
21126 parser->in_declarator_p = in_decl;
21128 type_specifier_seq->type = error_mark_node;
21129 return;
21131 /* If subsequent type-specifiers could not be found, the
21132 type-specifier-seq is complete. */
21133 break;
21136 seen_type_specifier = true;
21137 /* The standard says that a condition can be:
21139 type-specifier-seq declarator = assignment-expression
21141 However, given:
21143 struct S {};
21144 if (int S = ...)
21146 we should treat the "S" as a declarator, not as a
21147 type-specifier. The standard doesn't say that explicitly for
21148 type-specifier-seq, but it does say that for
21149 decl-specifier-seq in an ordinary declaration. Perhaps it
21150 would be clearer just to allow a decl-specifier-seq here, and
21151 then add a semantic restriction that if any decl-specifiers
21152 that are not type-specifiers appear, the program is invalid. */
21153 if (is_declaration && !is_cv_qualifier)
21154 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21158 /* Return whether the function currently being declared has an associated
21159 template parameter list. */
21161 static bool
21162 function_being_declared_is_template_p (cp_parser* parser)
21164 if (!current_template_parms || processing_template_parmlist)
21165 return false;
21167 if (parser->implicit_template_scope)
21168 return true;
21170 if (at_class_scope_p ()
21171 && TYPE_BEING_DEFINED (current_class_type))
21172 return parser->num_template_parameter_lists != 0;
21174 return ((int) parser->num_template_parameter_lists > template_class_depth
21175 (current_class_type));
21178 /* Parse a parameter-declaration-clause.
21180 parameter-declaration-clause:
21181 parameter-declaration-list [opt] ... [opt]
21182 parameter-declaration-list , ...
21184 Returns a representation for the parameter declarations. A return
21185 value of NULL indicates a parameter-declaration-clause consisting
21186 only of an ellipsis. */
21188 static tree
21189 cp_parser_parameter_declaration_clause (cp_parser* parser)
21191 tree parameters;
21192 cp_token *token;
21193 bool ellipsis_p;
21194 bool is_error;
21196 temp_override<bool> cleanup
21197 (parser->auto_is_implicit_function_template_parm_p);
21199 if (!processing_specialization
21200 && !processing_template_parmlist
21201 && !processing_explicit_instantiation
21202 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21203 actual function or a random abstract declarator. */
21204 && parser->default_arg_ok_p)
21205 if (!current_function_decl
21206 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21207 parser->auto_is_implicit_function_template_parm_p = true;
21209 /* Peek at the next token. */
21210 token = cp_lexer_peek_token (parser->lexer);
21211 /* Check for trivial parameter-declaration-clauses. */
21212 if (token->type == CPP_ELLIPSIS)
21214 /* Consume the `...' token. */
21215 cp_lexer_consume_token (parser->lexer);
21216 return NULL_TREE;
21218 else if (token->type == CPP_CLOSE_PAREN)
21219 /* There are no parameters. */
21221 #ifndef NO_IMPLICIT_EXTERN_C
21222 if (in_system_header_at (input_location)
21223 && current_class_type == NULL
21224 && current_lang_name == lang_name_c)
21225 return NULL_TREE;
21226 else
21227 #endif
21228 return void_list_node;
21230 /* Check for `(void)', too, which is a special case. */
21231 else if (token->keyword == RID_VOID
21232 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21233 == CPP_CLOSE_PAREN))
21235 /* Consume the `void' token. */
21236 cp_lexer_consume_token (parser->lexer);
21237 /* There are no parameters. */
21238 return void_list_node;
21241 /* Parse the parameter-declaration-list. */
21242 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21243 /* If a parse error occurred while parsing the
21244 parameter-declaration-list, then the entire
21245 parameter-declaration-clause is erroneous. */
21246 if (is_error)
21247 return NULL;
21249 /* Peek at the next token. */
21250 token = cp_lexer_peek_token (parser->lexer);
21251 /* If it's a `,', the clause should terminate with an ellipsis. */
21252 if (token->type == CPP_COMMA)
21254 /* Consume the `,'. */
21255 cp_lexer_consume_token (parser->lexer);
21256 /* Expect an ellipsis. */
21257 ellipsis_p
21258 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21260 /* It might also be `...' if the optional trailing `,' was
21261 omitted. */
21262 else if (token->type == CPP_ELLIPSIS)
21264 /* Consume the `...' token. */
21265 cp_lexer_consume_token (parser->lexer);
21266 /* And remember that we saw it. */
21267 ellipsis_p = true;
21269 else
21270 ellipsis_p = false;
21272 /* Finish the parameter list. */
21273 if (!ellipsis_p)
21274 parameters = chainon (parameters, void_list_node);
21276 return parameters;
21279 /* Parse a parameter-declaration-list.
21281 parameter-declaration-list:
21282 parameter-declaration
21283 parameter-declaration-list , parameter-declaration
21285 Returns a representation of the parameter-declaration-list, as for
21286 cp_parser_parameter_declaration_clause. However, the
21287 `void_list_node' is never appended to the list. Upon return,
21288 *IS_ERROR will be true iff an error occurred. */
21290 static tree
21291 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21293 tree parameters = NULL_TREE;
21294 tree *tail = &parameters;
21295 bool saved_in_unbraced_linkage_specification_p;
21296 int index = 0;
21298 /* Assume all will go well. */
21299 *is_error = false;
21300 /* The special considerations that apply to a function within an
21301 unbraced linkage specifications do not apply to the parameters
21302 to the function. */
21303 saved_in_unbraced_linkage_specification_p
21304 = parser->in_unbraced_linkage_specification_p;
21305 parser->in_unbraced_linkage_specification_p = false;
21307 /* Look for more parameters. */
21308 while (true)
21310 cp_parameter_declarator *parameter;
21311 tree decl = error_mark_node;
21312 bool parenthesized_p = false;
21314 /* Parse the parameter. */
21315 parameter
21316 = cp_parser_parameter_declaration (parser,
21317 /*template_parm_p=*/false,
21318 &parenthesized_p);
21320 /* We don't know yet if the enclosing context is deprecated, so wait
21321 and warn in grokparms if appropriate. */
21322 deprecated_state = DEPRECATED_SUPPRESS;
21324 if (parameter)
21326 decl = grokdeclarator (parameter->declarator,
21327 &parameter->decl_specifiers,
21328 PARM,
21329 parameter->default_argument != NULL_TREE,
21330 &parameter->decl_specifiers.attributes);
21331 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21332 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21335 deprecated_state = DEPRECATED_NORMAL;
21337 /* If a parse error occurred parsing the parameter declaration,
21338 then the entire parameter-declaration-list is erroneous. */
21339 if (decl == error_mark_node)
21341 *is_error = true;
21342 parameters = error_mark_node;
21343 break;
21346 if (parameter->decl_specifiers.attributes)
21347 cplus_decl_attributes (&decl,
21348 parameter->decl_specifiers.attributes,
21350 if (DECL_NAME (decl))
21351 decl = pushdecl (decl);
21353 if (decl != error_mark_node)
21355 retrofit_lang_decl (decl);
21356 DECL_PARM_INDEX (decl) = ++index;
21357 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21360 /* Add the new parameter to the list. */
21361 *tail = build_tree_list (parameter->default_argument, decl);
21362 tail = &TREE_CHAIN (*tail);
21364 /* Peek at the next token. */
21365 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21366 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21367 /* These are for Objective-C++ */
21368 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21369 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21370 /* The parameter-declaration-list is complete. */
21371 break;
21372 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21374 cp_token *token;
21376 /* Peek at the next token. */
21377 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21378 /* If it's an ellipsis, then the list is complete. */
21379 if (token->type == CPP_ELLIPSIS)
21380 break;
21381 /* Otherwise, there must be more parameters. Consume the
21382 `,'. */
21383 cp_lexer_consume_token (parser->lexer);
21384 /* When parsing something like:
21386 int i(float f, double d)
21388 we can tell after seeing the declaration for "f" that we
21389 are not looking at an initialization of a variable "i",
21390 but rather at the declaration of a function "i".
21392 Due to the fact that the parsing of template arguments
21393 (as specified to a template-id) requires backtracking we
21394 cannot use this technique when inside a template argument
21395 list. */
21396 if (!parser->in_template_argument_list_p
21397 && !parser->in_type_id_in_expr_p
21398 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21399 /* However, a parameter-declaration of the form
21400 "float(f)" (which is a valid declaration of a
21401 parameter "f") can also be interpreted as an
21402 expression (the conversion of "f" to "float"). */
21403 && !parenthesized_p)
21404 cp_parser_commit_to_tentative_parse (parser);
21406 else
21408 cp_parser_error (parser, "expected %<,%> or %<...%>");
21409 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21410 cp_parser_skip_to_closing_parenthesis (parser,
21411 /*recovering=*/true,
21412 /*or_comma=*/false,
21413 /*consume_paren=*/false);
21414 break;
21418 parser->in_unbraced_linkage_specification_p
21419 = saved_in_unbraced_linkage_specification_p;
21421 /* Reset implicit_template_scope if we are about to leave the function
21422 parameter list that introduced it. Note that for out-of-line member
21423 definitions, there will be one or more class scopes before we get to
21424 the template parameter scope. */
21426 if (cp_binding_level *its = parser->implicit_template_scope)
21427 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21429 while (maybe_its->kind == sk_class)
21430 maybe_its = maybe_its->level_chain;
21431 if (maybe_its == its)
21433 parser->implicit_template_parms = 0;
21434 parser->implicit_template_scope = 0;
21438 return parameters;
21441 /* Parse a parameter declaration.
21443 parameter-declaration:
21444 decl-specifier-seq ... [opt] declarator
21445 decl-specifier-seq declarator = assignment-expression
21446 decl-specifier-seq ... [opt] abstract-declarator [opt]
21447 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21449 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21450 declares a template parameter. (In that case, a non-nested `>'
21451 token encountered during the parsing of the assignment-expression
21452 is not interpreted as a greater-than operator.)
21454 Returns a representation of the parameter, or NULL if an error
21455 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21456 true iff the declarator is of the form "(p)". */
21458 static cp_parameter_declarator *
21459 cp_parser_parameter_declaration (cp_parser *parser,
21460 bool template_parm_p,
21461 bool *parenthesized_p)
21463 int declares_class_or_enum;
21464 cp_decl_specifier_seq decl_specifiers;
21465 cp_declarator *declarator;
21466 tree default_argument;
21467 cp_token *token = NULL, *declarator_token_start = NULL;
21468 const char *saved_message;
21469 bool template_parameter_pack_p = false;
21471 /* In a template parameter, `>' is not an operator.
21473 [temp.param]
21475 When parsing a default template-argument for a non-type
21476 template-parameter, the first non-nested `>' is taken as the end
21477 of the template parameter-list rather than a greater-than
21478 operator. */
21480 /* Type definitions may not appear in parameter types. */
21481 saved_message = parser->type_definition_forbidden_message;
21482 parser->type_definition_forbidden_message
21483 = G_("types may not be defined in parameter types");
21485 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
21486 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21487 (current_template_parms)) : 0);
21489 /* Parse the declaration-specifiers. */
21490 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21491 cp_parser_decl_specifier_seq (parser,
21492 CP_PARSER_FLAGS_NONE,
21493 &decl_specifiers,
21494 &declares_class_or_enum);
21496 /* Complain about missing 'typename' or other invalid type names. */
21497 if (!decl_specifiers.any_type_specifiers_p
21498 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21499 decl_specifiers.type = error_mark_node;
21501 /* If an error occurred, there's no reason to attempt to parse the
21502 rest of the declaration. */
21503 if (cp_parser_error_occurred (parser))
21505 parser->type_definition_forbidden_message = saved_message;
21506 return NULL;
21509 /* Peek at the next token. */
21510 token = cp_lexer_peek_token (parser->lexer);
21512 /* If the next token is a `)', `,', `=', `>', or `...', then there
21513 is no declarator. However, when variadic templates are enabled,
21514 there may be a declarator following `...'. */
21515 if (token->type == CPP_CLOSE_PAREN
21516 || token->type == CPP_COMMA
21517 || token->type == CPP_EQ
21518 || token->type == CPP_GREATER)
21520 declarator = NULL;
21521 if (parenthesized_p)
21522 *parenthesized_p = false;
21524 /* Otherwise, there should be a declarator. */
21525 else
21527 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21528 parser->default_arg_ok_p = false;
21530 /* After seeing a decl-specifier-seq, if the next token is not a
21531 "(", there is no possibility that the code is a valid
21532 expression. Therefore, if parsing tentatively, we commit at
21533 this point. */
21534 if (!parser->in_template_argument_list_p
21535 /* In an expression context, having seen:
21537 (int((char ...
21539 we cannot be sure whether we are looking at a
21540 function-type (taking a "char" as a parameter) or a cast
21541 of some object of type "char" to "int". */
21542 && !parser->in_type_id_in_expr_p
21543 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21544 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21545 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21546 cp_parser_commit_to_tentative_parse (parser);
21547 /* Parse the declarator. */
21548 declarator_token_start = token;
21549 declarator = cp_parser_declarator (parser,
21550 CP_PARSER_DECLARATOR_EITHER,
21551 /*ctor_dtor_or_conv_p=*/NULL,
21552 parenthesized_p,
21553 /*member_p=*/false,
21554 /*friend_p=*/false);
21555 parser->default_arg_ok_p = saved_default_arg_ok_p;
21556 /* After the declarator, allow more attributes. */
21557 decl_specifiers.attributes
21558 = attr_chainon (decl_specifiers.attributes,
21559 cp_parser_attributes_opt (parser));
21561 /* If the declarator is a template parameter pack, remember that and
21562 clear the flag in the declarator itself so we don't get errors
21563 from grokdeclarator. */
21564 if (template_parm_p && declarator && declarator->parameter_pack_p)
21566 declarator->parameter_pack_p = false;
21567 template_parameter_pack_p = true;
21571 /* If the next token is an ellipsis, and we have not seen a declarator
21572 name, and if either the type of the declarator contains parameter
21573 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21574 for, eg, abbreviated integral type names), then we actually have a
21575 parameter pack expansion expression. Otherwise, leave the ellipsis
21576 for a C-style variadic function. */
21577 token = cp_lexer_peek_token (parser->lexer);
21579 /* If a function parameter pack was specified and an implicit template
21580 parameter was introduced during cp_parser_parameter_declaration,
21581 change any implicit parameters introduced into packs. */
21582 if (parser->implicit_template_parms
21583 && (token->type == CPP_ELLIPSIS
21584 || (declarator && declarator->parameter_pack_p)))
21586 int latest_template_parm_idx = TREE_VEC_LENGTH
21587 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21589 if (latest_template_parm_idx != template_parm_idx)
21590 decl_specifiers.type = convert_generic_types_to_packs
21591 (decl_specifiers.type,
21592 template_parm_idx, latest_template_parm_idx);
21595 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21597 tree type = decl_specifiers.type;
21599 if (type && DECL_P (type))
21600 type = TREE_TYPE (type);
21602 if (((type
21603 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21604 && (template_parm_p || uses_parameter_packs (type)))
21605 || (!type && template_parm_p))
21606 && declarator_can_be_parameter_pack (declarator))
21608 /* Consume the `...'. */
21609 cp_lexer_consume_token (parser->lexer);
21610 maybe_warn_variadic_templates ();
21612 /* Build a pack expansion type */
21613 if (template_parm_p)
21614 template_parameter_pack_p = true;
21615 else if (declarator)
21616 declarator->parameter_pack_p = true;
21617 else
21618 decl_specifiers.type = make_pack_expansion (type);
21622 /* The restriction on defining new types applies only to the type
21623 of the parameter, not to the default argument. */
21624 parser->type_definition_forbidden_message = saved_message;
21626 /* If the next token is `=', then process a default argument. */
21627 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21629 tree type = decl_specifiers.type;
21630 token = cp_lexer_peek_token (parser->lexer);
21631 /* If we are defining a class, then the tokens that make up the
21632 default argument must be saved and processed later. */
21633 if (!template_parm_p && at_class_scope_p ()
21634 && TYPE_BEING_DEFINED (current_class_type)
21635 && !LAMBDA_TYPE_P (current_class_type))
21636 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21638 // A constrained-type-specifier may declare a type template-parameter.
21639 else if (declares_constrained_type_template_parameter (type))
21640 default_argument
21641 = cp_parser_default_type_template_argument (parser);
21643 // A constrained-type-specifier may declare a template-template-parameter.
21644 else if (declares_constrained_template_template_parameter (type))
21645 default_argument
21646 = cp_parser_default_template_template_argument (parser);
21648 /* Outside of a class definition, we can just parse the
21649 assignment-expression. */
21650 else
21651 default_argument
21652 = cp_parser_default_argument (parser, template_parm_p);
21654 if (!parser->default_arg_ok_p)
21656 permerror (token->location,
21657 "default arguments are only "
21658 "permitted for function parameters");
21660 else if ((declarator && declarator->parameter_pack_p)
21661 || template_parameter_pack_p
21662 || (decl_specifiers.type
21663 && PACK_EXPANSION_P (decl_specifiers.type)))
21665 /* Find the name of the parameter pack. */
21666 cp_declarator *id_declarator = declarator;
21667 while (id_declarator && id_declarator->kind != cdk_id)
21668 id_declarator = id_declarator->declarator;
21670 if (id_declarator && id_declarator->kind == cdk_id)
21671 error_at (declarator_token_start->location,
21672 template_parm_p
21673 ? G_("template parameter pack %qD "
21674 "cannot have a default argument")
21675 : G_("parameter pack %qD cannot have "
21676 "a default argument"),
21677 id_declarator->u.id.unqualified_name);
21678 else
21679 error_at (declarator_token_start->location,
21680 template_parm_p
21681 ? G_("template parameter pack cannot have "
21682 "a default argument")
21683 : G_("parameter pack cannot have a "
21684 "default argument"));
21686 default_argument = NULL_TREE;
21689 else
21690 default_argument = NULL_TREE;
21692 /* Generate a location for the parameter, ranging from the start of the
21693 initial token to the end of the final token (using input_location for
21694 the latter, set up by cp_lexer_set_source_position_from_token when
21695 consuming tokens).
21697 If we have a identifier, then use it for the caret location, e.g.
21699 extern int callee (int one, int (*two)(int, int), float three);
21700 ~~~~~~^~~~~~~~~~~~~~
21702 otherwise, reuse the start location for the caret location e.g.:
21704 extern int callee (int one, int (*)(int, int), float three);
21705 ^~~~~~~~~~~~~~~~~
21708 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21709 ? declarator->id_loc
21710 : decl_spec_token_start->location);
21711 location_t param_loc = make_location (caret_loc,
21712 decl_spec_token_start->location,
21713 input_location);
21715 return make_parameter_declarator (&decl_specifiers,
21716 declarator,
21717 default_argument,
21718 param_loc,
21719 template_parameter_pack_p);
21722 /* Parse a default argument and return it.
21724 TEMPLATE_PARM_P is true if this is a default argument for a
21725 non-type template parameter. */
21726 static tree
21727 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21729 tree default_argument = NULL_TREE;
21730 bool saved_greater_than_is_operator_p;
21731 bool saved_local_variables_forbidden_p;
21732 bool non_constant_p, is_direct_init;
21734 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21735 set correctly. */
21736 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21737 parser->greater_than_is_operator_p = !template_parm_p;
21738 /* Local variable names (and the `this' keyword) may not
21739 appear in a default argument. */
21740 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21741 parser->local_variables_forbidden_p = true;
21742 /* Parse the assignment-expression. */
21743 if (template_parm_p)
21744 push_deferring_access_checks (dk_no_deferred);
21745 tree saved_class_ptr = NULL_TREE;
21746 tree saved_class_ref = NULL_TREE;
21747 /* The "this" pointer is not valid in a default argument. */
21748 if (cfun)
21750 saved_class_ptr = current_class_ptr;
21751 cp_function_chain->x_current_class_ptr = NULL_TREE;
21752 saved_class_ref = current_class_ref;
21753 cp_function_chain->x_current_class_ref = NULL_TREE;
21755 default_argument
21756 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21757 /* Restore the "this" pointer. */
21758 if (cfun)
21760 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21761 cp_function_chain->x_current_class_ref = saved_class_ref;
21763 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21764 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21765 if (template_parm_p)
21766 pop_deferring_access_checks ();
21767 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21768 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21770 return default_argument;
21773 /* Parse a function-body.
21775 function-body:
21776 compound_statement */
21778 static void
21779 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21781 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21782 ? BCS_TRY_BLOCK : BCS_NORMAL),
21783 true);
21786 /* Parse a ctor-initializer-opt followed by a function-body. Return
21787 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21788 is true we are parsing a function-try-block. */
21790 static void
21791 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21792 bool in_function_try_block)
21794 tree body, list;
21795 const bool check_body_p =
21796 DECL_CONSTRUCTOR_P (current_function_decl)
21797 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21798 tree last = NULL;
21800 /* Begin the function body. */
21801 body = begin_function_body ();
21802 /* Parse the optional ctor-initializer. */
21803 cp_parser_ctor_initializer_opt (parser);
21805 /* If we're parsing a constexpr constructor definition, we need
21806 to check that the constructor body is indeed empty. However,
21807 before we get to cp_parser_function_body lot of junk has been
21808 generated, so we can't just check that we have an empty block.
21809 Rather we take a snapshot of the outermost block, and check whether
21810 cp_parser_function_body changed its state. */
21811 if (check_body_p)
21813 list = cur_stmt_list;
21814 if (STATEMENT_LIST_TAIL (list))
21815 last = STATEMENT_LIST_TAIL (list)->stmt;
21817 /* Parse the function-body. */
21818 cp_parser_function_body (parser, in_function_try_block);
21819 if (check_body_p)
21820 check_constexpr_ctor_body (last, list, /*complain=*/true);
21821 /* Finish the function body. */
21822 finish_function_body (body);
21825 /* Parse an initializer.
21827 initializer:
21828 = initializer-clause
21829 ( expression-list )
21831 Returns an expression representing the initializer. If no
21832 initializer is present, NULL_TREE is returned.
21834 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21835 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21836 set to TRUE if there is no initializer present. If there is an
21837 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21838 is set to true; otherwise it is set to false. */
21840 static tree
21841 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21842 bool* non_constant_p, bool subexpression_p)
21844 cp_token *token;
21845 tree init;
21847 /* Peek at the next token. */
21848 token = cp_lexer_peek_token (parser->lexer);
21850 /* Let our caller know whether or not this initializer was
21851 parenthesized. */
21852 *is_direct_init = (token->type != CPP_EQ);
21853 /* Assume that the initializer is constant. */
21854 *non_constant_p = false;
21856 if (token->type == CPP_EQ)
21858 /* Consume the `='. */
21859 cp_lexer_consume_token (parser->lexer);
21860 /* Parse the initializer-clause. */
21861 init = cp_parser_initializer_clause (parser, non_constant_p);
21863 else if (token->type == CPP_OPEN_PAREN)
21865 vec<tree, va_gc> *vec;
21866 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21867 /*cast_p=*/false,
21868 /*allow_expansion_p=*/true,
21869 non_constant_p);
21870 if (vec == NULL)
21871 return error_mark_node;
21872 init = build_tree_list_vec (vec);
21873 release_tree_vector (vec);
21875 else if (token->type == CPP_OPEN_BRACE)
21877 cp_lexer_set_source_position (parser->lexer);
21878 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21879 init = cp_parser_braced_list (parser, non_constant_p);
21880 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21882 else
21884 /* Anything else is an error. */
21885 cp_parser_error (parser, "expected initializer");
21886 init = error_mark_node;
21889 if (!subexpression_p && check_for_bare_parameter_packs (init))
21890 init = error_mark_node;
21892 return init;
21895 /* Parse an initializer-clause.
21897 initializer-clause:
21898 assignment-expression
21899 braced-init-list
21901 Returns an expression representing the initializer.
21903 If the `assignment-expression' production is used the value
21904 returned is simply a representation for the expression.
21906 Otherwise, calls cp_parser_braced_list. */
21908 static cp_expr
21909 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21911 cp_expr initializer;
21913 /* Assume the expression is constant. */
21914 *non_constant_p = false;
21916 /* If it is not a `{', then we are looking at an
21917 assignment-expression. */
21918 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21920 initializer
21921 = cp_parser_constant_expression (parser,
21922 /*allow_non_constant_p=*/true,
21923 non_constant_p);
21925 else
21926 initializer = cp_parser_braced_list (parser, non_constant_p);
21928 return initializer;
21931 /* Parse a brace-enclosed initializer list.
21933 braced-init-list:
21934 { initializer-list , [opt] }
21935 { designated-initializer-list , [opt] }
21938 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21939 the elements of the initializer-list (or NULL, if the last
21940 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21941 NULL_TREE. There is no way to detect whether or not the optional
21942 trailing `,' was provided. NON_CONSTANT_P is as for
21943 cp_parser_initializer. */
21945 static cp_expr
21946 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21948 tree initializer;
21949 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21951 /* Consume the `{' token. */
21952 matching_braces braces;
21953 braces.require_open (parser);
21954 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21955 initializer = make_node (CONSTRUCTOR);
21956 /* If it's not a `}', then there is a non-trivial initializer. */
21957 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21959 /* Parse the initializer list. */
21960 CONSTRUCTOR_ELTS (initializer)
21961 = cp_parser_initializer_list (parser, non_constant_p);
21962 /* A trailing `,' token is allowed. */
21963 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21964 cp_lexer_consume_token (parser->lexer);
21966 else
21967 *non_constant_p = false;
21968 /* Now, there should be a trailing `}'. */
21969 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21970 braces.require_close (parser);
21971 TREE_TYPE (initializer) = init_list_type_node;
21973 cp_expr result (initializer);
21974 /* Build a location of the form:
21975 { ... }
21976 ^~~~~~~
21977 with caret==start at the open brace, finish at the close brace. */
21978 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21979 result.set_location (combined_loc);
21980 return result;
21983 /* Consume tokens up to, and including, the next non-nested closing `]'.
21984 Returns true iff we found a closing `]'. */
21986 static bool
21987 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21989 unsigned square_depth = 0;
21991 while (true)
21993 cp_token * token = cp_lexer_peek_token (parser->lexer);
21995 switch (token->type)
21997 case CPP_EOF:
21998 case CPP_PRAGMA_EOL:
21999 /* If we've run out of tokens, then there is no closing `]'. */
22000 return false;
22002 case CPP_OPEN_SQUARE:
22003 ++square_depth;
22004 break;
22006 case CPP_CLOSE_SQUARE:
22007 if (!square_depth--)
22009 cp_lexer_consume_token (parser->lexer);
22010 return true;
22012 break;
22014 default:
22015 break;
22018 /* Consume the token. */
22019 cp_lexer_consume_token (parser->lexer);
22023 /* Return true if we are looking at an array-designator, false otherwise. */
22025 static bool
22026 cp_parser_array_designator_p (cp_parser *parser)
22028 /* Consume the `['. */
22029 cp_lexer_consume_token (parser->lexer);
22031 cp_lexer_save_tokens (parser->lexer);
22033 /* Skip tokens until the next token is a closing square bracket.
22034 If we find the closing `]', and the next token is a `=', then
22035 we are looking at an array designator. */
22036 bool array_designator_p
22037 = (cp_parser_skip_to_closing_square_bracket (parser)
22038 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22040 /* Roll back the tokens we skipped. */
22041 cp_lexer_rollback_tokens (parser->lexer);
22043 return array_designator_p;
22046 /* Parse an initializer-list.
22048 initializer-list:
22049 initializer-clause ... [opt]
22050 initializer-list , initializer-clause ... [opt]
22052 C++2A Extension:
22054 designated-initializer-list:
22055 designated-initializer-clause
22056 designated-initializer-list , designated-initializer-clause
22058 designated-initializer-clause:
22059 designator brace-or-equal-initializer
22061 designator:
22062 . identifier
22064 GNU Extension:
22066 initializer-list:
22067 designation initializer-clause ...[opt]
22068 initializer-list , designation initializer-clause ...[opt]
22070 designation:
22071 . identifier =
22072 identifier :
22073 [ constant-expression ] =
22075 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22076 for the initializer. If the INDEX of the elt is non-NULL, it is the
22077 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22078 as for cp_parser_initializer. */
22080 static vec<constructor_elt, va_gc> *
22081 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22083 vec<constructor_elt, va_gc> *v = NULL;
22084 bool first_p = true;
22085 tree first_designator = NULL_TREE;
22087 /* Assume all of the expressions are constant. */
22088 *non_constant_p = false;
22090 /* Parse the rest of the list. */
22091 while (true)
22093 cp_token *token;
22094 tree designator;
22095 tree initializer;
22096 bool clause_non_constant_p;
22097 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22099 /* Handle the C++2A syntax, '. id ='. */
22100 if ((cxx_dialect >= cxx2a
22101 || cp_parser_allow_gnu_extensions_p (parser))
22102 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22103 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22104 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22105 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22106 == CPP_OPEN_BRACE)))
22108 if (cxx_dialect < cxx2a)
22109 pedwarn (loc, OPT_Wpedantic,
22110 "C++ designated initializers only available with "
22111 "-std=c++2a or -std=gnu++2a");
22112 /* Consume the `.'. */
22113 cp_lexer_consume_token (parser->lexer);
22114 /* Consume the identifier. */
22115 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22116 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22117 /* Consume the `='. */
22118 cp_lexer_consume_token (parser->lexer);
22120 /* Also, if the next token is an identifier and the following one is a
22121 colon, we are looking at the GNU designated-initializer
22122 syntax. */
22123 else if (cp_parser_allow_gnu_extensions_p (parser)
22124 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22125 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22126 == CPP_COLON))
22128 /* Warn the user that they are using an extension. */
22129 pedwarn (loc, OPT_Wpedantic,
22130 "ISO C++ does not allow GNU designated initializers");
22131 /* Consume the identifier. */
22132 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22133 /* Consume the `:'. */
22134 cp_lexer_consume_token (parser->lexer);
22136 /* Also handle C99 array designators, '[ const ] ='. */
22137 else if (cp_parser_allow_gnu_extensions_p (parser)
22138 && !c_dialect_objc ()
22139 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22141 /* In C++11, [ could start a lambda-introducer. */
22142 bool non_const = false;
22144 cp_parser_parse_tentatively (parser);
22146 if (!cp_parser_array_designator_p (parser))
22148 cp_parser_simulate_error (parser);
22149 designator = NULL_TREE;
22151 else
22153 designator = cp_parser_constant_expression (parser, true,
22154 &non_const);
22155 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22156 cp_parser_require (parser, CPP_EQ, RT_EQ);
22159 if (!cp_parser_parse_definitely (parser))
22160 designator = NULL_TREE;
22161 else if (non_const
22162 && (!require_potential_rvalue_constant_expression
22163 (designator)))
22164 designator = NULL_TREE;
22165 if (designator)
22166 /* Warn the user that they are using an extension. */
22167 pedwarn (loc, OPT_Wpedantic,
22168 "ISO C++ does not allow C99 designated initializers");
22170 else
22171 designator = NULL_TREE;
22173 if (first_p)
22175 first_designator = designator;
22176 first_p = false;
22178 else if (cxx_dialect >= cxx2a
22179 && first_designator != error_mark_node
22180 && (!first_designator != !designator))
22182 error_at (loc, "either all initializer clauses should be designated "
22183 "or none of them should be");
22184 first_designator = error_mark_node;
22186 else if (cxx_dialect < cxx2a && !first_designator)
22187 first_designator = designator;
22189 /* Parse the initializer. */
22190 initializer = cp_parser_initializer_clause (parser,
22191 &clause_non_constant_p);
22192 /* If any clause is non-constant, so is the entire initializer. */
22193 if (clause_non_constant_p)
22194 *non_constant_p = true;
22196 /* If we have an ellipsis, this is an initializer pack
22197 expansion. */
22198 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22200 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22202 /* Consume the `...'. */
22203 cp_lexer_consume_token (parser->lexer);
22205 if (designator && cxx_dialect >= cxx2a)
22206 error_at (loc,
22207 "%<...%> not allowed in designated initializer list");
22209 /* Turn the initializer into an initializer expansion. */
22210 initializer = make_pack_expansion (initializer);
22213 /* Add it to the vector. */
22214 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22216 /* If the next token is not a comma, we have reached the end of
22217 the list. */
22218 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22219 break;
22221 /* Peek at the next token. */
22222 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22223 /* If the next token is a `}', then we're still done. An
22224 initializer-clause can have a trailing `,' after the
22225 initializer-list and before the closing `}'. */
22226 if (token->type == CPP_CLOSE_BRACE)
22227 break;
22229 /* Consume the `,' token. */
22230 cp_lexer_consume_token (parser->lexer);
22233 /* The same identifier shall not appear in multiple designators
22234 of a designated-initializer-list. */
22235 if (first_designator)
22237 unsigned int i;
22238 tree designator, val;
22239 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22240 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22242 if (IDENTIFIER_MARKED (designator))
22244 error_at (EXPR_LOC_OR_LOC (val, input_location),
22245 "%<.%s%> designator used multiple times in "
22246 "the same initializer list",
22247 IDENTIFIER_POINTER (designator));
22248 (*v)[i].index = NULL_TREE;
22250 else
22251 IDENTIFIER_MARKED (designator) = 1;
22253 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22254 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22255 IDENTIFIER_MARKED (designator) = 0;
22258 return v;
22261 /* Classes [gram.class] */
22263 /* Parse a class-name.
22265 class-name:
22266 identifier
22267 template-id
22269 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22270 to indicate that names looked up in dependent types should be
22271 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22272 keyword has been used to indicate that the name that appears next
22273 is a template. TAG_TYPE indicates the explicit tag given before
22274 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22275 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22276 is the class being defined in a class-head. If ENUM_OK is TRUE,
22277 enum-names are also accepted.
22279 Returns the TYPE_DECL representing the class. */
22281 static tree
22282 cp_parser_class_name (cp_parser *parser,
22283 bool typename_keyword_p,
22284 bool template_keyword_p,
22285 enum tag_types tag_type,
22286 bool check_dependency_p,
22287 bool class_head_p,
22288 bool is_declaration,
22289 bool enum_ok)
22291 tree decl;
22292 tree scope;
22293 bool typename_p;
22294 cp_token *token;
22295 tree identifier = NULL_TREE;
22297 /* All class-names start with an identifier. */
22298 token = cp_lexer_peek_token (parser->lexer);
22299 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22301 cp_parser_error (parser, "expected class-name");
22302 return error_mark_node;
22305 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22306 to a template-id, so we save it here. */
22307 scope = parser->scope;
22308 if (scope == error_mark_node)
22309 return error_mark_node;
22311 /* Any name names a type if we're following the `typename' keyword
22312 in a qualified name where the enclosing scope is type-dependent. */
22313 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22314 && dependent_type_p (scope));
22315 /* Handle the common case (an identifier, but not a template-id)
22316 efficiently. */
22317 if (token->type == CPP_NAME
22318 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22320 cp_token *identifier_token;
22321 bool ambiguous_p;
22323 /* Look for the identifier. */
22324 identifier_token = cp_lexer_peek_token (parser->lexer);
22325 ambiguous_p = identifier_token->error_reported;
22326 identifier = cp_parser_identifier (parser);
22327 /* If the next token isn't an identifier, we are certainly not
22328 looking at a class-name. */
22329 if (identifier == error_mark_node)
22330 decl = error_mark_node;
22331 /* If we know this is a type-name, there's no need to look it
22332 up. */
22333 else if (typename_p)
22334 decl = identifier;
22335 else
22337 tree ambiguous_decls;
22338 /* If we already know that this lookup is ambiguous, then
22339 we've already issued an error message; there's no reason
22340 to check again. */
22341 if (ambiguous_p)
22343 cp_parser_simulate_error (parser);
22344 return error_mark_node;
22346 /* If the next token is a `::', then the name must be a type
22347 name.
22349 [basic.lookup.qual]
22351 During the lookup for a name preceding the :: scope
22352 resolution operator, object, function, and enumerator
22353 names are ignored. */
22354 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22355 tag_type = scope_type;
22356 /* Look up the name. */
22357 decl = cp_parser_lookup_name (parser, identifier,
22358 tag_type,
22359 /*is_template=*/false,
22360 /*is_namespace=*/false,
22361 check_dependency_p,
22362 &ambiguous_decls,
22363 identifier_token->location);
22364 if (ambiguous_decls)
22366 if (cp_parser_parsing_tentatively (parser))
22367 cp_parser_simulate_error (parser);
22368 return error_mark_node;
22372 else
22374 /* Try a template-id. */
22375 decl = cp_parser_template_id (parser, template_keyword_p,
22376 check_dependency_p,
22377 tag_type,
22378 is_declaration);
22379 if (decl == error_mark_node)
22380 return error_mark_node;
22383 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22385 /* If this is a typename, create a TYPENAME_TYPE. */
22386 if (typename_p && decl != error_mark_node)
22388 decl = make_typename_type (scope, decl, typename_type,
22389 /*complain=*/tf_error);
22390 if (decl != error_mark_node)
22391 decl = TYPE_NAME (decl);
22394 decl = strip_using_decl (decl);
22396 /* Check to see that it is really the name of a class. */
22397 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22398 && identifier_p (TREE_OPERAND (decl, 0))
22399 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22400 /* Situations like this:
22402 template <typename T> struct A {
22403 typename T::template X<int>::I i;
22406 are problematic. Is `T::template X<int>' a class-name? The
22407 standard does not seem to be definitive, but there is no other
22408 valid interpretation of the following `::'. Therefore, those
22409 names are considered class-names. */
22411 decl = make_typename_type (scope, decl, tag_type, tf_error);
22412 if (decl != error_mark_node)
22413 decl = TYPE_NAME (decl);
22415 else if (TREE_CODE (decl) != TYPE_DECL
22416 || TREE_TYPE (decl) == error_mark_node
22417 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22418 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22419 /* In Objective-C 2.0, a classname followed by '.' starts a
22420 dot-syntax expression, and it's not a type-name. */
22421 || (c_dialect_objc ()
22422 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22423 && objc_is_class_name (decl)))
22424 decl = error_mark_node;
22426 if (decl == error_mark_node)
22427 cp_parser_error (parser, "expected class-name");
22428 else if (identifier && !parser->scope)
22429 maybe_note_name_used_in_class (identifier, decl);
22431 return decl;
22434 /* Parse a class-specifier.
22436 class-specifier:
22437 class-head { member-specification [opt] }
22439 Returns the TREE_TYPE representing the class. */
22441 static tree
22442 cp_parser_class_specifier_1 (cp_parser* parser)
22444 tree type;
22445 tree attributes = NULL_TREE;
22446 bool nested_name_specifier_p;
22447 unsigned saved_num_template_parameter_lists;
22448 bool saved_in_function_body;
22449 unsigned char in_statement;
22450 bool in_switch_statement_p;
22451 bool saved_in_unbraced_linkage_specification_p;
22452 tree old_scope = NULL_TREE;
22453 tree scope = NULL_TREE;
22454 cp_token *closing_brace;
22456 push_deferring_access_checks (dk_no_deferred);
22458 /* Parse the class-head. */
22459 type = cp_parser_class_head (parser,
22460 &nested_name_specifier_p);
22461 /* If the class-head was a semantic disaster, skip the entire body
22462 of the class. */
22463 if (!type)
22465 cp_parser_skip_to_end_of_block_or_statement (parser);
22466 pop_deferring_access_checks ();
22467 return error_mark_node;
22470 /* Look for the `{'. */
22471 matching_braces braces;
22472 if (!braces.require_open (parser))
22474 pop_deferring_access_checks ();
22475 return error_mark_node;
22478 cp_ensure_no_omp_declare_simd (parser);
22479 cp_ensure_no_oacc_routine (parser);
22481 /* Issue an error message if type-definitions are forbidden here. */
22482 cp_parser_check_type_definition (parser);
22483 /* Remember that we are defining one more class. */
22484 ++parser->num_classes_being_defined;
22485 /* Inside the class, surrounding template-parameter-lists do not
22486 apply. */
22487 saved_num_template_parameter_lists
22488 = parser->num_template_parameter_lists;
22489 parser->num_template_parameter_lists = 0;
22490 /* We are not in a function body. */
22491 saved_in_function_body = parser->in_function_body;
22492 parser->in_function_body = false;
22493 /* Or in a loop. */
22494 in_statement = parser->in_statement;
22495 parser->in_statement = 0;
22496 /* Or in a switch. */
22497 in_switch_statement_p = parser->in_switch_statement_p;
22498 parser->in_switch_statement_p = false;
22499 /* We are not immediately inside an extern "lang" block. */
22500 saved_in_unbraced_linkage_specification_p
22501 = parser->in_unbraced_linkage_specification_p;
22502 parser->in_unbraced_linkage_specification_p = false;
22504 // Associate constraints with the type.
22505 if (flag_concepts)
22506 type = associate_classtype_constraints (type);
22508 /* Start the class. */
22509 if (nested_name_specifier_p)
22511 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22512 old_scope = push_inner_scope (scope);
22514 type = begin_class_definition (type);
22516 if (type == error_mark_node)
22517 /* If the type is erroneous, skip the entire body of the class. */
22518 cp_parser_skip_to_closing_brace (parser);
22519 else
22520 /* Parse the member-specification. */
22521 cp_parser_member_specification_opt (parser);
22523 /* Look for the trailing `}'. */
22524 closing_brace = braces.require_close (parser);
22525 /* Look for trailing attributes to apply to this class. */
22526 if (cp_parser_allow_gnu_extensions_p (parser))
22527 attributes = cp_parser_gnu_attributes_opt (parser);
22528 if (type != error_mark_node)
22529 type = finish_struct (type, attributes);
22530 if (nested_name_specifier_p)
22531 pop_inner_scope (old_scope, scope);
22533 /* We've finished a type definition. Check for the common syntax
22534 error of forgetting a semicolon after the definition. We need to
22535 be careful, as we can't just check for not-a-semicolon and be done
22536 with it; the user might have typed:
22538 class X { } c = ...;
22539 class X { } *p = ...;
22541 and so forth. Instead, enumerate all the possible tokens that
22542 might follow this production; if we don't see one of them, then
22543 complain and silently insert the semicolon. */
22545 cp_token *token = cp_lexer_peek_token (parser->lexer);
22546 bool want_semicolon = true;
22548 if (cp_next_tokens_can_be_std_attribute_p (parser))
22549 /* Don't try to parse c++11 attributes here. As per the
22550 grammar, that should be a task for
22551 cp_parser_decl_specifier_seq. */
22552 want_semicolon = false;
22554 switch (token->type)
22556 case CPP_NAME:
22557 case CPP_SEMICOLON:
22558 case CPP_MULT:
22559 case CPP_AND:
22560 case CPP_OPEN_PAREN:
22561 case CPP_CLOSE_PAREN:
22562 case CPP_COMMA:
22563 want_semicolon = false;
22564 break;
22566 /* While it's legal for type qualifiers and storage class
22567 specifiers to follow type definitions in the grammar, only
22568 compiler testsuites contain code like that. Assume that if
22569 we see such code, then what we're really seeing is a case
22570 like:
22572 class X { }
22573 const <type> var = ...;
22577 class Y { }
22578 static <type> func (...) ...
22580 i.e. the qualifier or specifier applies to the next
22581 declaration. To do so, however, we need to look ahead one
22582 more token to see if *that* token is a type specifier.
22584 This code could be improved to handle:
22586 class Z { }
22587 static const <type> var = ...; */
22588 case CPP_KEYWORD:
22589 if (keyword_is_decl_specifier (token->keyword))
22591 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22593 /* Handling user-defined types here would be nice, but very
22594 tricky. */
22595 want_semicolon
22596 = (lookahead->type == CPP_KEYWORD
22597 && keyword_begins_type_specifier (lookahead->keyword));
22599 break;
22600 default:
22601 break;
22604 /* If we don't have a type, then something is very wrong and we
22605 shouldn't try to do anything clever. Likewise for not seeing the
22606 closing brace. */
22607 if (closing_brace && TYPE_P (type) && want_semicolon)
22609 /* Locate the closing brace. */
22610 cp_token_position prev
22611 = cp_lexer_previous_token_position (parser->lexer);
22612 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22613 location_t loc = prev_token->location;
22615 /* We want to suggest insertion of a ';' immediately *after* the
22616 closing brace, so, if we can, offset the location by 1 column. */
22617 location_t next_loc = loc;
22618 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22619 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22621 rich_location richloc (line_table, next_loc);
22623 /* If we successfully offset the location, suggest the fix-it. */
22624 if (next_loc != loc)
22625 richloc.add_fixit_insert_before (next_loc, ";");
22627 if (CLASSTYPE_DECLARED_CLASS (type))
22628 error_at (&richloc,
22629 "expected %<;%> after class definition");
22630 else if (TREE_CODE (type) == RECORD_TYPE)
22631 error_at (&richloc,
22632 "expected %<;%> after struct definition");
22633 else if (TREE_CODE (type) == UNION_TYPE)
22634 error_at (&richloc,
22635 "expected %<;%> after union definition");
22636 else
22637 gcc_unreachable ();
22639 /* Unget one token and smash it to look as though we encountered
22640 a semicolon in the input stream. */
22641 cp_lexer_set_token_position (parser->lexer, prev);
22642 token = cp_lexer_peek_token (parser->lexer);
22643 token->type = CPP_SEMICOLON;
22644 token->keyword = RID_MAX;
22648 /* If this class is not itself within the scope of another class,
22649 then we need to parse the bodies of all of the queued function
22650 definitions. Note that the queued functions defined in a class
22651 are not always processed immediately following the
22652 class-specifier for that class. Consider:
22654 struct A {
22655 struct B { void f() { sizeof (A); } };
22658 If `f' were processed before the processing of `A' were
22659 completed, there would be no way to compute the size of `A'.
22660 Note that the nesting we are interested in here is lexical --
22661 not the semantic nesting given by TYPE_CONTEXT. In particular,
22662 for:
22664 struct A { struct B; };
22665 struct A::B { void f() { } };
22667 there is no need to delay the parsing of `A::B::f'. */
22668 if (--parser->num_classes_being_defined == 0)
22670 tree decl;
22671 tree class_type = NULL_TREE;
22672 tree pushed_scope = NULL_TREE;
22673 unsigned ix;
22674 cp_default_arg_entry *e;
22675 tree save_ccp, save_ccr;
22677 if (any_erroneous_template_args_p (type))
22679 /* Skip default arguments, NSDMIs, etc, in order to improve
22680 error recovery (c++/71169, c++/71832). */
22681 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22682 vec_safe_truncate (unparsed_nsdmis, 0);
22683 vec_safe_truncate (unparsed_classes, 0);
22684 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22687 /* In a first pass, parse default arguments to the functions.
22688 Then, in a second pass, parse the bodies of the functions.
22689 This two-phased approach handles cases like:
22691 struct S {
22692 void f() { g(); }
22693 void g(int i = 3);
22697 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22699 decl = e->decl;
22700 /* If there are default arguments that have not yet been processed,
22701 take care of them now. */
22702 if (class_type != e->class_type)
22704 if (pushed_scope)
22705 pop_scope (pushed_scope);
22706 class_type = e->class_type;
22707 pushed_scope = push_scope (class_type);
22709 /* Make sure that any template parameters are in scope. */
22710 maybe_begin_member_template_processing (decl);
22711 /* Parse the default argument expressions. */
22712 cp_parser_late_parsing_default_args (parser, decl);
22713 /* Remove any template parameters from the symbol table. */
22714 maybe_end_member_template_processing ();
22716 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22717 /* Now parse any NSDMIs. */
22718 save_ccp = current_class_ptr;
22719 save_ccr = current_class_ref;
22720 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22722 if (class_type != DECL_CONTEXT (decl))
22724 if (pushed_scope)
22725 pop_scope (pushed_scope);
22726 class_type = DECL_CONTEXT (decl);
22727 pushed_scope = push_scope (class_type);
22729 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22730 cp_parser_late_parsing_nsdmi (parser, decl);
22732 vec_safe_truncate (unparsed_nsdmis, 0);
22733 current_class_ptr = save_ccp;
22734 current_class_ref = save_ccr;
22735 if (pushed_scope)
22736 pop_scope (pushed_scope);
22738 /* Now do some post-NSDMI bookkeeping. */
22739 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22740 after_nsdmi_defaulted_late_checks (class_type);
22741 vec_safe_truncate (unparsed_classes, 0);
22742 after_nsdmi_defaulted_late_checks (type);
22744 /* Now parse the body of the functions. */
22745 if (flag_openmp)
22747 /* OpenMP UDRs need to be parsed before all other functions. */
22748 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22749 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22750 cp_parser_late_parsing_for_member (parser, decl);
22751 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22752 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22753 cp_parser_late_parsing_for_member (parser, decl);
22755 else
22756 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22757 cp_parser_late_parsing_for_member (parser, decl);
22758 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22760 else
22761 vec_safe_push (unparsed_classes, type);
22763 /* Put back any saved access checks. */
22764 pop_deferring_access_checks ();
22766 /* Restore saved state. */
22767 parser->in_switch_statement_p = in_switch_statement_p;
22768 parser->in_statement = in_statement;
22769 parser->in_function_body = saved_in_function_body;
22770 parser->num_template_parameter_lists
22771 = saved_num_template_parameter_lists;
22772 parser->in_unbraced_linkage_specification_p
22773 = saved_in_unbraced_linkage_specification_p;
22775 return type;
22778 static tree
22779 cp_parser_class_specifier (cp_parser* parser)
22781 tree ret;
22782 timevar_push (TV_PARSE_STRUCT);
22783 ret = cp_parser_class_specifier_1 (parser);
22784 timevar_pop (TV_PARSE_STRUCT);
22785 return ret;
22788 /* Parse a class-head.
22790 class-head:
22791 class-key identifier [opt] base-clause [opt]
22792 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22793 class-key nested-name-specifier [opt] template-id
22794 base-clause [opt]
22796 class-virt-specifier:
22797 final
22799 GNU Extensions:
22800 class-key attributes identifier [opt] base-clause [opt]
22801 class-key attributes nested-name-specifier identifier base-clause [opt]
22802 class-key attributes nested-name-specifier [opt] template-id
22803 base-clause [opt]
22805 Upon return BASES is initialized to the list of base classes (or
22806 NULL, if there are none) in the same form returned by
22807 cp_parser_base_clause.
22809 Returns the TYPE of the indicated class. Sets
22810 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22811 involving a nested-name-specifier was used, and FALSE otherwise.
22813 Returns error_mark_node if this is not a class-head.
22815 Returns NULL_TREE if the class-head is syntactically valid, but
22816 semantically invalid in a way that means we should skip the entire
22817 body of the class. */
22819 static tree
22820 cp_parser_class_head (cp_parser* parser,
22821 bool* nested_name_specifier_p)
22823 tree nested_name_specifier;
22824 enum tag_types class_key;
22825 tree id = NULL_TREE;
22826 tree type = NULL_TREE;
22827 tree attributes;
22828 tree bases;
22829 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22830 bool template_id_p = false;
22831 bool qualified_p = false;
22832 bool invalid_nested_name_p = false;
22833 bool invalid_explicit_specialization_p = false;
22834 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22835 tree pushed_scope = NULL_TREE;
22836 unsigned num_templates;
22837 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22838 /* Assume no nested-name-specifier will be present. */
22839 *nested_name_specifier_p = false;
22840 /* Assume no template parameter lists will be used in defining the
22841 type. */
22842 num_templates = 0;
22843 parser->colon_corrects_to_scope_p = false;
22845 /* Look for the class-key. */
22846 class_key = cp_parser_class_key (parser);
22847 if (class_key == none_type)
22848 return error_mark_node;
22850 location_t class_head_start_location = input_location;
22852 /* Parse the attributes. */
22853 attributes = cp_parser_attributes_opt (parser);
22855 /* If the next token is `::', that is invalid -- but sometimes
22856 people do try to write:
22858 struct ::S {};
22860 Handle this gracefully by accepting the extra qualifier, and then
22861 issuing an error about it later if this really is a
22862 class-head. If it turns out just to be an elaborated type
22863 specifier, remain silent. */
22864 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22865 qualified_p = true;
22867 push_deferring_access_checks (dk_no_check);
22869 /* Determine the name of the class. Begin by looking for an
22870 optional nested-name-specifier. */
22871 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22872 nested_name_specifier
22873 = cp_parser_nested_name_specifier_opt (parser,
22874 /*typename_keyword_p=*/false,
22875 /*check_dependency_p=*/false,
22876 /*type_p=*/true,
22877 /*is_declaration=*/false);
22878 /* If there was a nested-name-specifier, then there *must* be an
22879 identifier. */
22881 cp_token *bad_template_keyword = NULL;
22883 if (nested_name_specifier)
22885 type_start_token = cp_lexer_peek_token (parser->lexer);
22886 /* Although the grammar says `identifier', it really means
22887 `class-name' or `template-name'. You are only allowed to
22888 define a class that has already been declared with this
22889 syntax.
22891 The proposed resolution for Core Issue 180 says that wherever
22892 you see `class T::X' you should treat `X' as a type-name.
22894 It is OK to define an inaccessible class; for example:
22896 class A { class B; };
22897 class A::B {};
22899 We do not know if we will see a class-name, or a
22900 template-name. We look for a class-name first, in case the
22901 class-name is a template-id; if we looked for the
22902 template-name first we would stop after the template-name. */
22903 cp_parser_parse_tentatively (parser);
22904 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22905 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
22906 type = cp_parser_class_name (parser,
22907 /*typename_keyword_p=*/false,
22908 /*template_keyword_p=*/false,
22909 class_type,
22910 /*check_dependency_p=*/false,
22911 /*class_head_p=*/true,
22912 /*is_declaration=*/false);
22913 /* If that didn't work, ignore the nested-name-specifier. */
22914 if (!cp_parser_parse_definitely (parser))
22916 invalid_nested_name_p = true;
22917 type_start_token = cp_lexer_peek_token (parser->lexer);
22918 id = cp_parser_identifier (parser);
22919 if (id == error_mark_node)
22920 id = NULL_TREE;
22922 /* If we could not find a corresponding TYPE, treat this
22923 declaration like an unqualified declaration. */
22924 if (type == error_mark_node)
22925 nested_name_specifier = NULL_TREE;
22926 /* Otherwise, count the number of templates used in TYPE and its
22927 containing scopes. */
22928 else
22929 num_templates = num_template_headers_for_class (TREE_TYPE (type));
22931 /* Otherwise, the identifier is optional. */
22932 else
22934 /* We don't know whether what comes next is a template-id,
22935 an identifier, or nothing at all. */
22936 cp_parser_parse_tentatively (parser);
22937 /* Check for a template-id. */
22938 type_start_token = cp_lexer_peek_token (parser->lexer);
22939 id = cp_parser_template_id (parser,
22940 /*template_keyword_p=*/false,
22941 /*check_dependency_p=*/true,
22942 class_key,
22943 /*is_declaration=*/true);
22944 /* If that didn't work, it could still be an identifier. */
22945 if (!cp_parser_parse_definitely (parser))
22947 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22949 type_start_token = cp_lexer_peek_token (parser->lexer);
22950 id = cp_parser_identifier (parser);
22952 else
22953 id = NULL_TREE;
22955 else
22957 template_id_p = true;
22958 ++num_templates;
22962 pop_deferring_access_checks ();
22964 if (id)
22966 cp_parser_check_for_invalid_template_id (parser, id,
22967 class_key,
22968 type_start_token->location);
22970 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22972 /* If it's not a `:' or a `{' then we can't really be looking at a
22973 class-head, since a class-head only appears as part of a
22974 class-specifier. We have to detect this situation before calling
22975 xref_tag, since that has irreversible side-effects. */
22976 if (!cp_parser_next_token_starts_class_definition_p (parser))
22978 cp_parser_error (parser, "expected %<{%> or %<:%>");
22979 type = error_mark_node;
22980 goto out;
22983 /* At this point, we're going ahead with the class-specifier, even
22984 if some other problem occurs. */
22985 cp_parser_commit_to_tentative_parse (parser);
22986 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22988 cp_parser_error (parser,
22989 "cannot specify %<override%> for a class");
22990 type = error_mark_node;
22991 goto out;
22993 /* Issue the error about the overly-qualified name now. */
22994 if (qualified_p)
22996 cp_parser_error (parser,
22997 "global qualification of class name is invalid");
22998 type = error_mark_node;
22999 goto out;
23001 else if (invalid_nested_name_p)
23003 cp_parser_error (parser,
23004 "qualified name does not name a class");
23005 type = error_mark_node;
23006 goto out;
23008 else if (nested_name_specifier)
23010 tree scope;
23012 if (bad_template_keyword)
23013 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23014 keyword template shall not appear at the top level. */
23015 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23016 "keyword %<template%> not allowed in class-head-name");
23018 /* Reject typedef-names in class heads. */
23019 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23021 error_at (type_start_token->location,
23022 "invalid class name in declaration of %qD",
23023 type);
23024 type = NULL_TREE;
23025 goto done;
23028 /* Figure out in what scope the declaration is being placed. */
23029 scope = current_scope ();
23030 /* If that scope does not contain the scope in which the
23031 class was originally declared, the program is invalid. */
23032 if (scope && !is_ancestor (scope, nested_name_specifier))
23034 if (at_namespace_scope_p ())
23035 error_at (type_start_token->location,
23036 "declaration of %qD in namespace %qD which does not "
23037 "enclose %qD",
23038 type, scope, nested_name_specifier);
23039 else
23040 error_at (type_start_token->location,
23041 "declaration of %qD in %qD which does not enclose %qD",
23042 type, scope, nested_name_specifier);
23043 type = NULL_TREE;
23044 goto done;
23046 /* [dcl.meaning]
23048 A declarator-id shall not be qualified except for the
23049 definition of a ... nested class outside of its class
23050 ... [or] the definition or explicit instantiation of a
23051 class member of a namespace outside of its namespace. */
23052 if (scope == nested_name_specifier)
23054 permerror (nested_name_specifier_token_start->location,
23055 "extra qualification not allowed");
23056 nested_name_specifier = NULL_TREE;
23057 num_templates = 0;
23060 /* An explicit-specialization must be preceded by "template <>". If
23061 it is not, try to recover gracefully. */
23062 if (at_namespace_scope_p ()
23063 && parser->num_template_parameter_lists == 0
23064 && !processing_template_parmlist
23065 && template_id_p)
23067 /* Build a location of this form:
23068 struct typename <ARGS>
23069 ^~~~~~~~~~~~~~~~~~~~~~
23070 with caret==start at the start token, and
23071 finishing at the end of the type. */
23072 location_t reported_loc
23073 = make_location (class_head_start_location,
23074 class_head_start_location,
23075 get_finish (type_start_token->location));
23076 rich_location richloc (line_table, reported_loc);
23077 richloc.add_fixit_insert_before (class_head_start_location,
23078 "template <> ");
23079 error_at (&richloc,
23080 "an explicit specialization must be preceded by"
23081 " %<template <>%>");
23082 invalid_explicit_specialization_p = true;
23083 /* Take the same action that would have been taken by
23084 cp_parser_explicit_specialization. */
23085 ++parser->num_template_parameter_lists;
23086 begin_specialization ();
23088 /* There must be no "return" statements between this point and the
23089 end of this function; set "type "to the correct return value and
23090 use "goto done;" to return. */
23091 /* Make sure that the right number of template parameters were
23092 present. */
23093 if (!cp_parser_check_template_parameters (parser, num_templates,
23094 template_id_p,
23095 type_start_token->location,
23096 /*declarator=*/NULL))
23098 /* If something went wrong, there is no point in even trying to
23099 process the class-definition. */
23100 type = NULL_TREE;
23101 goto done;
23104 /* Look up the type. */
23105 if (template_id_p)
23107 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23108 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23109 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23111 error_at (type_start_token->location,
23112 "function template %qD redeclared as a class template", id);
23113 type = error_mark_node;
23115 else
23117 type = TREE_TYPE (id);
23118 type = maybe_process_partial_specialization (type);
23120 /* Check the scope while we still know whether or not we had a
23121 nested-name-specifier. */
23122 if (type != error_mark_node)
23123 check_unqualified_spec_or_inst (type, type_start_token->location);
23125 if (nested_name_specifier)
23126 pushed_scope = push_scope (nested_name_specifier);
23128 else if (nested_name_specifier)
23130 tree class_type;
23132 /* Given:
23134 template <typename T> struct S { struct T };
23135 template <typename T> struct S<T>::T { };
23137 we will get a TYPENAME_TYPE when processing the definition of
23138 `S::T'. We need to resolve it to the actual type before we
23139 try to define it. */
23140 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23142 class_type = resolve_typename_type (TREE_TYPE (type),
23143 /*only_current_p=*/false);
23144 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23145 type = TYPE_NAME (class_type);
23146 else
23148 cp_parser_error (parser, "could not resolve typename type");
23149 type = error_mark_node;
23153 if (maybe_process_partial_specialization (TREE_TYPE (type))
23154 == error_mark_node)
23156 type = NULL_TREE;
23157 goto done;
23160 class_type = current_class_type;
23161 /* Enter the scope indicated by the nested-name-specifier. */
23162 pushed_scope = push_scope (nested_name_specifier);
23163 /* Get the canonical version of this type. */
23164 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23165 /* Call push_template_decl if it seems like we should be defining a
23166 template either from the template headers or the type we're
23167 defining, so that we diagnose both extra and missing headers. */
23168 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23169 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23170 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23172 type = push_template_decl (type);
23173 if (type == error_mark_node)
23175 type = NULL_TREE;
23176 goto done;
23180 type = TREE_TYPE (type);
23181 *nested_name_specifier_p = true;
23183 else /* The name is not a nested name. */
23185 /* If the class was unnamed, create a dummy name. */
23186 if (!id)
23187 id = make_anon_name ();
23188 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23189 ? ts_within_enclosing_non_class
23190 : ts_current);
23191 type = xref_tag (class_key, id, tag_scope,
23192 parser->num_template_parameter_lists);
23195 /* Indicate whether this class was declared as a `class' or as a
23196 `struct'. */
23197 if (TREE_CODE (type) == RECORD_TYPE)
23198 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23199 cp_parser_check_class_key (class_key, type);
23201 /* If this type was already complete, and we see another definition,
23202 that's an error. */
23203 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23205 error_at (type_start_token->location, "redefinition of %q#T",
23206 type);
23207 inform (location_of (type), "previous definition of %q#T",
23208 type);
23209 type = NULL_TREE;
23210 goto done;
23212 else if (type == error_mark_node)
23213 type = NULL_TREE;
23215 if (type)
23217 /* Apply attributes now, before any use of the class as a template
23218 argument in its base list. */
23219 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23220 fixup_attribute_variants (type);
23223 /* We will have entered the scope containing the class; the names of
23224 base classes should be looked up in that context. For example:
23226 struct A { struct B {}; struct C; };
23227 struct A::C : B {};
23229 is valid. */
23231 /* Get the list of base-classes, if there is one. */
23232 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23234 /* PR59482: enter the class scope so that base-specifiers are looked
23235 up correctly. */
23236 if (type)
23237 pushclass (type);
23238 bases = cp_parser_base_clause (parser);
23239 /* PR59482: get out of the previously pushed class scope so that the
23240 subsequent pops pop the right thing. */
23241 if (type)
23242 popclass ();
23244 else
23245 bases = NULL_TREE;
23247 /* If we're really defining a class, process the base classes.
23248 If they're invalid, fail. */
23249 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23250 xref_basetypes (type, bases);
23252 done:
23253 /* Leave the scope given by the nested-name-specifier. We will
23254 enter the class scope itself while processing the members. */
23255 if (pushed_scope)
23256 pop_scope (pushed_scope);
23258 if (invalid_explicit_specialization_p)
23260 end_specialization ();
23261 --parser->num_template_parameter_lists;
23264 if (type)
23265 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23266 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23267 CLASSTYPE_FINAL (type) = 1;
23268 out:
23269 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23270 return type;
23273 /* Parse a class-key.
23275 class-key:
23276 class
23277 struct
23278 union
23280 Returns the kind of class-key specified, or none_type to indicate
23281 error. */
23283 static enum tag_types
23284 cp_parser_class_key (cp_parser* parser)
23286 cp_token *token;
23287 enum tag_types tag_type;
23289 /* Look for the class-key. */
23290 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23291 if (!token)
23292 return none_type;
23294 /* Check to see if the TOKEN is a class-key. */
23295 tag_type = cp_parser_token_is_class_key (token);
23296 if (!tag_type)
23297 cp_parser_error (parser, "expected class-key");
23298 return tag_type;
23301 /* Parse a type-parameter-key.
23303 type-parameter-key:
23304 class
23305 typename
23308 static void
23309 cp_parser_type_parameter_key (cp_parser* parser)
23311 /* Look for the type-parameter-key. */
23312 enum tag_types tag_type = none_type;
23313 cp_token *token = cp_lexer_peek_token (parser->lexer);
23314 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23316 cp_lexer_consume_token (parser->lexer);
23317 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23318 /* typename is not allowed in a template template parameter
23319 by the standard until C++17. */
23320 pedwarn (token->location, OPT_Wpedantic,
23321 "ISO C++ forbids typename key in template template parameter;"
23322 " use -std=c++17 or -std=gnu++17");
23324 else
23325 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23327 return;
23330 /* Parse an (optional) member-specification.
23332 member-specification:
23333 member-declaration member-specification [opt]
23334 access-specifier : member-specification [opt] */
23336 static void
23337 cp_parser_member_specification_opt (cp_parser* parser)
23339 while (true)
23341 cp_token *token;
23342 enum rid keyword;
23344 /* Peek at the next token. */
23345 token = cp_lexer_peek_token (parser->lexer);
23346 /* If it's a `}', or EOF then we've seen all the members. */
23347 if (token->type == CPP_CLOSE_BRACE
23348 || token->type == CPP_EOF
23349 || token->type == CPP_PRAGMA_EOL)
23350 break;
23352 /* See if this token is a keyword. */
23353 keyword = token->keyword;
23354 switch (keyword)
23356 case RID_PUBLIC:
23357 case RID_PROTECTED:
23358 case RID_PRIVATE:
23359 /* Consume the access-specifier. */
23360 cp_lexer_consume_token (parser->lexer);
23361 /* Remember which access-specifier is active. */
23362 current_access_specifier = token->u.value;
23363 /* Look for the `:'. */
23364 cp_parser_require (parser, CPP_COLON, RT_COLON);
23365 break;
23367 default:
23368 /* Accept #pragmas at class scope. */
23369 if (token->type == CPP_PRAGMA)
23371 cp_parser_pragma (parser, pragma_member, NULL);
23372 break;
23375 /* Otherwise, the next construction must be a
23376 member-declaration. */
23377 cp_parser_member_declaration (parser);
23382 /* Parse a member-declaration.
23384 member-declaration:
23385 decl-specifier-seq [opt] member-declarator-list [opt] ;
23386 function-definition ; [opt]
23387 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23388 using-declaration
23389 template-declaration
23390 alias-declaration
23392 member-declarator-list:
23393 member-declarator
23394 member-declarator-list , member-declarator
23396 member-declarator:
23397 declarator pure-specifier [opt]
23398 declarator constant-initializer [opt]
23399 identifier [opt] : constant-expression
23401 GNU Extensions:
23403 member-declaration:
23404 __extension__ member-declaration
23406 member-declarator:
23407 declarator attributes [opt] pure-specifier [opt]
23408 declarator attributes [opt] constant-initializer [opt]
23409 identifier [opt] attributes [opt] : constant-expression
23411 C++0x Extensions:
23413 member-declaration:
23414 static_assert-declaration */
23416 static void
23417 cp_parser_member_declaration (cp_parser* parser)
23419 cp_decl_specifier_seq decl_specifiers;
23420 tree prefix_attributes;
23421 tree decl;
23422 int declares_class_or_enum;
23423 bool friend_p;
23424 cp_token *token = NULL;
23425 cp_token *decl_spec_token_start = NULL;
23426 cp_token *initializer_token_start = NULL;
23427 int saved_pedantic;
23428 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23430 /* Check for the `__extension__' keyword. */
23431 if (cp_parser_extension_opt (parser, &saved_pedantic))
23433 /* Recurse. */
23434 cp_parser_member_declaration (parser);
23435 /* Restore the old value of the PEDANTIC flag. */
23436 pedantic = saved_pedantic;
23438 return;
23441 /* Check for a template-declaration. */
23442 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23444 /* An explicit specialization here is an error condition, and we
23445 expect the specialization handler to detect and report this. */
23446 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23447 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23448 cp_parser_explicit_specialization (parser);
23449 else
23450 cp_parser_template_declaration (parser, /*member_p=*/true);
23452 return;
23454 /* Check for a template introduction. */
23455 else if (cp_parser_template_declaration_after_export (parser, true))
23456 return;
23458 /* Check for a using-declaration. */
23459 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23461 if (cxx_dialect < cxx11)
23463 /* Parse the using-declaration. */
23464 cp_parser_using_declaration (parser,
23465 /*access_declaration_p=*/false);
23466 return;
23468 else
23470 tree decl;
23471 bool alias_decl_expected;
23472 cp_parser_parse_tentatively (parser);
23473 decl = cp_parser_alias_declaration (parser);
23474 /* Note that if we actually see the '=' token after the
23475 identifier, cp_parser_alias_declaration commits the
23476 tentative parse. In that case, we really expect an
23477 alias-declaration. Otherwise, we expect a using
23478 declaration. */
23479 alias_decl_expected =
23480 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23481 cp_parser_parse_definitely (parser);
23483 if (alias_decl_expected)
23484 finish_member_declaration (decl);
23485 else
23486 cp_parser_using_declaration (parser,
23487 /*access_declaration_p=*/false);
23488 return;
23492 /* Check for @defs. */
23493 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23495 tree ivar, member;
23496 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23497 ivar = ivar_chains;
23498 while (ivar)
23500 member = ivar;
23501 ivar = TREE_CHAIN (member);
23502 TREE_CHAIN (member) = NULL_TREE;
23503 finish_member_declaration (member);
23505 return;
23508 /* If the next token is `static_assert' we have a static assertion. */
23509 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23511 cp_parser_static_assert (parser, /*member_p=*/true);
23512 return;
23515 parser->colon_corrects_to_scope_p = false;
23517 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23518 goto out;
23520 /* Parse the decl-specifier-seq. */
23521 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23522 cp_parser_decl_specifier_seq (parser,
23523 CP_PARSER_FLAGS_OPTIONAL,
23524 &decl_specifiers,
23525 &declares_class_or_enum);
23526 /* Check for an invalid type-name. */
23527 if (!decl_specifiers.any_type_specifiers_p
23528 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23529 goto out;
23530 /* If there is no declarator, then the decl-specifier-seq should
23531 specify a type. */
23532 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23534 /* If there was no decl-specifier-seq, and the next token is a
23535 `;', then we have something like:
23537 struct S { ; };
23539 [class.mem]
23541 Each member-declaration shall declare at least one member
23542 name of the class. */
23543 if (!decl_specifiers.any_specifiers_p)
23545 cp_token *token = cp_lexer_peek_token (parser->lexer);
23546 if (!in_system_header_at (token->location))
23548 gcc_rich_location richloc (token->location);
23549 richloc.add_fixit_remove ();
23550 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23553 else
23555 tree type;
23557 /* See if this declaration is a friend. */
23558 friend_p = cp_parser_friend_p (&decl_specifiers);
23559 /* If there were decl-specifiers, check to see if there was
23560 a class-declaration. */
23561 type = check_tag_decl (&decl_specifiers,
23562 /*explicit_type_instantiation_p=*/false);
23563 /* Nested classes have already been added to the class, but
23564 a `friend' needs to be explicitly registered. */
23565 if (friend_p)
23567 /* If the `friend' keyword was present, the friend must
23568 be introduced with a class-key. */
23569 if (!declares_class_or_enum && cxx_dialect < cxx11)
23570 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23571 "in C++03 a class-key must be used "
23572 "when declaring a friend");
23573 /* In this case:
23575 template <typename T> struct A {
23576 friend struct A<T>::B;
23579 A<T>::B will be represented by a TYPENAME_TYPE, and
23580 therefore not recognized by check_tag_decl. */
23581 if (!type)
23583 type = decl_specifiers.type;
23584 if (type && TREE_CODE (type) == TYPE_DECL)
23585 type = TREE_TYPE (type);
23587 if (!type || !TYPE_P (type))
23588 error_at (decl_spec_token_start->location,
23589 "friend declaration does not name a class or "
23590 "function");
23591 else
23592 make_friend_class (current_class_type, type,
23593 /*complain=*/true);
23595 /* If there is no TYPE, an error message will already have
23596 been issued. */
23597 else if (!type || type == error_mark_node)
23599 /* An anonymous aggregate has to be handled specially; such
23600 a declaration really declares a data member (with a
23601 particular type), as opposed to a nested class. */
23602 else if (ANON_AGGR_TYPE_P (type))
23604 /* C++11 9.5/6. */
23605 if (decl_specifiers.storage_class != sc_none)
23606 error_at (decl_spec_token_start->location,
23607 "a storage class on an anonymous aggregate "
23608 "in class scope is not allowed");
23610 /* Remove constructors and such from TYPE, now that we
23611 know it is an anonymous aggregate. */
23612 fixup_anonymous_aggr (type);
23613 /* And make the corresponding data member. */
23614 decl = build_decl (decl_spec_token_start->location,
23615 FIELD_DECL, NULL_TREE, type);
23616 /* Add it to the class. */
23617 finish_member_declaration (decl);
23619 else
23620 cp_parser_check_access_in_redeclaration
23621 (TYPE_NAME (type),
23622 decl_spec_token_start->location);
23625 else
23627 bool assume_semicolon = false;
23629 /* Clear attributes from the decl_specifiers but keep them
23630 around as prefix attributes that apply them to the entity
23631 being declared. */
23632 prefix_attributes = decl_specifiers.attributes;
23633 decl_specifiers.attributes = NULL_TREE;
23635 /* See if these declarations will be friends. */
23636 friend_p = cp_parser_friend_p (&decl_specifiers);
23638 /* Keep going until we hit the `;' at the end of the
23639 declaration. */
23640 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23642 tree attributes = NULL_TREE;
23643 tree first_attribute;
23644 tree initializer;
23645 bool named_bitfld = false;
23647 /* Peek at the next token. */
23648 token = cp_lexer_peek_token (parser->lexer);
23650 /* The following code wants to know early if it is a bit-field
23651 or some other declaration. Attributes can appear before
23652 the `:' token. Skip over them without consuming any tokens
23653 to peek if they are followed by `:'. */
23654 if (cp_next_tokens_can_be_attribute_p (parser)
23655 || (token->type == CPP_NAME
23656 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23657 && (named_bitfld = true)))
23659 size_t n
23660 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
23661 token = cp_lexer_peek_nth_token (parser->lexer, n);
23664 /* Check for a bitfield declaration. */
23665 if (token->type == CPP_COLON
23666 || (token->type == CPP_NAME
23667 && token == cp_lexer_peek_token (parser->lexer)
23668 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23669 && (named_bitfld = true)))
23671 tree identifier;
23672 tree width;
23673 tree late_attributes = NULL_TREE;
23675 if (named_bitfld)
23676 identifier = cp_parser_identifier (parser);
23677 else
23678 identifier = NULL_TREE;
23680 /* Look for attributes that apply to the bitfield. */
23681 attributes = cp_parser_attributes_opt (parser);
23683 /* Consume the `:' token. */
23684 cp_lexer_consume_token (parser->lexer);
23686 /* Get the width of the bitfield. */
23687 width = cp_parser_constant_expression (parser, false, NULL,
23688 cxx_dialect >= cxx11);
23690 /* In C++2A and as extension for C++11 and above we allow
23691 default member initializers for bit-fields. */
23692 initializer = NULL_TREE;
23693 if (cxx_dialect >= cxx11
23694 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23695 || cp_lexer_next_token_is (parser->lexer,
23696 CPP_OPEN_BRACE)))
23698 location_t loc
23699 = cp_lexer_peek_token (parser->lexer)->location;
23700 if (cxx_dialect < cxx2a
23701 && !in_system_header_at (loc)
23702 && identifier != NULL_TREE)
23703 pedwarn (loc, 0,
23704 "default member initializers for bit-fields "
23705 "only available with -std=c++2a or "
23706 "-std=gnu++2a");
23708 initializer = cp_parser_save_nsdmi (parser);
23709 if (identifier == NULL_TREE)
23711 error_at (loc, "default member initializer for "
23712 "unnamed bit-field");
23713 initializer = NULL_TREE;
23716 else
23718 /* Look for attributes that apply to the bitfield after
23719 the `:' token and width. This is where GCC used to
23720 parse attributes in the past, pedwarn if there is
23721 a std attribute. */
23722 if (cp_next_tokens_can_be_std_attribute_p (parser))
23723 pedwarn (input_location, OPT_Wpedantic,
23724 "ISO C++ allows bit-field attributes only "
23725 "before the %<:%> token");
23727 late_attributes = cp_parser_attributes_opt (parser);
23730 attributes = attr_chainon (attributes, late_attributes);
23732 /* Remember which attributes are prefix attributes and
23733 which are not. */
23734 first_attribute = attributes;
23735 /* Combine the attributes. */
23736 attributes = attr_chainon (prefix_attributes, attributes);
23738 /* Create the bitfield declaration. */
23739 decl = grokbitfield (identifier
23740 ? make_id_declarator (NULL_TREE,
23741 identifier,
23742 sfk_none)
23743 : NULL,
23744 &decl_specifiers,
23745 width, initializer,
23746 attributes);
23748 else
23750 cp_declarator *declarator;
23751 tree asm_specification;
23752 int ctor_dtor_or_conv_p;
23754 /* Parse the declarator. */
23755 declarator
23756 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23757 &ctor_dtor_or_conv_p,
23758 /*parenthesized_p=*/NULL,
23759 /*member_p=*/true,
23760 friend_p);
23762 /* If something went wrong parsing the declarator, make sure
23763 that we at least consume some tokens. */
23764 if (declarator == cp_error_declarator)
23766 /* Skip to the end of the statement. */
23767 cp_parser_skip_to_end_of_statement (parser);
23768 /* If the next token is not a semicolon, that is
23769 probably because we just skipped over the body of
23770 a function. So, we consume a semicolon if
23771 present, but do not issue an error message if it
23772 is not present. */
23773 if (cp_lexer_next_token_is (parser->lexer,
23774 CPP_SEMICOLON))
23775 cp_lexer_consume_token (parser->lexer);
23776 goto out;
23779 if (declares_class_or_enum & 2)
23780 cp_parser_check_for_definition_in_return_type
23781 (declarator, decl_specifiers.type,
23782 decl_specifiers.locations[ds_type_spec]);
23784 /* Look for an asm-specification. */
23785 asm_specification = cp_parser_asm_specification_opt (parser);
23786 /* Look for attributes that apply to the declaration. */
23787 attributes = cp_parser_attributes_opt (parser);
23788 /* Remember which attributes are prefix attributes and
23789 which are not. */
23790 first_attribute = attributes;
23791 /* Combine the attributes. */
23792 attributes = attr_chainon (prefix_attributes, attributes);
23794 /* If it's an `=', then we have a constant-initializer or a
23795 pure-specifier. It is not correct to parse the
23796 initializer before registering the member declaration
23797 since the member declaration should be in scope while
23798 its initializer is processed. However, the rest of the
23799 front end does not yet provide an interface that allows
23800 us to handle this correctly. */
23801 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23803 /* In [class.mem]:
23805 A pure-specifier shall be used only in the declaration of
23806 a virtual function.
23808 A member-declarator can contain a constant-initializer
23809 only if it declares a static member of integral or
23810 enumeration type.
23812 Therefore, if the DECLARATOR is for a function, we look
23813 for a pure-specifier; otherwise, we look for a
23814 constant-initializer. When we call `grokfield', it will
23815 perform more stringent semantics checks. */
23816 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23817 if (function_declarator_p (declarator)
23818 || (decl_specifiers.type
23819 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23820 && declarator->kind == cdk_id
23821 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23822 == FUNCTION_TYPE)))
23823 initializer = cp_parser_pure_specifier (parser);
23824 else if (decl_specifiers.storage_class != sc_static)
23825 initializer = cp_parser_save_nsdmi (parser);
23826 else if (cxx_dialect >= cxx11)
23828 bool nonconst;
23829 /* Don't require a constant rvalue in C++11, since we
23830 might want a reference constant. We'll enforce
23831 constancy later. */
23832 cp_lexer_consume_token (parser->lexer);
23833 /* Parse the initializer. */
23834 initializer = cp_parser_initializer_clause (parser,
23835 &nonconst);
23837 else
23838 /* Parse the initializer. */
23839 initializer = cp_parser_constant_initializer (parser);
23841 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23842 && !function_declarator_p (declarator))
23844 bool x;
23845 if (decl_specifiers.storage_class != sc_static)
23846 initializer = cp_parser_save_nsdmi (parser);
23847 else
23848 initializer = cp_parser_initializer (parser, &x, &x);
23850 /* Otherwise, there is no initializer. */
23851 else
23852 initializer = NULL_TREE;
23854 /* See if we are probably looking at a function
23855 definition. We are certainly not looking at a
23856 member-declarator. Calling `grokfield' has
23857 side-effects, so we must not do it unless we are sure
23858 that we are looking at a member-declarator. */
23859 if (cp_parser_token_starts_function_definition_p
23860 (cp_lexer_peek_token (parser->lexer)))
23862 /* The grammar does not allow a pure-specifier to be
23863 used when a member function is defined. (It is
23864 possible that this fact is an oversight in the
23865 standard, since a pure function may be defined
23866 outside of the class-specifier. */
23867 if (initializer && initializer_token_start)
23868 error_at (initializer_token_start->location,
23869 "pure-specifier on function-definition");
23870 decl = cp_parser_save_member_function_body (parser,
23871 &decl_specifiers,
23872 declarator,
23873 attributes);
23874 if (parser->fully_implicit_function_template_p)
23875 decl = finish_fully_implicit_template (parser, decl);
23876 /* If the member was not a friend, declare it here. */
23877 if (!friend_p)
23878 finish_member_declaration (decl);
23879 /* Peek at the next token. */
23880 token = cp_lexer_peek_token (parser->lexer);
23881 /* If the next token is a semicolon, consume it. */
23882 if (token->type == CPP_SEMICOLON)
23884 location_t semicolon_loc
23885 = cp_lexer_consume_token (parser->lexer)->location;
23886 gcc_rich_location richloc (semicolon_loc);
23887 richloc.add_fixit_remove ();
23888 warning_at (&richloc, OPT_Wextra_semi,
23889 "extra %<;%> after in-class "
23890 "function definition");
23892 goto out;
23894 else
23895 if (declarator->kind == cdk_function)
23896 declarator->id_loc = token->location;
23897 /* Create the declaration. */
23898 decl = grokfield (declarator, &decl_specifiers,
23899 initializer, /*init_const_expr_p=*/true,
23900 asm_specification, attributes);
23901 if (parser->fully_implicit_function_template_p)
23903 if (friend_p)
23904 finish_fully_implicit_template (parser, 0);
23905 else
23906 decl = finish_fully_implicit_template (parser, decl);
23910 cp_finalize_omp_declare_simd (parser, decl);
23911 cp_finalize_oacc_routine (parser, decl, false);
23913 /* Reset PREFIX_ATTRIBUTES. */
23914 if (attributes != error_mark_node)
23916 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23917 attributes = TREE_CHAIN (attributes);
23918 if (attributes)
23919 TREE_CHAIN (attributes) = NULL_TREE;
23922 /* If there is any qualification still in effect, clear it
23923 now; we will be starting fresh with the next declarator. */
23924 parser->scope = NULL_TREE;
23925 parser->qualifying_scope = NULL_TREE;
23926 parser->object_scope = NULL_TREE;
23927 /* If it's a `,', then there are more declarators. */
23928 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23930 cp_lexer_consume_token (parser->lexer);
23931 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23933 cp_token *token = cp_lexer_previous_token (parser->lexer);
23934 gcc_rich_location richloc (token->location);
23935 richloc.add_fixit_remove ();
23936 error_at (&richloc, "stray %<,%> at end of "
23937 "member declaration");
23940 /* If the next token isn't a `;', then we have a parse error. */
23941 else if (cp_lexer_next_token_is_not (parser->lexer,
23942 CPP_SEMICOLON))
23944 /* The next token might be a ways away from where the
23945 actual semicolon is missing. Find the previous token
23946 and use that for our error position. */
23947 cp_token *token = cp_lexer_previous_token (parser->lexer);
23948 gcc_rich_location richloc (token->location);
23949 richloc.add_fixit_insert_after (";");
23950 error_at (&richloc, "expected %<;%> at end of "
23951 "member declaration");
23953 /* Assume that the user meant to provide a semicolon. If
23954 we were to cp_parser_skip_to_end_of_statement, we might
23955 skip to a semicolon inside a member function definition
23956 and issue nonsensical error messages. */
23957 assume_semicolon = true;
23960 if (decl)
23962 /* Add DECL to the list of members. */
23963 if (!friend_p
23964 /* Explicitly include, eg, NSDMIs, for better error
23965 recovery (c++/58650). */
23966 || !DECL_DECLARES_FUNCTION_P (decl))
23967 finish_member_declaration (decl);
23969 if (TREE_CODE (decl) == FUNCTION_DECL)
23970 cp_parser_save_default_args (parser, decl);
23971 else if (TREE_CODE (decl) == FIELD_DECL
23972 && DECL_INITIAL (decl))
23973 /* Add DECL to the queue of NSDMI to be parsed later. */
23974 vec_safe_push (unparsed_nsdmis, decl);
23977 if (assume_semicolon)
23978 goto out;
23982 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23983 out:
23984 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23987 /* Parse a pure-specifier.
23989 pure-specifier:
23992 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23993 Otherwise, ERROR_MARK_NODE is returned. */
23995 static tree
23996 cp_parser_pure_specifier (cp_parser* parser)
23998 cp_token *token;
24000 /* Look for the `=' token. */
24001 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24002 return error_mark_node;
24003 /* Look for the `0' token. */
24004 token = cp_lexer_peek_token (parser->lexer);
24006 if (token->type == CPP_EOF
24007 || token->type == CPP_PRAGMA_EOL)
24008 return error_mark_node;
24010 cp_lexer_consume_token (parser->lexer);
24012 /* Accept = default or = delete in c++0x mode. */
24013 if (token->keyword == RID_DEFAULT
24014 || token->keyword == RID_DELETE)
24016 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24017 return token->u.value;
24020 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24021 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24023 cp_parser_error (parser,
24024 "invalid pure specifier (only %<= 0%> is allowed)");
24025 cp_parser_skip_to_end_of_statement (parser);
24026 return error_mark_node;
24028 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24030 error_at (token->location, "templates may not be %<virtual%>");
24031 return error_mark_node;
24034 return integer_zero_node;
24037 /* Parse a constant-initializer.
24039 constant-initializer:
24040 = constant-expression
24042 Returns a representation of the constant-expression. */
24044 static tree
24045 cp_parser_constant_initializer (cp_parser* parser)
24047 /* Look for the `=' token. */
24048 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24049 return error_mark_node;
24051 /* It is invalid to write:
24053 struct S { static const int i = { 7 }; };
24056 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24058 cp_parser_error (parser,
24059 "a brace-enclosed initializer is not allowed here");
24060 /* Consume the opening brace. */
24061 matching_braces braces;
24062 braces.consume_open (parser);
24063 /* Skip the initializer. */
24064 cp_parser_skip_to_closing_brace (parser);
24065 /* Look for the trailing `}'. */
24066 braces.require_close (parser);
24068 return error_mark_node;
24071 return cp_parser_constant_expression (parser);
24074 /* Derived classes [gram.class.derived] */
24076 /* Parse a base-clause.
24078 base-clause:
24079 : base-specifier-list
24081 base-specifier-list:
24082 base-specifier ... [opt]
24083 base-specifier-list , base-specifier ... [opt]
24085 Returns a TREE_LIST representing the base-classes, in the order in
24086 which they were declared. The representation of each node is as
24087 described by cp_parser_base_specifier.
24089 In the case that no bases are specified, this function will return
24090 NULL_TREE, not ERROR_MARK_NODE. */
24092 static tree
24093 cp_parser_base_clause (cp_parser* parser)
24095 tree bases = NULL_TREE;
24097 /* Look for the `:' that begins the list. */
24098 cp_parser_require (parser, CPP_COLON, RT_COLON);
24100 /* Scan the base-specifier-list. */
24101 while (true)
24103 cp_token *token;
24104 tree base;
24105 bool pack_expansion_p = false;
24107 /* Look for the base-specifier. */
24108 base = cp_parser_base_specifier (parser);
24109 /* Look for the (optional) ellipsis. */
24110 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24112 /* Consume the `...'. */
24113 cp_lexer_consume_token (parser->lexer);
24115 pack_expansion_p = true;
24118 /* Add BASE to the front of the list. */
24119 if (base && base != error_mark_node)
24121 if (pack_expansion_p)
24122 /* Make this a pack expansion type. */
24123 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24125 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24127 TREE_CHAIN (base) = bases;
24128 bases = base;
24131 /* Peek at the next token. */
24132 token = cp_lexer_peek_token (parser->lexer);
24133 /* If it's not a comma, then the list is complete. */
24134 if (token->type != CPP_COMMA)
24135 break;
24136 /* Consume the `,'. */
24137 cp_lexer_consume_token (parser->lexer);
24140 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24141 base class had a qualified name. However, the next name that
24142 appears is certainly not qualified. */
24143 parser->scope = NULL_TREE;
24144 parser->qualifying_scope = NULL_TREE;
24145 parser->object_scope = NULL_TREE;
24147 return nreverse (bases);
24150 /* Parse a base-specifier.
24152 base-specifier:
24153 :: [opt] nested-name-specifier [opt] class-name
24154 virtual access-specifier [opt] :: [opt] nested-name-specifier
24155 [opt] class-name
24156 access-specifier virtual [opt] :: [opt] nested-name-specifier
24157 [opt] class-name
24159 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24160 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24161 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24162 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24164 static tree
24165 cp_parser_base_specifier (cp_parser* parser)
24167 cp_token *token;
24168 bool done = false;
24169 bool virtual_p = false;
24170 bool duplicate_virtual_error_issued_p = false;
24171 bool duplicate_access_error_issued_p = false;
24172 bool class_scope_p, template_p;
24173 tree access = access_default_node;
24174 tree type;
24176 /* Process the optional `virtual' and `access-specifier'. */
24177 while (!done)
24179 /* Peek at the next token. */
24180 token = cp_lexer_peek_token (parser->lexer);
24181 /* Process `virtual'. */
24182 switch (token->keyword)
24184 case RID_VIRTUAL:
24185 /* If `virtual' appears more than once, issue an error. */
24186 if (virtual_p && !duplicate_virtual_error_issued_p)
24188 cp_parser_error (parser,
24189 "%<virtual%> specified more than once in base-specifier");
24190 duplicate_virtual_error_issued_p = true;
24193 virtual_p = true;
24195 /* Consume the `virtual' token. */
24196 cp_lexer_consume_token (parser->lexer);
24198 break;
24200 case RID_PUBLIC:
24201 case RID_PROTECTED:
24202 case RID_PRIVATE:
24203 /* If more than one access specifier appears, issue an
24204 error. */
24205 if (access != access_default_node
24206 && !duplicate_access_error_issued_p)
24208 cp_parser_error (parser,
24209 "more than one access specifier in base-specifier");
24210 duplicate_access_error_issued_p = true;
24213 access = ridpointers[(int) token->keyword];
24215 /* Consume the access-specifier. */
24216 cp_lexer_consume_token (parser->lexer);
24218 break;
24220 default:
24221 done = true;
24222 break;
24225 /* It is not uncommon to see programs mechanically, erroneously, use
24226 the 'typename' keyword to denote (dependent) qualified types
24227 as base classes. */
24228 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24230 token = cp_lexer_peek_token (parser->lexer);
24231 if (!processing_template_decl)
24232 error_at (token->location,
24233 "keyword %<typename%> not allowed outside of templates");
24234 else
24235 error_at (token->location,
24236 "keyword %<typename%> not allowed in this context "
24237 "(the base class is implicitly a type)");
24238 cp_lexer_consume_token (parser->lexer);
24241 /* Look for the optional `::' operator. */
24242 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24243 /* Look for the nested-name-specifier. The simplest way to
24244 implement:
24246 [temp.res]
24248 The keyword `typename' is not permitted in a base-specifier or
24249 mem-initializer; in these contexts a qualified name that
24250 depends on a template-parameter is implicitly assumed to be a
24251 type name.
24253 is to pretend that we have seen the `typename' keyword at this
24254 point. */
24255 cp_parser_nested_name_specifier_opt (parser,
24256 /*typename_keyword_p=*/true,
24257 /*check_dependency_p=*/true,
24258 /*type_p=*/true,
24259 /*is_declaration=*/true);
24260 /* If the base class is given by a qualified name, assume that names
24261 we see are type names or templates, as appropriate. */
24262 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24263 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24265 if (!parser->scope
24266 && cp_lexer_next_token_is_decltype (parser->lexer))
24267 /* DR 950 allows decltype as a base-specifier. */
24268 type = cp_parser_decltype (parser);
24269 else
24271 /* Otherwise, look for the class-name. */
24272 type = cp_parser_class_name (parser,
24273 class_scope_p,
24274 template_p,
24275 typename_type,
24276 /*check_dependency_p=*/true,
24277 /*class_head_p=*/false,
24278 /*is_declaration=*/true);
24279 type = TREE_TYPE (type);
24282 if (type == error_mark_node)
24283 return error_mark_node;
24285 return finish_base_specifier (type, access, virtual_p);
24288 /* Exception handling [gram.exception] */
24290 /* Parse an (optional) noexcept-specification.
24292 noexcept-specification:
24293 noexcept ( constant-expression ) [opt]
24295 If no noexcept-specification is present, returns NULL_TREE.
24296 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24297 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24298 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24299 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24300 in which case a boolean condition is returned instead. */
24302 static tree
24303 cp_parser_noexcept_specification_opt (cp_parser* parser,
24304 bool require_constexpr,
24305 bool* consumed_expr,
24306 bool return_cond)
24308 cp_token *token;
24309 const char *saved_message;
24311 /* Peek at the next token. */
24312 token = cp_lexer_peek_token (parser->lexer);
24314 /* Is it a noexcept-specification? */
24315 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24317 tree expr;
24318 cp_lexer_consume_token (parser->lexer);
24320 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24322 matching_parens parens;
24323 parens.consume_open (parser);
24325 if (require_constexpr)
24327 /* Types may not be defined in an exception-specification. */
24328 saved_message = parser->type_definition_forbidden_message;
24329 parser->type_definition_forbidden_message
24330 = G_("types may not be defined in an exception-specification");
24332 expr = cp_parser_constant_expression (parser);
24334 /* Restore the saved message. */
24335 parser->type_definition_forbidden_message = saved_message;
24337 else
24339 expr = cp_parser_expression (parser);
24340 *consumed_expr = true;
24343 parens.require_close (parser);
24345 else
24347 expr = boolean_true_node;
24348 if (!require_constexpr)
24349 *consumed_expr = false;
24352 /* We cannot build a noexcept-spec right away because this will check
24353 that expr is a constexpr. */
24354 if (!return_cond)
24355 return build_noexcept_spec (expr, tf_warning_or_error);
24356 else
24357 return expr;
24359 else
24360 return NULL_TREE;
24363 /* Parse an (optional) exception-specification.
24365 exception-specification:
24366 throw ( type-id-list [opt] )
24368 Returns a TREE_LIST representing the exception-specification. The
24369 TREE_VALUE of each node is a type. */
24371 static tree
24372 cp_parser_exception_specification_opt (cp_parser* parser)
24374 cp_token *token;
24375 tree type_id_list;
24376 const char *saved_message;
24378 /* Peek at the next token. */
24379 token = cp_lexer_peek_token (parser->lexer);
24381 /* Is it a noexcept-specification? */
24382 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24383 false);
24384 if (type_id_list != NULL_TREE)
24385 return type_id_list;
24387 /* If it's not `throw', then there's no exception-specification. */
24388 if (!cp_parser_is_keyword (token, RID_THROW))
24389 return NULL_TREE;
24391 location_t loc = token->location;
24393 /* Consume the `throw'. */
24394 cp_lexer_consume_token (parser->lexer);
24396 /* Look for the `('. */
24397 matching_parens parens;
24398 parens.require_open (parser);
24400 /* Peek at the next token. */
24401 token = cp_lexer_peek_token (parser->lexer);
24402 /* If it's not a `)', then there is a type-id-list. */
24403 if (token->type != CPP_CLOSE_PAREN)
24405 /* Types may not be defined in an exception-specification. */
24406 saved_message = parser->type_definition_forbidden_message;
24407 parser->type_definition_forbidden_message
24408 = G_("types may not be defined in an exception-specification");
24409 /* Parse the type-id-list. */
24410 type_id_list = cp_parser_type_id_list (parser);
24411 /* Restore the saved message. */
24412 parser->type_definition_forbidden_message = saved_message;
24414 if (cxx_dialect >= cxx17)
24416 error_at (loc, "ISO C++17 does not allow dynamic exception "
24417 "specifications");
24418 type_id_list = NULL_TREE;
24420 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24421 warning_at (loc, OPT_Wdeprecated,
24422 "dynamic exception specifications are deprecated in "
24423 "C++11");
24425 /* In C++17, throw() is equivalent to noexcept (true). throw()
24426 is deprecated in C++11 and above as well, but is still widely used,
24427 so don't warn about it yet. */
24428 else if (cxx_dialect >= cxx17)
24429 type_id_list = noexcept_true_spec;
24430 else
24431 type_id_list = empty_except_spec;
24433 /* Look for the `)'. */
24434 parens.require_close (parser);
24436 return type_id_list;
24439 /* Parse an (optional) type-id-list.
24441 type-id-list:
24442 type-id ... [opt]
24443 type-id-list , type-id ... [opt]
24445 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24446 in the order that the types were presented. */
24448 static tree
24449 cp_parser_type_id_list (cp_parser* parser)
24451 tree types = NULL_TREE;
24453 while (true)
24455 cp_token *token;
24456 tree type;
24458 token = cp_lexer_peek_token (parser->lexer);
24460 /* Get the next type-id. */
24461 type = cp_parser_type_id (parser);
24462 /* Check for invalid 'auto'. */
24463 if (flag_concepts && type_uses_auto (type))
24465 error_at (token->location,
24466 "invalid use of %<auto%> in exception-specification");
24467 type = error_mark_node;
24469 /* Parse the optional ellipsis. */
24470 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24472 /* Consume the `...'. */
24473 cp_lexer_consume_token (parser->lexer);
24475 /* Turn the type into a pack expansion expression. */
24476 type = make_pack_expansion (type);
24478 /* Add it to the list. */
24479 types = add_exception_specifier (types, type, /*complain=*/1);
24480 /* Peek at the next token. */
24481 token = cp_lexer_peek_token (parser->lexer);
24482 /* If it is not a `,', we are done. */
24483 if (token->type != CPP_COMMA)
24484 break;
24485 /* Consume the `,'. */
24486 cp_lexer_consume_token (parser->lexer);
24489 return nreverse (types);
24492 /* Parse a try-block.
24494 try-block:
24495 try compound-statement handler-seq */
24497 static tree
24498 cp_parser_try_block (cp_parser* parser)
24500 tree try_block;
24502 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24503 if (parser->in_function_body
24504 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24505 error ("%<try%> in %<constexpr%> function");
24507 try_block = begin_try_block ();
24508 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24509 finish_try_block (try_block);
24510 cp_parser_handler_seq (parser);
24511 finish_handler_sequence (try_block);
24513 return try_block;
24516 /* Parse a function-try-block.
24518 function-try-block:
24519 try ctor-initializer [opt] function-body handler-seq */
24521 static void
24522 cp_parser_function_try_block (cp_parser* parser)
24524 tree compound_stmt;
24525 tree try_block;
24527 /* Look for the `try' keyword. */
24528 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24529 return;
24530 /* Let the rest of the front end know where we are. */
24531 try_block = begin_function_try_block (&compound_stmt);
24532 /* Parse the function-body. */
24533 cp_parser_ctor_initializer_opt_and_function_body
24534 (parser, /*in_function_try_block=*/true);
24535 /* We're done with the `try' part. */
24536 finish_function_try_block (try_block);
24537 /* Parse the handlers. */
24538 cp_parser_handler_seq (parser);
24539 /* We're done with the handlers. */
24540 finish_function_handler_sequence (try_block, compound_stmt);
24543 /* Parse a handler-seq.
24545 handler-seq:
24546 handler handler-seq [opt] */
24548 static void
24549 cp_parser_handler_seq (cp_parser* parser)
24551 while (true)
24553 cp_token *token;
24555 /* Parse the handler. */
24556 cp_parser_handler (parser);
24557 /* Peek at the next token. */
24558 token = cp_lexer_peek_token (parser->lexer);
24559 /* If it's not `catch' then there are no more handlers. */
24560 if (!cp_parser_is_keyword (token, RID_CATCH))
24561 break;
24565 /* Parse a handler.
24567 handler:
24568 catch ( exception-declaration ) compound-statement */
24570 static void
24571 cp_parser_handler (cp_parser* parser)
24573 tree handler;
24574 tree declaration;
24576 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24577 handler = begin_handler ();
24578 matching_parens parens;
24579 parens.require_open (parser);
24580 declaration = cp_parser_exception_declaration (parser);
24581 finish_handler_parms (declaration, handler);
24582 parens.require_close (parser);
24583 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24584 finish_handler (handler);
24587 /* Parse an exception-declaration.
24589 exception-declaration:
24590 type-specifier-seq declarator
24591 type-specifier-seq abstract-declarator
24592 type-specifier-seq
24595 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24596 ellipsis variant is used. */
24598 static tree
24599 cp_parser_exception_declaration (cp_parser* parser)
24601 cp_decl_specifier_seq type_specifiers;
24602 cp_declarator *declarator;
24603 const char *saved_message;
24605 /* If it's an ellipsis, it's easy to handle. */
24606 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24608 /* Consume the `...' token. */
24609 cp_lexer_consume_token (parser->lexer);
24610 return NULL_TREE;
24613 /* Types may not be defined in exception-declarations. */
24614 saved_message = parser->type_definition_forbidden_message;
24615 parser->type_definition_forbidden_message
24616 = G_("types may not be defined in exception-declarations");
24618 /* Parse the type-specifier-seq. */
24619 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24620 /*is_trailing_return=*/false,
24621 &type_specifiers);
24622 /* If it's a `)', then there is no declarator. */
24623 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24624 declarator = NULL;
24625 else
24626 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24627 /*ctor_dtor_or_conv_p=*/NULL,
24628 /*parenthesized_p=*/NULL,
24629 /*member_p=*/false,
24630 /*friend_p=*/false);
24632 /* Restore the saved message. */
24633 parser->type_definition_forbidden_message = saved_message;
24635 if (!type_specifiers.any_specifiers_p)
24636 return error_mark_node;
24638 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24641 /* Parse a throw-expression.
24643 throw-expression:
24644 throw assignment-expression [opt]
24646 Returns a THROW_EXPR representing the throw-expression. */
24648 static tree
24649 cp_parser_throw_expression (cp_parser* parser)
24651 tree expression;
24652 cp_token* token;
24654 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24655 token = cp_lexer_peek_token (parser->lexer);
24656 /* Figure out whether or not there is an assignment-expression
24657 following the "throw" keyword. */
24658 if (token->type == CPP_COMMA
24659 || token->type == CPP_SEMICOLON
24660 || token->type == CPP_CLOSE_PAREN
24661 || token->type == CPP_CLOSE_SQUARE
24662 || token->type == CPP_CLOSE_BRACE
24663 || token->type == CPP_COLON)
24664 expression = NULL_TREE;
24665 else
24666 expression = cp_parser_assignment_expression (parser);
24668 return build_throw (expression);
24671 /* GNU Extensions */
24673 /* Parse an (optional) asm-specification.
24675 asm-specification:
24676 asm ( string-literal )
24678 If the asm-specification is present, returns a STRING_CST
24679 corresponding to the string-literal. Otherwise, returns
24680 NULL_TREE. */
24682 static tree
24683 cp_parser_asm_specification_opt (cp_parser* parser)
24685 cp_token *token;
24686 tree asm_specification;
24688 /* Peek at the next token. */
24689 token = cp_lexer_peek_token (parser->lexer);
24690 /* If the next token isn't the `asm' keyword, then there's no
24691 asm-specification. */
24692 if (!cp_parser_is_keyword (token, RID_ASM))
24693 return NULL_TREE;
24695 /* Consume the `asm' token. */
24696 cp_lexer_consume_token (parser->lexer);
24697 /* Look for the `('. */
24698 matching_parens parens;
24699 parens.require_open (parser);
24701 /* Look for the string-literal. */
24702 asm_specification = cp_parser_string_literal (parser, false, false);
24704 /* Look for the `)'. */
24705 parens.require_close (parser);
24707 return asm_specification;
24710 /* Parse an asm-operand-list.
24712 asm-operand-list:
24713 asm-operand
24714 asm-operand-list , asm-operand
24716 asm-operand:
24717 string-literal ( expression )
24718 [ string-literal ] string-literal ( expression )
24720 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24721 each node is the expression. The TREE_PURPOSE is itself a
24722 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24723 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24724 is a STRING_CST for the string literal before the parenthesis. Returns
24725 ERROR_MARK_NODE if any of the operands are invalid. */
24727 static tree
24728 cp_parser_asm_operand_list (cp_parser* parser)
24730 tree asm_operands = NULL_TREE;
24731 bool invalid_operands = false;
24733 while (true)
24735 tree string_literal;
24736 tree expression;
24737 tree name;
24739 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24741 /* Consume the `[' token. */
24742 cp_lexer_consume_token (parser->lexer);
24743 /* Read the operand name. */
24744 name = cp_parser_identifier (parser);
24745 if (name != error_mark_node)
24746 name = build_string (IDENTIFIER_LENGTH (name),
24747 IDENTIFIER_POINTER (name));
24748 /* Look for the closing `]'. */
24749 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24751 else
24752 name = NULL_TREE;
24753 /* Look for the string-literal. */
24754 string_literal = cp_parser_string_literal (parser, false, false);
24756 /* Look for the `('. */
24757 matching_parens parens;
24758 parens.require_open (parser);
24759 /* Parse the expression. */
24760 expression = cp_parser_expression (parser);
24761 /* Look for the `)'. */
24762 parens.require_close (parser);
24764 if (name == error_mark_node
24765 || string_literal == error_mark_node
24766 || expression == error_mark_node)
24767 invalid_operands = true;
24769 /* Add this operand to the list. */
24770 asm_operands = tree_cons (build_tree_list (name, string_literal),
24771 expression,
24772 asm_operands);
24773 /* If the next token is not a `,', there are no more
24774 operands. */
24775 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24776 break;
24777 /* Consume the `,'. */
24778 cp_lexer_consume_token (parser->lexer);
24781 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24784 /* Parse an asm-clobber-list.
24786 asm-clobber-list:
24787 string-literal
24788 asm-clobber-list , string-literal
24790 Returns a TREE_LIST, indicating the clobbers in the order that they
24791 appeared. The TREE_VALUE of each node is a STRING_CST. */
24793 static tree
24794 cp_parser_asm_clobber_list (cp_parser* parser)
24796 tree clobbers = NULL_TREE;
24798 while (true)
24800 tree string_literal;
24802 /* Look for the string literal. */
24803 string_literal = cp_parser_string_literal (parser, false, false);
24804 /* Add it to the list. */
24805 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24806 /* If the next token is not a `,', then the list is
24807 complete. */
24808 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24809 break;
24810 /* Consume the `,' token. */
24811 cp_lexer_consume_token (parser->lexer);
24814 return clobbers;
24817 /* Parse an asm-label-list.
24819 asm-label-list:
24820 identifier
24821 asm-label-list , identifier
24823 Returns a TREE_LIST, indicating the labels in the order that they
24824 appeared. The TREE_VALUE of each node is a label. */
24826 static tree
24827 cp_parser_asm_label_list (cp_parser* parser)
24829 tree labels = NULL_TREE;
24831 while (true)
24833 tree identifier, label, name;
24835 /* Look for the identifier. */
24836 identifier = cp_parser_identifier (parser);
24837 if (!error_operand_p (identifier))
24839 label = lookup_label (identifier);
24840 if (TREE_CODE (label) == LABEL_DECL)
24842 TREE_USED (label) = 1;
24843 check_goto (label);
24844 name = build_string (IDENTIFIER_LENGTH (identifier),
24845 IDENTIFIER_POINTER (identifier));
24846 labels = tree_cons (name, label, labels);
24849 /* If the next token is not a `,', then the list is
24850 complete. */
24851 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24852 break;
24853 /* Consume the `,' token. */
24854 cp_lexer_consume_token (parser->lexer);
24857 return nreverse (labels);
24860 /* Return TRUE iff the next tokens in the stream are possibly the
24861 beginning of a GNU extension attribute. */
24863 static bool
24864 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24866 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24869 /* Return TRUE iff the next tokens in the stream are possibly the
24870 beginning of a standard C++-11 attribute specifier. */
24872 static bool
24873 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24875 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24878 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24879 beginning of a standard C++-11 attribute specifier. */
24881 static bool
24882 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24884 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24886 return (cxx_dialect >= cxx11
24887 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24888 || (token->type == CPP_OPEN_SQUARE
24889 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24890 && token->type == CPP_OPEN_SQUARE)));
24893 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24894 beginning of a GNU extension attribute. */
24896 static bool
24897 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24899 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24901 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24904 /* Return true iff the next tokens can be the beginning of either a
24905 GNU attribute list, or a standard C++11 attribute sequence. */
24907 static bool
24908 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24910 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24911 || cp_next_tokens_can_be_std_attribute_p (parser));
24914 /* Return true iff the next Nth tokens can be the beginning of either
24915 a GNU attribute list, or a standard C++11 attribute sequence. */
24917 static bool
24918 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24920 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24921 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24924 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24925 of GNU attributes, or return NULL. */
24927 static tree
24928 cp_parser_attributes_opt (cp_parser *parser)
24930 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24931 return cp_parser_gnu_attributes_opt (parser);
24932 return cp_parser_std_attribute_spec_seq (parser);
24935 /* Parse an (optional) series of attributes.
24937 attributes:
24938 attributes attribute
24940 attribute:
24941 __attribute__ (( attribute-list [opt] ))
24943 The return value is as for cp_parser_gnu_attribute_list. */
24945 static tree
24946 cp_parser_gnu_attributes_opt (cp_parser* parser)
24948 tree attributes = NULL_TREE;
24950 temp_override<bool> cleanup
24951 (parser->auto_is_implicit_function_template_parm_p, false);
24953 while (true)
24955 cp_token *token;
24956 tree attribute_list;
24957 bool ok = true;
24959 /* Peek at the next token. */
24960 token = cp_lexer_peek_token (parser->lexer);
24961 /* If it's not `__attribute__', then we're done. */
24962 if (token->keyword != RID_ATTRIBUTE)
24963 break;
24965 /* Consume the `__attribute__' keyword. */
24966 cp_lexer_consume_token (parser->lexer);
24967 /* Look for the two `(' tokens. */
24968 matching_parens outer_parens;
24969 outer_parens.require_open (parser);
24970 matching_parens inner_parens;
24971 inner_parens.require_open (parser);
24973 /* Peek at the next token. */
24974 token = cp_lexer_peek_token (parser->lexer);
24975 if (token->type != CPP_CLOSE_PAREN)
24976 /* Parse the attribute-list. */
24977 attribute_list = cp_parser_gnu_attribute_list (parser);
24978 else
24979 /* If the next token is a `)', then there is no attribute
24980 list. */
24981 attribute_list = NULL;
24983 /* Look for the two `)' tokens. */
24984 if (!inner_parens.require_close (parser))
24985 ok = false;
24986 if (!outer_parens.require_close (parser))
24987 ok = false;
24988 if (!ok)
24989 cp_parser_skip_to_end_of_statement (parser);
24991 /* Add these new attributes to the list. */
24992 attributes = attr_chainon (attributes, attribute_list);
24995 return attributes;
24998 /* Parse a GNU attribute-list.
25000 attribute-list:
25001 attribute
25002 attribute-list , attribute
25004 attribute:
25005 identifier
25006 identifier ( identifier )
25007 identifier ( identifier , expression-list )
25008 identifier ( expression-list )
25010 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25011 to an attribute. The TREE_PURPOSE of each node is the identifier
25012 indicating which attribute is in use. The TREE_VALUE represents
25013 the arguments, if any. */
25015 static tree
25016 cp_parser_gnu_attribute_list (cp_parser* parser)
25018 tree attribute_list = NULL_TREE;
25019 bool save_translate_strings_p = parser->translate_strings_p;
25021 parser->translate_strings_p = false;
25022 while (true)
25024 cp_token *token;
25025 tree identifier;
25026 tree attribute;
25028 /* Look for the identifier. We also allow keywords here; for
25029 example `__attribute__ ((const))' is legal. */
25030 token = cp_lexer_peek_token (parser->lexer);
25031 if (token->type == CPP_NAME
25032 || token->type == CPP_KEYWORD)
25034 tree arguments = NULL_TREE;
25036 /* Consume the token, but save it since we need it for the
25037 SIMD enabled function parsing. */
25038 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25040 /* Save away the identifier that indicates which attribute
25041 this is. */
25042 identifier = (token->type == CPP_KEYWORD)
25043 /* For keywords, use the canonical spelling, not the
25044 parsed identifier. */
25045 ? ridpointers[(int) token->keyword]
25046 : id_token->u.value;
25048 identifier = canonicalize_attr_name (identifier);
25049 attribute = build_tree_list (identifier, NULL_TREE);
25051 /* Peek at the next token. */
25052 token = cp_lexer_peek_token (parser->lexer);
25053 /* If it's an `(', then parse the attribute arguments. */
25054 if (token->type == CPP_OPEN_PAREN)
25056 vec<tree, va_gc> *vec;
25057 int attr_flag = (attribute_takes_identifier_p (identifier)
25058 ? id_attr : normal_attr);
25059 vec = cp_parser_parenthesized_expression_list
25060 (parser, attr_flag, /*cast_p=*/false,
25061 /*allow_expansion_p=*/false,
25062 /*non_constant_p=*/NULL);
25063 if (vec == NULL)
25064 arguments = error_mark_node;
25065 else
25067 arguments = build_tree_list_vec (vec);
25068 release_tree_vector (vec);
25070 /* Save the arguments away. */
25071 TREE_VALUE (attribute) = arguments;
25074 if (arguments != error_mark_node)
25076 /* Add this attribute to the list. */
25077 TREE_CHAIN (attribute) = attribute_list;
25078 attribute_list = attribute;
25081 token = cp_lexer_peek_token (parser->lexer);
25083 /* Now, look for more attributes. If the next token isn't a
25084 `,', we're done. */
25085 if (token->type != CPP_COMMA)
25086 break;
25088 /* Consume the comma and keep going. */
25089 cp_lexer_consume_token (parser->lexer);
25091 parser->translate_strings_p = save_translate_strings_p;
25093 /* We built up the list in reverse order. */
25094 return nreverse (attribute_list);
25097 /* Parse a standard C++11 attribute.
25099 The returned representation is a TREE_LIST which TREE_PURPOSE is
25100 the scoped name of the attribute, and the TREE_VALUE is its
25101 arguments list.
25103 Note that the scoped name of the attribute is itself a TREE_LIST
25104 which TREE_PURPOSE is the namespace of the attribute, and
25105 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25106 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25107 and which TREE_PURPOSE is directly the attribute name.
25109 Clients of the attribute code should use get_attribute_namespace
25110 and get_attribute_name to get the actual namespace and name of
25111 attributes, regardless of their being GNU or C++11 attributes.
25113 attribute:
25114 attribute-token attribute-argument-clause [opt]
25116 attribute-token:
25117 identifier
25118 attribute-scoped-token
25120 attribute-scoped-token:
25121 attribute-namespace :: identifier
25123 attribute-namespace:
25124 identifier
25126 attribute-argument-clause:
25127 ( balanced-token-seq )
25129 balanced-token-seq:
25130 balanced-token [opt]
25131 balanced-token-seq balanced-token
25133 balanced-token:
25134 ( balanced-token-seq )
25135 [ balanced-token-seq ]
25136 { balanced-token-seq }. */
25138 static tree
25139 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25141 tree attribute, attr_id = NULL_TREE, arguments;
25142 cp_token *token;
25144 temp_override<bool> cleanup
25145 (parser->auto_is_implicit_function_template_parm_p, false);
25147 /* First, parse name of the attribute, a.k.a attribute-token. */
25149 token = cp_lexer_peek_token (parser->lexer);
25150 if (token->type == CPP_NAME)
25151 attr_id = token->u.value;
25152 else if (token->type == CPP_KEYWORD)
25153 attr_id = ridpointers[(int) token->keyword];
25154 else if (token->flags & NAMED_OP)
25155 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25157 if (attr_id == NULL_TREE)
25158 return NULL_TREE;
25160 cp_lexer_consume_token (parser->lexer);
25162 token = cp_lexer_peek_token (parser->lexer);
25163 if (token->type == CPP_SCOPE)
25165 /* We are seeing a scoped attribute token. */
25167 cp_lexer_consume_token (parser->lexer);
25168 if (attr_ns)
25169 error_at (token->location, "attribute using prefix used together "
25170 "with scoped attribute token");
25171 attr_ns = attr_id;
25173 token = cp_lexer_consume_token (parser->lexer);
25174 if (token->type == CPP_NAME)
25175 attr_id = token->u.value;
25176 else if (token->type == CPP_KEYWORD)
25177 attr_id = ridpointers[(int) token->keyword];
25178 else if (token->flags & NAMED_OP)
25179 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25180 else
25182 error_at (token->location,
25183 "expected an identifier for the attribute name");
25184 return error_mark_node;
25187 attr_id = canonicalize_attr_name (attr_id);
25188 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25189 NULL_TREE);
25190 token = cp_lexer_peek_token (parser->lexer);
25192 else if (attr_ns)
25193 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25194 NULL_TREE);
25195 else
25197 attr_id = canonicalize_attr_name (attr_id);
25198 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25199 NULL_TREE);
25200 /* C++11 noreturn attribute is equivalent to GNU's. */
25201 if (is_attribute_p ("noreturn", attr_id))
25202 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25203 /* C++14 deprecated attribute is equivalent to GNU's. */
25204 else if (is_attribute_p ("deprecated", attr_id))
25205 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25206 /* C++17 fallthrough attribute is equivalent to GNU's. */
25207 else if (is_attribute_p ("fallthrough", attr_id))
25208 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25209 /* Transactional Memory TS optimize_for_synchronized attribute is
25210 equivalent to GNU transaction_callable. */
25211 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25212 TREE_PURPOSE (attribute)
25213 = get_identifier ("transaction_callable");
25214 /* Transactional Memory attributes are GNU attributes. */
25215 else if (tm_attr_to_mask (attr_id))
25216 TREE_PURPOSE (attribute) = attr_id;
25219 /* Now parse the optional argument clause of the attribute. */
25221 if (token->type != CPP_OPEN_PAREN)
25222 return attribute;
25225 vec<tree, va_gc> *vec;
25226 int attr_flag = normal_attr;
25228 if (attr_ns == get_identifier ("gnu")
25229 && attribute_takes_identifier_p (attr_id))
25230 /* A GNU attribute that takes an identifier in parameter. */
25231 attr_flag = id_attr;
25233 vec = cp_parser_parenthesized_expression_list
25234 (parser, attr_flag, /*cast_p=*/false,
25235 /*allow_expansion_p=*/true,
25236 /*non_constant_p=*/NULL);
25237 if (vec == NULL)
25238 arguments = error_mark_node;
25239 else
25241 arguments = build_tree_list_vec (vec);
25242 release_tree_vector (vec);
25245 if (arguments == error_mark_node)
25246 attribute = error_mark_node;
25247 else
25248 TREE_VALUE (attribute) = arguments;
25251 return attribute;
25254 /* Check that the attribute ATTRIBUTE appears at most once in the
25255 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25256 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25257 isn't implemented yet in GCC. */
25259 static void
25260 cp_parser_check_std_attribute (tree attributes, tree attribute)
25262 if (attributes)
25264 tree name = get_attribute_name (attribute);
25265 if (is_attribute_p ("noreturn", name)
25266 && lookup_attribute ("noreturn", attributes))
25267 error ("attribute %<noreturn%> can appear at most once "
25268 "in an attribute-list");
25269 else if (is_attribute_p ("deprecated", name)
25270 && lookup_attribute ("deprecated", attributes))
25271 error ("attribute %<deprecated%> can appear at most once "
25272 "in an attribute-list");
25276 /* Parse a list of standard C++-11 attributes.
25278 attribute-list:
25279 attribute [opt]
25280 attribute-list , attribute[opt]
25281 attribute ...
25282 attribute-list , attribute ...
25285 static tree
25286 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25288 tree attributes = NULL_TREE, attribute = NULL_TREE;
25289 cp_token *token = NULL;
25291 while (true)
25293 attribute = cp_parser_std_attribute (parser, attr_ns);
25294 if (attribute == error_mark_node)
25295 break;
25296 if (attribute != NULL_TREE)
25298 cp_parser_check_std_attribute (attributes, attribute);
25299 TREE_CHAIN (attribute) = attributes;
25300 attributes = attribute;
25302 token = cp_lexer_peek_token (parser->lexer);
25303 if (token->type == CPP_ELLIPSIS)
25305 cp_lexer_consume_token (parser->lexer);
25306 if (attribute == NULL_TREE)
25307 error_at (token->location,
25308 "expected attribute before %<...%>");
25309 else
25311 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25312 if (pack == error_mark_node)
25313 return error_mark_node;
25314 TREE_VALUE (attribute) = pack;
25316 token = cp_lexer_peek_token (parser->lexer);
25318 if (token->type != CPP_COMMA)
25319 break;
25320 cp_lexer_consume_token (parser->lexer);
25322 attributes = nreverse (attributes);
25323 return attributes;
25326 /* Parse a standard C++-11 attribute specifier.
25328 attribute-specifier:
25329 [ [ attribute-using-prefix [opt] attribute-list ] ]
25330 alignment-specifier
25332 attribute-using-prefix:
25333 using attribute-namespace :
25335 alignment-specifier:
25336 alignas ( type-id ... [opt] )
25337 alignas ( alignment-expression ... [opt] ). */
25339 static tree
25340 cp_parser_std_attribute_spec (cp_parser *parser)
25342 tree attributes = NULL_TREE;
25343 cp_token *token = cp_lexer_peek_token (parser->lexer);
25345 if (token->type == CPP_OPEN_SQUARE
25346 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25348 tree attr_ns = NULL_TREE;
25350 cp_lexer_consume_token (parser->lexer);
25351 cp_lexer_consume_token (parser->lexer);
25353 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25355 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25356 if (token->type == CPP_NAME)
25357 attr_ns = token->u.value;
25358 else if (token->type == CPP_KEYWORD)
25359 attr_ns = ridpointers[(int) token->keyword];
25360 else if (token->flags & NAMED_OP)
25361 attr_ns = get_identifier (cpp_type2name (token->type,
25362 token->flags));
25363 if (attr_ns
25364 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25366 if (cxx_dialect < cxx17
25367 && !in_system_header_at (input_location))
25368 pedwarn (input_location, 0,
25369 "attribute using prefix only available "
25370 "with -std=c++17 or -std=gnu++17");
25372 cp_lexer_consume_token (parser->lexer);
25373 cp_lexer_consume_token (parser->lexer);
25374 cp_lexer_consume_token (parser->lexer);
25376 else
25377 attr_ns = NULL_TREE;
25380 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25382 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25383 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25384 cp_parser_skip_to_end_of_statement (parser);
25385 else
25386 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25387 when we are sure that we have actually parsed them. */
25388 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25390 else
25392 tree alignas_expr;
25394 /* Look for an alignment-specifier. */
25396 token = cp_lexer_peek_token (parser->lexer);
25398 if (token->type != CPP_KEYWORD
25399 || token->keyword != RID_ALIGNAS)
25400 return NULL_TREE;
25402 cp_lexer_consume_token (parser->lexer);
25403 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25405 matching_parens parens;
25406 if (!parens.require_open (parser))
25407 return error_mark_node;
25409 cp_parser_parse_tentatively (parser);
25410 alignas_expr = cp_parser_type_id (parser);
25412 if (!cp_parser_parse_definitely (parser))
25414 alignas_expr = cp_parser_assignment_expression (parser);
25415 if (alignas_expr == error_mark_node)
25416 cp_parser_skip_to_end_of_statement (parser);
25417 if (alignas_expr == NULL_TREE
25418 || alignas_expr == error_mark_node)
25419 return alignas_expr;
25422 alignas_expr = cxx_alignas_expr (alignas_expr);
25423 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25425 /* Handle alignas (pack...). */
25426 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25428 cp_lexer_consume_token (parser->lexer);
25429 alignas_expr = make_pack_expansion (alignas_expr);
25432 /* Something went wrong, so don't build the attribute. */
25433 if (alignas_expr == error_mark_node)
25434 return error_mark_node;
25436 if (!parens.require_close (parser))
25437 return error_mark_node;
25439 /* Build the C++-11 representation of an 'aligned'
25440 attribute. */
25441 attributes =
25442 build_tree_list (build_tree_list (get_identifier ("gnu"),
25443 get_identifier ("aligned")),
25444 alignas_expr);
25447 return attributes;
25450 /* Parse a standard C++-11 attribute-specifier-seq.
25452 attribute-specifier-seq:
25453 attribute-specifier-seq [opt] attribute-specifier
25456 static tree
25457 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25459 tree attr_specs = NULL_TREE;
25460 tree attr_last = NULL_TREE;
25462 while (true)
25464 tree attr_spec = cp_parser_std_attribute_spec (parser);
25465 if (attr_spec == NULL_TREE)
25466 break;
25467 if (attr_spec == error_mark_node)
25468 return error_mark_node;
25470 if (attr_last)
25471 TREE_CHAIN (attr_last) = attr_spec;
25472 else
25473 attr_specs = attr_last = attr_spec;
25474 attr_last = tree_last (attr_last);
25477 return attr_specs;
25480 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25481 return index of the first token after balanced-token, or N on failure. */
25483 static size_t
25484 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25486 size_t orig_n = n;
25487 int nparens = 0, nbraces = 0, nsquares = 0;
25489 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25491 case CPP_EOF:
25492 case CPP_PRAGMA_EOL:
25493 /* Ran out of tokens. */
25494 return orig_n;
25495 case CPP_OPEN_PAREN:
25496 ++nparens;
25497 break;
25498 case CPP_OPEN_BRACE:
25499 ++nbraces;
25500 break;
25501 case CPP_OPEN_SQUARE:
25502 ++nsquares;
25503 break;
25504 case CPP_CLOSE_PAREN:
25505 --nparens;
25506 break;
25507 case CPP_CLOSE_BRACE:
25508 --nbraces;
25509 break;
25510 case CPP_CLOSE_SQUARE:
25511 --nsquares;
25512 break;
25513 default:
25514 break;
25516 while (nparens || nbraces || nsquares);
25517 return n;
25520 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25521 return index of the first token after the GNU attribute tokens, or N on
25522 failure. */
25524 static size_t
25525 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25527 while (true)
25529 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25530 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25531 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25532 break;
25534 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25535 if (n2 == n + 2)
25536 break;
25537 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25538 break;
25539 n = n2 + 1;
25541 return n;
25544 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25545 next token), return index of the first token after the standard C++11
25546 attribute tokens, or N on failure. */
25548 static size_t
25549 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25551 while (true)
25553 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25554 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25556 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25557 if (n2 == n + 1)
25558 break;
25559 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25560 break;
25561 n = n2 + 1;
25563 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25564 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25566 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25567 if (n2 == n + 1)
25568 break;
25569 n = n2;
25571 else
25572 break;
25574 return n;
25577 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25578 as the next token), return index of the first token after the attribute
25579 tokens, or N on failure. */
25581 static size_t
25582 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25584 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25585 return cp_parser_skip_gnu_attributes_opt (parser, n);
25586 return cp_parser_skip_std_attribute_spec_seq (parser, n);
25589 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25590 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25591 current value of the PEDANTIC flag, regardless of whether or not
25592 the `__extension__' keyword is present. The caller is responsible
25593 for restoring the value of the PEDANTIC flag. */
25595 static bool
25596 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25598 /* Save the old value of the PEDANTIC flag. */
25599 *saved_pedantic = pedantic;
25601 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25603 /* Consume the `__extension__' token. */
25604 cp_lexer_consume_token (parser->lexer);
25605 /* We're not being pedantic while the `__extension__' keyword is
25606 in effect. */
25607 pedantic = 0;
25609 return true;
25612 return false;
25615 /* Parse a label declaration.
25617 label-declaration:
25618 __label__ label-declarator-seq ;
25620 label-declarator-seq:
25621 identifier , label-declarator-seq
25622 identifier */
25624 static void
25625 cp_parser_label_declaration (cp_parser* parser)
25627 /* Look for the `__label__' keyword. */
25628 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25630 while (true)
25632 tree identifier;
25634 /* Look for an identifier. */
25635 identifier = cp_parser_identifier (parser);
25636 /* If we failed, stop. */
25637 if (identifier == error_mark_node)
25638 break;
25639 /* Declare it as a label. */
25640 finish_label_decl (identifier);
25641 /* If the next token is a `;', stop. */
25642 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25643 break;
25644 /* Look for the `,' separating the label declarations. */
25645 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25648 /* Look for the final `;'. */
25649 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25652 // -------------------------------------------------------------------------- //
25653 // Requires Clause
25655 // Parse a requires clause.
25657 // requires-clause:
25658 // 'requires' logical-or-expression
25660 // The required logical-or-expression must be a constant expression. Note
25661 // that we don't check that the expression is constepxr here. We defer until
25662 // we analyze constraints and then, we only check atomic constraints.
25663 static tree
25664 cp_parser_requires_clause (cp_parser *parser)
25666 // Parse the requires clause so that it is not automatically folded.
25667 ++processing_template_decl;
25668 tree expr = cp_parser_binary_expression (parser, false, false,
25669 PREC_NOT_OPERATOR, NULL);
25670 if (check_for_bare_parameter_packs (expr))
25671 expr = error_mark_node;
25672 --processing_template_decl;
25673 return expr;
25676 // Optionally parse a requires clause:
25677 static tree
25678 cp_parser_requires_clause_opt (cp_parser *parser)
25680 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25681 if (tok->keyword != RID_REQUIRES)
25683 if (!flag_concepts && tok->type == CPP_NAME
25684 && tok->u.value == ridpointers[RID_REQUIRES])
25686 error_at (cp_lexer_peek_token (parser->lexer)->location,
25687 "%<requires%> only available with -fconcepts");
25688 /* Parse and discard the requires-clause. */
25689 cp_lexer_consume_token (parser->lexer);
25690 cp_parser_requires_clause (parser);
25692 return NULL_TREE;
25694 cp_lexer_consume_token (parser->lexer);
25695 return cp_parser_requires_clause (parser);
25699 /*---------------------------------------------------------------------------
25700 Requires expressions
25701 ---------------------------------------------------------------------------*/
25703 /* Parse a requires expression
25705 requirement-expression:
25706 'requires' requirement-parameter-list [opt] requirement-body */
25707 static tree
25708 cp_parser_requires_expression (cp_parser *parser)
25710 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25711 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25713 /* A requires-expression shall appear only within a concept
25714 definition or a requires-clause.
25716 TODO: Implement this diagnostic correctly. */
25717 if (!processing_template_decl)
25719 error_at (loc, "a requires expression cannot appear outside a template");
25720 cp_parser_skip_to_end_of_statement (parser);
25721 return error_mark_node;
25724 tree parms, reqs;
25726 /* Local parameters are delared as variables within the scope
25727 of the expression. They are not visible past the end of
25728 the expression. Expressions within the requires-expression
25729 are unevaluated. */
25730 struct scope_sentinel
25732 scope_sentinel ()
25734 ++cp_unevaluated_operand;
25735 begin_scope (sk_block, NULL_TREE);
25738 ~scope_sentinel ()
25740 pop_bindings_and_leave_scope ();
25741 --cp_unevaluated_operand;
25743 } s;
25745 /* Parse the optional parameter list. */
25746 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25748 parms = cp_parser_requirement_parameter_list (parser);
25749 if (parms == error_mark_node)
25750 return error_mark_node;
25752 else
25753 parms = NULL_TREE;
25755 /* Parse the requirement body. */
25756 reqs = cp_parser_requirement_body (parser);
25757 if (reqs == error_mark_node)
25758 return error_mark_node;
25761 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25762 the parm chain. */
25763 grokparms (parms, &parms);
25764 return finish_requires_expr (parms, reqs);
25767 /* Parse a parameterized requirement.
25769 requirement-parameter-list:
25770 '(' parameter-declaration-clause ')' */
25771 static tree
25772 cp_parser_requirement_parameter_list (cp_parser *parser)
25774 matching_parens parens;
25775 if (!parens.require_open (parser))
25776 return error_mark_node;
25778 tree parms = cp_parser_parameter_declaration_clause (parser);
25780 if (!parens.require_close (parser))
25781 return error_mark_node;
25783 return parms;
25786 /* Parse the body of a requirement.
25788 requirement-body:
25789 '{' requirement-list '}' */
25790 static tree
25791 cp_parser_requirement_body (cp_parser *parser)
25793 matching_braces braces;
25794 if (!braces.require_open (parser))
25795 return error_mark_node;
25797 tree reqs = cp_parser_requirement_list (parser);
25799 if (!braces.require_close (parser))
25800 return error_mark_node;
25802 return reqs;
25805 /* Parse a list of requirements.
25807 requirement-list:
25808 requirement
25809 requirement-list ';' requirement[opt] */
25810 static tree
25811 cp_parser_requirement_list (cp_parser *parser)
25813 tree result = NULL_TREE;
25814 while (true)
25816 tree req = cp_parser_requirement (parser);
25817 if (req == error_mark_node)
25818 return error_mark_node;
25820 result = tree_cons (NULL_TREE, req, result);
25822 /* If we see a semi-colon, consume it. */
25823 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25824 cp_lexer_consume_token (parser->lexer);
25826 /* Stop processing at the end of the list. */
25827 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25828 break;
25831 /* Reverse the order of requirements so they are analyzed in
25832 declaration order. */
25833 return nreverse (result);
25836 /* Parse a syntactic requirement or type requirement.
25838 requirement:
25839 simple-requirement
25840 compound-requirement
25841 type-requirement
25842 nested-requirement */
25843 static tree
25844 cp_parser_requirement (cp_parser *parser)
25846 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25847 return cp_parser_compound_requirement (parser);
25848 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25849 return cp_parser_type_requirement (parser);
25850 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25851 return cp_parser_nested_requirement (parser);
25852 else
25853 return cp_parser_simple_requirement (parser);
25856 /* Parse a simple requirement.
25858 simple-requirement:
25859 expression ';' */
25860 static tree
25861 cp_parser_simple_requirement (cp_parser *parser)
25863 tree expr = cp_parser_expression (parser, NULL, false, false);
25864 if (!expr || expr == error_mark_node)
25865 return error_mark_node;
25867 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25868 return error_mark_node;
25870 return finish_simple_requirement (expr);
25873 /* Parse a type requirement
25875 type-requirement
25876 nested-name-specifier [opt] required-type-name ';'
25878 required-type-name:
25879 type-name
25880 'template' [opt] simple-template-id */
25881 static tree
25882 cp_parser_type_requirement (cp_parser *parser)
25884 cp_lexer_consume_token (parser->lexer);
25886 // Save the scope before parsing name specifiers.
25887 tree saved_scope = parser->scope;
25888 tree saved_object_scope = parser->object_scope;
25889 tree saved_qualifying_scope = parser->qualifying_scope;
25890 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25891 cp_parser_nested_name_specifier_opt (parser,
25892 /*typename_keyword_p=*/true,
25893 /*check_dependency_p=*/false,
25894 /*type_p=*/true,
25895 /*is_declaration=*/false);
25897 tree type;
25898 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25900 cp_lexer_consume_token (parser->lexer);
25901 type = cp_parser_template_id (parser,
25902 /*template_keyword_p=*/true,
25903 /*check_dependency=*/false,
25904 /*tag_type=*/none_type,
25905 /*is_declaration=*/false);
25906 type = make_typename_type (parser->scope, type, typename_type,
25907 /*complain=*/tf_error);
25909 else
25910 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25912 if (TREE_CODE (type) == TYPE_DECL)
25913 type = TREE_TYPE (type);
25915 parser->scope = saved_scope;
25916 parser->object_scope = saved_object_scope;
25917 parser->qualifying_scope = saved_qualifying_scope;
25919 if (type == error_mark_node)
25920 cp_parser_skip_to_end_of_statement (parser);
25922 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25923 return error_mark_node;
25924 if (type == error_mark_node)
25925 return error_mark_node;
25927 return finish_type_requirement (type);
25930 /* Parse a compound requirement
25932 compound-requirement:
25933 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25934 static tree
25935 cp_parser_compound_requirement (cp_parser *parser)
25937 /* Parse an expression enclosed in '{ }'s. */
25938 matching_braces braces;
25939 if (!braces.require_open (parser))
25940 return error_mark_node;
25942 tree expr = cp_parser_expression (parser, NULL, false, false);
25943 if (!expr || expr == error_mark_node)
25944 return error_mark_node;
25946 if (!braces.require_close (parser))
25947 return error_mark_node;
25949 /* Parse the optional noexcept. */
25950 bool noexcept_p = false;
25951 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25953 cp_lexer_consume_token (parser->lexer);
25954 noexcept_p = true;
25957 /* Parse the optional trailing return type. */
25958 tree type = NULL_TREE;
25959 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25961 cp_lexer_consume_token (parser->lexer);
25962 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25963 parser->in_result_type_constraint_p = true;
25964 type = cp_parser_trailing_type_id (parser);
25965 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25966 if (type == error_mark_node)
25967 return error_mark_node;
25970 return finish_compound_requirement (expr, type, noexcept_p);
25973 /* Parse a nested requirement. This is the same as a requires clause.
25975 nested-requirement:
25976 requires-clause */
25977 static tree
25978 cp_parser_nested_requirement (cp_parser *parser)
25980 cp_lexer_consume_token (parser->lexer);
25981 tree req = cp_parser_requires_clause (parser);
25982 if (req == error_mark_node)
25983 return error_mark_node;
25984 return finish_nested_requirement (req);
25987 /* Support Functions */
25989 /* Return the appropriate prefer_type argument for lookup_name_real based on
25990 tag_type and template_mem_access. */
25992 static inline int
25993 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25995 /* DR 141: When looking in the current enclosing context for a template-name
25996 after -> or ., only consider class templates. */
25997 if (template_mem_access)
25998 return 2;
25999 switch (tag_type)
26001 case none_type: return 0; // No preference.
26002 case scope_type: return 1; // Type or namespace.
26003 default: return 2; // Type only.
26007 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26008 NAME should have one of the representations used for an
26009 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26010 is returned. If PARSER->SCOPE is a dependent type, then a
26011 SCOPE_REF is returned.
26013 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26014 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26015 was formed. Abstractly, such entities should not be passed to this
26016 function, because they do not need to be looked up, but it is
26017 simpler to check for this special case here, rather than at the
26018 call-sites.
26020 In cases not explicitly covered above, this function returns a
26021 DECL, OVERLOAD, or baselink representing the result of the lookup.
26022 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26023 is returned.
26025 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26026 (e.g., "struct") that was used. In that case bindings that do not
26027 refer to types are ignored.
26029 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26030 ignored.
26032 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26033 are ignored.
26035 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26036 types.
26038 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26039 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26040 NULL_TREE otherwise. */
26042 static cp_expr
26043 cp_parser_lookup_name (cp_parser *parser, tree name,
26044 enum tag_types tag_type,
26045 bool is_template,
26046 bool is_namespace,
26047 bool check_dependency,
26048 tree *ambiguous_decls,
26049 location_t name_location)
26051 tree decl;
26052 tree object_type = parser->context->object_type;
26054 /* Assume that the lookup will be unambiguous. */
26055 if (ambiguous_decls)
26056 *ambiguous_decls = NULL_TREE;
26058 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26059 no longer valid. Note that if we are parsing tentatively, and
26060 the parse fails, OBJECT_TYPE will be automatically restored. */
26061 parser->context->object_type = NULL_TREE;
26063 if (name == error_mark_node)
26064 return error_mark_node;
26066 /* A template-id has already been resolved; there is no lookup to
26067 do. */
26068 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26069 return name;
26070 if (BASELINK_P (name))
26072 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26073 == TEMPLATE_ID_EXPR);
26074 return name;
26077 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26078 it should already have been checked to make sure that the name
26079 used matches the type being destroyed. */
26080 if (TREE_CODE (name) == BIT_NOT_EXPR)
26082 tree type;
26084 /* Figure out to which type this destructor applies. */
26085 if (parser->scope)
26086 type = parser->scope;
26087 else if (object_type)
26088 type = object_type;
26089 else
26090 type = current_class_type;
26091 /* If that's not a class type, there is no destructor. */
26092 if (!type || !CLASS_TYPE_P (type))
26093 return error_mark_node;
26095 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26096 lazily_declare_fn (sfk_destructor, type);
26098 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26099 return dtor;
26101 return error_mark_node;
26104 /* By this point, the NAME should be an ordinary identifier. If
26105 the id-expression was a qualified name, the qualifying scope is
26106 stored in PARSER->SCOPE at this point. */
26107 gcc_assert (identifier_p (name));
26109 /* Perform the lookup. */
26110 if (parser->scope)
26112 bool dependent_p;
26114 if (parser->scope == error_mark_node)
26115 return error_mark_node;
26117 /* If the SCOPE is dependent, the lookup must be deferred until
26118 the template is instantiated -- unless we are explicitly
26119 looking up names in uninstantiated templates. Even then, we
26120 cannot look up the name if the scope is not a class type; it
26121 might, for example, be a template type parameter. */
26122 dependent_p = (TYPE_P (parser->scope)
26123 && dependent_scope_p (parser->scope));
26124 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26125 && dependent_p)
26126 /* Defer lookup. */
26127 decl = error_mark_node;
26128 else
26130 tree pushed_scope = NULL_TREE;
26132 /* If PARSER->SCOPE is a dependent type, then it must be a
26133 class type, and we must not be checking dependencies;
26134 otherwise, we would have processed this lookup above. So
26135 that PARSER->SCOPE is not considered a dependent base by
26136 lookup_member, we must enter the scope here. */
26137 if (dependent_p)
26138 pushed_scope = push_scope (parser->scope);
26140 /* If the PARSER->SCOPE is a template specialization, it
26141 may be instantiated during name lookup. In that case,
26142 errors may be issued. Even if we rollback the current
26143 tentative parse, those errors are valid. */
26144 decl = lookup_qualified_name (parser->scope, name,
26145 prefer_type_arg (tag_type),
26146 /*complain=*/true);
26148 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26149 lookup result and the nested-name-specifier nominates a class C:
26150 * if the name specified after the nested-name-specifier, when
26151 looked up in C, is the injected-class-name of C (Clause 9), or
26152 * if the name specified after the nested-name-specifier is the
26153 same as the identifier or the simple-template-id's template-
26154 name in the last component of the nested-name-specifier,
26155 the name is instead considered to name the constructor of
26156 class C. [ Note: for example, the constructor is not an
26157 acceptable lookup result in an elaborated-type-specifier so
26158 the constructor would not be used in place of the
26159 injected-class-name. --end note ] Such a constructor name
26160 shall be used only in the declarator-id of a declaration that
26161 names a constructor or in a using-declaration. */
26162 if (tag_type == none_type
26163 && DECL_SELF_REFERENCE_P (decl)
26164 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26165 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26166 prefer_type_arg (tag_type),
26167 /*complain=*/true);
26169 /* If we have a single function from a using decl, pull it out. */
26170 if (TREE_CODE (decl) == OVERLOAD
26171 && !really_overloaded_fn (decl))
26172 decl = OVL_FUNCTION (decl);
26174 if (pushed_scope)
26175 pop_scope (pushed_scope);
26178 /* If the scope is a dependent type and either we deferred lookup or
26179 we did lookup but didn't find the name, rememeber the name. */
26180 if (decl == error_mark_node && TYPE_P (parser->scope)
26181 && dependent_type_p (parser->scope))
26183 if (tag_type)
26185 tree type;
26187 /* The resolution to Core Issue 180 says that `struct
26188 A::B' should be considered a type-name, even if `A'
26189 is dependent. */
26190 type = make_typename_type (parser->scope, name, tag_type,
26191 /*complain=*/tf_error);
26192 if (type != error_mark_node)
26193 decl = TYPE_NAME (type);
26195 else if (is_template
26196 && (cp_parser_next_token_ends_template_argument_p (parser)
26197 || cp_lexer_next_token_is (parser->lexer,
26198 CPP_CLOSE_PAREN)))
26199 decl = make_unbound_class_template (parser->scope,
26200 name, NULL_TREE,
26201 /*complain=*/tf_error);
26202 else
26203 decl = build_qualified_name (/*type=*/NULL_TREE,
26204 parser->scope, name,
26205 is_template);
26207 parser->qualifying_scope = parser->scope;
26208 parser->object_scope = NULL_TREE;
26210 else if (object_type)
26212 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26213 OBJECT_TYPE is not a class. */
26214 if (CLASS_TYPE_P (object_type))
26215 /* If the OBJECT_TYPE is a template specialization, it may
26216 be instantiated during name lookup. In that case, errors
26217 may be issued. Even if we rollback the current tentative
26218 parse, those errors are valid. */
26219 decl = lookup_member (object_type,
26220 name,
26221 /*protect=*/0,
26222 prefer_type_arg (tag_type),
26223 tf_warning_or_error);
26224 else
26225 decl = NULL_TREE;
26227 if (!decl)
26228 /* Look it up in the enclosing context. DR 141: When looking for a
26229 template-name after -> or ., only consider class templates. */
26230 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26231 /*nonclass=*/0,
26232 /*block_p=*/true, is_namespace, 0);
26233 if (object_type == unknown_type_node)
26234 /* The object is type-dependent, so we can't look anything up; we used
26235 this to get the DR 141 behavior. */
26236 object_type = NULL_TREE;
26237 parser->object_scope = object_type;
26238 parser->qualifying_scope = NULL_TREE;
26240 else
26242 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26243 /*nonclass=*/0,
26244 /*block_p=*/true, is_namespace, 0);
26245 parser->qualifying_scope = NULL_TREE;
26246 parser->object_scope = NULL_TREE;
26249 /* If the lookup failed, let our caller know. */
26250 if (!decl || decl == error_mark_node)
26251 return error_mark_node;
26253 /* Pull out the template from an injected-class-name (or multiple). */
26254 if (is_template)
26255 decl = maybe_get_template_decl_from_type_decl (decl);
26257 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26258 if (TREE_CODE (decl) == TREE_LIST)
26260 if (ambiguous_decls)
26261 *ambiguous_decls = decl;
26262 /* The error message we have to print is too complicated for
26263 cp_parser_error, so we incorporate its actions directly. */
26264 if (!cp_parser_simulate_error (parser))
26266 error_at (name_location, "reference to %qD is ambiguous",
26267 name);
26268 print_candidates (decl);
26270 return error_mark_node;
26273 gcc_assert (DECL_P (decl)
26274 || TREE_CODE (decl) == OVERLOAD
26275 || TREE_CODE (decl) == SCOPE_REF
26276 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26277 || BASELINK_P (decl));
26279 /* If we have resolved the name of a member declaration, check to
26280 see if the declaration is accessible. When the name resolves to
26281 set of overloaded functions, accessibility is checked when
26282 overload resolution is done.
26284 During an explicit instantiation, access is not checked at all,
26285 as per [temp.explicit]. */
26286 if (DECL_P (decl))
26287 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26289 maybe_record_typedef_use (decl);
26291 return cp_expr (decl, name_location);
26294 /* Like cp_parser_lookup_name, but for use in the typical case where
26295 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26296 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26298 static tree
26299 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26301 return cp_parser_lookup_name (parser, name,
26302 none_type,
26303 /*is_template=*/false,
26304 /*is_namespace=*/false,
26305 /*check_dependency=*/true,
26306 /*ambiguous_decls=*/NULL,
26307 location);
26310 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26311 the current context, return the TYPE_DECL. If TAG_NAME_P is
26312 true, the DECL indicates the class being defined in a class-head,
26313 or declared in an elaborated-type-specifier.
26315 Otherwise, return DECL. */
26317 static tree
26318 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26320 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26321 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26323 struct A {
26324 template <typename T> struct B;
26327 template <typename T> struct A::B {};
26329 Similarly, in an elaborated-type-specifier:
26331 namespace N { struct X{}; }
26333 struct A {
26334 template <typename T> friend struct N::X;
26337 However, if the DECL refers to a class type, and we are in
26338 the scope of the class, then the name lookup automatically
26339 finds the TYPE_DECL created by build_self_reference rather
26340 than a TEMPLATE_DECL. For example, in:
26342 template <class T> struct S {
26343 S s;
26346 there is no need to handle such case. */
26348 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26349 return DECL_TEMPLATE_RESULT (decl);
26351 return decl;
26354 /* If too many, or too few, template-parameter lists apply to the
26355 declarator, issue an error message. Returns TRUE if all went well,
26356 and FALSE otherwise. */
26358 static bool
26359 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26360 cp_declarator *declarator,
26361 location_t declarator_location)
26363 switch (declarator->kind)
26365 case cdk_id:
26367 unsigned num_templates = 0;
26368 tree scope = declarator->u.id.qualifying_scope;
26369 bool template_id_p = false;
26371 if (scope)
26372 num_templates = num_template_headers_for_class (scope);
26373 else if (TREE_CODE (declarator->u.id.unqualified_name)
26374 == TEMPLATE_ID_EXPR)
26376 /* If the DECLARATOR has the form `X<y>' then it uses one
26377 additional level of template parameters. */
26378 ++num_templates;
26379 template_id_p = true;
26382 return cp_parser_check_template_parameters
26383 (parser, num_templates, template_id_p, declarator_location,
26384 declarator);
26387 case cdk_function:
26388 case cdk_array:
26389 case cdk_pointer:
26390 case cdk_reference:
26391 case cdk_ptrmem:
26392 return (cp_parser_check_declarator_template_parameters
26393 (parser, declarator->declarator, declarator_location));
26395 case cdk_decomp:
26396 case cdk_error:
26397 return true;
26399 default:
26400 gcc_unreachable ();
26402 return false;
26405 /* NUM_TEMPLATES were used in the current declaration. If that is
26406 invalid, return FALSE and issue an error messages. Otherwise,
26407 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26408 declarator and we can print more accurate diagnostics. */
26410 static bool
26411 cp_parser_check_template_parameters (cp_parser* parser,
26412 unsigned num_templates,
26413 bool template_id_p,
26414 location_t location,
26415 cp_declarator *declarator)
26417 /* If there are the same number of template classes and parameter
26418 lists, that's OK. */
26419 if (parser->num_template_parameter_lists == num_templates)
26420 return true;
26421 /* If there are more, but only one more, and the name ends in an identifier,
26422 then we are declaring a primary template. That's OK too. */
26423 if (!template_id_p
26424 && parser->num_template_parameter_lists == num_templates + 1)
26425 return true;
26426 /* If there are more template classes than parameter lists, we have
26427 something like:
26429 template <class T> void S<T>::R<T>::f (); */
26430 if (parser->num_template_parameter_lists < num_templates)
26432 if (declarator && !current_function_decl)
26433 error_at (location, "specializing member %<%T::%E%> "
26434 "requires %<template<>%> syntax",
26435 declarator->u.id.qualifying_scope,
26436 declarator->u.id.unqualified_name);
26437 else if (declarator)
26438 error_at (location, "invalid declaration of %<%T::%E%>",
26439 declarator->u.id.qualifying_scope,
26440 declarator->u.id.unqualified_name);
26441 else
26442 error_at (location, "too few template-parameter-lists");
26443 return false;
26445 /* Otherwise, there are too many template parameter lists. We have
26446 something like:
26448 template <class T> template <class U> void S::f(); */
26449 error_at (location, "too many template-parameter-lists");
26450 return false;
26453 /* Parse an optional `::' token indicating that the following name is
26454 from the global namespace. If so, PARSER->SCOPE is set to the
26455 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26456 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26457 Returns the new value of PARSER->SCOPE, if the `::' token is
26458 present, and NULL_TREE otherwise. */
26460 static tree
26461 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26463 cp_token *token;
26465 /* Peek at the next token. */
26466 token = cp_lexer_peek_token (parser->lexer);
26467 /* If we're looking at a `::' token then we're starting from the
26468 global namespace, not our current location. */
26469 if (token->type == CPP_SCOPE)
26471 /* Consume the `::' token. */
26472 cp_lexer_consume_token (parser->lexer);
26473 /* Set the SCOPE so that we know where to start the lookup. */
26474 parser->scope = global_namespace;
26475 parser->qualifying_scope = global_namespace;
26476 parser->object_scope = NULL_TREE;
26478 return parser->scope;
26480 else if (!current_scope_valid_p)
26482 parser->scope = NULL_TREE;
26483 parser->qualifying_scope = NULL_TREE;
26484 parser->object_scope = NULL_TREE;
26487 return NULL_TREE;
26490 /* Returns TRUE if the upcoming token sequence is the start of a
26491 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26492 declarator is preceded by the `friend' specifier. */
26494 static bool
26495 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26497 bool constructor_p;
26498 bool outside_class_specifier_p;
26499 tree nested_name_specifier;
26500 cp_token *next_token;
26502 /* The common case is that this is not a constructor declarator, so
26503 try to avoid doing lots of work if at all possible. It's not
26504 valid declare a constructor at function scope. */
26505 if (parser->in_function_body)
26506 return false;
26507 /* And only certain tokens can begin a constructor declarator. */
26508 next_token = cp_lexer_peek_token (parser->lexer);
26509 if (next_token->type != CPP_NAME
26510 && next_token->type != CPP_SCOPE
26511 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26512 && next_token->type != CPP_TEMPLATE_ID)
26513 return false;
26515 /* Parse tentatively; we are going to roll back all of the tokens
26516 consumed here. */
26517 cp_parser_parse_tentatively (parser);
26518 /* Assume that we are looking at a constructor declarator. */
26519 constructor_p = true;
26521 /* Look for the optional `::' operator. */
26522 cp_parser_global_scope_opt (parser,
26523 /*current_scope_valid_p=*/false);
26524 /* Look for the nested-name-specifier. */
26525 nested_name_specifier
26526 = (cp_parser_nested_name_specifier_opt (parser,
26527 /*typename_keyword_p=*/false,
26528 /*check_dependency_p=*/false,
26529 /*type_p=*/false,
26530 /*is_declaration=*/false));
26532 outside_class_specifier_p = (!at_class_scope_p ()
26533 || !TYPE_BEING_DEFINED (current_class_type)
26534 || friend_p);
26536 /* Outside of a class-specifier, there must be a
26537 nested-name-specifier. Except in C++17 mode, where we
26538 might be declaring a guiding declaration. */
26539 if (!nested_name_specifier && outside_class_specifier_p
26540 && cxx_dialect < cxx17)
26541 constructor_p = false;
26542 else if (nested_name_specifier == error_mark_node)
26543 constructor_p = false;
26545 /* If we have a class scope, this is easy; DR 147 says that S::S always
26546 names the constructor, and no other qualified name could. */
26547 if (constructor_p && nested_name_specifier
26548 && CLASS_TYPE_P (nested_name_specifier))
26550 tree id = cp_parser_unqualified_id (parser,
26551 /*template_keyword_p=*/false,
26552 /*check_dependency_p=*/false,
26553 /*declarator_p=*/true,
26554 /*optional_p=*/false);
26555 if (is_overloaded_fn (id))
26556 id = DECL_NAME (get_first_fn (id));
26557 if (!constructor_name_p (id, nested_name_specifier))
26558 constructor_p = false;
26560 /* If we still think that this might be a constructor-declarator,
26561 look for a class-name. */
26562 else if (constructor_p)
26564 /* If we have:
26566 template <typename T> struct S {
26567 S();
26570 we must recognize that the nested `S' names a class. */
26571 if (cxx_dialect >= cxx17)
26572 cp_parser_parse_tentatively (parser);
26574 tree type_decl;
26575 type_decl = cp_parser_class_name (parser,
26576 /*typename_keyword_p=*/false,
26577 /*template_keyword_p=*/false,
26578 none_type,
26579 /*check_dependency_p=*/false,
26580 /*class_head_p=*/false,
26581 /*is_declaration=*/false);
26583 if (cxx_dialect >= cxx17
26584 && !cp_parser_parse_definitely (parser))
26586 type_decl = NULL_TREE;
26587 tree tmpl = cp_parser_template_name (parser,
26588 /*template_keyword*/false,
26589 /*check_dependency_p*/false,
26590 /*is_declaration*/false,
26591 none_type,
26592 /*is_identifier*/NULL);
26593 if (DECL_CLASS_TEMPLATE_P (tmpl)
26594 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26595 /* It's a deduction guide, return true. */;
26596 else
26597 cp_parser_simulate_error (parser);
26600 /* If there was no class-name, then this is not a constructor.
26601 Otherwise, if we are in a class-specifier and we aren't
26602 handling a friend declaration, check that its type matches
26603 current_class_type (c++/38313). Note: error_mark_node
26604 is left alone for error recovery purposes. */
26605 constructor_p = (!cp_parser_error_occurred (parser)
26606 && (outside_class_specifier_p
26607 || type_decl == NULL_TREE
26608 || type_decl == error_mark_node
26609 || same_type_p (current_class_type,
26610 TREE_TYPE (type_decl))));
26612 /* If we're still considering a constructor, we have to see a `(',
26613 to begin the parameter-declaration-clause, followed by either a
26614 `)', an `...', or a decl-specifier. We need to check for a
26615 type-specifier to avoid being fooled into thinking that:
26617 S (f) (int);
26619 is a constructor. (It is actually a function named `f' that
26620 takes one parameter (of type `int') and returns a value of type
26621 `S'. */
26622 if (constructor_p
26623 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26624 constructor_p = false;
26626 if (constructor_p
26627 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26628 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26629 /* A parameter declaration begins with a decl-specifier,
26630 which is either the "attribute" keyword, a storage class
26631 specifier, or (usually) a type-specifier. */
26632 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26634 tree type;
26635 tree pushed_scope = NULL_TREE;
26636 unsigned saved_num_template_parameter_lists;
26638 /* Names appearing in the type-specifier should be looked up
26639 in the scope of the class. */
26640 if (current_class_type)
26641 type = NULL_TREE;
26642 else if (type_decl)
26644 type = TREE_TYPE (type_decl);
26645 if (TREE_CODE (type) == TYPENAME_TYPE)
26647 type = resolve_typename_type (type,
26648 /*only_current_p=*/false);
26649 if (TREE_CODE (type) == TYPENAME_TYPE)
26651 cp_parser_abort_tentative_parse (parser);
26652 return false;
26655 pushed_scope = push_scope (type);
26658 /* Inside the constructor parameter list, surrounding
26659 template-parameter-lists do not apply. */
26660 saved_num_template_parameter_lists
26661 = parser->num_template_parameter_lists;
26662 parser->num_template_parameter_lists = 0;
26664 /* Look for the type-specifier. */
26665 cp_parser_type_specifier (parser,
26666 CP_PARSER_FLAGS_NONE,
26667 /*decl_specs=*/NULL,
26668 /*is_declarator=*/true,
26669 /*declares_class_or_enum=*/NULL,
26670 /*is_cv_qualifier=*/NULL);
26672 parser->num_template_parameter_lists
26673 = saved_num_template_parameter_lists;
26675 /* Leave the scope of the class. */
26676 if (pushed_scope)
26677 pop_scope (pushed_scope);
26679 constructor_p = !cp_parser_error_occurred (parser);
26683 /* We did not really want to consume any tokens. */
26684 cp_parser_abort_tentative_parse (parser);
26686 return constructor_p;
26689 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26690 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26691 they must be performed once we are in the scope of the function.
26693 Returns the function defined. */
26695 static tree
26696 cp_parser_function_definition_from_specifiers_and_declarator
26697 (cp_parser* parser,
26698 cp_decl_specifier_seq *decl_specifiers,
26699 tree attributes,
26700 const cp_declarator *declarator)
26702 tree fn;
26703 bool success_p;
26705 /* Begin the function-definition. */
26706 success_p = start_function (decl_specifiers, declarator, attributes);
26708 /* The things we're about to see are not directly qualified by any
26709 template headers we've seen thus far. */
26710 reset_specialization ();
26712 /* If there were names looked up in the decl-specifier-seq that we
26713 did not check, check them now. We must wait until we are in the
26714 scope of the function to perform the checks, since the function
26715 might be a friend. */
26716 perform_deferred_access_checks (tf_warning_or_error);
26718 if (success_p)
26720 cp_finalize_omp_declare_simd (parser, current_function_decl);
26721 parser->omp_declare_simd = NULL;
26722 cp_finalize_oacc_routine (parser, current_function_decl, true);
26723 parser->oacc_routine = NULL;
26726 if (!success_p)
26728 /* Skip the entire function. */
26729 cp_parser_skip_to_end_of_block_or_statement (parser);
26730 fn = error_mark_node;
26732 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26734 /* Seen already, skip it. An error message has already been output. */
26735 cp_parser_skip_to_end_of_block_or_statement (parser);
26736 fn = current_function_decl;
26737 current_function_decl = NULL_TREE;
26738 /* If this is a function from a class, pop the nested class. */
26739 if (current_class_name)
26740 pop_nested_class ();
26742 else
26744 timevar_id_t tv;
26745 if (DECL_DECLARED_INLINE_P (current_function_decl))
26746 tv = TV_PARSE_INLINE;
26747 else
26748 tv = TV_PARSE_FUNC;
26749 timevar_push (tv);
26750 fn = cp_parser_function_definition_after_declarator (parser,
26751 /*inline_p=*/false);
26752 timevar_pop (tv);
26755 return fn;
26758 /* Parse the part of a function-definition that follows the
26759 declarator. INLINE_P is TRUE iff this function is an inline
26760 function defined within a class-specifier.
26762 Returns the function defined. */
26764 static tree
26765 cp_parser_function_definition_after_declarator (cp_parser* parser,
26766 bool inline_p)
26768 tree fn;
26769 bool saved_in_unbraced_linkage_specification_p;
26770 bool saved_in_function_body;
26771 unsigned saved_num_template_parameter_lists;
26772 cp_token *token;
26773 bool fully_implicit_function_template_p
26774 = parser->fully_implicit_function_template_p;
26775 parser->fully_implicit_function_template_p = false;
26776 tree implicit_template_parms
26777 = parser->implicit_template_parms;
26778 parser->implicit_template_parms = 0;
26779 cp_binding_level* implicit_template_scope
26780 = parser->implicit_template_scope;
26781 parser->implicit_template_scope = 0;
26783 saved_in_function_body = parser->in_function_body;
26784 parser->in_function_body = true;
26785 /* If the next token is `return', then the code may be trying to
26786 make use of the "named return value" extension that G++ used to
26787 support. */
26788 token = cp_lexer_peek_token (parser->lexer);
26789 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26791 /* Consume the `return' keyword. */
26792 cp_lexer_consume_token (parser->lexer);
26793 /* Look for the identifier that indicates what value is to be
26794 returned. */
26795 cp_parser_identifier (parser);
26796 /* Issue an error message. */
26797 error_at (token->location,
26798 "named return values are no longer supported");
26799 /* Skip tokens until we reach the start of the function body. */
26800 while (true)
26802 cp_token *token = cp_lexer_peek_token (parser->lexer);
26803 if (token->type == CPP_OPEN_BRACE
26804 || token->type == CPP_EOF
26805 || token->type == CPP_PRAGMA_EOL)
26806 break;
26807 cp_lexer_consume_token (parser->lexer);
26810 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26811 anything declared inside `f'. */
26812 saved_in_unbraced_linkage_specification_p
26813 = parser->in_unbraced_linkage_specification_p;
26814 parser->in_unbraced_linkage_specification_p = false;
26815 /* Inside the function, surrounding template-parameter-lists do not
26816 apply. */
26817 saved_num_template_parameter_lists
26818 = parser->num_template_parameter_lists;
26819 parser->num_template_parameter_lists = 0;
26821 /* If the next token is `try', `__transaction_atomic', or
26822 `__transaction_relaxed`, then we are looking at either function-try-block
26823 or function-transaction-block. Note that all of these include the
26824 function-body. */
26825 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26826 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26827 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26828 RID_TRANSACTION_RELAXED))
26829 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26830 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26831 cp_parser_function_try_block (parser);
26832 else
26833 cp_parser_ctor_initializer_opt_and_function_body
26834 (parser, /*in_function_try_block=*/false);
26836 /* Finish the function. */
26837 fn = finish_function (inline_p);
26838 /* Generate code for it, if necessary. */
26839 expand_or_defer_fn (fn);
26840 /* Restore the saved values. */
26841 parser->in_unbraced_linkage_specification_p
26842 = saved_in_unbraced_linkage_specification_p;
26843 parser->num_template_parameter_lists
26844 = saved_num_template_parameter_lists;
26845 parser->in_function_body = saved_in_function_body;
26847 parser->fully_implicit_function_template_p
26848 = fully_implicit_function_template_p;
26849 parser->implicit_template_parms
26850 = implicit_template_parms;
26851 parser->implicit_template_scope
26852 = implicit_template_scope;
26854 if (parser->fully_implicit_function_template_p)
26855 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26857 return fn;
26860 /* Parse a template-declaration body (following argument list). */
26862 static void
26863 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26864 tree parameter_list,
26865 bool member_p)
26867 tree decl = NULL_TREE;
26868 bool friend_p = false;
26870 /* We just processed one more parameter list. */
26871 ++parser->num_template_parameter_lists;
26873 /* Get the deferred access checks from the parameter list. These
26874 will be checked once we know what is being declared, as for a
26875 member template the checks must be performed in the scope of the
26876 class containing the member. */
26877 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26879 /* Tentatively parse for a new template parameter list, which can either be
26880 the template keyword or a template introduction. */
26881 if (cp_parser_template_declaration_after_export (parser, member_p))
26882 /* OK */;
26883 else if (cxx_dialect >= cxx11
26884 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26885 decl = cp_parser_alias_declaration (parser);
26886 else
26888 /* There are no access checks when parsing a template, as we do not
26889 know if a specialization will be a friend. */
26890 push_deferring_access_checks (dk_no_check);
26891 cp_token *token = cp_lexer_peek_token (parser->lexer);
26892 decl = cp_parser_single_declaration (parser,
26893 checks,
26894 member_p,
26895 /*explicit_specialization_p=*/false,
26896 &friend_p);
26897 pop_deferring_access_checks ();
26899 /* If this is a member template declaration, let the front
26900 end know. */
26901 if (member_p && !friend_p && decl)
26903 if (TREE_CODE (decl) == TYPE_DECL)
26904 cp_parser_check_access_in_redeclaration (decl, token->location);
26906 decl = finish_member_template_decl (decl);
26908 else if (friend_p && decl
26909 && DECL_DECLARES_TYPE_P (decl))
26910 make_friend_class (current_class_type, TREE_TYPE (decl),
26911 /*complain=*/true);
26913 /* We are done with the current parameter list. */
26914 --parser->num_template_parameter_lists;
26916 pop_deferring_access_checks ();
26918 /* Finish up. */
26919 finish_template_decl (parameter_list);
26921 /* Check the template arguments for a literal operator template. */
26922 if (decl
26923 && DECL_DECLARES_FUNCTION_P (decl)
26924 && UDLIT_OPER_P (DECL_NAME (decl)))
26926 bool ok = true;
26927 if (parameter_list == NULL_TREE)
26928 ok = false;
26929 else
26931 int num_parms = TREE_VEC_LENGTH (parameter_list);
26932 if (num_parms == 1)
26934 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26935 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26936 if (TREE_TYPE (parm) != char_type_node
26937 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26938 ok = false;
26940 else if (num_parms == 2 && cxx_dialect >= cxx14)
26942 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26943 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26944 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26945 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26946 if (parm == error_mark_node
26947 || TREE_TYPE (parm) != TREE_TYPE (type)
26948 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26949 ok = false;
26951 else
26952 ok = false;
26954 if (!ok)
26956 if (cxx_dialect >= cxx14)
26957 error ("literal operator template %qD has invalid parameter list."
26958 " Expected non-type template argument pack <char...>"
26959 " or <typename CharT, CharT...>",
26960 decl);
26961 else
26962 error ("literal operator template %qD has invalid parameter list."
26963 " Expected non-type template argument pack <char...>",
26964 decl);
26968 /* Register member declarations. */
26969 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26970 finish_member_declaration (decl);
26971 /* If DECL is a function template, we must return to parse it later.
26972 (Even though there is no definition, there might be default
26973 arguments that need handling.) */
26974 if (member_p && decl
26975 && DECL_DECLARES_FUNCTION_P (decl))
26976 vec_safe_push (unparsed_funs_with_definitions, decl);
26979 /* Parse a template introduction header for a template-declaration. Returns
26980 false if tentative parse fails. */
26982 static bool
26983 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26985 cp_parser_parse_tentatively (parser);
26987 tree saved_scope = parser->scope;
26988 tree saved_object_scope = parser->object_scope;
26989 tree saved_qualifying_scope = parser->qualifying_scope;
26991 /* Look for the optional `::' operator. */
26992 cp_parser_global_scope_opt (parser,
26993 /*current_scope_valid_p=*/false);
26994 /* Look for the nested-name-specifier. */
26995 cp_parser_nested_name_specifier_opt (parser,
26996 /*typename_keyword_p=*/false,
26997 /*check_dependency_p=*/true,
26998 /*type_p=*/false,
26999 /*is_declaration=*/false);
27001 cp_token *token = cp_lexer_peek_token (parser->lexer);
27002 tree concept_name = cp_parser_identifier (parser);
27004 /* Look up the concept for which we will be matching
27005 template parameters. */
27006 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27007 token->location);
27008 parser->scope = saved_scope;
27009 parser->object_scope = saved_object_scope;
27010 parser->qualifying_scope = saved_qualifying_scope;
27012 if (concept_name == error_mark_node)
27013 cp_parser_simulate_error (parser);
27015 /* Look for opening brace for introduction. */
27016 matching_braces braces;
27017 braces.require_open (parser);
27019 if (!cp_parser_parse_definitely (parser))
27020 return false;
27022 push_deferring_access_checks (dk_deferred);
27024 /* Build vector of placeholder parameters and grab
27025 matching identifiers. */
27026 tree introduction_list = cp_parser_introduction_list (parser);
27028 /* The introduction-list shall not be empty. */
27029 int nargs = TREE_VEC_LENGTH (introduction_list);
27030 if (nargs == 0)
27032 error ("empty introduction-list");
27033 return true;
27036 /* Look for closing brace for introduction. */
27037 if (!braces.require_close (parser))
27038 return true;
27040 if (tmpl_decl == error_mark_node)
27042 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27043 token->location);
27044 return true;
27047 /* Build and associate the constraint. */
27048 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27049 if (parms && parms != error_mark_node)
27051 cp_parser_template_declaration_after_parameters (parser, parms,
27052 member_p);
27053 return true;
27056 error_at (token->location, "no matching concept for template-introduction");
27057 return true;
27060 /* Parse a normal template-declaration following the template keyword. */
27062 static void
27063 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27065 tree parameter_list;
27066 bool need_lang_pop;
27067 location_t location = input_location;
27069 /* Look for the `<' token. */
27070 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27071 return;
27072 if (at_class_scope_p () && current_function_decl)
27074 /* 14.5.2.2 [temp.mem]
27076 A local class shall not have member templates. */
27077 error_at (location,
27078 "invalid declaration of member template in local class");
27079 cp_parser_skip_to_end_of_block_or_statement (parser);
27080 return;
27082 /* [temp]
27084 A template ... shall not have C linkage. */
27085 if (current_lang_name == lang_name_c)
27087 error_at (location, "template with C linkage");
27088 maybe_show_extern_c_location ();
27089 /* Give it C++ linkage to avoid confusing other parts of the
27090 front end. */
27091 push_lang_context (lang_name_cplusplus);
27092 need_lang_pop = true;
27094 else
27095 need_lang_pop = false;
27097 /* We cannot perform access checks on the template parameter
27098 declarations until we know what is being declared, just as we
27099 cannot check the decl-specifier list. */
27100 push_deferring_access_checks (dk_deferred);
27102 /* If the next token is `>', then we have an invalid
27103 specialization. Rather than complain about an invalid template
27104 parameter, issue an error message here. */
27105 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27107 cp_parser_error (parser, "invalid explicit specialization");
27108 begin_specialization ();
27109 parameter_list = NULL_TREE;
27111 else
27113 /* Parse the template parameters. */
27114 parameter_list = cp_parser_template_parameter_list (parser);
27117 /* Look for the `>'. */
27118 cp_parser_skip_to_end_of_template_parameter_list (parser);
27120 /* Manage template requirements */
27121 if (flag_concepts)
27123 tree reqs = get_shorthand_constraints (current_template_parms);
27124 if (tree r = cp_parser_requires_clause_opt (parser))
27125 reqs = conjoin_constraints (reqs, normalize_expression (r));
27126 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27129 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27130 member_p);
27132 /* For the erroneous case of a template with C linkage, we pushed an
27133 implicit C++ linkage scope; exit that scope now. */
27134 if (need_lang_pop)
27135 pop_lang_context ();
27138 /* Parse a template-declaration, assuming that the `export' (and
27139 `extern') keywords, if present, has already been scanned. MEMBER_P
27140 is as for cp_parser_template_declaration. */
27142 static bool
27143 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27145 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27147 cp_lexer_consume_token (parser->lexer);
27148 cp_parser_explicit_template_declaration (parser, member_p);
27149 return true;
27151 else if (flag_concepts)
27152 return cp_parser_template_introduction (parser, member_p);
27154 return false;
27157 /* Perform the deferred access checks from a template-parameter-list.
27158 CHECKS is a TREE_LIST of access checks, as returned by
27159 get_deferred_access_checks. */
27161 static void
27162 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27164 ++processing_template_parmlist;
27165 perform_access_checks (checks, tf_warning_or_error);
27166 --processing_template_parmlist;
27169 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27170 `function-definition' sequence that follows a template header.
27171 If MEMBER_P is true, this declaration appears in a class scope.
27173 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27174 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27176 static tree
27177 cp_parser_single_declaration (cp_parser* parser,
27178 vec<deferred_access_check, va_gc> *checks,
27179 bool member_p,
27180 bool explicit_specialization_p,
27181 bool* friend_p)
27183 int declares_class_or_enum;
27184 tree decl = NULL_TREE;
27185 cp_decl_specifier_seq decl_specifiers;
27186 bool function_definition_p = false;
27187 cp_token *decl_spec_token_start;
27189 /* This function is only used when processing a template
27190 declaration. */
27191 gcc_assert (innermost_scope_kind () == sk_template_parms
27192 || innermost_scope_kind () == sk_template_spec);
27194 /* Defer access checks until we know what is being declared. */
27195 push_deferring_access_checks (dk_deferred);
27197 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27198 alternative. */
27199 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27200 cp_parser_decl_specifier_seq (parser,
27201 CP_PARSER_FLAGS_OPTIONAL,
27202 &decl_specifiers,
27203 &declares_class_or_enum);
27204 if (friend_p)
27205 *friend_p = cp_parser_friend_p (&decl_specifiers);
27207 /* There are no template typedefs. */
27208 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27210 error_at (decl_spec_token_start->location,
27211 "template declaration of %<typedef%>");
27212 decl = error_mark_node;
27215 /* Gather up the access checks that occurred the
27216 decl-specifier-seq. */
27217 stop_deferring_access_checks ();
27219 /* Check for the declaration of a template class. */
27220 if (declares_class_or_enum)
27222 if (cp_parser_declares_only_class_p (parser)
27223 || (declares_class_or_enum & 2))
27225 // If this is a declaration, but not a definition, associate
27226 // any constraints with the type declaration. Constraints
27227 // are associated with definitions in cp_parser_class_specifier.
27228 if (declares_class_or_enum == 1)
27229 associate_classtype_constraints (decl_specifiers.type);
27231 decl = shadow_tag (&decl_specifiers);
27233 /* In this case:
27235 struct C {
27236 friend template <typename T> struct A<T>::B;
27239 A<T>::B will be represented by a TYPENAME_TYPE, and
27240 therefore not recognized by shadow_tag. */
27241 if (friend_p && *friend_p
27242 && !decl
27243 && decl_specifiers.type
27244 && TYPE_P (decl_specifiers.type))
27245 decl = decl_specifiers.type;
27247 if (decl && decl != error_mark_node)
27248 decl = TYPE_NAME (decl);
27249 else
27250 decl = error_mark_node;
27252 /* Perform access checks for template parameters. */
27253 cp_parser_perform_template_parameter_access_checks (checks);
27255 /* Give a helpful diagnostic for
27256 template <class T> struct A { } a;
27257 if we aren't already recovering from an error. */
27258 if (!cp_parser_declares_only_class_p (parser)
27259 && !seen_error ())
27261 error_at (cp_lexer_peek_token (parser->lexer)->location,
27262 "a class template declaration must not declare "
27263 "anything else");
27264 cp_parser_skip_to_end_of_block_or_statement (parser);
27265 goto out;
27270 /* Complain about missing 'typename' or other invalid type names. */
27271 if (!decl_specifiers.any_type_specifiers_p
27272 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27274 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27275 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27276 the rest of this declaration. */
27277 decl = error_mark_node;
27278 goto out;
27281 /* If it's not a template class, try for a template function. If
27282 the next token is a `;', then this declaration does not declare
27283 anything. But, if there were errors in the decl-specifiers, then
27284 the error might well have come from an attempted class-specifier.
27285 In that case, there's no need to warn about a missing declarator. */
27286 if (!decl
27287 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27288 || decl_specifiers.type != error_mark_node))
27290 decl = cp_parser_init_declarator (parser,
27291 &decl_specifiers,
27292 checks,
27293 /*function_definition_allowed_p=*/true,
27294 member_p,
27295 declares_class_or_enum,
27296 &function_definition_p,
27297 NULL, NULL, NULL);
27299 /* 7.1.1-1 [dcl.stc]
27301 A storage-class-specifier shall not be specified in an explicit
27302 specialization... */
27303 if (decl
27304 && explicit_specialization_p
27305 && decl_specifiers.storage_class != sc_none)
27307 error_at (decl_spec_token_start->location,
27308 "explicit template specialization cannot have a storage class");
27309 decl = error_mark_node;
27312 if (decl && VAR_P (decl))
27313 check_template_variable (decl);
27316 /* Look for a trailing `;' after the declaration. */
27317 if (!function_definition_p
27318 && (decl == error_mark_node
27319 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27320 cp_parser_skip_to_end_of_block_or_statement (parser);
27322 out:
27323 pop_deferring_access_checks ();
27325 /* Clear any current qualification; whatever comes next is the start
27326 of something new. */
27327 parser->scope = NULL_TREE;
27328 parser->qualifying_scope = NULL_TREE;
27329 parser->object_scope = NULL_TREE;
27331 return decl;
27334 /* Parse a cast-expression that is not the operand of a unary "&". */
27336 static cp_expr
27337 cp_parser_simple_cast_expression (cp_parser *parser)
27339 return cp_parser_cast_expression (parser, /*address_p=*/false,
27340 /*cast_p=*/false, /*decltype*/false, NULL);
27343 /* Parse a functional cast to TYPE. Returns an expression
27344 representing the cast. */
27346 static cp_expr
27347 cp_parser_functional_cast (cp_parser* parser, tree type)
27349 vec<tree, va_gc> *vec;
27350 tree expression_list;
27351 cp_expr cast;
27352 bool nonconst_p;
27354 location_t start_loc = input_location;
27356 if (!type)
27357 type = error_mark_node;
27359 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27361 cp_lexer_set_source_position (parser->lexer);
27362 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27363 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27364 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27365 if (TREE_CODE (type) == TYPE_DECL)
27366 type = TREE_TYPE (type);
27368 cast = finish_compound_literal (type, expression_list,
27369 tf_warning_or_error, fcl_functional);
27370 /* Create a location of the form:
27371 type_name{i, f}
27372 ^~~~~~~~~~~~~~~
27373 with caret == start at the start of the type name,
27374 finishing at the closing brace. */
27375 location_t finish_loc
27376 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27377 location_t combined_loc = make_location (start_loc, start_loc,
27378 finish_loc);
27379 cast.set_location (combined_loc);
27380 return cast;
27384 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27385 /*cast_p=*/true,
27386 /*allow_expansion_p=*/true,
27387 /*non_constant_p=*/NULL);
27388 if (vec == NULL)
27389 expression_list = error_mark_node;
27390 else
27392 expression_list = build_tree_list_vec (vec);
27393 release_tree_vector (vec);
27396 cast = build_functional_cast (type, expression_list,
27397 tf_warning_or_error);
27398 /* [expr.const]/1: In an integral constant expression "only type
27399 conversions to integral or enumeration type can be used". */
27400 if (TREE_CODE (type) == TYPE_DECL)
27401 type = TREE_TYPE (type);
27402 if (cast != error_mark_node
27403 && !cast_valid_in_integral_constant_expression_p (type)
27404 && cp_parser_non_integral_constant_expression (parser,
27405 NIC_CONSTRUCTOR))
27406 return error_mark_node;
27408 /* Create a location of the form:
27409 float(i)
27410 ^~~~~~~~
27411 with caret == start at the start of the type name,
27412 finishing at the closing paren. */
27413 location_t finish_loc
27414 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27415 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27416 cast.set_location (combined_loc);
27417 return cast;
27420 /* Save the tokens that make up the body of a member function defined
27421 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27422 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27423 specifiers applied to the declaration. Returns the FUNCTION_DECL
27424 for the member function. */
27426 static tree
27427 cp_parser_save_member_function_body (cp_parser* parser,
27428 cp_decl_specifier_seq *decl_specifiers,
27429 cp_declarator *declarator,
27430 tree attributes)
27432 cp_token *first;
27433 cp_token *last;
27434 tree fn;
27435 bool function_try_block = false;
27437 /* Create the FUNCTION_DECL. */
27438 fn = grokmethod (decl_specifiers, declarator, attributes);
27439 cp_finalize_omp_declare_simd (parser, fn);
27440 cp_finalize_oacc_routine (parser, fn, true);
27441 /* If something went badly wrong, bail out now. */
27442 if (fn == error_mark_node)
27444 /* If there's a function-body, skip it. */
27445 if (cp_parser_token_starts_function_definition_p
27446 (cp_lexer_peek_token (parser->lexer)))
27447 cp_parser_skip_to_end_of_block_or_statement (parser);
27448 return error_mark_node;
27451 /* Remember it, if there default args to post process. */
27452 cp_parser_save_default_args (parser, fn);
27454 /* Save away the tokens that make up the body of the
27455 function. */
27456 first = parser->lexer->next_token;
27458 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27459 cp_lexer_consume_token (parser->lexer);
27460 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27461 RID_TRANSACTION_ATOMIC))
27463 cp_lexer_consume_token (parser->lexer);
27464 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27465 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27466 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27467 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27468 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27469 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27470 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27472 cp_lexer_consume_token (parser->lexer);
27473 cp_lexer_consume_token (parser->lexer);
27474 cp_lexer_consume_token (parser->lexer);
27475 cp_lexer_consume_token (parser->lexer);
27476 cp_lexer_consume_token (parser->lexer);
27478 else
27479 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27480 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27482 cp_lexer_consume_token (parser->lexer);
27483 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27484 break;
27488 /* Handle function try blocks. */
27489 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27491 cp_lexer_consume_token (parser->lexer);
27492 function_try_block = true;
27494 /* We can have braced-init-list mem-initializers before the fn body. */
27495 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27497 cp_lexer_consume_token (parser->lexer);
27498 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27500 /* cache_group will stop after an un-nested { } pair, too. */
27501 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27502 break;
27504 /* variadic mem-inits have ... after the ')'. */
27505 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27506 cp_lexer_consume_token (parser->lexer);
27509 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27510 /* Handle function try blocks. */
27511 if (function_try_block)
27512 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27513 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27514 last = parser->lexer->next_token;
27516 /* Save away the inline definition; we will process it when the
27517 class is complete. */
27518 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27519 DECL_PENDING_INLINE_P (fn) = 1;
27521 /* We need to know that this was defined in the class, so that
27522 friend templates are handled correctly. */
27523 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27525 /* Add FN to the queue of functions to be parsed later. */
27526 vec_safe_push (unparsed_funs_with_definitions, fn);
27528 return fn;
27531 /* Save the tokens that make up the in-class initializer for a non-static
27532 data member. Returns a DEFAULT_ARG. */
27534 static tree
27535 cp_parser_save_nsdmi (cp_parser* parser)
27537 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27540 /* Parse a template-argument-list, as well as the trailing ">" (but
27541 not the opening "<"). See cp_parser_template_argument_list for the
27542 return value. */
27544 static tree
27545 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27547 tree arguments;
27548 tree saved_scope;
27549 tree saved_qualifying_scope;
27550 tree saved_object_scope;
27551 bool saved_greater_than_is_operator_p;
27552 int saved_unevaluated_operand;
27553 int saved_inhibit_evaluation_warnings;
27555 /* [temp.names]
27557 When parsing a template-id, the first non-nested `>' is taken as
27558 the end of the template-argument-list rather than a greater-than
27559 operator. */
27560 saved_greater_than_is_operator_p
27561 = parser->greater_than_is_operator_p;
27562 parser->greater_than_is_operator_p = false;
27563 /* Parsing the argument list may modify SCOPE, so we save it
27564 here. */
27565 saved_scope = parser->scope;
27566 saved_qualifying_scope = parser->qualifying_scope;
27567 saved_object_scope = parser->object_scope;
27568 /* We need to evaluate the template arguments, even though this
27569 template-id may be nested within a "sizeof". */
27570 saved_unevaluated_operand = cp_unevaluated_operand;
27571 cp_unevaluated_operand = 0;
27572 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27573 c_inhibit_evaluation_warnings = 0;
27574 /* Parse the template-argument-list itself. */
27575 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27576 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27577 arguments = NULL_TREE;
27578 else
27579 arguments = cp_parser_template_argument_list (parser);
27580 /* Look for the `>' that ends the template-argument-list. If we find
27581 a '>>' instead, it's probably just a typo. */
27582 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27584 if (cxx_dialect != cxx98)
27586 /* In C++0x, a `>>' in a template argument list or cast
27587 expression is considered to be two separate `>'
27588 tokens. So, change the current token to a `>', but don't
27589 consume it: it will be consumed later when the outer
27590 template argument list (or cast expression) is parsed.
27591 Note that this replacement of `>' for `>>' is necessary
27592 even if we are parsing tentatively: in the tentative
27593 case, after calling
27594 cp_parser_enclosed_template_argument_list we will always
27595 throw away all of the template arguments and the first
27596 closing `>', either because the template argument list
27597 was erroneous or because we are replacing those tokens
27598 with a CPP_TEMPLATE_ID token. The second `>' (which will
27599 not have been thrown away) is needed either to close an
27600 outer template argument list or to complete a new-style
27601 cast. */
27602 cp_token *token = cp_lexer_peek_token (parser->lexer);
27603 token->type = CPP_GREATER;
27605 else if (!saved_greater_than_is_operator_p)
27607 /* If we're in a nested template argument list, the '>>' has
27608 to be a typo for '> >'. We emit the error message, but we
27609 continue parsing and we push a '>' as next token, so that
27610 the argument list will be parsed correctly. Note that the
27611 global source location is still on the token before the
27612 '>>', so we need to say explicitly where we want it. */
27613 cp_token *token = cp_lexer_peek_token (parser->lexer);
27614 gcc_rich_location richloc (token->location);
27615 richloc.add_fixit_replace ("> >");
27616 error_at (&richloc, "%<>>%> should be %<> >%> "
27617 "within a nested template argument list");
27619 token->type = CPP_GREATER;
27621 else
27623 /* If this is not a nested template argument list, the '>>'
27624 is a typo for '>'. Emit an error message and continue.
27625 Same deal about the token location, but here we can get it
27626 right by consuming the '>>' before issuing the diagnostic. */
27627 cp_token *token = cp_lexer_consume_token (parser->lexer);
27628 error_at (token->location,
27629 "spurious %<>>%>, use %<>%> to terminate "
27630 "a template argument list");
27633 else
27634 cp_parser_skip_to_end_of_template_parameter_list (parser);
27635 /* The `>' token might be a greater-than operator again now. */
27636 parser->greater_than_is_operator_p
27637 = saved_greater_than_is_operator_p;
27638 /* Restore the SAVED_SCOPE. */
27639 parser->scope = saved_scope;
27640 parser->qualifying_scope = saved_qualifying_scope;
27641 parser->object_scope = saved_object_scope;
27642 cp_unevaluated_operand = saved_unevaluated_operand;
27643 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27645 return arguments;
27648 /* MEMBER_FUNCTION is a member function, or a friend. If default
27649 arguments, or the body of the function have not yet been parsed,
27650 parse them now. */
27652 static void
27653 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27655 timevar_push (TV_PARSE_INMETH);
27656 /* If this member is a template, get the underlying
27657 FUNCTION_DECL. */
27658 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27659 member_function = DECL_TEMPLATE_RESULT (member_function);
27661 /* There should not be any class definitions in progress at this
27662 point; the bodies of members are only parsed outside of all class
27663 definitions. */
27664 gcc_assert (parser->num_classes_being_defined == 0);
27665 /* While we're parsing the member functions we might encounter more
27666 classes. We want to handle them right away, but we don't want
27667 them getting mixed up with functions that are currently in the
27668 queue. */
27669 push_unparsed_function_queues (parser);
27671 /* Make sure that any template parameters are in scope. */
27672 maybe_begin_member_template_processing (member_function);
27674 /* If the body of the function has not yet been parsed, parse it
27675 now. */
27676 if (DECL_PENDING_INLINE_P (member_function))
27678 tree function_scope;
27679 cp_token_cache *tokens;
27681 /* The function is no longer pending; we are processing it. */
27682 tokens = DECL_PENDING_INLINE_INFO (member_function);
27683 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27684 DECL_PENDING_INLINE_P (member_function) = 0;
27686 /* If this is a local class, enter the scope of the containing
27687 function. */
27688 function_scope = current_function_decl;
27689 if (function_scope)
27690 push_function_context ();
27692 /* Push the body of the function onto the lexer stack. */
27693 cp_parser_push_lexer_for_tokens (parser, tokens);
27695 /* Let the front end know that we going to be defining this
27696 function. */
27697 start_preparsed_function (member_function, NULL_TREE,
27698 SF_PRE_PARSED | SF_INCLASS_INLINE);
27700 /* Don't do access checking if it is a templated function. */
27701 if (processing_template_decl)
27702 push_deferring_access_checks (dk_no_check);
27704 /* #pragma omp declare reduction needs special parsing. */
27705 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27707 parser->lexer->in_pragma = true;
27708 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27709 finish_function (/*inline_p=*/true);
27710 cp_check_omp_declare_reduction (member_function);
27712 else
27713 /* Now, parse the body of the function. */
27714 cp_parser_function_definition_after_declarator (parser,
27715 /*inline_p=*/true);
27717 if (processing_template_decl)
27718 pop_deferring_access_checks ();
27720 /* Leave the scope of the containing function. */
27721 if (function_scope)
27722 pop_function_context ();
27723 cp_parser_pop_lexer (parser);
27726 /* Remove any template parameters from the symbol table. */
27727 maybe_end_member_template_processing ();
27729 /* Restore the queue. */
27730 pop_unparsed_function_queues (parser);
27731 timevar_pop (TV_PARSE_INMETH);
27734 /* If DECL contains any default args, remember it on the unparsed
27735 functions queue. */
27737 static void
27738 cp_parser_save_default_args (cp_parser* parser, tree decl)
27740 tree probe;
27742 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27743 probe;
27744 probe = TREE_CHAIN (probe))
27745 if (TREE_PURPOSE (probe))
27747 cp_default_arg_entry entry = {current_class_type, decl};
27748 vec_safe_push (unparsed_funs_with_default_args, entry);
27749 break;
27753 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27754 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27755 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27756 from the parameter-type-list. */
27758 static tree
27759 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27760 tree default_arg, tree parmtype)
27762 cp_token_cache *tokens;
27763 tree parsed_arg;
27764 bool dummy;
27766 if (default_arg == error_mark_node)
27767 return error_mark_node;
27769 /* Push the saved tokens for the default argument onto the parser's
27770 lexer stack. */
27771 tokens = DEFARG_TOKENS (default_arg);
27772 cp_parser_push_lexer_for_tokens (parser, tokens);
27774 start_lambda_scope (decl);
27776 /* Parse the default argument. */
27777 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27778 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27779 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27781 finish_lambda_scope ();
27783 if (parsed_arg == error_mark_node)
27784 cp_parser_skip_to_end_of_statement (parser);
27786 if (!processing_template_decl)
27788 /* In a non-template class, check conversions now. In a template,
27789 we'll wait and instantiate these as needed. */
27790 if (TREE_CODE (decl) == PARM_DECL)
27791 parsed_arg = check_default_argument (parmtype, parsed_arg,
27792 tf_warning_or_error);
27793 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27794 parsed_arg = error_mark_node;
27795 else
27796 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27799 /* If the token stream has not been completely used up, then
27800 there was extra junk after the end of the default
27801 argument. */
27802 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27804 if (TREE_CODE (decl) == PARM_DECL)
27805 cp_parser_error (parser, "expected %<,%>");
27806 else
27807 cp_parser_error (parser, "expected %<;%>");
27810 /* Revert to the main lexer. */
27811 cp_parser_pop_lexer (parser);
27813 return parsed_arg;
27816 /* FIELD is a non-static data member with an initializer which we saved for
27817 later; parse it now. */
27819 static void
27820 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27822 tree def;
27824 maybe_begin_member_template_processing (field);
27826 push_unparsed_function_queues (parser);
27827 def = cp_parser_late_parse_one_default_arg (parser, field,
27828 DECL_INITIAL (field),
27829 NULL_TREE);
27830 pop_unparsed_function_queues (parser);
27832 maybe_end_member_template_processing ();
27834 DECL_INITIAL (field) = def;
27837 /* FN is a FUNCTION_DECL which may contains a parameter with an
27838 unparsed DEFAULT_ARG. Parse the default args now. This function
27839 assumes that the current scope is the scope in which the default
27840 argument should be processed. */
27842 static void
27843 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27845 bool saved_local_variables_forbidden_p;
27846 tree parm, parmdecl;
27848 /* While we're parsing the default args, we might (due to the
27849 statement expression extension) encounter more classes. We want
27850 to handle them right away, but we don't want them getting mixed
27851 up with default args that are currently in the queue. */
27852 push_unparsed_function_queues (parser);
27854 /* Local variable names (and the `this' keyword) may not appear
27855 in a default argument. */
27856 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27857 parser->local_variables_forbidden_p = true;
27859 push_defarg_context (fn);
27861 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27862 parmdecl = DECL_ARGUMENTS (fn);
27863 parm && parm != void_list_node;
27864 parm = TREE_CHAIN (parm),
27865 parmdecl = DECL_CHAIN (parmdecl))
27867 tree default_arg = TREE_PURPOSE (parm);
27868 tree parsed_arg;
27869 vec<tree, va_gc> *insts;
27870 tree copy;
27871 unsigned ix;
27873 if (!default_arg)
27874 continue;
27876 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27877 /* This can happen for a friend declaration for a function
27878 already declared with default arguments. */
27879 continue;
27881 parsed_arg
27882 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27883 default_arg,
27884 TREE_VALUE (parm));
27885 TREE_PURPOSE (parm) = parsed_arg;
27887 /* Update any instantiations we've already created. */
27888 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27889 vec_safe_iterate (insts, ix, &copy); ix++)
27890 TREE_PURPOSE (copy) = parsed_arg;
27893 pop_defarg_context ();
27895 /* Make sure no default arg is missing. */
27896 check_default_args (fn);
27898 /* Restore the state of local_variables_forbidden_p. */
27899 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27901 /* Restore the queue. */
27902 pop_unparsed_function_queues (parser);
27905 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27907 sizeof ... ( identifier )
27909 where the 'sizeof' token has already been consumed. */
27911 static tree
27912 cp_parser_sizeof_pack (cp_parser *parser)
27914 /* Consume the `...'. */
27915 cp_lexer_consume_token (parser->lexer);
27916 maybe_warn_variadic_templates ();
27918 matching_parens parens;
27919 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27920 if (paren)
27921 parens.consume_open (parser);
27922 else
27923 permerror (cp_lexer_peek_token (parser->lexer)->location,
27924 "%<sizeof...%> argument must be surrounded by parentheses");
27926 cp_token *token = cp_lexer_peek_token (parser->lexer);
27927 tree name = cp_parser_identifier (parser);
27928 if (name == error_mark_node)
27929 return error_mark_node;
27930 /* The name is not qualified. */
27931 parser->scope = NULL_TREE;
27932 parser->qualifying_scope = NULL_TREE;
27933 parser->object_scope = NULL_TREE;
27934 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27935 if (expr == error_mark_node)
27936 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27937 token->location);
27938 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27939 expr = TREE_TYPE (expr);
27940 else if (TREE_CODE (expr) == CONST_DECL)
27941 expr = DECL_INITIAL (expr);
27942 expr = make_pack_expansion (expr);
27943 PACK_EXPANSION_SIZEOF_P (expr) = true;
27945 if (paren)
27946 parens.require_close (parser);
27948 return expr;
27951 /* Parse the operand of `sizeof' (or a similar operator). Returns
27952 either a TYPE or an expression, depending on the form of the
27953 input. The KEYWORD indicates which kind of expression we have
27954 encountered. */
27956 static tree
27957 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27959 tree expr = NULL_TREE;
27960 const char *saved_message;
27961 char *tmp;
27962 bool saved_integral_constant_expression_p;
27963 bool saved_non_integral_constant_expression_p;
27965 /* If it's a `...', then we are computing the length of a parameter
27966 pack. */
27967 if (keyword == RID_SIZEOF
27968 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27969 return cp_parser_sizeof_pack (parser);
27971 /* Types cannot be defined in a `sizeof' expression. Save away the
27972 old message. */
27973 saved_message = parser->type_definition_forbidden_message;
27974 /* And create the new one. */
27975 tmp = concat ("types may not be defined in %<",
27976 IDENTIFIER_POINTER (ridpointers[keyword]),
27977 "%> expressions", NULL);
27978 parser->type_definition_forbidden_message = tmp;
27980 /* The restrictions on constant-expressions do not apply inside
27981 sizeof expressions. */
27982 saved_integral_constant_expression_p
27983 = parser->integral_constant_expression_p;
27984 saved_non_integral_constant_expression_p
27985 = parser->non_integral_constant_expression_p;
27986 parser->integral_constant_expression_p = false;
27988 /* Do not actually evaluate the expression. */
27989 ++cp_unevaluated_operand;
27990 ++c_inhibit_evaluation_warnings;
27991 /* If it's a `(', then we might be looking at the type-id
27992 construction. */
27993 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27995 tree type = NULL_TREE;
27997 /* We can't be sure yet whether we're looking at a type-id or an
27998 expression. */
27999 cp_parser_parse_tentatively (parser);
28001 matching_parens parens;
28002 parens.consume_open (parser);
28004 /* Note: as a GNU Extension, compound literals are considered
28005 postfix-expressions as they are in C99, so they are valid
28006 arguments to sizeof. See comment in cp_parser_cast_expression
28007 for details. */
28008 if (cp_parser_compound_literal_p (parser))
28009 cp_parser_simulate_error (parser);
28010 else
28012 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28013 parser->in_type_id_in_expr_p = true;
28014 /* Look for the type-id. */
28015 type = cp_parser_type_id (parser);
28016 /* Look for the closing `)'. */
28017 parens.require_close (parser);
28018 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28021 /* If all went well, then we're done. */
28022 if (cp_parser_parse_definitely (parser))
28024 cp_decl_specifier_seq decl_specs;
28026 /* Build a trivial decl-specifier-seq. */
28027 clear_decl_specs (&decl_specs);
28028 decl_specs.type = type;
28030 /* Call grokdeclarator to figure out what type this is. */
28031 expr = grokdeclarator (NULL,
28032 &decl_specs,
28033 TYPENAME,
28034 /*initialized=*/0,
28035 /*attrlist=*/NULL);
28039 /* If the type-id production did not work out, then we must be
28040 looking at the unary-expression production. */
28041 if (!expr)
28042 expr = cp_parser_unary_expression (parser);
28044 /* Go back to evaluating expressions. */
28045 --cp_unevaluated_operand;
28046 --c_inhibit_evaluation_warnings;
28048 /* Free the message we created. */
28049 free (tmp);
28050 /* And restore the old one. */
28051 parser->type_definition_forbidden_message = saved_message;
28052 parser->integral_constant_expression_p
28053 = saved_integral_constant_expression_p;
28054 parser->non_integral_constant_expression_p
28055 = saved_non_integral_constant_expression_p;
28057 return expr;
28060 /* If the current declaration has no declarator, return true. */
28062 static bool
28063 cp_parser_declares_only_class_p (cp_parser *parser)
28065 /* If the next token is a `;' or a `,' then there is no
28066 declarator. */
28067 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28068 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28071 /* Update the DECL_SPECS to reflect the storage class indicated by
28072 KEYWORD. */
28074 static void
28075 cp_parser_set_storage_class (cp_parser *parser,
28076 cp_decl_specifier_seq *decl_specs,
28077 enum rid keyword,
28078 cp_token *token)
28080 cp_storage_class storage_class;
28082 if (parser->in_unbraced_linkage_specification_p)
28084 error_at (token->location, "invalid use of %qD in linkage specification",
28085 ridpointers[keyword]);
28086 return;
28088 else if (decl_specs->storage_class != sc_none)
28090 decl_specs->conflicting_specifiers_p = true;
28091 return;
28094 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28095 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28096 && decl_specs->gnu_thread_keyword_p)
28098 pedwarn (decl_specs->locations[ds_thread], 0,
28099 "%<__thread%> before %qD", ridpointers[keyword]);
28102 switch (keyword)
28104 case RID_AUTO:
28105 storage_class = sc_auto;
28106 break;
28107 case RID_REGISTER:
28108 storage_class = sc_register;
28109 break;
28110 case RID_STATIC:
28111 storage_class = sc_static;
28112 break;
28113 case RID_EXTERN:
28114 storage_class = sc_extern;
28115 break;
28116 case RID_MUTABLE:
28117 storage_class = sc_mutable;
28118 break;
28119 default:
28120 gcc_unreachable ();
28122 decl_specs->storage_class = storage_class;
28123 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28125 /* A storage class specifier cannot be applied alongside a typedef
28126 specifier. If there is a typedef specifier present then set
28127 conflicting_specifiers_p which will trigger an error later
28128 on in grokdeclarator. */
28129 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28130 decl_specs->conflicting_specifiers_p = true;
28133 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28134 is true, the type is a class or enum definition. */
28136 static void
28137 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28138 tree type_spec,
28139 cp_token *token,
28140 bool type_definition_p)
28142 decl_specs->any_specifiers_p = true;
28144 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28145 (with, for example, in "typedef int wchar_t;") we remember that
28146 this is what happened. In system headers, we ignore these
28147 declarations so that G++ can work with system headers that are not
28148 C++-safe. */
28149 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28150 && !type_definition_p
28151 && (type_spec == boolean_type_node
28152 || type_spec == char16_type_node
28153 || type_spec == char32_type_node
28154 || type_spec == wchar_type_node)
28155 && (decl_specs->type
28156 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28157 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28158 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28159 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28161 decl_specs->redefined_builtin_type = type_spec;
28162 set_and_check_decl_spec_loc (decl_specs,
28163 ds_redefined_builtin_type_spec,
28164 token);
28165 if (!decl_specs->type)
28167 decl_specs->type = type_spec;
28168 decl_specs->type_definition_p = false;
28169 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28172 else if (decl_specs->type)
28173 decl_specs->multiple_types_p = true;
28174 else
28176 decl_specs->type = type_spec;
28177 decl_specs->type_definition_p = type_definition_p;
28178 decl_specs->redefined_builtin_type = NULL_TREE;
28179 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28183 /* True iff TOKEN is the GNU keyword __thread. */
28185 static bool
28186 token_is__thread (cp_token *token)
28188 gcc_assert (token->keyword == RID_THREAD);
28189 return id_equal (token->u.value, "__thread");
28192 /* Set the location for a declarator specifier and check if it is
28193 duplicated.
28195 DECL_SPECS is the sequence of declarator specifiers onto which to
28196 set the location.
28198 DS is the single declarator specifier to set which location is to
28199 be set onto the existing sequence of declarators.
28201 LOCATION is the location for the declarator specifier to
28202 consider. */
28204 static void
28205 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28206 cp_decl_spec ds, cp_token *token)
28208 gcc_assert (ds < ds_last);
28210 if (decl_specs == NULL)
28211 return;
28213 source_location location = token->location;
28215 if (decl_specs->locations[ds] == 0)
28217 decl_specs->locations[ds] = location;
28218 if (ds == ds_thread)
28219 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28221 else
28223 if (ds == ds_long)
28225 if (decl_specs->locations[ds_long_long] != 0)
28226 error_at (location,
28227 "%<long long long%> is too long for GCC");
28228 else
28230 decl_specs->locations[ds_long_long] = location;
28231 pedwarn_cxx98 (location,
28232 OPT_Wlong_long,
28233 "ISO C++ 1998 does not support %<long long%>");
28236 else if (ds == ds_thread)
28238 bool gnu = token_is__thread (token);
28239 if (gnu != decl_specs->gnu_thread_keyword_p)
28240 error_at (location,
28241 "both %<__thread%> and %<thread_local%> specified");
28242 else
28244 gcc_rich_location richloc (location);
28245 richloc.add_fixit_remove ();
28246 error_at (&richloc, "duplicate %qD", token->u.value);
28249 else
28251 static const char *const decl_spec_names[] = {
28252 "signed",
28253 "unsigned",
28254 "short",
28255 "long",
28256 "const",
28257 "volatile",
28258 "restrict",
28259 "inline",
28260 "virtual",
28261 "explicit",
28262 "friend",
28263 "typedef",
28264 "using",
28265 "constexpr",
28266 "__complex"
28268 gcc_rich_location richloc (location);
28269 richloc.add_fixit_remove ();
28270 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28275 /* Return true iff the declarator specifier DS is present in the
28276 sequence of declarator specifiers DECL_SPECS. */
28278 bool
28279 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28280 cp_decl_spec ds)
28282 gcc_assert (ds < ds_last);
28284 if (decl_specs == NULL)
28285 return false;
28287 return decl_specs->locations[ds] != 0;
28290 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28291 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28293 static bool
28294 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28296 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28299 /* Issue an error message indicating that TOKEN_DESC was expected.
28300 If KEYWORD is true, it indicated this function is called by
28301 cp_parser_require_keword and the required token can only be
28302 a indicated keyword.
28304 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28305 within any error as the location of an "opening" token matching
28306 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28307 RT_CLOSE_PAREN). */
28309 static void
28310 cp_parser_required_error (cp_parser *parser,
28311 required_token token_desc,
28312 bool keyword,
28313 location_t matching_location)
28315 if (cp_parser_simulate_error (parser))
28316 return;
28318 const char *gmsgid = NULL;
28319 switch (token_desc)
28321 case RT_NEW:
28322 gmsgid = G_("expected %<new%>");
28323 break;
28324 case RT_DELETE:
28325 gmsgid = G_("expected %<delete%>");
28326 break;
28327 case RT_RETURN:
28328 gmsgid = G_("expected %<return%>");
28329 break;
28330 case RT_WHILE:
28331 gmsgid = G_("expected %<while%>");
28332 break;
28333 case RT_EXTERN:
28334 gmsgid = G_("expected %<extern%>");
28335 break;
28336 case RT_STATIC_ASSERT:
28337 gmsgid = G_("expected %<static_assert%>");
28338 break;
28339 case RT_DECLTYPE:
28340 gmsgid = G_("expected %<decltype%>");
28341 break;
28342 case RT_OPERATOR:
28343 gmsgid = G_("expected %<operator%>");
28344 break;
28345 case RT_CLASS:
28346 gmsgid = G_("expected %<class%>");
28347 break;
28348 case RT_TEMPLATE:
28349 gmsgid = G_("expected %<template%>");
28350 break;
28351 case RT_NAMESPACE:
28352 gmsgid = G_("expected %<namespace%>");
28353 break;
28354 case RT_USING:
28355 gmsgid = G_("expected %<using%>");
28356 break;
28357 case RT_ASM:
28358 gmsgid = G_("expected %<asm%>");
28359 break;
28360 case RT_TRY:
28361 gmsgid = G_("expected %<try%>");
28362 break;
28363 case RT_CATCH:
28364 gmsgid = G_("expected %<catch%>");
28365 break;
28366 case RT_THROW:
28367 gmsgid = G_("expected %<throw%>");
28368 break;
28369 case RT_LABEL:
28370 gmsgid = G_("expected %<__label__%>");
28371 break;
28372 case RT_AT_TRY:
28373 gmsgid = G_("expected %<@try%>");
28374 break;
28375 case RT_AT_SYNCHRONIZED:
28376 gmsgid = G_("expected %<@synchronized%>");
28377 break;
28378 case RT_AT_THROW:
28379 gmsgid = G_("expected %<@throw%>");
28380 break;
28381 case RT_TRANSACTION_ATOMIC:
28382 gmsgid = G_("expected %<__transaction_atomic%>");
28383 break;
28384 case RT_TRANSACTION_RELAXED:
28385 gmsgid = G_("expected %<__transaction_relaxed%>");
28386 break;
28387 default:
28388 break;
28391 if (!gmsgid && !keyword)
28393 switch (token_desc)
28395 case RT_SEMICOLON:
28396 gmsgid = G_("expected %<;%>");
28397 break;
28398 case RT_OPEN_PAREN:
28399 gmsgid = G_("expected %<(%>");
28400 break;
28401 case RT_CLOSE_BRACE:
28402 gmsgid = G_("expected %<}%>");
28403 break;
28404 case RT_OPEN_BRACE:
28405 gmsgid = G_("expected %<{%>");
28406 break;
28407 case RT_CLOSE_SQUARE:
28408 gmsgid = G_("expected %<]%>");
28409 break;
28410 case RT_OPEN_SQUARE:
28411 gmsgid = G_("expected %<[%>");
28412 break;
28413 case RT_COMMA:
28414 gmsgid = G_("expected %<,%>");
28415 break;
28416 case RT_SCOPE:
28417 gmsgid = G_("expected %<::%>");
28418 break;
28419 case RT_LESS:
28420 gmsgid = G_("expected %<<%>");
28421 break;
28422 case RT_GREATER:
28423 gmsgid = G_("expected %<>%>");
28424 break;
28425 case RT_EQ:
28426 gmsgid = G_("expected %<=%>");
28427 break;
28428 case RT_ELLIPSIS:
28429 gmsgid = G_("expected %<...%>");
28430 break;
28431 case RT_MULT:
28432 gmsgid = G_("expected %<*%>");
28433 break;
28434 case RT_COMPL:
28435 gmsgid = G_("expected %<~%>");
28436 break;
28437 case RT_COLON:
28438 gmsgid = G_("expected %<:%>");
28439 break;
28440 case RT_COLON_SCOPE:
28441 gmsgid = G_("expected %<:%> or %<::%>");
28442 break;
28443 case RT_CLOSE_PAREN:
28444 gmsgid = G_("expected %<)%>");
28445 break;
28446 case RT_COMMA_CLOSE_PAREN:
28447 gmsgid = G_("expected %<,%> or %<)%>");
28448 break;
28449 case RT_PRAGMA_EOL:
28450 gmsgid = G_("expected end of line");
28451 break;
28452 case RT_NAME:
28453 gmsgid = G_("expected identifier");
28454 break;
28455 case RT_SELECT:
28456 gmsgid = G_("expected selection-statement");
28457 break;
28458 case RT_ITERATION:
28459 gmsgid = G_("expected iteration-statement");
28460 break;
28461 case RT_JUMP:
28462 gmsgid = G_("expected jump-statement");
28463 break;
28464 case RT_CLASS_KEY:
28465 gmsgid = G_("expected class-key");
28466 break;
28467 case RT_CLASS_TYPENAME_TEMPLATE:
28468 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28469 break;
28470 default:
28471 gcc_unreachable ();
28475 if (gmsgid)
28476 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28480 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28481 issue an error message indicating that TOKEN_DESC was expected.
28483 Returns the token consumed, if the token had the appropriate type.
28484 Otherwise, returns NULL.
28486 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28487 within any error as the location of an "opening" token matching
28488 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28489 RT_CLOSE_PAREN). */
28491 static cp_token *
28492 cp_parser_require (cp_parser* parser,
28493 enum cpp_ttype type,
28494 required_token token_desc,
28495 location_t matching_location)
28497 if (cp_lexer_next_token_is (parser->lexer, type))
28498 return cp_lexer_consume_token (parser->lexer);
28499 else
28501 /* Output the MESSAGE -- unless we're parsing tentatively. */
28502 if (!cp_parser_simulate_error (parser))
28503 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28504 matching_location);
28505 return NULL;
28509 /* An error message is produced if the next token is not '>'.
28510 All further tokens are skipped until the desired token is
28511 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28513 static void
28514 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28516 /* Current level of '< ... >'. */
28517 unsigned level = 0;
28518 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28519 unsigned nesting_depth = 0;
28521 /* Are we ready, yet? If not, issue error message. */
28522 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28523 return;
28525 /* Skip tokens until the desired token is found. */
28526 while (true)
28528 /* Peek at the next token. */
28529 switch (cp_lexer_peek_token (parser->lexer)->type)
28531 case CPP_LESS:
28532 if (!nesting_depth)
28533 ++level;
28534 break;
28536 case CPP_RSHIFT:
28537 if (cxx_dialect == cxx98)
28538 /* C++0x views the `>>' operator as two `>' tokens, but
28539 C++98 does not. */
28540 break;
28541 else if (!nesting_depth && level-- == 0)
28543 /* We've hit a `>>' where the first `>' closes the
28544 template argument list, and the second `>' is
28545 spurious. Just consume the `>>' and stop; we've
28546 already produced at least one error. */
28547 cp_lexer_consume_token (parser->lexer);
28548 return;
28550 /* Fall through for C++0x, so we handle the second `>' in
28551 the `>>'. */
28552 gcc_fallthrough ();
28554 case CPP_GREATER:
28555 if (!nesting_depth && level-- == 0)
28557 /* We've reached the token we want, consume it and stop. */
28558 cp_lexer_consume_token (parser->lexer);
28559 return;
28561 break;
28563 case CPP_OPEN_PAREN:
28564 case CPP_OPEN_SQUARE:
28565 ++nesting_depth;
28566 break;
28568 case CPP_CLOSE_PAREN:
28569 case CPP_CLOSE_SQUARE:
28570 if (nesting_depth-- == 0)
28571 return;
28572 break;
28574 case CPP_EOF:
28575 case CPP_PRAGMA_EOL:
28576 case CPP_SEMICOLON:
28577 case CPP_OPEN_BRACE:
28578 case CPP_CLOSE_BRACE:
28579 /* The '>' was probably forgotten, don't look further. */
28580 return;
28582 default:
28583 break;
28586 /* Consume this token. */
28587 cp_lexer_consume_token (parser->lexer);
28591 /* If the next token is the indicated keyword, consume it. Otherwise,
28592 issue an error message indicating that TOKEN_DESC was expected.
28594 Returns the token consumed, if the token had the appropriate type.
28595 Otherwise, returns NULL. */
28597 static cp_token *
28598 cp_parser_require_keyword (cp_parser* parser,
28599 enum rid keyword,
28600 required_token token_desc)
28602 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28604 if (token && token->keyword != keyword)
28606 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28607 UNKNOWN_LOCATION);
28608 return NULL;
28611 return token;
28614 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28615 function-definition. */
28617 static bool
28618 cp_parser_token_starts_function_definition_p (cp_token* token)
28620 return (/* An ordinary function-body begins with an `{'. */
28621 token->type == CPP_OPEN_BRACE
28622 /* A ctor-initializer begins with a `:'. */
28623 || token->type == CPP_COLON
28624 /* A function-try-block begins with `try'. */
28625 || token->keyword == RID_TRY
28626 /* A function-transaction-block begins with `__transaction_atomic'
28627 or `__transaction_relaxed'. */
28628 || token->keyword == RID_TRANSACTION_ATOMIC
28629 || token->keyword == RID_TRANSACTION_RELAXED
28630 /* The named return value extension begins with `return'. */
28631 || token->keyword == RID_RETURN);
28634 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28635 definition. */
28637 static bool
28638 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28640 cp_token *token;
28642 token = cp_lexer_peek_token (parser->lexer);
28643 return (token->type == CPP_OPEN_BRACE
28644 || (token->type == CPP_COLON
28645 && !parser->colon_doesnt_start_class_def_p));
28648 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28649 C++0x) ending a template-argument. */
28651 static bool
28652 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28654 cp_token *token;
28656 token = cp_lexer_peek_token (parser->lexer);
28657 return (token->type == CPP_COMMA
28658 || token->type == CPP_GREATER
28659 || token->type == CPP_ELLIPSIS
28660 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28663 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28664 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28666 static bool
28667 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28668 size_t n)
28670 cp_token *token;
28672 token = cp_lexer_peek_nth_token (parser->lexer, n);
28673 if (token->type == CPP_LESS)
28674 return true;
28675 /* Check for the sequence `<::' in the original code. It would be lexed as
28676 `[:', where `[' is a digraph, and there is no whitespace before
28677 `:'. */
28678 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28680 cp_token *token2;
28681 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28682 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28683 return true;
28685 return false;
28688 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28689 or none_type otherwise. */
28691 static enum tag_types
28692 cp_parser_token_is_class_key (cp_token* token)
28694 switch (token->keyword)
28696 case RID_CLASS:
28697 return class_type;
28698 case RID_STRUCT:
28699 return record_type;
28700 case RID_UNION:
28701 return union_type;
28703 default:
28704 return none_type;
28708 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28709 or none_type otherwise or if the token is null. */
28711 static enum tag_types
28712 cp_parser_token_is_type_parameter_key (cp_token* token)
28714 if (!token)
28715 return none_type;
28717 switch (token->keyword)
28719 case RID_CLASS:
28720 return class_type;
28721 case RID_TYPENAME:
28722 return typename_type;
28724 default:
28725 return none_type;
28729 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28731 static void
28732 cp_parser_check_class_key (enum tag_types class_key, tree type)
28734 if (type == error_mark_node)
28735 return;
28736 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28738 if (permerror (input_location, "%qs tag used in naming %q#T",
28739 class_key == union_type ? "union"
28740 : class_key == record_type ? "struct" : "class",
28741 type))
28742 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28743 "%q#T was previously declared here", type);
28747 /* Issue an error message if DECL is redeclared with different
28748 access than its original declaration [class.access.spec/3].
28749 This applies to nested classes, nested class templates and
28750 enumerations [class.mem/1]. */
28752 static void
28753 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28755 if (!decl
28756 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28757 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28758 return;
28760 if ((TREE_PRIVATE (decl)
28761 != (current_access_specifier == access_private_node))
28762 || (TREE_PROTECTED (decl)
28763 != (current_access_specifier == access_protected_node)))
28764 error_at (location, "%qD redeclared with different access", decl);
28767 /* Look for the `template' keyword, as a syntactic disambiguator.
28768 Return TRUE iff it is present, in which case it will be
28769 consumed. */
28771 static bool
28772 cp_parser_optional_template_keyword (cp_parser *parser)
28774 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28776 /* In C++98 the `template' keyword can only be used within templates;
28777 outside templates the parser can always figure out what is a
28778 template and what is not. In C++11, per the resolution of DR 468,
28779 `template' is allowed in cases where it is not strictly necessary. */
28780 if (!processing_template_decl
28781 && pedantic && cxx_dialect == cxx98)
28783 cp_token *token = cp_lexer_peek_token (parser->lexer);
28784 pedwarn (token->location, OPT_Wpedantic,
28785 "in C++98 %<template%> (as a disambiguator) is only "
28786 "allowed within templates");
28787 /* If this part of the token stream is rescanned, the same
28788 error message would be generated. So, we purge the token
28789 from the stream. */
28790 cp_lexer_purge_token (parser->lexer);
28791 return false;
28793 else
28795 /* Consume the `template' keyword. */
28796 cp_lexer_consume_token (parser->lexer);
28797 return true;
28800 return false;
28803 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28804 set PARSER->SCOPE, and perform other related actions. */
28806 static void
28807 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28809 struct tree_check *check_value;
28811 /* Get the stored value. */
28812 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28813 /* Set the scope from the stored value. */
28814 parser->scope = saved_checks_value (check_value);
28815 parser->qualifying_scope = check_value->qualifying_scope;
28816 parser->object_scope = NULL_TREE;
28819 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28820 encounter the end of a block before what we were looking for. */
28822 static bool
28823 cp_parser_cache_group (cp_parser *parser,
28824 enum cpp_ttype end,
28825 unsigned depth)
28827 while (true)
28829 cp_token *token = cp_lexer_peek_token (parser->lexer);
28831 /* Abort a parenthesized expression if we encounter a semicolon. */
28832 if ((end == CPP_CLOSE_PAREN || depth == 0)
28833 && token->type == CPP_SEMICOLON)
28834 return true;
28835 /* If we've reached the end of the file, stop. */
28836 if (token->type == CPP_EOF
28837 || (end != CPP_PRAGMA_EOL
28838 && token->type == CPP_PRAGMA_EOL))
28839 return true;
28840 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28841 /* We've hit the end of an enclosing block, so there's been some
28842 kind of syntax error. */
28843 return true;
28845 /* Consume the token. */
28846 cp_lexer_consume_token (parser->lexer);
28847 /* See if it starts a new group. */
28848 if (token->type == CPP_OPEN_BRACE)
28850 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28851 /* In theory this should probably check end == '}', but
28852 cp_parser_save_member_function_body needs it to exit
28853 after either '}' or ')' when called with ')'. */
28854 if (depth == 0)
28855 return false;
28857 else if (token->type == CPP_OPEN_PAREN)
28859 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28860 if (depth == 0 && end == CPP_CLOSE_PAREN)
28861 return false;
28863 else if (token->type == CPP_PRAGMA)
28864 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28865 else if (token->type == end)
28866 return false;
28870 /* Like above, for caching a default argument or NSDMI. Both of these are
28871 terminated by a non-nested comma, but it can be unclear whether or not a
28872 comma is nested in a template argument list unless we do more parsing.
28873 In order to handle this ambiguity, when we encounter a ',' after a '<'
28874 we try to parse what follows as a parameter-declaration-list (in the
28875 case of a default argument) or a member-declarator (in the case of an
28876 NSDMI). If that succeeds, then we stop caching. */
28878 static tree
28879 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28881 unsigned depth = 0;
28882 int maybe_template_id = 0;
28883 cp_token *first_token;
28884 cp_token *token;
28885 tree default_argument;
28887 /* Add tokens until we have processed the entire default
28888 argument. We add the range [first_token, token). */
28889 first_token = cp_lexer_peek_token (parser->lexer);
28890 if (first_token->type == CPP_OPEN_BRACE)
28892 /* For list-initialization, this is straightforward. */
28893 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28894 token = cp_lexer_peek_token (parser->lexer);
28896 else while (true)
28898 bool done = false;
28900 /* Peek at the next token. */
28901 token = cp_lexer_peek_token (parser->lexer);
28902 /* What we do depends on what token we have. */
28903 switch (token->type)
28905 /* In valid code, a default argument must be
28906 immediately followed by a `,' `)', or `...'. */
28907 case CPP_COMMA:
28908 if (depth == 0 && maybe_template_id)
28910 /* If we've seen a '<', we might be in a
28911 template-argument-list. Until Core issue 325 is
28912 resolved, we don't know how this situation ought
28913 to be handled, so try to DTRT. We check whether
28914 what comes after the comma is a valid parameter
28915 declaration list. If it is, then the comma ends
28916 the default argument; otherwise the default
28917 argument continues. */
28918 bool error = false;
28919 cp_token *peek;
28921 /* Set ITALP so cp_parser_parameter_declaration_list
28922 doesn't decide to commit to this parse. */
28923 bool saved_italp = parser->in_template_argument_list_p;
28924 parser->in_template_argument_list_p = true;
28926 cp_parser_parse_tentatively (parser);
28928 if (nsdmi)
28930 /* Parse declarators until we reach a non-comma or
28931 somthing that cannot be an initializer.
28932 Just checking whether we're looking at a single
28933 declarator is insufficient. Consider:
28934 int var = tuple<T,U>::x;
28935 The template parameter 'U' looks exactly like a
28936 declarator. */
28939 int ctor_dtor_or_conv_p;
28940 cp_lexer_consume_token (parser->lexer);
28941 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28942 &ctor_dtor_or_conv_p,
28943 /*parenthesized_p=*/NULL,
28944 /*member_p=*/true,
28945 /*friend_p=*/false);
28946 peek = cp_lexer_peek_token (parser->lexer);
28947 if (cp_parser_error_occurred (parser))
28948 break;
28950 while (peek->type == CPP_COMMA);
28951 /* If we met an '=' or ';' then the original comma
28952 was the end of the NSDMI. Otherwise assume
28953 we're still in the NSDMI. */
28954 error = (peek->type != CPP_EQ
28955 && peek->type != CPP_SEMICOLON);
28957 else
28959 cp_lexer_consume_token (parser->lexer);
28960 begin_scope (sk_function_parms, NULL_TREE);
28961 cp_parser_parameter_declaration_list (parser, &error);
28962 pop_bindings_and_leave_scope ();
28964 if (!cp_parser_error_occurred (parser) && !error)
28965 done = true;
28966 cp_parser_abort_tentative_parse (parser);
28968 parser->in_template_argument_list_p = saved_italp;
28969 break;
28971 /* FALLTHRU */
28972 case CPP_CLOSE_PAREN:
28973 case CPP_ELLIPSIS:
28974 /* If we run into a non-nested `;', `}', or `]',
28975 then the code is invalid -- but the default
28976 argument is certainly over. */
28977 case CPP_SEMICOLON:
28978 case CPP_CLOSE_BRACE:
28979 case CPP_CLOSE_SQUARE:
28980 if (depth == 0
28981 /* Handle correctly int n = sizeof ... ( p ); */
28982 && token->type != CPP_ELLIPSIS)
28983 done = true;
28984 /* Update DEPTH, if necessary. */
28985 else if (token->type == CPP_CLOSE_PAREN
28986 || token->type == CPP_CLOSE_BRACE
28987 || token->type == CPP_CLOSE_SQUARE)
28988 --depth;
28989 break;
28991 case CPP_OPEN_PAREN:
28992 case CPP_OPEN_SQUARE:
28993 case CPP_OPEN_BRACE:
28994 ++depth;
28995 break;
28997 case CPP_LESS:
28998 if (depth == 0)
28999 /* This might be the comparison operator, or it might
29000 start a template argument list. */
29001 ++maybe_template_id;
29002 break;
29004 case CPP_RSHIFT:
29005 if (cxx_dialect == cxx98)
29006 break;
29007 /* Fall through for C++0x, which treats the `>>'
29008 operator like two `>' tokens in certain
29009 cases. */
29010 gcc_fallthrough ();
29012 case CPP_GREATER:
29013 if (depth == 0)
29015 /* This might be an operator, or it might close a
29016 template argument list. But if a previous '<'
29017 started a template argument list, this will have
29018 closed it, so we can't be in one anymore. */
29019 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29020 if (maybe_template_id < 0)
29021 maybe_template_id = 0;
29023 break;
29025 /* If we run out of tokens, issue an error message. */
29026 case CPP_EOF:
29027 case CPP_PRAGMA_EOL:
29028 error_at (token->location, "file ends in default argument");
29029 return error_mark_node;
29031 case CPP_NAME:
29032 case CPP_SCOPE:
29033 /* In these cases, we should look for template-ids.
29034 For example, if the default argument is
29035 `X<int, double>()', we need to do name lookup to
29036 figure out whether or not `X' is a template; if
29037 so, the `,' does not end the default argument.
29039 That is not yet done. */
29040 break;
29042 default:
29043 break;
29046 /* If we've reached the end, stop. */
29047 if (done)
29048 break;
29050 /* Add the token to the token block. */
29051 token = cp_lexer_consume_token (parser->lexer);
29054 /* Create a DEFAULT_ARG to represent the unparsed default
29055 argument. */
29056 default_argument = make_node (DEFAULT_ARG);
29057 DEFARG_TOKENS (default_argument)
29058 = cp_token_cache_new (first_token, token);
29059 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29061 return default_argument;
29064 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29066 location_t
29067 defarg_location (tree default_argument)
29069 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29070 location_t start = tokens->first->location;
29071 location_t end = tokens->last->location;
29072 return make_location (start, start, end);
29075 /* Begin parsing tentatively. We always save tokens while parsing
29076 tentatively so that if the tentative parsing fails we can restore the
29077 tokens. */
29079 static void
29080 cp_parser_parse_tentatively (cp_parser* parser)
29082 /* Enter a new parsing context. */
29083 parser->context = cp_parser_context_new (parser->context);
29084 /* Begin saving tokens. */
29085 cp_lexer_save_tokens (parser->lexer);
29086 /* In order to avoid repetitive access control error messages,
29087 access checks are queued up until we are no longer parsing
29088 tentatively. */
29089 push_deferring_access_checks (dk_deferred);
29092 /* Commit to the currently active tentative parse. */
29094 static void
29095 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29097 cp_parser_context *context;
29098 cp_lexer *lexer;
29100 /* Mark all of the levels as committed. */
29101 lexer = parser->lexer;
29102 for (context = parser->context; context->next; context = context->next)
29104 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29105 break;
29106 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29107 while (!cp_lexer_saving_tokens (lexer))
29108 lexer = lexer->next;
29109 cp_lexer_commit_tokens (lexer);
29113 /* Commit to the topmost currently active tentative parse.
29115 Note that this function shouldn't be called when there are
29116 irreversible side-effects while in a tentative state. For
29117 example, we shouldn't create a permanent entry in the symbol
29118 table, or issue an error message that might not apply if the
29119 tentative parse is aborted. */
29121 static void
29122 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29124 cp_parser_context *context = parser->context;
29125 cp_lexer *lexer = parser->lexer;
29127 if (context)
29129 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29130 return;
29131 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29133 while (!cp_lexer_saving_tokens (lexer))
29134 lexer = lexer->next;
29135 cp_lexer_commit_tokens (lexer);
29139 /* Abort the currently active tentative parse. All consumed tokens
29140 will be rolled back, and no diagnostics will be issued. */
29142 static void
29143 cp_parser_abort_tentative_parse (cp_parser* parser)
29145 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29146 || errorcount > 0);
29147 cp_parser_simulate_error (parser);
29148 /* Now, pretend that we want to see if the construct was
29149 successfully parsed. */
29150 cp_parser_parse_definitely (parser);
29153 /* Stop parsing tentatively. If a parse error has occurred, restore the
29154 token stream. Otherwise, commit to the tokens we have consumed.
29155 Returns true if no error occurred; false otherwise. */
29157 static bool
29158 cp_parser_parse_definitely (cp_parser* parser)
29160 bool error_occurred;
29161 cp_parser_context *context;
29163 /* Remember whether or not an error occurred, since we are about to
29164 destroy that information. */
29165 error_occurred = cp_parser_error_occurred (parser);
29166 /* Remove the topmost context from the stack. */
29167 context = parser->context;
29168 parser->context = context->next;
29169 /* If no parse errors occurred, commit to the tentative parse. */
29170 if (!error_occurred)
29172 /* Commit to the tokens read tentatively, unless that was
29173 already done. */
29174 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29175 cp_lexer_commit_tokens (parser->lexer);
29177 pop_to_parent_deferring_access_checks ();
29179 /* Otherwise, if errors occurred, roll back our state so that things
29180 are just as they were before we began the tentative parse. */
29181 else
29183 cp_lexer_rollback_tokens (parser->lexer);
29184 pop_deferring_access_checks ();
29186 /* Add the context to the front of the free list. */
29187 context->next = cp_parser_context_free_list;
29188 cp_parser_context_free_list = context;
29190 return !error_occurred;
29193 /* Returns true if we are parsing tentatively and are not committed to
29194 this tentative parse. */
29196 static bool
29197 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29199 return (cp_parser_parsing_tentatively (parser)
29200 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29203 /* Returns nonzero iff an error has occurred during the most recent
29204 tentative parse. */
29206 static bool
29207 cp_parser_error_occurred (cp_parser* parser)
29209 return (cp_parser_parsing_tentatively (parser)
29210 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29213 /* Returns nonzero if GNU extensions are allowed. */
29215 static bool
29216 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29218 return parser->allow_gnu_extensions_p;
29221 /* Objective-C++ Productions */
29224 /* Parse an Objective-C expression, which feeds into a primary-expression
29225 above.
29227 objc-expression:
29228 objc-message-expression
29229 objc-string-literal
29230 objc-encode-expression
29231 objc-protocol-expression
29232 objc-selector-expression
29234 Returns a tree representation of the expression. */
29236 static cp_expr
29237 cp_parser_objc_expression (cp_parser* parser)
29239 /* Try to figure out what kind of declaration is present. */
29240 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29242 switch (kwd->type)
29244 case CPP_OPEN_SQUARE:
29245 return cp_parser_objc_message_expression (parser);
29247 case CPP_OBJC_STRING:
29248 kwd = cp_lexer_consume_token (parser->lexer);
29249 return objc_build_string_object (kwd->u.value);
29251 case CPP_KEYWORD:
29252 switch (kwd->keyword)
29254 case RID_AT_ENCODE:
29255 return cp_parser_objc_encode_expression (parser);
29257 case RID_AT_PROTOCOL:
29258 return cp_parser_objc_protocol_expression (parser);
29260 case RID_AT_SELECTOR:
29261 return cp_parser_objc_selector_expression (parser);
29263 default:
29264 break;
29266 /* FALLTHRU */
29267 default:
29268 error_at (kwd->location,
29269 "misplaced %<@%D%> Objective-C++ construct",
29270 kwd->u.value);
29271 cp_parser_skip_to_end_of_block_or_statement (parser);
29274 return error_mark_node;
29277 /* Parse an Objective-C message expression.
29279 objc-message-expression:
29280 [ objc-message-receiver objc-message-args ]
29282 Returns a representation of an Objective-C message. */
29284 static tree
29285 cp_parser_objc_message_expression (cp_parser* parser)
29287 tree receiver, messageargs;
29289 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29290 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29291 receiver = cp_parser_objc_message_receiver (parser);
29292 messageargs = cp_parser_objc_message_args (parser);
29293 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29294 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29296 tree result = objc_build_message_expr (receiver, messageargs);
29298 /* Construct a location e.g.
29299 [self func1:5]
29300 ^~~~~~~~~~~~~~
29301 ranging from the '[' to the ']', with the caret at the start. */
29302 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29303 protected_set_expr_location (result, combined_loc);
29305 return result;
29308 /* Parse an objc-message-receiver.
29310 objc-message-receiver:
29311 expression
29312 simple-type-specifier
29314 Returns a representation of the type or expression. */
29316 static tree
29317 cp_parser_objc_message_receiver (cp_parser* parser)
29319 tree rcv;
29321 /* An Objective-C message receiver may be either (1) a type
29322 or (2) an expression. */
29323 cp_parser_parse_tentatively (parser);
29324 rcv = cp_parser_expression (parser);
29326 /* If that worked out, fine. */
29327 if (cp_parser_parse_definitely (parser))
29328 return rcv;
29330 cp_parser_parse_tentatively (parser);
29331 rcv = cp_parser_simple_type_specifier (parser,
29332 /*decl_specs=*/NULL,
29333 CP_PARSER_FLAGS_NONE);
29335 if (cp_parser_parse_definitely (parser))
29336 return objc_get_class_reference (rcv);
29338 cp_parser_error (parser, "objective-c++ message receiver expected");
29339 return error_mark_node;
29342 /* Parse the arguments and selectors comprising an Objective-C message.
29344 objc-message-args:
29345 objc-selector
29346 objc-selector-args
29347 objc-selector-args , objc-comma-args
29349 objc-selector-args:
29350 objc-selector [opt] : assignment-expression
29351 objc-selector-args objc-selector [opt] : assignment-expression
29353 objc-comma-args:
29354 assignment-expression
29355 objc-comma-args , assignment-expression
29357 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29358 selector arguments and TREE_VALUE containing a list of comma
29359 arguments. */
29361 static tree
29362 cp_parser_objc_message_args (cp_parser* parser)
29364 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29365 bool maybe_unary_selector_p = true;
29366 cp_token *token = cp_lexer_peek_token (parser->lexer);
29368 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29370 tree selector = NULL_TREE, arg;
29372 if (token->type != CPP_COLON)
29373 selector = cp_parser_objc_selector (parser);
29375 /* Detect if we have a unary selector. */
29376 if (maybe_unary_selector_p
29377 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29378 return build_tree_list (selector, NULL_TREE);
29380 maybe_unary_selector_p = false;
29381 cp_parser_require (parser, CPP_COLON, RT_COLON);
29382 arg = cp_parser_assignment_expression (parser);
29384 sel_args
29385 = chainon (sel_args,
29386 build_tree_list (selector, arg));
29388 token = cp_lexer_peek_token (parser->lexer);
29391 /* Handle non-selector arguments, if any. */
29392 while (token->type == CPP_COMMA)
29394 tree arg;
29396 cp_lexer_consume_token (parser->lexer);
29397 arg = cp_parser_assignment_expression (parser);
29399 addl_args
29400 = chainon (addl_args,
29401 build_tree_list (NULL_TREE, arg));
29403 token = cp_lexer_peek_token (parser->lexer);
29406 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29408 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29409 return build_tree_list (error_mark_node, error_mark_node);
29412 return build_tree_list (sel_args, addl_args);
29415 /* Parse an Objective-C encode expression.
29417 objc-encode-expression:
29418 @encode objc-typename
29420 Returns an encoded representation of the type argument. */
29422 static cp_expr
29423 cp_parser_objc_encode_expression (cp_parser* parser)
29425 tree type;
29426 cp_token *token;
29427 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29429 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29430 matching_parens parens;
29431 parens.require_open (parser);
29432 token = cp_lexer_peek_token (parser->lexer);
29433 type = complete_type (cp_parser_type_id (parser));
29434 parens.require_close (parser);
29436 if (!type)
29438 error_at (token->location,
29439 "%<@encode%> must specify a type as an argument");
29440 return error_mark_node;
29443 /* This happens if we find @encode(T) (where T is a template
29444 typename or something dependent on a template typename) when
29445 parsing a template. In that case, we can't compile it
29446 immediately, but we rather create an AT_ENCODE_EXPR which will
29447 need to be instantiated when the template is used.
29449 if (dependent_type_p (type))
29451 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29452 TREE_READONLY (value) = 1;
29453 return value;
29457 /* Build a location of the form:
29458 @encode(int)
29459 ^~~~~~~~~~~~
29460 with caret==start at the @ token, finishing at the close paren. */
29461 location_t combined_loc
29462 = make_location (start_loc, start_loc,
29463 cp_lexer_previous_token (parser->lexer)->location);
29465 return cp_expr (objc_build_encode_expr (type), combined_loc);
29468 /* Parse an Objective-C @defs expression. */
29470 static tree
29471 cp_parser_objc_defs_expression (cp_parser *parser)
29473 tree name;
29475 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29476 matching_parens parens;
29477 parens.require_open (parser);
29478 name = cp_parser_identifier (parser);
29479 parens.require_close (parser);
29481 return objc_get_class_ivars (name);
29484 /* Parse an Objective-C protocol expression.
29486 objc-protocol-expression:
29487 @protocol ( identifier )
29489 Returns a representation of the protocol expression. */
29491 static tree
29492 cp_parser_objc_protocol_expression (cp_parser* parser)
29494 tree proto;
29495 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29497 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29498 matching_parens parens;
29499 parens.require_open (parser);
29500 proto = cp_parser_identifier (parser);
29501 parens.require_close (parser);
29503 /* Build a location of the form:
29504 @protocol(prot)
29505 ^~~~~~~~~~~~~~~
29506 with caret==start at the @ token, finishing at the close paren. */
29507 location_t combined_loc
29508 = make_location (start_loc, start_loc,
29509 cp_lexer_previous_token (parser->lexer)->location);
29510 tree result = objc_build_protocol_expr (proto);
29511 protected_set_expr_location (result, combined_loc);
29512 return result;
29515 /* Parse an Objective-C selector expression.
29517 objc-selector-expression:
29518 @selector ( objc-method-signature )
29520 objc-method-signature:
29521 objc-selector
29522 objc-selector-seq
29524 objc-selector-seq:
29525 objc-selector :
29526 objc-selector-seq objc-selector :
29528 Returns a representation of the method selector. */
29530 static tree
29531 cp_parser_objc_selector_expression (cp_parser* parser)
29533 tree sel_seq = NULL_TREE;
29534 bool maybe_unary_selector_p = true;
29535 cp_token *token;
29536 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29538 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29539 matching_parens parens;
29540 parens.require_open (parser);
29541 token = cp_lexer_peek_token (parser->lexer);
29543 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29544 || token->type == CPP_SCOPE)
29546 tree selector = NULL_TREE;
29548 if (token->type != CPP_COLON
29549 || token->type == CPP_SCOPE)
29550 selector = cp_parser_objc_selector (parser);
29552 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29553 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29555 /* Detect if we have a unary selector. */
29556 if (maybe_unary_selector_p)
29558 sel_seq = selector;
29559 goto finish_selector;
29561 else
29563 cp_parser_error (parser, "expected %<:%>");
29566 maybe_unary_selector_p = false;
29567 token = cp_lexer_consume_token (parser->lexer);
29569 if (token->type == CPP_SCOPE)
29571 sel_seq
29572 = chainon (sel_seq,
29573 build_tree_list (selector, NULL_TREE));
29574 sel_seq
29575 = chainon (sel_seq,
29576 build_tree_list (NULL_TREE, NULL_TREE));
29578 else
29579 sel_seq
29580 = chainon (sel_seq,
29581 build_tree_list (selector, NULL_TREE));
29583 token = cp_lexer_peek_token (parser->lexer);
29586 finish_selector:
29587 parens.require_close (parser);
29590 /* Build a location of the form:
29591 @selector(func)
29592 ^~~~~~~~~~~~~~~
29593 with caret==start at the @ token, finishing at the close paren. */
29594 location_t combined_loc
29595 = make_location (loc, loc,
29596 cp_lexer_previous_token (parser->lexer)->location);
29597 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29598 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29599 protected_set_expr_location (result, combined_loc);
29600 return result;
29603 /* Parse a list of identifiers.
29605 objc-identifier-list:
29606 identifier
29607 objc-identifier-list , identifier
29609 Returns a TREE_LIST of identifier nodes. */
29611 static tree
29612 cp_parser_objc_identifier_list (cp_parser* parser)
29614 tree identifier;
29615 tree list;
29616 cp_token *sep;
29618 identifier = cp_parser_identifier (parser);
29619 if (identifier == error_mark_node)
29620 return error_mark_node;
29622 list = build_tree_list (NULL_TREE, identifier);
29623 sep = cp_lexer_peek_token (parser->lexer);
29625 while (sep->type == CPP_COMMA)
29627 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29628 identifier = cp_parser_identifier (parser);
29629 if (identifier == error_mark_node)
29630 return list;
29632 list = chainon (list, build_tree_list (NULL_TREE,
29633 identifier));
29634 sep = cp_lexer_peek_token (parser->lexer);
29637 return list;
29640 /* Parse an Objective-C alias declaration.
29642 objc-alias-declaration:
29643 @compatibility_alias identifier identifier ;
29645 This function registers the alias mapping with the Objective-C front end.
29646 It returns nothing. */
29648 static void
29649 cp_parser_objc_alias_declaration (cp_parser* parser)
29651 tree alias, orig;
29653 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29654 alias = cp_parser_identifier (parser);
29655 orig = cp_parser_identifier (parser);
29656 objc_declare_alias (alias, orig);
29657 cp_parser_consume_semicolon_at_end_of_statement (parser);
29660 /* Parse an Objective-C class forward-declaration.
29662 objc-class-declaration:
29663 @class objc-identifier-list ;
29665 The function registers the forward declarations with the Objective-C
29666 front end. It returns nothing. */
29668 static void
29669 cp_parser_objc_class_declaration (cp_parser* parser)
29671 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29672 while (true)
29674 tree id;
29676 id = cp_parser_identifier (parser);
29677 if (id == error_mark_node)
29678 break;
29680 objc_declare_class (id);
29682 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29683 cp_lexer_consume_token (parser->lexer);
29684 else
29685 break;
29687 cp_parser_consume_semicolon_at_end_of_statement (parser);
29690 /* Parse a list of Objective-C protocol references.
29692 objc-protocol-refs-opt:
29693 objc-protocol-refs [opt]
29695 objc-protocol-refs:
29696 < objc-identifier-list >
29698 Returns a TREE_LIST of identifiers, if any. */
29700 static tree
29701 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29703 tree protorefs = NULL_TREE;
29705 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29707 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29708 protorefs = cp_parser_objc_identifier_list (parser);
29709 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29712 return protorefs;
29715 /* Parse a Objective-C visibility specification. */
29717 static void
29718 cp_parser_objc_visibility_spec (cp_parser* parser)
29720 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29722 switch (vis->keyword)
29724 case RID_AT_PRIVATE:
29725 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29726 break;
29727 case RID_AT_PROTECTED:
29728 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29729 break;
29730 case RID_AT_PUBLIC:
29731 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29732 break;
29733 case RID_AT_PACKAGE:
29734 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29735 break;
29736 default:
29737 return;
29740 /* Eat '@private'/'@protected'/'@public'. */
29741 cp_lexer_consume_token (parser->lexer);
29744 /* Parse an Objective-C method type. Return 'true' if it is a class
29745 (+) method, and 'false' if it is an instance (-) method. */
29747 static inline bool
29748 cp_parser_objc_method_type (cp_parser* parser)
29750 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29751 return true;
29752 else
29753 return false;
29756 /* Parse an Objective-C protocol qualifier. */
29758 static tree
29759 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29761 tree quals = NULL_TREE, node;
29762 cp_token *token = cp_lexer_peek_token (parser->lexer);
29764 node = token->u.value;
29766 while (node && identifier_p (node)
29767 && (node == ridpointers [(int) RID_IN]
29768 || node == ridpointers [(int) RID_OUT]
29769 || node == ridpointers [(int) RID_INOUT]
29770 || node == ridpointers [(int) RID_BYCOPY]
29771 || node == ridpointers [(int) RID_BYREF]
29772 || node == ridpointers [(int) RID_ONEWAY]))
29774 quals = tree_cons (NULL_TREE, node, quals);
29775 cp_lexer_consume_token (parser->lexer);
29776 token = cp_lexer_peek_token (parser->lexer);
29777 node = token->u.value;
29780 return quals;
29783 /* Parse an Objective-C typename. */
29785 static tree
29786 cp_parser_objc_typename (cp_parser* parser)
29788 tree type_name = NULL_TREE;
29790 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29792 tree proto_quals, cp_type = NULL_TREE;
29794 matching_parens parens;
29795 parens.consume_open (parser); /* Eat '('. */
29796 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29798 /* An ObjC type name may consist of just protocol qualifiers, in which
29799 case the type shall default to 'id'. */
29800 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29802 cp_type = cp_parser_type_id (parser);
29804 /* If the type could not be parsed, an error has already
29805 been produced. For error recovery, behave as if it had
29806 not been specified, which will use the default type
29807 'id'. */
29808 if (cp_type == error_mark_node)
29810 cp_type = NULL_TREE;
29811 /* We need to skip to the closing parenthesis as
29812 cp_parser_type_id() does not seem to do it for
29813 us. */
29814 cp_parser_skip_to_closing_parenthesis (parser,
29815 /*recovering=*/true,
29816 /*or_comma=*/false,
29817 /*consume_paren=*/false);
29821 parens.require_close (parser);
29822 type_name = build_tree_list (proto_quals, cp_type);
29825 return type_name;
29828 /* Check to see if TYPE refers to an Objective-C selector name. */
29830 static bool
29831 cp_parser_objc_selector_p (enum cpp_ttype type)
29833 return (type == CPP_NAME || type == CPP_KEYWORD
29834 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29835 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29836 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29837 || type == CPP_XOR || type == CPP_XOR_EQ);
29840 /* Parse an Objective-C selector. */
29842 static tree
29843 cp_parser_objc_selector (cp_parser* parser)
29845 cp_token *token = cp_lexer_consume_token (parser->lexer);
29847 if (!cp_parser_objc_selector_p (token->type))
29849 error_at (token->location, "invalid Objective-C++ selector name");
29850 return error_mark_node;
29853 /* C++ operator names are allowed to appear in ObjC selectors. */
29854 switch (token->type)
29856 case CPP_AND_AND: return get_identifier ("and");
29857 case CPP_AND_EQ: return get_identifier ("and_eq");
29858 case CPP_AND: return get_identifier ("bitand");
29859 case CPP_OR: return get_identifier ("bitor");
29860 case CPP_COMPL: return get_identifier ("compl");
29861 case CPP_NOT: return get_identifier ("not");
29862 case CPP_NOT_EQ: return get_identifier ("not_eq");
29863 case CPP_OR_OR: return get_identifier ("or");
29864 case CPP_OR_EQ: return get_identifier ("or_eq");
29865 case CPP_XOR: return get_identifier ("xor");
29866 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29867 default: return token->u.value;
29871 /* Parse an Objective-C params list. */
29873 static tree
29874 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29876 tree params = NULL_TREE;
29877 bool maybe_unary_selector_p = true;
29878 cp_token *token = cp_lexer_peek_token (parser->lexer);
29880 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29882 tree selector = NULL_TREE, type_name, identifier;
29883 tree parm_attr = NULL_TREE;
29885 if (token->keyword == RID_ATTRIBUTE)
29886 break;
29888 if (token->type != CPP_COLON)
29889 selector = cp_parser_objc_selector (parser);
29891 /* Detect if we have a unary selector. */
29892 if (maybe_unary_selector_p
29893 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29895 params = selector; /* Might be followed by attributes. */
29896 break;
29899 maybe_unary_selector_p = false;
29900 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29902 /* Something went quite wrong. There should be a colon
29903 here, but there is not. Stop parsing parameters. */
29904 break;
29906 type_name = cp_parser_objc_typename (parser);
29907 /* New ObjC allows attributes on parameters too. */
29908 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29909 parm_attr = cp_parser_attributes_opt (parser);
29910 identifier = cp_parser_identifier (parser);
29912 params
29913 = chainon (params,
29914 objc_build_keyword_decl (selector,
29915 type_name,
29916 identifier,
29917 parm_attr));
29919 token = cp_lexer_peek_token (parser->lexer);
29922 if (params == NULL_TREE)
29924 cp_parser_error (parser, "objective-c++ method declaration is expected");
29925 return error_mark_node;
29928 /* We allow tail attributes for the method. */
29929 if (token->keyword == RID_ATTRIBUTE)
29931 *attributes = cp_parser_attributes_opt (parser);
29932 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29933 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29934 return params;
29935 cp_parser_error (parser,
29936 "method attributes must be specified at the end");
29937 return error_mark_node;
29940 if (params == NULL_TREE)
29942 cp_parser_error (parser, "objective-c++ method declaration is expected");
29943 return error_mark_node;
29945 return params;
29948 /* Parse the non-keyword Objective-C params. */
29950 static tree
29951 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29952 tree* attributes)
29954 tree params = make_node (TREE_LIST);
29955 cp_token *token = cp_lexer_peek_token (parser->lexer);
29956 *ellipsisp = false; /* Initially, assume no ellipsis. */
29958 while (token->type == CPP_COMMA)
29960 cp_parameter_declarator *parmdecl;
29961 tree parm;
29963 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29964 token = cp_lexer_peek_token (parser->lexer);
29966 if (token->type == CPP_ELLIPSIS)
29968 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29969 *ellipsisp = true;
29970 token = cp_lexer_peek_token (parser->lexer);
29971 break;
29974 /* TODO: parse attributes for tail parameters. */
29975 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29976 parm = grokdeclarator (parmdecl->declarator,
29977 &parmdecl->decl_specifiers,
29978 PARM, /*initialized=*/0,
29979 /*attrlist=*/NULL);
29981 chainon (params, build_tree_list (NULL_TREE, parm));
29982 token = cp_lexer_peek_token (parser->lexer);
29985 /* We allow tail attributes for the method. */
29986 if (token->keyword == RID_ATTRIBUTE)
29988 if (*attributes == NULL_TREE)
29990 *attributes = cp_parser_attributes_opt (parser);
29991 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29992 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29993 return params;
29995 else
29996 /* We have an error, but parse the attributes, so that we can
29997 carry on. */
29998 *attributes = cp_parser_attributes_opt (parser);
30000 cp_parser_error (parser,
30001 "method attributes must be specified at the end");
30002 return error_mark_node;
30005 return params;
30008 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30010 static void
30011 cp_parser_objc_interstitial_code (cp_parser* parser)
30013 cp_token *token = cp_lexer_peek_token (parser->lexer);
30015 /* If the next token is `extern' and the following token is a string
30016 literal, then we have a linkage specification. */
30017 if (token->keyword == RID_EXTERN
30018 && cp_parser_is_pure_string_literal
30019 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30020 cp_parser_linkage_specification (parser);
30021 /* Handle #pragma, if any. */
30022 else if (token->type == CPP_PRAGMA)
30023 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30024 /* Allow stray semicolons. */
30025 else if (token->type == CPP_SEMICOLON)
30026 cp_lexer_consume_token (parser->lexer);
30027 /* Mark methods as optional or required, when building protocols. */
30028 else if (token->keyword == RID_AT_OPTIONAL)
30030 cp_lexer_consume_token (parser->lexer);
30031 objc_set_method_opt (true);
30033 else if (token->keyword == RID_AT_REQUIRED)
30035 cp_lexer_consume_token (parser->lexer);
30036 objc_set_method_opt (false);
30038 else if (token->keyword == RID_NAMESPACE)
30039 cp_parser_namespace_definition (parser);
30040 /* Other stray characters must generate errors. */
30041 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30043 cp_lexer_consume_token (parser->lexer);
30044 error ("stray %qs between Objective-C++ methods",
30045 token->type == CPP_OPEN_BRACE ? "{" : "}");
30047 /* Finally, try to parse a block-declaration, or a function-definition. */
30048 else
30049 cp_parser_block_declaration (parser, /*statement_p=*/false);
30052 /* Parse a method signature. */
30054 static tree
30055 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30057 tree rettype, kwdparms, optparms;
30058 bool ellipsis = false;
30059 bool is_class_method;
30061 is_class_method = cp_parser_objc_method_type (parser);
30062 rettype = cp_parser_objc_typename (parser);
30063 *attributes = NULL_TREE;
30064 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30065 if (kwdparms == error_mark_node)
30066 return error_mark_node;
30067 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30068 if (optparms == error_mark_node)
30069 return error_mark_node;
30071 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30074 static bool
30075 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30077 tree tattr;
30078 cp_lexer_save_tokens (parser->lexer);
30079 tattr = cp_parser_attributes_opt (parser);
30080 gcc_assert (tattr) ;
30082 /* If the attributes are followed by a method introducer, this is not allowed.
30083 Dump the attributes and flag the situation. */
30084 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30085 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30086 return true;
30088 /* Otherwise, the attributes introduce some interstitial code, possibly so
30089 rewind to allow that check. */
30090 cp_lexer_rollback_tokens (parser->lexer);
30091 return false;
30094 /* Parse an Objective-C method prototype list. */
30096 static void
30097 cp_parser_objc_method_prototype_list (cp_parser* parser)
30099 cp_token *token = cp_lexer_peek_token (parser->lexer);
30101 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30103 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30105 tree attributes, sig;
30106 bool is_class_method;
30107 if (token->type == CPP_PLUS)
30108 is_class_method = true;
30109 else
30110 is_class_method = false;
30111 sig = cp_parser_objc_method_signature (parser, &attributes);
30112 if (sig == error_mark_node)
30114 cp_parser_skip_to_end_of_block_or_statement (parser);
30115 token = cp_lexer_peek_token (parser->lexer);
30116 continue;
30118 objc_add_method_declaration (is_class_method, sig, attributes);
30119 cp_parser_consume_semicolon_at_end_of_statement (parser);
30121 else if (token->keyword == RID_AT_PROPERTY)
30122 cp_parser_objc_at_property_declaration (parser);
30123 else if (token->keyword == RID_ATTRIBUTE
30124 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30125 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30126 OPT_Wattributes,
30127 "prefix attributes are ignored for methods");
30128 else
30129 /* Allow for interspersed non-ObjC++ code. */
30130 cp_parser_objc_interstitial_code (parser);
30132 token = cp_lexer_peek_token (parser->lexer);
30135 if (token->type != CPP_EOF)
30136 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30137 else
30138 cp_parser_error (parser, "expected %<@end%>");
30140 objc_finish_interface ();
30143 /* Parse an Objective-C method definition list. */
30145 static void
30146 cp_parser_objc_method_definition_list (cp_parser* parser)
30148 cp_token *token = cp_lexer_peek_token (parser->lexer);
30150 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30152 tree meth;
30154 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30156 cp_token *ptk;
30157 tree sig, attribute;
30158 bool is_class_method;
30159 if (token->type == CPP_PLUS)
30160 is_class_method = true;
30161 else
30162 is_class_method = false;
30163 push_deferring_access_checks (dk_deferred);
30164 sig = cp_parser_objc_method_signature (parser, &attribute);
30165 if (sig == error_mark_node)
30167 cp_parser_skip_to_end_of_block_or_statement (parser);
30168 token = cp_lexer_peek_token (parser->lexer);
30169 continue;
30171 objc_start_method_definition (is_class_method, sig, attribute,
30172 NULL_TREE);
30174 /* For historical reasons, we accept an optional semicolon. */
30175 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30176 cp_lexer_consume_token (parser->lexer);
30178 ptk = cp_lexer_peek_token (parser->lexer);
30179 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30180 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30182 perform_deferred_access_checks (tf_warning_or_error);
30183 stop_deferring_access_checks ();
30184 meth = cp_parser_function_definition_after_declarator (parser,
30185 false);
30186 pop_deferring_access_checks ();
30187 objc_finish_method_definition (meth);
30190 /* The following case will be removed once @synthesize is
30191 completely implemented. */
30192 else if (token->keyword == RID_AT_PROPERTY)
30193 cp_parser_objc_at_property_declaration (parser);
30194 else if (token->keyword == RID_AT_SYNTHESIZE)
30195 cp_parser_objc_at_synthesize_declaration (parser);
30196 else if (token->keyword == RID_AT_DYNAMIC)
30197 cp_parser_objc_at_dynamic_declaration (parser);
30198 else if (token->keyword == RID_ATTRIBUTE
30199 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30200 warning_at (token->location, OPT_Wattributes,
30201 "prefix attributes are ignored for methods");
30202 else
30203 /* Allow for interspersed non-ObjC++ code. */
30204 cp_parser_objc_interstitial_code (parser);
30206 token = cp_lexer_peek_token (parser->lexer);
30209 if (token->type != CPP_EOF)
30210 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30211 else
30212 cp_parser_error (parser, "expected %<@end%>");
30214 objc_finish_implementation ();
30217 /* Parse Objective-C ivars. */
30219 static void
30220 cp_parser_objc_class_ivars (cp_parser* parser)
30222 cp_token *token = cp_lexer_peek_token (parser->lexer);
30224 if (token->type != CPP_OPEN_BRACE)
30225 return; /* No ivars specified. */
30227 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30228 token = cp_lexer_peek_token (parser->lexer);
30230 while (token->type != CPP_CLOSE_BRACE
30231 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30233 cp_decl_specifier_seq declspecs;
30234 int decl_class_or_enum_p;
30235 tree prefix_attributes;
30237 cp_parser_objc_visibility_spec (parser);
30239 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30240 break;
30242 cp_parser_decl_specifier_seq (parser,
30243 CP_PARSER_FLAGS_OPTIONAL,
30244 &declspecs,
30245 &decl_class_or_enum_p);
30247 /* auto, register, static, extern, mutable. */
30248 if (declspecs.storage_class != sc_none)
30250 cp_parser_error (parser, "invalid type for instance variable");
30251 declspecs.storage_class = sc_none;
30254 /* thread_local. */
30255 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30257 cp_parser_error (parser, "invalid type for instance variable");
30258 declspecs.locations[ds_thread] = 0;
30261 /* typedef. */
30262 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30264 cp_parser_error (parser, "invalid type for instance variable");
30265 declspecs.locations[ds_typedef] = 0;
30268 prefix_attributes = declspecs.attributes;
30269 declspecs.attributes = NULL_TREE;
30271 /* Keep going until we hit the `;' at the end of the
30272 declaration. */
30273 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30275 tree width = NULL_TREE, attributes, first_attribute, decl;
30276 cp_declarator *declarator = NULL;
30277 int ctor_dtor_or_conv_p;
30279 /* Check for a (possibly unnamed) bitfield declaration. */
30280 token = cp_lexer_peek_token (parser->lexer);
30281 if (token->type == CPP_COLON)
30282 goto eat_colon;
30284 if (token->type == CPP_NAME
30285 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30286 == CPP_COLON))
30288 /* Get the name of the bitfield. */
30289 declarator = make_id_declarator (NULL_TREE,
30290 cp_parser_identifier (parser),
30291 sfk_none);
30293 eat_colon:
30294 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30295 /* Get the width of the bitfield. */
30296 width
30297 = cp_parser_constant_expression (parser);
30299 else
30301 /* Parse the declarator. */
30302 declarator
30303 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30304 &ctor_dtor_or_conv_p,
30305 /*parenthesized_p=*/NULL,
30306 /*member_p=*/false,
30307 /*friend_p=*/false);
30310 /* Look for attributes that apply to the ivar. */
30311 attributes = cp_parser_attributes_opt (parser);
30312 /* Remember which attributes are prefix attributes and
30313 which are not. */
30314 first_attribute = attributes;
30315 /* Combine the attributes. */
30316 attributes = attr_chainon (prefix_attributes, attributes);
30318 if (width)
30319 /* Create the bitfield declaration. */
30320 decl = grokbitfield (declarator, &declspecs,
30321 width, NULL_TREE, attributes);
30322 else
30323 decl = grokfield (declarator, &declspecs,
30324 NULL_TREE, /*init_const_expr_p=*/false,
30325 NULL_TREE, attributes);
30327 /* Add the instance variable. */
30328 if (decl != error_mark_node && decl != NULL_TREE)
30329 objc_add_instance_variable (decl);
30331 /* Reset PREFIX_ATTRIBUTES. */
30332 if (attributes != error_mark_node)
30334 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30335 attributes = TREE_CHAIN (attributes);
30336 if (attributes)
30337 TREE_CHAIN (attributes) = NULL_TREE;
30340 token = cp_lexer_peek_token (parser->lexer);
30342 if (token->type == CPP_COMMA)
30344 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30345 continue;
30347 break;
30350 cp_parser_consume_semicolon_at_end_of_statement (parser);
30351 token = cp_lexer_peek_token (parser->lexer);
30354 if (token->keyword == RID_AT_END)
30355 cp_parser_error (parser, "expected %<}%>");
30357 /* Do not consume the RID_AT_END, so it will be read again as terminating
30358 the @interface of @implementation. */
30359 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30360 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30362 /* For historical reasons, we accept an optional semicolon. */
30363 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30364 cp_lexer_consume_token (parser->lexer);
30367 /* Parse an Objective-C protocol declaration. */
30369 static void
30370 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30372 tree proto, protorefs;
30373 cp_token *tok;
30375 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30376 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30378 tok = cp_lexer_peek_token (parser->lexer);
30379 error_at (tok->location, "identifier expected after %<@protocol%>");
30380 cp_parser_consume_semicolon_at_end_of_statement (parser);
30381 return;
30384 /* See if we have a forward declaration or a definition. */
30385 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30387 /* Try a forward declaration first. */
30388 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30390 while (true)
30392 tree id;
30394 id = cp_parser_identifier (parser);
30395 if (id == error_mark_node)
30396 break;
30398 objc_declare_protocol (id, attributes);
30400 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30401 cp_lexer_consume_token (parser->lexer);
30402 else
30403 break;
30405 cp_parser_consume_semicolon_at_end_of_statement (parser);
30408 /* Ok, we got a full-fledged definition (or at least should). */
30409 else
30411 proto = cp_parser_identifier (parser);
30412 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30413 objc_start_protocol (proto, protorefs, attributes);
30414 cp_parser_objc_method_prototype_list (parser);
30418 /* Parse an Objective-C superclass or category. */
30420 static void
30421 cp_parser_objc_superclass_or_category (cp_parser *parser,
30422 bool iface_p,
30423 tree *super,
30424 tree *categ, bool *is_class_extension)
30426 cp_token *next = cp_lexer_peek_token (parser->lexer);
30428 *super = *categ = NULL_TREE;
30429 *is_class_extension = false;
30430 if (next->type == CPP_COLON)
30432 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30433 *super = cp_parser_identifier (parser);
30435 else if (next->type == CPP_OPEN_PAREN)
30437 matching_parens parens;
30438 parens.consume_open (parser); /* Eat '('. */
30440 /* If there is no category name, and this is an @interface, we
30441 have a class extension. */
30442 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30444 *categ = NULL_TREE;
30445 *is_class_extension = true;
30447 else
30448 *categ = cp_parser_identifier (parser);
30450 parens.require_close (parser);
30454 /* Parse an Objective-C class interface. */
30456 static void
30457 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30459 tree name, super, categ, protos;
30460 bool is_class_extension;
30462 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30463 name = cp_parser_identifier (parser);
30464 if (name == error_mark_node)
30466 /* It's hard to recover because even if valid @interface stuff
30467 is to follow, we can't compile it (or validate it) if we
30468 don't even know which class it refers to. Let's assume this
30469 was a stray '@interface' token in the stream and skip it.
30471 return;
30473 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30474 &is_class_extension);
30475 protos = cp_parser_objc_protocol_refs_opt (parser);
30477 /* We have either a class or a category on our hands. */
30478 if (categ || is_class_extension)
30479 objc_start_category_interface (name, categ, protos, attributes);
30480 else
30482 objc_start_class_interface (name, super, protos, attributes);
30483 /* Handle instance variable declarations, if any. */
30484 cp_parser_objc_class_ivars (parser);
30485 objc_continue_interface ();
30488 cp_parser_objc_method_prototype_list (parser);
30491 /* Parse an Objective-C class implementation. */
30493 static void
30494 cp_parser_objc_class_implementation (cp_parser* parser)
30496 tree name, super, categ;
30497 bool is_class_extension;
30499 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30500 name = cp_parser_identifier (parser);
30501 if (name == error_mark_node)
30503 /* It's hard to recover because even if valid @implementation
30504 stuff is to follow, we can't compile it (or validate it) if
30505 we don't even know which class it refers to. Let's assume
30506 this was a stray '@implementation' token in the stream and
30507 skip it.
30509 return;
30511 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30512 &is_class_extension);
30514 /* We have either a class or a category on our hands. */
30515 if (categ)
30516 objc_start_category_implementation (name, categ);
30517 else
30519 objc_start_class_implementation (name, super);
30520 /* Handle instance variable declarations, if any. */
30521 cp_parser_objc_class_ivars (parser);
30522 objc_continue_implementation ();
30525 cp_parser_objc_method_definition_list (parser);
30528 /* Consume the @end token and finish off the implementation. */
30530 static void
30531 cp_parser_objc_end_implementation (cp_parser* parser)
30533 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30534 objc_finish_implementation ();
30537 /* Parse an Objective-C declaration. */
30539 static void
30540 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30542 /* Try to figure out what kind of declaration is present. */
30543 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30545 if (attributes)
30546 switch (kwd->keyword)
30548 case RID_AT_ALIAS:
30549 case RID_AT_CLASS:
30550 case RID_AT_END:
30551 error_at (kwd->location, "attributes may not be specified before"
30552 " the %<@%D%> Objective-C++ keyword",
30553 kwd->u.value);
30554 attributes = NULL;
30555 break;
30556 case RID_AT_IMPLEMENTATION:
30557 warning_at (kwd->location, OPT_Wattributes,
30558 "prefix attributes are ignored before %<@%D%>",
30559 kwd->u.value);
30560 attributes = NULL;
30561 default:
30562 break;
30565 switch (kwd->keyword)
30567 case RID_AT_ALIAS:
30568 cp_parser_objc_alias_declaration (parser);
30569 break;
30570 case RID_AT_CLASS:
30571 cp_parser_objc_class_declaration (parser);
30572 break;
30573 case RID_AT_PROTOCOL:
30574 cp_parser_objc_protocol_declaration (parser, attributes);
30575 break;
30576 case RID_AT_INTERFACE:
30577 cp_parser_objc_class_interface (parser, attributes);
30578 break;
30579 case RID_AT_IMPLEMENTATION:
30580 cp_parser_objc_class_implementation (parser);
30581 break;
30582 case RID_AT_END:
30583 cp_parser_objc_end_implementation (parser);
30584 break;
30585 default:
30586 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30587 kwd->u.value);
30588 cp_parser_skip_to_end_of_block_or_statement (parser);
30592 /* Parse an Objective-C try-catch-finally statement.
30594 objc-try-catch-finally-stmt:
30595 @try compound-statement objc-catch-clause-seq [opt]
30596 objc-finally-clause [opt]
30598 objc-catch-clause-seq:
30599 objc-catch-clause objc-catch-clause-seq [opt]
30601 objc-catch-clause:
30602 @catch ( objc-exception-declaration ) compound-statement
30604 objc-finally-clause:
30605 @finally compound-statement
30607 objc-exception-declaration:
30608 parameter-declaration
30609 '...'
30611 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30613 Returns NULL_TREE.
30615 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30616 for C. Keep them in sync. */
30618 static tree
30619 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30621 location_t location;
30622 tree stmt;
30624 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30625 location = cp_lexer_peek_token (parser->lexer)->location;
30626 objc_maybe_warn_exceptions (location);
30627 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30628 node, lest it get absorbed into the surrounding block. */
30629 stmt = push_stmt_list ();
30630 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30631 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30633 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30635 cp_parameter_declarator *parm;
30636 tree parameter_declaration = error_mark_node;
30637 bool seen_open_paren = false;
30638 matching_parens parens;
30640 cp_lexer_consume_token (parser->lexer);
30641 if (parens.require_open (parser))
30642 seen_open_paren = true;
30643 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30645 /* We have "@catch (...)" (where the '...' are literally
30646 what is in the code). Skip the '...'.
30647 parameter_declaration is set to NULL_TREE, and
30648 objc_being_catch_clauses() knows that that means
30649 '...'. */
30650 cp_lexer_consume_token (parser->lexer);
30651 parameter_declaration = NULL_TREE;
30653 else
30655 /* We have "@catch (NSException *exception)" or something
30656 like that. Parse the parameter declaration. */
30657 parm = cp_parser_parameter_declaration (parser, false, NULL);
30658 if (parm == NULL)
30659 parameter_declaration = error_mark_node;
30660 else
30661 parameter_declaration = grokdeclarator (parm->declarator,
30662 &parm->decl_specifiers,
30663 PARM, /*initialized=*/0,
30664 /*attrlist=*/NULL);
30666 if (seen_open_paren)
30667 parens.require_close (parser);
30668 else
30670 /* If there was no open parenthesis, we are recovering from
30671 an error, and we are trying to figure out what mistake
30672 the user has made. */
30674 /* If there is an immediate closing parenthesis, the user
30675 probably forgot the opening one (ie, they typed "@catch
30676 NSException *e)". Parse the closing parenthesis and keep
30677 going. */
30678 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30679 cp_lexer_consume_token (parser->lexer);
30681 /* If these is no immediate closing parenthesis, the user
30682 probably doesn't know that parenthesis are required at
30683 all (ie, they typed "@catch NSException *e"). So, just
30684 forget about the closing parenthesis and keep going. */
30686 objc_begin_catch_clause (parameter_declaration);
30687 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30688 objc_finish_catch_clause ();
30690 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30692 cp_lexer_consume_token (parser->lexer);
30693 location = cp_lexer_peek_token (parser->lexer)->location;
30694 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30695 node, lest it get absorbed into the surrounding block. */
30696 stmt = push_stmt_list ();
30697 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30698 objc_build_finally_clause (location, pop_stmt_list (stmt));
30701 return objc_finish_try_stmt ();
30704 /* Parse an Objective-C synchronized statement.
30706 objc-synchronized-stmt:
30707 @synchronized ( expression ) compound-statement
30709 Returns NULL_TREE. */
30711 static tree
30712 cp_parser_objc_synchronized_statement (cp_parser *parser)
30714 location_t location;
30715 tree lock, stmt;
30717 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30719 location = cp_lexer_peek_token (parser->lexer)->location;
30720 objc_maybe_warn_exceptions (location);
30721 matching_parens parens;
30722 parens.require_open (parser);
30723 lock = cp_parser_expression (parser);
30724 parens.require_close (parser);
30726 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30727 node, lest it get absorbed into the surrounding block. */
30728 stmt = push_stmt_list ();
30729 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30731 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30734 /* Parse an Objective-C throw statement.
30736 objc-throw-stmt:
30737 @throw assignment-expression [opt] ;
30739 Returns a constructed '@throw' statement. */
30741 static tree
30742 cp_parser_objc_throw_statement (cp_parser *parser)
30744 tree expr = NULL_TREE;
30745 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30747 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30749 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30750 expr = cp_parser_expression (parser);
30752 cp_parser_consume_semicolon_at_end_of_statement (parser);
30754 return objc_build_throw_stmt (loc, expr);
30757 /* Parse an Objective-C statement. */
30759 static tree
30760 cp_parser_objc_statement (cp_parser * parser)
30762 /* Try to figure out what kind of declaration is present. */
30763 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30765 switch (kwd->keyword)
30767 case RID_AT_TRY:
30768 return cp_parser_objc_try_catch_finally_statement (parser);
30769 case RID_AT_SYNCHRONIZED:
30770 return cp_parser_objc_synchronized_statement (parser);
30771 case RID_AT_THROW:
30772 return cp_parser_objc_throw_statement (parser);
30773 default:
30774 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30775 kwd->u.value);
30776 cp_parser_skip_to_end_of_block_or_statement (parser);
30779 return error_mark_node;
30782 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30783 look ahead to see if an objc keyword follows the attributes. This
30784 is to detect the use of prefix attributes on ObjC @interface and
30785 @protocol. */
30787 static bool
30788 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30790 cp_lexer_save_tokens (parser->lexer);
30791 *attrib = cp_parser_attributes_opt (parser);
30792 gcc_assert (*attrib);
30793 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30795 cp_lexer_commit_tokens (parser->lexer);
30796 return true;
30798 cp_lexer_rollback_tokens (parser->lexer);
30799 return false;
30802 /* This routine is a minimal replacement for
30803 c_parser_struct_declaration () used when parsing the list of
30804 types/names or ObjC++ properties. For example, when parsing the
30805 code
30807 @property (readonly) int a, b, c;
30809 this function is responsible for parsing "int a, int b, int c" and
30810 returning the declarations as CHAIN of DECLs.
30812 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30813 similar parsing. */
30814 static tree
30815 cp_parser_objc_struct_declaration (cp_parser *parser)
30817 tree decls = NULL_TREE;
30818 cp_decl_specifier_seq declspecs;
30819 int decl_class_or_enum_p;
30820 tree prefix_attributes;
30822 cp_parser_decl_specifier_seq (parser,
30823 CP_PARSER_FLAGS_NONE,
30824 &declspecs,
30825 &decl_class_or_enum_p);
30827 if (declspecs.type == error_mark_node)
30828 return error_mark_node;
30830 /* auto, register, static, extern, mutable. */
30831 if (declspecs.storage_class != sc_none)
30833 cp_parser_error (parser, "invalid type for property");
30834 declspecs.storage_class = sc_none;
30837 /* thread_local. */
30838 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30840 cp_parser_error (parser, "invalid type for property");
30841 declspecs.locations[ds_thread] = 0;
30844 /* typedef. */
30845 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30847 cp_parser_error (parser, "invalid type for property");
30848 declspecs.locations[ds_typedef] = 0;
30851 prefix_attributes = declspecs.attributes;
30852 declspecs.attributes = NULL_TREE;
30854 /* Keep going until we hit the `;' at the end of the declaration. */
30855 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30857 tree attributes, first_attribute, decl;
30858 cp_declarator *declarator;
30859 cp_token *token;
30861 /* Parse the declarator. */
30862 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30863 NULL, NULL, false, false);
30865 /* Look for attributes that apply to the ivar. */
30866 attributes = cp_parser_attributes_opt (parser);
30867 /* Remember which attributes are prefix attributes and
30868 which are not. */
30869 first_attribute = attributes;
30870 /* Combine the attributes. */
30871 attributes = attr_chainon (prefix_attributes, attributes);
30873 decl = grokfield (declarator, &declspecs,
30874 NULL_TREE, /*init_const_expr_p=*/false,
30875 NULL_TREE, attributes);
30877 if (decl == error_mark_node || decl == NULL_TREE)
30878 return error_mark_node;
30880 /* Reset PREFIX_ATTRIBUTES. */
30881 if (attributes != error_mark_node)
30883 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30884 attributes = TREE_CHAIN (attributes);
30885 if (attributes)
30886 TREE_CHAIN (attributes) = NULL_TREE;
30889 DECL_CHAIN (decl) = decls;
30890 decls = decl;
30892 token = cp_lexer_peek_token (parser->lexer);
30893 if (token->type == CPP_COMMA)
30895 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30896 continue;
30898 else
30899 break;
30901 return decls;
30904 /* Parse an Objective-C @property declaration. The syntax is:
30906 objc-property-declaration:
30907 '@property' objc-property-attributes[opt] struct-declaration ;
30909 objc-property-attributes:
30910 '(' objc-property-attribute-list ')'
30912 objc-property-attribute-list:
30913 objc-property-attribute
30914 objc-property-attribute-list, objc-property-attribute
30916 objc-property-attribute
30917 'getter' = identifier
30918 'setter' = identifier
30919 'readonly'
30920 'readwrite'
30921 'assign'
30922 'retain'
30923 'copy'
30924 'nonatomic'
30926 For example:
30927 @property NSString *name;
30928 @property (readonly) id object;
30929 @property (retain, nonatomic, getter=getTheName) id name;
30930 @property int a, b, c;
30932 PS: This function is identical to
30933 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30934 static void
30935 cp_parser_objc_at_property_declaration (cp_parser *parser)
30937 /* The following variables hold the attributes of the properties as
30938 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30939 seen. When we see an attribute, we set them to 'true' (if they
30940 are boolean properties) or to the identifier (if they have an
30941 argument, ie, for getter and setter). Note that here we only
30942 parse the list of attributes, check the syntax and accumulate the
30943 attributes that we find. objc_add_property_declaration() will
30944 then process the information. */
30945 bool property_assign = false;
30946 bool property_copy = false;
30947 tree property_getter_ident = NULL_TREE;
30948 bool property_nonatomic = false;
30949 bool property_readonly = false;
30950 bool property_readwrite = false;
30951 bool property_retain = false;
30952 tree property_setter_ident = NULL_TREE;
30954 /* 'properties' is the list of properties that we read. Usually a
30955 single one, but maybe more (eg, in "@property int a, b, c;" there
30956 are three). */
30957 tree properties;
30958 location_t loc;
30960 loc = cp_lexer_peek_token (parser->lexer)->location;
30962 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30964 /* Parse the optional attribute list... */
30965 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30967 /* Eat the '('. */
30968 matching_parens parens;
30969 parens.consume_open (parser);
30971 while (true)
30973 bool syntax_error = false;
30974 cp_token *token = cp_lexer_peek_token (parser->lexer);
30975 enum rid keyword;
30977 if (token->type != CPP_NAME)
30979 cp_parser_error (parser, "expected identifier");
30980 break;
30982 keyword = C_RID_CODE (token->u.value);
30983 cp_lexer_consume_token (parser->lexer);
30984 switch (keyword)
30986 case RID_ASSIGN: property_assign = true; break;
30987 case RID_COPY: property_copy = true; break;
30988 case RID_NONATOMIC: property_nonatomic = true; break;
30989 case RID_READONLY: property_readonly = true; break;
30990 case RID_READWRITE: property_readwrite = true; break;
30991 case RID_RETAIN: property_retain = true; break;
30993 case RID_GETTER:
30994 case RID_SETTER:
30995 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30997 if (keyword == RID_GETTER)
30998 cp_parser_error (parser,
30999 "missing %<=%> (after %<getter%> attribute)");
31000 else
31001 cp_parser_error (parser,
31002 "missing %<=%> (after %<setter%> attribute)");
31003 syntax_error = true;
31004 break;
31006 cp_lexer_consume_token (parser->lexer); /* eat the = */
31007 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31009 cp_parser_error (parser, "expected identifier");
31010 syntax_error = true;
31011 break;
31013 if (keyword == RID_SETTER)
31015 if (property_setter_ident != NULL_TREE)
31017 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31018 cp_lexer_consume_token (parser->lexer);
31020 else
31021 property_setter_ident = cp_parser_objc_selector (parser);
31022 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31023 cp_parser_error (parser, "setter name must terminate with %<:%>");
31024 else
31025 cp_lexer_consume_token (parser->lexer);
31027 else
31029 if (property_getter_ident != NULL_TREE)
31031 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31032 cp_lexer_consume_token (parser->lexer);
31034 else
31035 property_getter_ident = cp_parser_objc_selector (parser);
31037 break;
31038 default:
31039 cp_parser_error (parser, "unknown property attribute");
31040 syntax_error = true;
31041 break;
31044 if (syntax_error)
31045 break;
31047 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31048 cp_lexer_consume_token (parser->lexer);
31049 else
31050 break;
31053 /* FIXME: "@property (setter, assign);" will generate a spurious
31054 "error: expected ‘)’ before ‘,’ token". This is because
31055 cp_parser_require, unlike the C counterpart, will produce an
31056 error even if we are in error recovery. */
31057 if (!parens.require_close (parser))
31059 cp_parser_skip_to_closing_parenthesis (parser,
31060 /*recovering=*/true,
31061 /*or_comma=*/false,
31062 /*consume_paren=*/true);
31066 /* ... and the property declaration(s). */
31067 properties = cp_parser_objc_struct_declaration (parser);
31069 if (properties == error_mark_node)
31071 cp_parser_skip_to_end_of_statement (parser);
31072 /* If the next token is now a `;', consume it. */
31073 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31074 cp_lexer_consume_token (parser->lexer);
31075 return;
31078 if (properties == NULL_TREE)
31079 cp_parser_error (parser, "expected identifier");
31080 else
31082 /* Comma-separated properties are chained together in
31083 reverse order; add them one by one. */
31084 properties = nreverse (properties);
31086 for (; properties; properties = TREE_CHAIN (properties))
31087 objc_add_property_declaration (loc, copy_node (properties),
31088 property_readonly, property_readwrite,
31089 property_assign, property_retain,
31090 property_copy, property_nonatomic,
31091 property_getter_ident, property_setter_ident);
31094 cp_parser_consume_semicolon_at_end_of_statement (parser);
31097 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31099 objc-synthesize-declaration:
31100 @synthesize objc-synthesize-identifier-list ;
31102 objc-synthesize-identifier-list:
31103 objc-synthesize-identifier
31104 objc-synthesize-identifier-list, objc-synthesize-identifier
31106 objc-synthesize-identifier
31107 identifier
31108 identifier = identifier
31110 For example:
31111 @synthesize MyProperty;
31112 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31114 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31115 for C. Keep them in sync.
31117 static void
31118 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31120 tree list = NULL_TREE;
31121 location_t loc;
31122 loc = cp_lexer_peek_token (parser->lexer)->location;
31124 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31125 while (true)
31127 tree property, ivar;
31128 property = cp_parser_identifier (parser);
31129 if (property == error_mark_node)
31131 cp_parser_consume_semicolon_at_end_of_statement (parser);
31132 return;
31134 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31136 cp_lexer_consume_token (parser->lexer);
31137 ivar = cp_parser_identifier (parser);
31138 if (ivar == error_mark_node)
31140 cp_parser_consume_semicolon_at_end_of_statement (parser);
31141 return;
31144 else
31145 ivar = NULL_TREE;
31146 list = chainon (list, build_tree_list (ivar, property));
31147 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31148 cp_lexer_consume_token (parser->lexer);
31149 else
31150 break;
31152 cp_parser_consume_semicolon_at_end_of_statement (parser);
31153 objc_add_synthesize_declaration (loc, list);
31156 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31158 objc-dynamic-declaration:
31159 @dynamic identifier-list ;
31161 For example:
31162 @dynamic MyProperty;
31163 @dynamic MyProperty, AnotherProperty;
31165 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31166 for C. Keep them in sync.
31168 static void
31169 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31171 tree list = NULL_TREE;
31172 location_t loc;
31173 loc = cp_lexer_peek_token (parser->lexer)->location;
31175 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31176 while (true)
31178 tree property;
31179 property = cp_parser_identifier (parser);
31180 if (property == error_mark_node)
31182 cp_parser_consume_semicolon_at_end_of_statement (parser);
31183 return;
31185 list = chainon (list, build_tree_list (NULL, property));
31186 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31187 cp_lexer_consume_token (parser->lexer);
31188 else
31189 break;
31191 cp_parser_consume_semicolon_at_end_of_statement (parser);
31192 objc_add_dynamic_declaration (loc, list);
31196 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31198 /* Returns name of the next clause.
31199 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31200 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31201 returned and the token is consumed. */
31203 static pragma_omp_clause
31204 cp_parser_omp_clause_name (cp_parser *parser)
31206 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31208 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31209 result = PRAGMA_OACC_CLAUSE_AUTO;
31210 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31211 result = PRAGMA_OMP_CLAUSE_IF;
31212 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31213 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31214 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31215 result = PRAGMA_OACC_CLAUSE_DELETE;
31216 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31217 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31218 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31219 result = PRAGMA_OMP_CLAUSE_FOR;
31220 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31222 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31223 const char *p = IDENTIFIER_POINTER (id);
31225 switch (p[0])
31227 case 'a':
31228 if (!strcmp ("aligned", p))
31229 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31230 else if (!strcmp ("async", p))
31231 result = PRAGMA_OACC_CLAUSE_ASYNC;
31232 break;
31233 case 'c':
31234 if (!strcmp ("collapse", p))
31235 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31236 else if (!strcmp ("copy", p))
31237 result = PRAGMA_OACC_CLAUSE_COPY;
31238 else if (!strcmp ("copyin", p))
31239 result = PRAGMA_OMP_CLAUSE_COPYIN;
31240 else if (!strcmp ("copyout", p))
31241 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31242 else if (!strcmp ("copyprivate", p))
31243 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31244 else if (!strcmp ("create", p))
31245 result = PRAGMA_OACC_CLAUSE_CREATE;
31246 break;
31247 case 'd':
31248 if (!strcmp ("defaultmap", p))
31249 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31250 else if (!strcmp ("depend", p))
31251 result = PRAGMA_OMP_CLAUSE_DEPEND;
31252 else if (!strcmp ("device", p))
31253 result = PRAGMA_OMP_CLAUSE_DEVICE;
31254 else if (!strcmp ("deviceptr", p))
31255 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31256 else if (!strcmp ("device_resident", p))
31257 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31258 else if (!strcmp ("dist_schedule", p))
31259 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31260 break;
31261 case 'f':
31262 if (!strcmp ("final", p))
31263 result = PRAGMA_OMP_CLAUSE_FINAL;
31264 else if (!strcmp ("firstprivate", p))
31265 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31266 else if (!strcmp ("from", p))
31267 result = PRAGMA_OMP_CLAUSE_FROM;
31268 break;
31269 case 'g':
31270 if (!strcmp ("gang", p))
31271 result = PRAGMA_OACC_CLAUSE_GANG;
31272 else if (!strcmp ("grainsize", p))
31273 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31274 break;
31275 case 'h':
31276 if (!strcmp ("hint", p))
31277 result = PRAGMA_OMP_CLAUSE_HINT;
31278 else if (!strcmp ("host", p))
31279 result = PRAGMA_OACC_CLAUSE_HOST;
31280 break;
31281 case 'i':
31282 if (!strcmp ("inbranch", p))
31283 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31284 else if (!strcmp ("independent", p))
31285 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31286 else if (!strcmp ("is_device_ptr", p))
31287 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31288 break;
31289 case 'l':
31290 if (!strcmp ("lastprivate", p))
31291 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31292 else if (!strcmp ("linear", p))
31293 result = PRAGMA_OMP_CLAUSE_LINEAR;
31294 else if (!strcmp ("link", p))
31295 result = PRAGMA_OMP_CLAUSE_LINK;
31296 break;
31297 case 'm':
31298 if (!strcmp ("map", p))
31299 result = PRAGMA_OMP_CLAUSE_MAP;
31300 else if (!strcmp ("mergeable", p))
31301 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31302 break;
31303 case 'n':
31304 if (!strcmp ("nogroup", p))
31305 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31306 else if (!strcmp ("notinbranch", p))
31307 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31308 else if (!strcmp ("nowait", p))
31309 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31310 else if (!strcmp ("num_gangs", p))
31311 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31312 else if (!strcmp ("num_tasks", p))
31313 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31314 else if (!strcmp ("num_teams", p))
31315 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31316 else if (!strcmp ("num_threads", p))
31317 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31318 else if (!strcmp ("num_workers", p))
31319 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31320 break;
31321 case 'o':
31322 if (!strcmp ("ordered", p))
31323 result = PRAGMA_OMP_CLAUSE_ORDERED;
31324 break;
31325 case 'p':
31326 if (!strcmp ("parallel", p))
31327 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31328 else if (!strcmp ("present", p))
31329 result = PRAGMA_OACC_CLAUSE_PRESENT;
31330 else if (!strcmp ("present_or_copy", p)
31331 || !strcmp ("pcopy", p))
31332 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
31333 else if (!strcmp ("present_or_copyin", p)
31334 || !strcmp ("pcopyin", p))
31335 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
31336 else if (!strcmp ("present_or_copyout", p)
31337 || !strcmp ("pcopyout", p))
31338 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
31339 else if (!strcmp ("present_or_create", p)
31340 || !strcmp ("pcreate", p))
31341 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
31342 else if (!strcmp ("priority", p))
31343 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31344 else if (!strcmp ("proc_bind", p))
31345 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31346 break;
31347 case 'r':
31348 if (!strcmp ("reduction", p))
31349 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31350 break;
31351 case 's':
31352 if (!strcmp ("safelen", p))
31353 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31354 else if (!strcmp ("schedule", p))
31355 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31356 else if (!strcmp ("sections", p))
31357 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31358 else if (!strcmp ("self", p))
31359 result = PRAGMA_OACC_CLAUSE_SELF;
31360 else if (!strcmp ("seq", p))
31361 result = PRAGMA_OACC_CLAUSE_SEQ;
31362 else if (!strcmp ("shared", p))
31363 result = PRAGMA_OMP_CLAUSE_SHARED;
31364 else if (!strcmp ("simd", p))
31365 result = PRAGMA_OMP_CLAUSE_SIMD;
31366 else if (!strcmp ("simdlen", p))
31367 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31368 break;
31369 case 't':
31370 if (!strcmp ("taskgroup", p))
31371 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31372 else if (!strcmp ("thread_limit", p))
31373 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31374 else if (!strcmp ("threads", p))
31375 result = PRAGMA_OMP_CLAUSE_THREADS;
31376 else if (!strcmp ("tile", p))
31377 result = PRAGMA_OACC_CLAUSE_TILE;
31378 else if (!strcmp ("to", p))
31379 result = PRAGMA_OMP_CLAUSE_TO;
31380 break;
31381 case 'u':
31382 if (!strcmp ("uniform", p))
31383 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31384 else if (!strcmp ("untied", p))
31385 result = PRAGMA_OMP_CLAUSE_UNTIED;
31386 else if (!strcmp ("use_device", p))
31387 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31388 else if (!strcmp ("use_device_ptr", p))
31389 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31390 break;
31391 case 'v':
31392 if (!strcmp ("vector", p))
31393 result = PRAGMA_OACC_CLAUSE_VECTOR;
31394 else if (!strcmp ("vector_length", p))
31395 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31396 break;
31397 case 'w':
31398 if (!strcmp ("wait", p))
31399 result = PRAGMA_OACC_CLAUSE_WAIT;
31400 else if (!strcmp ("worker", p))
31401 result = PRAGMA_OACC_CLAUSE_WORKER;
31402 break;
31406 if (result != PRAGMA_OMP_CLAUSE_NONE)
31407 cp_lexer_consume_token (parser->lexer);
31409 return result;
31412 /* Validate that a clause of the given type does not already exist. */
31414 static void
31415 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31416 const char *name, location_t location)
31418 tree c;
31420 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31421 if (OMP_CLAUSE_CODE (c) == code)
31423 error_at (location, "too many %qs clauses", name);
31424 break;
31428 /* OpenMP 2.5:
31429 variable-list:
31430 identifier
31431 variable-list , identifier
31433 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31434 colon). An opening parenthesis will have been consumed by the caller.
31436 If KIND is nonzero, create the appropriate node and install the decl
31437 in OMP_CLAUSE_DECL and add the node to the head of the list.
31439 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31440 return the list created.
31442 COLON can be NULL if only closing parenthesis should end the list,
31443 or pointer to bool which will receive false if the list is terminated
31444 by closing parenthesis or true if the list is terminated by colon. */
31446 static tree
31447 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31448 tree list, bool *colon)
31450 cp_token *token;
31451 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31452 if (colon)
31454 parser->colon_corrects_to_scope_p = false;
31455 *colon = false;
31457 while (1)
31459 tree name, decl;
31461 token = cp_lexer_peek_token (parser->lexer);
31462 if (kind != 0
31463 && current_class_ptr
31464 && cp_parser_is_keyword (token, RID_THIS))
31466 decl = finish_this_expr ();
31467 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31468 || CONVERT_EXPR_P (decl))
31469 decl = TREE_OPERAND (decl, 0);
31470 cp_lexer_consume_token (parser->lexer);
31472 else
31474 name = cp_parser_id_expression (parser, /*template_p=*/false,
31475 /*check_dependency_p=*/true,
31476 /*template_p=*/NULL,
31477 /*declarator_p=*/false,
31478 /*optional_p=*/false);
31479 if (name == error_mark_node)
31480 goto skip_comma;
31482 if (identifier_p (name))
31483 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31484 else
31485 decl = name;
31486 if (decl == error_mark_node)
31487 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31488 token->location);
31490 if (decl == error_mark_node)
31492 else if (kind != 0)
31494 switch (kind)
31496 case OMP_CLAUSE__CACHE_:
31497 /* The OpenACC cache directive explicitly only allows "array
31498 elements or subarrays". */
31499 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31501 error_at (token->location, "expected %<[%>");
31502 decl = error_mark_node;
31503 break;
31505 /* FALLTHROUGH. */
31506 case OMP_CLAUSE_MAP:
31507 case OMP_CLAUSE_FROM:
31508 case OMP_CLAUSE_TO:
31509 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31511 location_t loc
31512 = cp_lexer_peek_token (parser->lexer)->location;
31513 cp_id_kind idk = CP_ID_KIND_NONE;
31514 cp_lexer_consume_token (parser->lexer);
31515 decl = convert_from_reference (decl);
31516 decl
31517 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31518 decl, false,
31519 &idk, loc);
31521 /* FALLTHROUGH. */
31522 case OMP_CLAUSE_DEPEND:
31523 case OMP_CLAUSE_REDUCTION:
31524 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31526 tree low_bound = NULL_TREE, length = NULL_TREE;
31528 parser->colon_corrects_to_scope_p = false;
31529 cp_lexer_consume_token (parser->lexer);
31530 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31531 low_bound = cp_parser_expression (parser);
31532 if (!colon)
31533 parser->colon_corrects_to_scope_p
31534 = saved_colon_corrects_to_scope_p;
31535 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31536 length = integer_one_node;
31537 else
31539 /* Look for `:'. */
31540 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31541 goto skip_comma;
31542 if (!cp_lexer_next_token_is (parser->lexer,
31543 CPP_CLOSE_SQUARE))
31544 length = cp_parser_expression (parser);
31546 /* Look for the closing `]'. */
31547 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31548 RT_CLOSE_SQUARE))
31549 goto skip_comma;
31551 decl = tree_cons (low_bound, length, decl);
31553 break;
31554 default:
31555 break;
31558 tree u = build_omp_clause (token->location, kind);
31559 OMP_CLAUSE_DECL (u) = decl;
31560 OMP_CLAUSE_CHAIN (u) = list;
31561 list = u;
31563 else
31564 list = tree_cons (decl, NULL_TREE, list);
31566 get_comma:
31567 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31568 break;
31569 cp_lexer_consume_token (parser->lexer);
31572 if (colon)
31573 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31575 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31577 *colon = true;
31578 cp_parser_require (parser, CPP_COLON, RT_COLON);
31579 return list;
31582 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31584 int ending;
31586 /* Try to resync to an unnested comma. Copied from
31587 cp_parser_parenthesized_expression_list. */
31588 skip_comma:
31589 if (colon)
31590 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31591 ending = cp_parser_skip_to_closing_parenthesis (parser,
31592 /*recovering=*/true,
31593 /*or_comma=*/true,
31594 /*consume_paren=*/true);
31595 if (ending < 0)
31596 goto get_comma;
31599 return list;
31602 /* Similarly, but expect leading and trailing parenthesis. This is a very
31603 common case for omp clauses. */
31605 static tree
31606 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31608 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31609 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31610 return list;
31613 /* OpenACC 2.0:
31614 copy ( variable-list )
31615 copyin ( variable-list )
31616 copyout ( variable-list )
31617 create ( variable-list )
31618 delete ( variable-list )
31619 present ( variable-list )
31620 present_or_copy ( variable-list )
31621 pcopy ( variable-list )
31622 present_or_copyin ( variable-list )
31623 pcopyin ( variable-list )
31624 present_or_copyout ( variable-list )
31625 pcopyout ( variable-list )
31626 present_or_create ( variable-list )
31627 pcreate ( variable-list ) */
31629 static tree
31630 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31631 tree list)
31633 enum gomp_map_kind kind;
31634 switch (c_kind)
31636 case PRAGMA_OACC_CLAUSE_COPY:
31637 kind = GOMP_MAP_FORCE_TOFROM;
31638 break;
31639 case PRAGMA_OACC_CLAUSE_COPYIN:
31640 kind = GOMP_MAP_FORCE_TO;
31641 break;
31642 case PRAGMA_OACC_CLAUSE_COPYOUT:
31643 kind = GOMP_MAP_FORCE_FROM;
31644 break;
31645 case PRAGMA_OACC_CLAUSE_CREATE:
31646 kind = GOMP_MAP_FORCE_ALLOC;
31647 break;
31648 case PRAGMA_OACC_CLAUSE_DELETE:
31649 kind = GOMP_MAP_DELETE;
31650 break;
31651 case PRAGMA_OACC_CLAUSE_DEVICE:
31652 kind = GOMP_MAP_FORCE_TO;
31653 break;
31654 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31655 kind = GOMP_MAP_DEVICE_RESIDENT;
31656 break;
31657 case PRAGMA_OACC_CLAUSE_HOST:
31658 case PRAGMA_OACC_CLAUSE_SELF:
31659 kind = GOMP_MAP_FORCE_FROM;
31660 break;
31661 case PRAGMA_OACC_CLAUSE_LINK:
31662 kind = GOMP_MAP_LINK;
31663 break;
31664 case PRAGMA_OACC_CLAUSE_PRESENT:
31665 kind = GOMP_MAP_FORCE_PRESENT;
31666 break;
31667 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31668 kind = GOMP_MAP_TOFROM;
31669 break;
31670 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31671 kind = GOMP_MAP_TO;
31672 break;
31673 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31674 kind = GOMP_MAP_FROM;
31675 break;
31676 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31677 kind = GOMP_MAP_ALLOC;
31678 break;
31679 default:
31680 gcc_unreachable ();
31682 tree nl, c;
31683 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31685 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31686 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31688 return nl;
31691 /* OpenACC 2.0:
31692 deviceptr ( variable-list ) */
31694 static tree
31695 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31697 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31698 tree vars, t;
31700 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31701 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31702 variable-list must only allow for pointer variables. */
31703 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31704 for (t = vars; t; t = TREE_CHAIN (t))
31706 tree v = TREE_PURPOSE (t);
31707 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31708 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31709 OMP_CLAUSE_DECL (u) = v;
31710 OMP_CLAUSE_CHAIN (u) = list;
31711 list = u;
31714 return list;
31717 /* OpenACC 2.0:
31718 auto
31719 independent
31720 nohost
31721 seq */
31723 static tree
31724 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31725 enum omp_clause_code code,
31726 tree list, location_t location)
31728 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31729 tree c = build_omp_clause (location, code);
31730 OMP_CLAUSE_CHAIN (c) = list;
31731 return c;
31734 /* OpenACC:
31735 num_gangs ( expression )
31736 num_workers ( expression )
31737 vector_length ( expression ) */
31739 static tree
31740 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31741 const char *str, tree list)
31743 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31745 matching_parens parens;
31746 if (!parens.require_open (parser))
31747 return list;
31749 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31751 if (t == error_mark_node
31752 || !parens.require_close (parser))
31754 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31755 /*or_comma=*/false,
31756 /*consume_paren=*/true);
31757 return list;
31760 check_no_duplicate_clause (list, code, str, loc);
31762 tree c = build_omp_clause (loc, code);
31763 OMP_CLAUSE_OPERAND (c, 0) = t;
31764 OMP_CLAUSE_CHAIN (c) = list;
31765 return c;
31768 /* OpenACC:
31770 gang [( gang-arg-list )]
31771 worker [( [num:] int-expr )]
31772 vector [( [length:] int-expr )]
31774 where gang-arg is one of:
31776 [num:] int-expr
31777 static: size-expr
31779 and size-expr may be:
31782 int-expr
31785 static tree
31786 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31787 const char *str, tree list)
31789 const char *id = "num";
31790 cp_lexer *lexer = parser->lexer;
31791 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31792 location_t loc = cp_lexer_peek_token (lexer)->location;
31794 if (kind == OMP_CLAUSE_VECTOR)
31795 id = "length";
31797 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31799 matching_parens parens;
31800 parens.consume_open (parser);
31804 cp_token *next = cp_lexer_peek_token (lexer);
31805 int idx = 0;
31807 /* Gang static argument. */
31808 if (kind == OMP_CLAUSE_GANG
31809 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31811 cp_lexer_consume_token (lexer);
31813 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31814 goto cleanup_error;
31816 idx = 1;
31817 if (ops[idx] != NULL)
31819 cp_parser_error (parser, "too many %<static%> arguments");
31820 goto cleanup_error;
31823 /* Check for the '*' argument. */
31824 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31825 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31826 || cp_lexer_nth_token_is (parser->lexer, 2,
31827 CPP_CLOSE_PAREN)))
31829 cp_lexer_consume_token (lexer);
31830 ops[idx] = integer_minus_one_node;
31832 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31834 cp_lexer_consume_token (lexer);
31835 continue;
31837 else break;
31840 /* Worker num: argument and vector length: arguments. */
31841 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31842 && id_equal (next->u.value, id)
31843 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31845 cp_lexer_consume_token (lexer); /* id */
31846 cp_lexer_consume_token (lexer); /* ':' */
31849 /* Now collect the actual argument. */
31850 if (ops[idx] != NULL_TREE)
31852 cp_parser_error (parser, "unexpected argument");
31853 goto cleanup_error;
31856 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31857 false);
31858 if (expr == error_mark_node)
31859 goto cleanup_error;
31861 mark_exp_read (expr);
31862 ops[idx] = expr;
31864 if (kind == OMP_CLAUSE_GANG
31865 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31867 cp_lexer_consume_token (lexer);
31868 continue;
31870 break;
31872 while (1);
31874 if (!parens.require_close (parser))
31875 goto cleanup_error;
31878 check_no_duplicate_clause (list, kind, str, loc);
31880 c = build_omp_clause (loc, kind);
31882 if (ops[1])
31883 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31885 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31886 OMP_CLAUSE_CHAIN (c) = list;
31888 return c;
31890 cleanup_error:
31891 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31892 return list;
31895 /* OpenACC 2.0:
31896 tile ( size-expr-list ) */
31898 static tree
31899 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31901 tree c, expr = error_mark_node;
31902 tree tile = NULL_TREE;
31904 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31905 so, but the spec authors never considered such a case and have
31906 differing opinions on what it might mean, including 'not
31907 allowed'.) */
31908 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31909 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31910 clause_loc);
31912 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31913 return list;
31917 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31918 return list;
31920 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31921 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31922 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31924 cp_lexer_consume_token (parser->lexer);
31925 expr = integer_zero_node;
31927 else
31928 expr = cp_parser_constant_expression (parser);
31930 tile = tree_cons (NULL_TREE, expr, tile);
31932 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31934 /* Consume the trailing ')'. */
31935 cp_lexer_consume_token (parser->lexer);
31937 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31938 tile = nreverse (tile);
31939 OMP_CLAUSE_TILE_LIST (c) = tile;
31940 OMP_CLAUSE_CHAIN (c) = list;
31941 return c;
31944 /* OpenACC 2.0
31945 Parse wait clause or directive parameters. */
31947 static tree
31948 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31950 vec<tree, va_gc> *args;
31951 tree t, args_tree;
31953 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31954 /*cast_p=*/false,
31955 /*allow_expansion_p=*/true,
31956 /*non_constant_p=*/NULL);
31958 if (args == NULL || args->length () == 0)
31960 cp_parser_error (parser, "expected integer expression before ')'");
31961 if (args != NULL)
31962 release_tree_vector (args);
31963 return list;
31966 args_tree = build_tree_list_vec (args);
31968 release_tree_vector (args);
31970 for (t = args_tree; t; t = TREE_CHAIN (t))
31972 tree targ = TREE_VALUE (t);
31974 if (targ != error_mark_node)
31976 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31977 error ("%<wait%> expression must be integral");
31978 else
31980 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31982 targ = mark_rvalue_use (targ);
31983 OMP_CLAUSE_DECL (c) = targ;
31984 OMP_CLAUSE_CHAIN (c) = list;
31985 list = c;
31990 return list;
31993 /* OpenACC:
31994 wait ( int-expr-list ) */
31996 static tree
31997 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31999 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32001 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32002 return list;
32004 list = cp_parser_oacc_wait_list (parser, location, list);
32006 return list;
32009 /* OpenMP 3.0:
32010 collapse ( constant-expression ) */
32012 static tree
32013 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32015 tree c, num;
32016 location_t loc;
32017 HOST_WIDE_INT n;
32019 loc = cp_lexer_peek_token (parser->lexer)->location;
32020 matching_parens parens;
32021 if (!parens.require_open (parser))
32022 return list;
32024 num = cp_parser_constant_expression (parser);
32026 if (!parens.require_close (parser))
32027 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32028 /*or_comma=*/false,
32029 /*consume_paren=*/true);
32031 if (num == error_mark_node)
32032 return list;
32033 num = fold_non_dependent_expr (num);
32034 if (!tree_fits_shwi_p (num)
32035 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32036 || (n = tree_to_shwi (num)) <= 0
32037 || (int) n != n)
32039 error_at (loc, "collapse argument needs positive constant integer expression");
32040 return list;
32043 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32044 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32045 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32046 OMP_CLAUSE_CHAIN (c) = list;
32047 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32049 return c;
32052 /* OpenMP 2.5:
32053 default ( none | shared )
32055 OpenACC:
32056 default ( none | present ) */
32058 static tree
32059 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32060 location_t location, bool is_oacc)
32062 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32063 tree c;
32065 matching_parens parens;
32066 if (!parens.require_open (parser))
32067 return list;
32068 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32070 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32071 const char *p = IDENTIFIER_POINTER (id);
32073 switch (p[0])
32075 case 'n':
32076 if (strcmp ("none", p) != 0)
32077 goto invalid_kind;
32078 kind = OMP_CLAUSE_DEFAULT_NONE;
32079 break;
32081 case 'p':
32082 if (strcmp ("present", p) != 0 || !is_oacc)
32083 goto invalid_kind;
32084 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32085 break;
32087 case 's':
32088 if (strcmp ("shared", p) != 0 || is_oacc)
32089 goto invalid_kind;
32090 kind = OMP_CLAUSE_DEFAULT_SHARED;
32091 break;
32093 default:
32094 goto invalid_kind;
32097 cp_lexer_consume_token (parser->lexer);
32099 else
32101 invalid_kind:
32102 if (is_oacc)
32103 cp_parser_error (parser, "expected %<none%> or %<present%>");
32104 else
32105 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32108 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32109 || !parens.require_close (parser))
32110 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32111 /*or_comma=*/false,
32112 /*consume_paren=*/true);
32114 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32115 return list;
32117 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32118 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32119 OMP_CLAUSE_CHAIN (c) = list;
32120 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32122 return c;
32125 /* OpenMP 3.1:
32126 final ( expression ) */
32128 static tree
32129 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32131 tree t, c;
32133 matching_parens parens;
32134 if (!parens.require_open (parser))
32135 return list;
32137 t = cp_parser_condition (parser);
32139 if (t == error_mark_node
32140 || !parens.require_close (parser))
32141 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32142 /*or_comma=*/false,
32143 /*consume_paren=*/true);
32145 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32147 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32148 OMP_CLAUSE_FINAL_EXPR (c) = t;
32149 OMP_CLAUSE_CHAIN (c) = list;
32151 return c;
32154 /* OpenMP 2.5:
32155 if ( expression )
32157 OpenMP 4.5:
32158 if ( directive-name-modifier : expression )
32160 directive-name-modifier:
32161 parallel | task | taskloop | target data | target | target update
32162 | target enter data | target exit data */
32164 static tree
32165 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32166 bool is_omp)
32168 tree t, c;
32169 enum tree_code if_modifier = ERROR_MARK;
32171 matching_parens parens;
32172 if (!parens.require_open (parser))
32173 return list;
32175 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32177 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32178 const char *p = IDENTIFIER_POINTER (id);
32179 int n = 2;
32181 if (strcmp ("parallel", p) == 0)
32182 if_modifier = OMP_PARALLEL;
32183 else if (strcmp ("task", p) == 0)
32184 if_modifier = OMP_TASK;
32185 else if (strcmp ("taskloop", p) == 0)
32186 if_modifier = OMP_TASKLOOP;
32187 else if (strcmp ("target", p) == 0)
32189 if_modifier = OMP_TARGET;
32190 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32192 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32193 p = IDENTIFIER_POINTER (id);
32194 if (strcmp ("data", p) == 0)
32195 if_modifier = OMP_TARGET_DATA;
32196 else if (strcmp ("update", p) == 0)
32197 if_modifier = OMP_TARGET_UPDATE;
32198 else if (strcmp ("enter", p) == 0)
32199 if_modifier = OMP_TARGET_ENTER_DATA;
32200 else if (strcmp ("exit", p) == 0)
32201 if_modifier = OMP_TARGET_EXIT_DATA;
32202 if (if_modifier != OMP_TARGET)
32203 n = 3;
32204 else
32206 location_t loc
32207 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32208 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32209 "or %<exit%>");
32210 if_modifier = ERROR_MARK;
32212 if (if_modifier == OMP_TARGET_ENTER_DATA
32213 || if_modifier == OMP_TARGET_EXIT_DATA)
32215 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32217 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32218 p = IDENTIFIER_POINTER (id);
32219 if (strcmp ("data", p) == 0)
32220 n = 4;
32222 if (n != 4)
32224 location_t loc
32225 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32226 error_at (loc, "expected %<data%>");
32227 if_modifier = ERROR_MARK;
32232 if (if_modifier != ERROR_MARK)
32234 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32236 while (n-- > 0)
32237 cp_lexer_consume_token (parser->lexer);
32239 else
32241 if (n > 2)
32243 location_t loc
32244 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32245 error_at (loc, "expected %<:%>");
32247 if_modifier = ERROR_MARK;
32252 t = cp_parser_condition (parser);
32254 if (t == error_mark_node
32255 || !parens.require_close (parser))
32256 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32257 /*or_comma=*/false,
32258 /*consume_paren=*/true);
32260 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32261 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32263 if (if_modifier != ERROR_MARK
32264 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32266 const char *p = NULL;
32267 switch (if_modifier)
32269 case OMP_PARALLEL: p = "parallel"; break;
32270 case OMP_TASK: p = "task"; break;
32271 case OMP_TASKLOOP: p = "taskloop"; break;
32272 case OMP_TARGET_DATA: p = "target data"; break;
32273 case OMP_TARGET: p = "target"; break;
32274 case OMP_TARGET_UPDATE: p = "target update"; break;
32275 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32276 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32277 default: gcc_unreachable ();
32279 error_at (location, "too many %<if%> clauses with %qs modifier",
32281 return list;
32283 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32285 if (!is_omp)
32286 error_at (location, "too many %<if%> clauses");
32287 else
32288 error_at (location, "too many %<if%> clauses without modifier");
32289 return list;
32291 else if (if_modifier == ERROR_MARK
32292 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32294 error_at (location, "if any %<if%> clause has modifier, then all "
32295 "%<if%> clauses have to use modifier");
32296 return list;
32300 c = build_omp_clause (location, OMP_CLAUSE_IF);
32301 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32302 OMP_CLAUSE_IF_EXPR (c) = t;
32303 OMP_CLAUSE_CHAIN (c) = list;
32305 return c;
32308 /* OpenMP 3.1:
32309 mergeable */
32311 static tree
32312 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32313 tree list, location_t location)
32315 tree c;
32317 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32318 location);
32320 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32321 OMP_CLAUSE_CHAIN (c) = list;
32322 return c;
32325 /* OpenMP 2.5:
32326 nowait */
32328 static tree
32329 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32330 tree list, location_t location)
32332 tree c;
32334 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32336 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32337 OMP_CLAUSE_CHAIN (c) = list;
32338 return c;
32341 /* OpenMP 2.5:
32342 num_threads ( expression ) */
32344 static tree
32345 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32346 location_t location)
32348 tree t, c;
32350 matching_parens parens;
32351 if (!parens.require_open (parser))
32352 return list;
32354 t = cp_parser_expression (parser);
32356 if (t == error_mark_node
32357 || !parens.require_close (parser))
32358 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32359 /*or_comma=*/false,
32360 /*consume_paren=*/true);
32362 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32363 "num_threads", location);
32365 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32366 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32367 OMP_CLAUSE_CHAIN (c) = list;
32369 return c;
32372 /* OpenMP 4.5:
32373 num_tasks ( expression ) */
32375 static tree
32376 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32377 location_t location)
32379 tree t, c;
32381 matching_parens parens;
32382 if (!parens.require_open (parser))
32383 return list;
32385 t = cp_parser_expression (parser);
32387 if (t == error_mark_node
32388 || !parens.require_close (parser))
32389 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32390 /*or_comma=*/false,
32391 /*consume_paren=*/true);
32393 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32394 "num_tasks", location);
32396 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32397 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32398 OMP_CLAUSE_CHAIN (c) = list;
32400 return c;
32403 /* OpenMP 4.5:
32404 grainsize ( expression ) */
32406 static tree
32407 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32408 location_t location)
32410 tree t, c;
32412 matching_parens parens;
32413 if (!parens.require_open (parser))
32414 return list;
32416 t = cp_parser_expression (parser);
32418 if (t == error_mark_node
32419 || !parens.require_close (parser))
32420 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32421 /*or_comma=*/false,
32422 /*consume_paren=*/true);
32424 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32425 "grainsize", location);
32427 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32428 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32429 OMP_CLAUSE_CHAIN (c) = list;
32431 return c;
32434 /* OpenMP 4.5:
32435 priority ( expression ) */
32437 static tree
32438 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32439 location_t location)
32441 tree t, c;
32443 matching_parens parens;
32444 if (!parens.require_open (parser))
32445 return list;
32447 t = cp_parser_expression (parser);
32449 if (t == error_mark_node
32450 || !parens.require_close (parser))
32451 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32452 /*or_comma=*/false,
32453 /*consume_paren=*/true);
32455 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32456 "priority", location);
32458 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32459 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32460 OMP_CLAUSE_CHAIN (c) = list;
32462 return c;
32465 /* OpenMP 4.5:
32466 hint ( expression ) */
32468 static tree
32469 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32470 location_t location)
32472 tree t, c;
32474 matching_parens parens;
32475 if (!parens.require_open (parser))
32476 return list;
32478 t = cp_parser_expression (parser);
32480 if (t == error_mark_node
32481 || !parens.require_close (parser))
32482 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32483 /*or_comma=*/false,
32484 /*consume_paren=*/true);
32486 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32488 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32489 OMP_CLAUSE_HINT_EXPR (c) = t;
32490 OMP_CLAUSE_CHAIN (c) = list;
32492 return c;
32495 /* OpenMP 4.5:
32496 defaultmap ( tofrom : scalar ) */
32498 static tree
32499 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32500 location_t location)
32502 tree c, id;
32503 const char *p;
32505 matching_parens parens;
32506 if (!parens.require_open (parser))
32507 return list;
32509 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32511 cp_parser_error (parser, "expected %<tofrom%>");
32512 goto out_err;
32514 id = cp_lexer_peek_token (parser->lexer)->u.value;
32515 p = IDENTIFIER_POINTER (id);
32516 if (strcmp (p, "tofrom") != 0)
32518 cp_parser_error (parser, "expected %<tofrom%>");
32519 goto out_err;
32521 cp_lexer_consume_token (parser->lexer);
32522 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32523 goto out_err;
32525 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32527 cp_parser_error (parser, "expected %<scalar%>");
32528 goto out_err;
32530 id = cp_lexer_peek_token (parser->lexer)->u.value;
32531 p = IDENTIFIER_POINTER (id);
32532 if (strcmp (p, "scalar") != 0)
32534 cp_parser_error (parser, "expected %<scalar%>");
32535 goto out_err;
32537 cp_lexer_consume_token (parser->lexer);
32538 if (!parens.require_close (parser))
32539 goto out_err;
32541 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32542 location);
32544 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32545 OMP_CLAUSE_CHAIN (c) = list;
32546 return c;
32548 out_err:
32549 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32550 /*or_comma=*/false,
32551 /*consume_paren=*/true);
32552 return list;
32555 /* OpenMP 2.5:
32556 ordered
32558 OpenMP 4.5:
32559 ordered ( constant-expression ) */
32561 static tree
32562 cp_parser_omp_clause_ordered (cp_parser *parser,
32563 tree list, location_t location)
32565 tree c, num = NULL_TREE;
32566 HOST_WIDE_INT n;
32568 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32569 "ordered", location);
32571 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32573 matching_parens parens;
32574 parens.consume_open (parser);
32576 num = cp_parser_constant_expression (parser);
32578 if (!parens.require_close (parser))
32579 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32580 /*or_comma=*/false,
32581 /*consume_paren=*/true);
32583 if (num == error_mark_node)
32584 return list;
32585 num = fold_non_dependent_expr (num);
32586 if (!tree_fits_shwi_p (num)
32587 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32588 || (n = tree_to_shwi (num)) <= 0
32589 || (int) n != n)
32591 error_at (location,
32592 "ordered argument needs positive constant integer "
32593 "expression");
32594 return list;
32598 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32599 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32600 OMP_CLAUSE_CHAIN (c) = list;
32601 return c;
32604 /* OpenMP 2.5:
32605 reduction ( reduction-operator : variable-list )
32607 reduction-operator:
32608 One of: + * - & ^ | && ||
32610 OpenMP 3.1:
32612 reduction-operator:
32613 One of: + * - & ^ | && || min max
32615 OpenMP 4.0:
32617 reduction-operator:
32618 One of: + * - & ^ | && ||
32619 id-expression */
32621 static tree
32622 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32624 enum tree_code code = ERROR_MARK;
32625 tree nlist, c, id = NULL_TREE;
32627 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32628 return list;
32630 switch (cp_lexer_peek_token (parser->lexer)->type)
32632 case CPP_PLUS: code = PLUS_EXPR; break;
32633 case CPP_MULT: code = MULT_EXPR; break;
32634 case CPP_MINUS: code = MINUS_EXPR; break;
32635 case CPP_AND: code = BIT_AND_EXPR; break;
32636 case CPP_XOR: code = BIT_XOR_EXPR; break;
32637 case CPP_OR: code = BIT_IOR_EXPR; break;
32638 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32639 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32640 default: break;
32643 if (code != ERROR_MARK)
32644 cp_lexer_consume_token (parser->lexer);
32645 else
32647 bool saved_colon_corrects_to_scope_p;
32648 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32649 parser->colon_corrects_to_scope_p = false;
32650 id = cp_parser_id_expression (parser, /*template_p=*/false,
32651 /*check_dependency_p=*/true,
32652 /*template_p=*/NULL,
32653 /*declarator_p=*/false,
32654 /*optional_p=*/false);
32655 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32656 if (identifier_p (id))
32658 const char *p = IDENTIFIER_POINTER (id);
32660 if (strcmp (p, "min") == 0)
32661 code = MIN_EXPR;
32662 else if (strcmp (p, "max") == 0)
32663 code = MAX_EXPR;
32664 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32665 code = PLUS_EXPR;
32666 else if (id == ovl_op_identifier (false, MULT_EXPR))
32667 code = MULT_EXPR;
32668 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32669 code = MINUS_EXPR;
32670 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32671 code = BIT_AND_EXPR;
32672 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32673 code = BIT_IOR_EXPR;
32674 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32675 code = BIT_XOR_EXPR;
32676 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32677 code = TRUTH_ANDIF_EXPR;
32678 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32679 code = TRUTH_ORIF_EXPR;
32680 id = omp_reduction_id (code, id, NULL_TREE);
32681 tree scope = parser->scope;
32682 if (scope)
32683 id = build_qualified_name (NULL_TREE, scope, id, false);
32684 parser->scope = NULL_TREE;
32685 parser->qualifying_scope = NULL_TREE;
32686 parser->object_scope = NULL_TREE;
32688 else
32690 error ("invalid reduction-identifier");
32691 resync_fail:
32692 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32693 /*or_comma=*/false,
32694 /*consume_paren=*/true);
32695 return list;
32699 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32700 goto resync_fail;
32702 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32703 NULL);
32704 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32706 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32707 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32710 return nlist;
32713 /* OpenMP 2.5:
32714 schedule ( schedule-kind )
32715 schedule ( schedule-kind , expression )
32717 schedule-kind:
32718 static | dynamic | guided | runtime | auto
32720 OpenMP 4.5:
32721 schedule ( schedule-modifier : schedule-kind )
32722 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32724 schedule-modifier:
32725 simd
32726 monotonic
32727 nonmonotonic */
32729 static tree
32730 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32732 tree c, t;
32733 int modifiers = 0, nmodifiers = 0;
32735 matching_parens parens;
32736 if (!parens.require_open (parser))
32737 return list;
32739 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32741 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32743 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32744 const char *p = IDENTIFIER_POINTER (id);
32745 if (strcmp ("simd", p) == 0)
32746 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32747 else if (strcmp ("monotonic", p) == 0)
32748 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32749 else if (strcmp ("nonmonotonic", p) == 0)
32750 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32751 else
32752 break;
32753 cp_lexer_consume_token (parser->lexer);
32754 if (nmodifiers++ == 0
32755 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32756 cp_lexer_consume_token (parser->lexer);
32757 else
32759 cp_parser_require (parser, CPP_COLON, RT_COLON);
32760 break;
32764 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32766 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32767 const char *p = IDENTIFIER_POINTER (id);
32769 switch (p[0])
32771 case 'd':
32772 if (strcmp ("dynamic", p) != 0)
32773 goto invalid_kind;
32774 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32775 break;
32777 case 'g':
32778 if (strcmp ("guided", p) != 0)
32779 goto invalid_kind;
32780 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32781 break;
32783 case 'r':
32784 if (strcmp ("runtime", p) != 0)
32785 goto invalid_kind;
32786 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32787 break;
32789 default:
32790 goto invalid_kind;
32793 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32794 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32795 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32796 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32797 else
32798 goto invalid_kind;
32799 cp_lexer_consume_token (parser->lexer);
32801 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32802 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32803 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32804 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32806 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32807 "specified");
32808 modifiers = 0;
32811 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32813 cp_token *token;
32814 cp_lexer_consume_token (parser->lexer);
32816 token = cp_lexer_peek_token (parser->lexer);
32817 t = cp_parser_assignment_expression (parser);
32819 if (t == error_mark_node)
32820 goto resync_fail;
32821 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32822 error_at (token->location, "schedule %<runtime%> does not take "
32823 "a %<chunk_size%> parameter");
32824 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32825 error_at (token->location, "schedule %<auto%> does not take "
32826 "a %<chunk_size%> parameter");
32827 else
32828 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32830 if (!parens.require_close (parser))
32831 goto resync_fail;
32833 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32834 goto resync_fail;
32836 OMP_CLAUSE_SCHEDULE_KIND (c)
32837 = (enum omp_clause_schedule_kind)
32838 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32840 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32841 OMP_CLAUSE_CHAIN (c) = list;
32842 return c;
32844 invalid_kind:
32845 cp_parser_error (parser, "invalid schedule kind");
32846 resync_fail:
32847 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32848 /*or_comma=*/false,
32849 /*consume_paren=*/true);
32850 return list;
32853 /* OpenMP 3.0:
32854 untied */
32856 static tree
32857 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32858 tree list, location_t location)
32860 tree c;
32862 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32864 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32865 OMP_CLAUSE_CHAIN (c) = list;
32866 return c;
32869 /* OpenMP 4.0:
32870 inbranch
32871 notinbranch */
32873 static tree
32874 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32875 tree list, location_t location)
32877 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32878 tree c = build_omp_clause (location, code);
32879 OMP_CLAUSE_CHAIN (c) = list;
32880 return c;
32883 /* OpenMP 4.0:
32884 parallel
32886 sections
32887 taskgroup */
32889 static tree
32890 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32891 enum omp_clause_code code,
32892 tree list, location_t location)
32894 tree c = build_omp_clause (location, code);
32895 OMP_CLAUSE_CHAIN (c) = list;
32896 return c;
32899 /* OpenMP 4.5:
32900 nogroup */
32902 static tree
32903 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32904 tree list, location_t location)
32906 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32907 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32908 OMP_CLAUSE_CHAIN (c) = list;
32909 return c;
32912 /* OpenMP 4.5:
32913 simd
32914 threads */
32916 static tree
32917 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32918 enum omp_clause_code code,
32919 tree list, location_t location)
32921 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32922 tree c = build_omp_clause (location, code);
32923 OMP_CLAUSE_CHAIN (c) = list;
32924 return c;
32927 /* OpenMP 4.0:
32928 num_teams ( expression ) */
32930 static tree
32931 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32932 location_t location)
32934 tree t, c;
32936 matching_parens parens;
32937 if (!parens.require_open (parser))
32938 return list;
32940 t = cp_parser_expression (parser);
32942 if (t == error_mark_node
32943 || !parens.require_close (parser))
32944 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32945 /*or_comma=*/false,
32946 /*consume_paren=*/true);
32948 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32949 "num_teams", location);
32951 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32952 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32953 OMP_CLAUSE_CHAIN (c) = list;
32955 return c;
32958 /* OpenMP 4.0:
32959 thread_limit ( expression ) */
32961 static tree
32962 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32963 location_t location)
32965 tree t, c;
32967 matching_parens parens;
32968 if (!parens.require_open (parser))
32969 return list;
32971 t = cp_parser_expression (parser);
32973 if (t == error_mark_node
32974 || !parens.require_close (parser))
32975 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32976 /*or_comma=*/false,
32977 /*consume_paren=*/true);
32979 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32980 "thread_limit", location);
32982 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32983 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32984 OMP_CLAUSE_CHAIN (c) = list;
32986 return c;
32989 /* OpenMP 4.0:
32990 aligned ( variable-list )
32991 aligned ( variable-list : constant-expression ) */
32993 static tree
32994 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32996 tree nlist, c, alignment = NULL_TREE;
32997 bool colon;
32999 matching_parens parens;
33000 if (!parens.require_open (parser))
33001 return list;
33003 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33004 &colon);
33006 if (colon)
33008 alignment = cp_parser_constant_expression (parser);
33010 if (!parens.require_close (parser))
33011 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33012 /*or_comma=*/false,
33013 /*consume_paren=*/true);
33015 if (alignment == error_mark_node)
33016 alignment = NULL_TREE;
33019 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33020 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33022 return nlist;
33025 /* OpenMP 4.0:
33026 linear ( variable-list )
33027 linear ( variable-list : expression )
33029 OpenMP 4.5:
33030 linear ( modifier ( variable-list ) )
33031 linear ( modifier ( variable-list ) : expression ) */
33033 static tree
33034 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33035 bool declare_simd)
33037 tree nlist, c, step = integer_one_node;
33038 bool colon;
33039 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33041 matching_parens parens;
33042 if (!parens.require_open (parser))
33043 return list;
33045 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33047 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33048 const char *p = IDENTIFIER_POINTER (id);
33050 if (strcmp ("ref", p) == 0)
33051 kind = OMP_CLAUSE_LINEAR_REF;
33052 else if (strcmp ("val", p) == 0)
33053 kind = OMP_CLAUSE_LINEAR_VAL;
33054 else if (strcmp ("uval", p) == 0)
33055 kind = OMP_CLAUSE_LINEAR_UVAL;
33056 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33057 cp_lexer_consume_token (parser->lexer);
33058 else
33059 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33062 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33063 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33064 &colon);
33065 else
33067 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33068 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33069 if (colon)
33070 cp_parser_require (parser, CPP_COLON, RT_COLON);
33071 else if (!parens.require_close (parser))
33072 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33073 /*or_comma=*/false,
33074 /*consume_paren=*/true);
33077 if (colon)
33079 step = NULL_TREE;
33080 if (declare_simd
33081 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33082 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33084 cp_token *token = cp_lexer_peek_token (parser->lexer);
33085 cp_parser_parse_tentatively (parser);
33086 step = cp_parser_id_expression (parser, /*template_p=*/false,
33087 /*check_dependency_p=*/true,
33088 /*template_p=*/NULL,
33089 /*declarator_p=*/false,
33090 /*optional_p=*/false);
33091 if (step != error_mark_node)
33092 step = cp_parser_lookup_name_simple (parser, step, token->location);
33093 if (step == error_mark_node)
33095 step = NULL_TREE;
33096 cp_parser_abort_tentative_parse (parser);
33098 else if (!cp_parser_parse_definitely (parser))
33099 step = NULL_TREE;
33101 if (!step)
33102 step = cp_parser_expression (parser);
33104 if (!parens.require_close (parser))
33105 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33106 /*or_comma=*/false,
33107 /*consume_paren=*/true);
33109 if (step == error_mark_node)
33110 return list;
33113 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33115 OMP_CLAUSE_LINEAR_STEP (c) = step;
33116 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33119 return nlist;
33122 /* OpenMP 4.0:
33123 safelen ( constant-expression ) */
33125 static tree
33126 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33127 location_t location)
33129 tree t, c;
33131 matching_parens parens;
33132 if (!parens.require_open (parser))
33133 return list;
33135 t = cp_parser_constant_expression (parser);
33137 if (t == error_mark_node
33138 || !parens.require_close (parser))
33139 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33140 /*or_comma=*/false,
33141 /*consume_paren=*/true);
33143 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33145 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33146 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33147 OMP_CLAUSE_CHAIN (c) = list;
33149 return c;
33152 /* OpenMP 4.0:
33153 simdlen ( constant-expression ) */
33155 static tree
33156 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33157 location_t location)
33159 tree t, c;
33161 matching_parens parens;
33162 if (!parens.require_open (parser))
33163 return list;
33165 t = cp_parser_constant_expression (parser);
33167 if (t == error_mark_node
33168 || !parens.require_close (parser))
33169 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33170 /*or_comma=*/false,
33171 /*consume_paren=*/true);
33173 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33175 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33176 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33177 OMP_CLAUSE_CHAIN (c) = list;
33179 return c;
33182 /* OpenMP 4.5:
33183 vec:
33184 identifier [+/- integer]
33185 vec , identifier [+/- integer]
33188 static tree
33189 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33190 tree list)
33192 tree vec = NULL;
33194 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33196 cp_parser_error (parser, "expected identifier");
33197 return list;
33200 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33202 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33203 tree t, identifier = cp_parser_identifier (parser);
33204 tree addend = NULL;
33206 if (identifier == error_mark_node)
33207 t = error_mark_node;
33208 else
33210 t = cp_parser_lookup_name_simple
33211 (parser, identifier,
33212 cp_lexer_peek_token (parser->lexer)->location);
33213 if (t == error_mark_node)
33214 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33215 id_loc);
33218 bool neg = false;
33219 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33220 neg = true;
33221 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33223 addend = integer_zero_node;
33224 goto add_to_vector;
33226 cp_lexer_consume_token (parser->lexer);
33228 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33230 cp_parser_error (parser, "expected integer");
33231 return list;
33234 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33235 if (TREE_CODE (addend) != INTEGER_CST)
33237 cp_parser_error (parser, "expected integer");
33238 return list;
33240 cp_lexer_consume_token (parser->lexer);
33242 add_to_vector:
33243 if (t != error_mark_node)
33245 vec = tree_cons (addend, t, vec);
33246 if (neg)
33247 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33250 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33251 break;
33253 cp_lexer_consume_token (parser->lexer);
33256 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33258 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33259 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33260 OMP_CLAUSE_DECL (u) = nreverse (vec);
33261 OMP_CLAUSE_CHAIN (u) = list;
33262 return u;
33264 return list;
33267 /* OpenMP 4.0:
33268 depend ( depend-kind : variable-list )
33270 depend-kind:
33271 in | out | inout
33273 OpenMP 4.5:
33274 depend ( source )
33276 depend ( sink : vec ) */
33278 static tree
33279 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33281 tree nlist, c;
33282 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33284 matching_parens parens;
33285 if (!parens.require_open (parser))
33286 return list;
33288 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33290 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33291 const char *p = IDENTIFIER_POINTER (id);
33293 if (strcmp ("in", p) == 0)
33294 kind = OMP_CLAUSE_DEPEND_IN;
33295 else if (strcmp ("inout", p) == 0)
33296 kind = OMP_CLAUSE_DEPEND_INOUT;
33297 else if (strcmp ("out", p) == 0)
33298 kind = OMP_CLAUSE_DEPEND_OUT;
33299 else if (strcmp ("source", p) == 0)
33300 kind = OMP_CLAUSE_DEPEND_SOURCE;
33301 else if (strcmp ("sink", p) == 0)
33302 kind = OMP_CLAUSE_DEPEND_SINK;
33303 else
33304 goto invalid_kind;
33306 else
33307 goto invalid_kind;
33309 cp_lexer_consume_token (parser->lexer);
33311 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33313 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33314 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33315 OMP_CLAUSE_DECL (c) = NULL_TREE;
33316 OMP_CLAUSE_CHAIN (c) = list;
33317 if (!parens.require_close (parser))
33318 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33319 /*or_comma=*/false,
33320 /*consume_paren=*/true);
33321 return c;
33324 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33325 goto resync_fail;
33327 if (kind == OMP_CLAUSE_DEPEND_SINK)
33328 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33329 else
33331 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33332 list, NULL);
33334 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33335 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33337 return nlist;
33339 invalid_kind:
33340 cp_parser_error (parser, "invalid depend kind");
33341 resync_fail:
33342 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33343 /*or_comma=*/false,
33344 /*consume_paren=*/true);
33345 return list;
33348 /* OpenMP 4.0:
33349 map ( map-kind : variable-list )
33350 map ( variable-list )
33352 map-kind:
33353 alloc | to | from | tofrom
33355 OpenMP 4.5:
33356 map-kind:
33357 alloc | to | from | tofrom | release | delete
33359 map ( always [,] map-kind: variable-list ) */
33361 static tree
33362 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33364 tree nlist, c;
33365 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33366 bool always = false;
33368 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33369 return list;
33371 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33373 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33374 const char *p = IDENTIFIER_POINTER (id);
33376 if (strcmp ("always", p) == 0)
33378 int nth = 2;
33379 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33380 nth++;
33381 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33382 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33383 == RID_DELETE))
33384 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33385 == CPP_COLON))
33387 always = true;
33388 cp_lexer_consume_token (parser->lexer);
33389 if (nth == 3)
33390 cp_lexer_consume_token (parser->lexer);
33395 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33396 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33398 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33399 const char *p = IDENTIFIER_POINTER (id);
33401 if (strcmp ("alloc", p) == 0)
33402 kind = GOMP_MAP_ALLOC;
33403 else if (strcmp ("to", p) == 0)
33404 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33405 else if (strcmp ("from", p) == 0)
33406 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33407 else if (strcmp ("tofrom", p) == 0)
33408 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33409 else if (strcmp ("release", p) == 0)
33410 kind = GOMP_MAP_RELEASE;
33411 else
33413 cp_parser_error (parser, "invalid map kind");
33414 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33415 /*or_comma=*/false,
33416 /*consume_paren=*/true);
33417 return list;
33419 cp_lexer_consume_token (parser->lexer);
33420 cp_lexer_consume_token (parser->lexer);
33422 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33423 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33425 kind = GOMP_MAP_DELETE;
33426 cp_lexer_consume_token (parser->lexer);
33427 cp_lexer_consume_token (parser->lexer);
33430 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33431 NULL);
33433 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33434 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33436 return nlist;
33439 /* OpenMP 4.0:
33440 device ( expression ) */
33442 static tree
33443 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33444 location_t location)
33446 tree t, c;
33448 matching_parens parens;
33449 if (!parens.require_open (parser))
33450 return list;
33452 t = cp_parser_expression (parser);
33454 if (t == error_mark_node
33455 || !parens.require_close (parser))
33456 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33457 /*or_comma=*/false,
33458 /*consume_paren=*/true);
33460 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33461 "device", location);
33463 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33464 OMP_CLAUSE_DEVICE_ID (c) = t;
33465 OMP_CLAUSE_CHAIN (c) = list;
33467 return c;
33470 /* OpenMP 4.0:
33471 dist_schedule ( static )
33472 dist_schedule ( static , expression ) */
33474 static tree
33475 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33476 location_t location)
33478 tree c, t;
33480 matching_parens parens;
33481 if (!parens.require_open (parser))
33482 return list;
33484 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33486 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33487 goto invalid_kind;
33488 cp_lexer_consume_token (parser->lexer);
33490 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33492 cp_lexer_consume_token (parser->lexer);
33494 t = cp_parser_assignment_expression (parser);
33496 if (t == error_mark_node)
33497 goto resync_fail;
33498 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33500 if (!parens.require_close (parser))
33501 goto resync_fail;
33503 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33504 goto resync_fail;
33506 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33507 location);
33508 OMP_CLAUSE_CHAIN (c) = list;
33509 return c;
33511 invalid_kind:
33512 cp_parser_error (parser, "invalid dist_schedule kind");
33513 resync_fail:
33514 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33515 /*or_comma=*/false,
33516 /*consume_paren=*/true);
33517 return list;
33520 /* OpenMP 4.0:
33521 proc_bind ( proc-bind-kind )
33523 proc-bind-kind:
33524 master | close | spread */
33526 static tree
33527 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33528 location_t location)
33530 tree c;
33531 enum omp_clause_proc_bind_kind kind;
33533 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33534 return list;
33536 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33538 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33539 const char *p = IDENTIFIER_POINTER (id);
33541 if (strcmp ("master", p) == 0)
33542 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33543 else if (strcmp ("close", p) == 0)
33544 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33545 else if (strcmp ("spread", p) == 0)
33546 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33547 else
33548 goto invalid_kind;
33550 else
33551 goto invalid_kind;
33553 cp_lexer_consume_token (parser->lexer);
33554 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33555 goto resync_fail;
33557 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33558 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33559 location);
33560 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33561 OMP_CLAUSE_CHAIN (c) = list;
33562 return c;
33564 invalid_kind:
33565 cp_parser_error (parser, "invalid depend kind");
33566 resync_fail:
33567 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33568 /*or_comma=*/false,
33569 /*consume_paren=*/true);
33570 return list;
33573 /* OpenACC:
33574 async [( int-expr )] */
33576 static tree
33577 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33579 tree c, t;
33580 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33582 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33584 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33586 matching_parens parens;
33587 parens.consume_open (parser);
33589 t = cp_parser_expression (parser);
33590 if (t == error_mark_node
33591 || !parens.require_close (parser))
33592 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33593 /*or_comma=*/false,
33594 /*consume_paren=*/true);
33597 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33599 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33600 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33601 OMP_CLAUSE_CHAIN (c) = list;
33602 list = c;
33604 return list;
33607 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33608 is a bitmask in MASK. Return the list of clauses found. */
33610 static tree
33611 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33612 const char *where, cp_token *pragma_tok,
33613 bool finish_p = true)
33615 tree clauses = NULL;
33616 bool first = true;
33618 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33620 location_t here;
33621 pragma_omp_clause c_kind;
33622 omp_clause_code code;
33623 const char *c_name;
33624 tree prev = clauses;
33626 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33627 cp_lexer_consume_token (parser->lexer);
33629 here = cp_lexer_peek_token (parser->lexer)->location;
33630 c_kind = cp_parser_omp_clause_name (parser);
33632 switch (c_kind)
33634 case PRAGMA_OACC_CLAUSE_ASYNC:
33635 clauses = cp_parser_oacc_clause_async (parser, clauses);
33636 c_name = "async";
33637 break;
33638 case PRAGMA_OACC_CLAUSE_AUTO:
33639 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33640 clauses, here);
33641 c_name = "auto";
33642 break;
33643 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33644 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33645 c_name = "collapse";
33646 break;
33647 case PRAGMA_OACC_CLAUSE_COPY:
33648 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33649 c_name = "copy";
33650 break;
33651 case PRAGMA_OACC_CLAUSE_COPYIN:
33652 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33653 c_name = "copyin";
33654 break;
33655 case PRAGMA_OACC_CLAUSE_COPYOUT:
33656 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33657 c_name = "copyout";
33658 break;
33659 case PRAGMA_OACC_CLAUSE_CREATE:
33660 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33661 c_name = "create";
33662 break;
33663 case PRAGMA_OACC_CLAUSE_DELETE:
33664 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33665 c_name = "delete";
33666 break;
33667 case PRAGMA_OMP_CLAUSE_DEFAULT:
33668 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33669 c_name = "default";
33670 break;
33671 case PRAGMA_OACC_CLAUSE_DEVICE:
33672 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33673 c_name = "device";
33674 break;
33675 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33676 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33677 c_name = "deviceptr";
33678 break;
33679 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33680 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33681 c_name = "device_resident";
33682 break;
33683 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33684 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33685 clauses);
33686 c_name = "firstprivate";
33687 break;
33688 case PRAGMA_OACC_CLAUSE_GANG:
33689 c_name = "gang";
33690 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33691 c_name, clauses);
33692 break;
33693 case PRAGMA_OACC_CLAUSE_HOST:
33694 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33695 c_name = "host";
33696 break;
33697 case PRAGMA_OACC_CLAUSE_IF:
33698 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33699 c_name = "if";
33700 break;
33701 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33702 clauses = cp_parser_oacc_simple_clause (parser,
33703 OMP_CLAUSE_INDEPENDENT,
33704 clauses, here);
33705 c_name = "independent";
33706 break;
33707 case PRAGMA_OACC_CLAUSE_LINK:
33708 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33709 c_name = "link";
33710 break;
33711 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33712 code = OMP_CLAUSE_NUM_GANGS;
33713 c_name = "num_gangs";
33714 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33715 clauses);
33716 break;
33717 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33718 c_name = "num_workers";
33719 code = OMP_CLAUSE_NUM_WORKERS;
33720 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33721 clauses);
33722 break;
33723 case PRAGMA_OACC_CLAUSE_PRESENT:
33724 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33725 c_name = "present";
33726 break;
33727 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33728 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33729 c_name = "present_or_copy";
33730 break;
33731 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33732 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33733 c_name = "present_or_copyin";
33734 break;
33735 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33736 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33737 c_name = "present_or_copyout";
33738 break;
33739 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33740 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33741 c_name = "present_or_create";
33742 break;
33743 case PRAGMA_OACC_CLAUSE_PRIVATE:
33744 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33745 clauses);
33746 c_name = "private";
33747 break;
33748 case PRAGMA_OACC_CLAUSE_REDUCTION:
33749 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33750 c_name = "reduction";
33751 break;
33752 case PRAGMA_OACC_CLAUSE_SELF:
33753 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33754 c_name = "self";
33755 break;
33756 case PRAGMA_OACC_CLAUSE_SEQ:
33757 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33758 clauses, here);
33759 c_name = "seq";
33760 break;
33761 case PRAGMA_OACC_CLAUSE_TILE:
33762 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33763 c_name = "tile";
33764 break;
33765 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33766 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33767 clauses);
33768 c_name = "use_device";
33769 break;
33770 case PRAGMA_OACC_CLAUSE_VECTOR:
33771 c_name = "vector";
33772 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33773 c_name, clauses);
33774 break;
33775 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33776 c_name = "vector_length";
33777 code = OMP_CLAUSE_VECTOR_LENGTH;
33778 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33779 clauses);
33780 break;
33781 case PRAGMA_OACC_CLAUSE_WAIT:
33782 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33783 c_name = "wait";
33784 break;
33785 case PRAGMA_OACC_CLAUSE_WORKER:
33786 c_name = "worker";
33787 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33788 c_name, clauses);
33789 break;
33790 default:
33791 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33792 goto saw_error;
33795 first = false;
33797 if (((mask >> c_kind) & 1) == 0)
33799 /* Remove the invalid clause(s) from the list to avoid
33800 confusing the rest of the compiler. */
33801 clauses = prev;
33802 error_at (here, "%qs is not valid for %qs", c_name, where);
33806 saw_error:
33807 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33809 if (finish_p)
33810 return finish_omp_clauses (clauses, C_ORT_ACC);
33812 return clauses;
33815 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33816 is a bitmask in MASK. Return the list of clauses found; the result
33817 of clause default goes in *pdefault. */
33819 static tree
33820 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33821 const char *where, cp_token *pragma_tok,
33822 bool finish_p = true)
33824 tree clauses = NULL;
33825 bool first = true;
33826 cp_token *token = NULL;
33828 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33830 pragma_omp_clause c_kind;
33831 const char *c_name;
33832 tree prev = clauses;
33834 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33835 cp_lexer_consume_token (parser->lexer);
33837 token = cp_lexer_peek_token (parser->lexer);
33838 c_kind = cp_parser_omp_clause_name (parser);
33840 switch (c_kind)
33842 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33843 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33844 token->location);
33845 c_name = "collapse";
33846 break;
33847 case PRAGMA_OMP_CLAUSE_COPYIN:
33848 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33849 c_name = "copyin";
33850 break;
33851 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33852 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33853 clauses);
33854 c_name = "copyprivate";
33855 break;
33856 case PRAGMA_OMP_CLAUSE_DEFAULT:
33857 clauses = cp_parser_omp_clause_default (parser, clauses,
33858 token->location, false);
33859 c_name = "default";
33860 break;
33861 case PRAGMA_OMP_CLAUSE_FINAL:
33862 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33863 c_name = "final";
33864 break;
33865 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33866 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33867 clauses);
33868 c_name = "firstprivate";
33869 break;
33870 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33871 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33872 token->location);
33873 c_name = "grainsize";
33874 break;
33875 case PRAGMA_OMP_CLAUSE_HINT:
33876 clauses = cp_parser_omp_clause_hint (parser, clauses,
33877 token->location);
33878 c_name = "hint";
33879 break;
33880 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33881 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33882 token->location);
33883 c_name = "defaultmap";
33884 break;
33885 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33886 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33887 clauses);
33888 c_name = "use_device_ptr";
33889 break;
33890 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33891 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33892 clauses);
33893 c_name = "is_device_ptr";
33894 break;
33895 case PRAGMA_OMP_CLAUSE_IF:
33896 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33897 true);
33898 c_name = "if";
33899 break;
33900 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33901 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33902 clauses);
33903 c_name = "lastprivate";
33904 break;
33905 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33906 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33907 token->location);
33908 c_name = "mergeable";
33909 break;
33910 case PRAGMA_OMP_CLAUSE_NOWAIT:
33911 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33912 c_name = "nowait";
33913 break;
33914 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33915 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33916 token->location);
33917 c_name = "num_tasks";
33918 break;
33919 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33920 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33921 token->location);
33922 c_name = "num_threads";
33923 break;
33924 case PRAGMA_OMP_CLAUSE_ORDERED:
33925 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33926 token->location);
33927 c_name = "ordered";
33928 break;
33929 case PRAGMA_OMP_CLAUSE_PRIORITY:
33930 clauses = cp_parser_omp_clause_priority (parser, clauses,
33931 token->location);
33932 c_name = "priority";
33933 break;
33934 case PRAGMA_OMP_CLAUSE_PRIVATE:
33935 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33936 clauses);
33937 c_name = "private";
33938 break;
33939 case PRAGMA_OMP_CLAUSE_REDUCTION:
33940 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33941 c_name = "reduction";
33942 break;
33943 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33944 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33945 token->location);
33946 c_name = "schedule";
33947 break;
33948 case PRAGMA_OMP_CLAUSE_SHARED:
33949 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33950 clauses);
33951 c_name = "shared";
33952 break;
33953 case PRAGMA_OMP_CLAUSE_UNTIED:
33954 clauses = cp_parser_omp_clause_untied (parser, clauses,
33955 token->location);
33956 c_name = "untied";
33957 break;
33958 case PRAGMA_OMP_CLAUSE_INBRANCH:
33959 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33960 clauses, token->location);
33961 c_name = "inbranch";
33962 break;
33963 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33964 clauses = cp_parser_omp_clause_branch (parser,
33965 OMP_CLAUSE_NOTINBRANCH,
33966 clauses, token->location);
33967 c_name = "notinbranch";
33968 break;
33969 case PRAGMA_OMP_CLAUSE_PARALLEL:
33970 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33971 clauses, token->location);
33972 c_name = "parallel";
33973 if (!first)
33975 clause_not_first:
33976 error_at (token->location, "%qs must be the first clause of %qs",
33977 c_name, where);
33978 clauses = prev;
33980 break;
33981 case PRAGMA_OMP_CLAUSE_FOR:
33982 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33983 clauses, token->location);
33984 c_name = "for";
33985 if (!first)
33986 goto clause_not_first;
33987 break;
33988 case PRAGMA_OMP_CLAUSE_SECTIONS:
33989 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33990 clauses, token->location);
33991 c_name = "sections";
33992 if (!first)
33993 goto clause_not_first;
33994 break;
33995 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33996 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33997 clauses, token->location);
33998 c_name = "taskgroup";
33999 if (!first)
34000 goto clause_not_first;
34001 break;
34002 case PRAGMA_OMP_CLAUSE_LINK:
34003 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34004 c_name = "to";
34005 break;
34006 case PRAGMA_OMP_CLAUSE_TO:
34007 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34008 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34009 clauses);
34010 else
34011 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34012 c_name = "to";
34013 break;
34014 case PRAGMA_OMP_CLAUSE_FROM:
34015 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34016 c_name = "from";
34017 break;
34018 case PRAGMA_OMP_CLAUSE_UNIFORM:
34019 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34020 clauses);
34021 c_name = "uniform";
34022 break;
34023 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34024 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34025 token->location);
34026 c_name = "num_teams";
34027 break;
34028 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34029 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34030 token->location);
34031 c_name = "thread_limit";
34032 break;
34033 case PRAGMA_OMP_CLAUSE_ALIGNED:
34034 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34035 c_name = "aligned";
34036 break;
34037 case PRAGMA_OMP_CLAUSE_LINEAR:
34039 bool declare_simd = false;
34040 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34041 declare_simd = true;
34042 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34044 c_name = "linear";
34045 break;
34046 case PRAGMA_OMP_CLAUSE_DEPEND:
34047 clauses = cp_parser_omp_clause_depend (parser, clauses,
34048 token->location);
34049 c_name = "depend";
34050 break;
34051 case PRAGMA_OMP_CLAUSE_MAP:
34052 clauses = cp_parser_omp_clause_map (parser, clauses);
34053 c_name = "map";
34054 break;
34055 case PRAGMA_OMP_CLAUSE_DEVICE:
34056 clauses = cp_parser_omp_clause_device (parser, clauses,
34057 token->location);
34058 c_name = "device";
34059 break;
34060 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34061 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34062 token->location);
34063 c_name = "dist_schedule";
34064 break;
34065 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34066 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34067 token->location);
34068 c_name = "proc_bind";
34069 break;
34070 case PRAGMA_OMP_CLAUSE_SAFELEN:
34071 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34072 token->location);
34073 c_name = "safelen";
34074 break;
34075 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34076 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34077 token->location);
34078 c_name = "simdlen";
34079 break;
34080 case PRAGMA_OMP_CLAUSE_NOGROUP:
34081 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34082 token->location);
34083 c_name = "nogroup";
34084 break;
34085 case PRAGMA_OMP_CLAUSE_THREADS:
34086 clauses
34087 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34088 clauses, token->location);
34089 c_name = "threads";
34090 break;
34091 case PRAGMA_OMP_CLAUSE_SIMD:
34092 clauses
34093 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34094 clauses, token->location);
34095 c_name = "simd";
34096 break;
34097 default:
34098 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34099 goto saw_error;
34102 first = false;
34104 if (((mask >> c_kind) & 1) == 0)
34106 /* Remove the invalid clause(s) from the list to avoid
34107 confusing the rest of the compiler. */
34108 clauses = prev;
34109 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34112 saw_error:
34113 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34114 if (finish_p)
34116 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34117 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34118 else
34119 return finish_omp_clauses (clauses, C_ORT_OMP);
34121 return clauses;
34124 /* OpenMP 2.5:
34125 structured-block:
34126 statement
34128 In practice, we're also interested in adding the statement to an
34129 outer node. So it is convenient if we work around the fact that
34130 cp_parser_statement calls add_stmt. */
34132 static unsigned
34133 cp_parser_begin_omp_structured_block (cp_parser *parser)
34135 unsigned save = parser->in_statement;
34137 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34138 This preserves the "not within loop or switch" style error messages
34139 for nonsense cases like
34140 void foo() {
34141 #pragma omp single
34142 break;
34145 if (parser->in_statement)
34146 parser->in_statement = IN_OMP_BLOCK;
34148 return save;
34151 static void
34152 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34154 parser->in_statement = save;
34157 static tree
34158 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34160 tree stmt = begin_omp_structured_block ();
34161 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34163 cp_parser_statement (parser, NULL_TREE, false, if_p);
34165 cp_parser_end_omp_structured_block (parser, save);
34166 return finish_omp_structured_block (stmt);
34169 /* OpenMP 2.5:
34170 # pragma omp atomic new-line
34171 expression-stmt
34173 expression-stmt:
34174 x binop= expr | x++ | ++x | x-- | --x
34175 binop:
34176 +, *, -, /, &, ^, |, <<, >>
34178 where x is an lvalue expression with scalar type.
34180 OpenMP 3.1:
34181 # pragma omp atomic new-line
34182 update-stmt
34184 # pragma omp atomic read new-line
34185 read-stmt
34187 # pragma omp atomic write new-line
34188 write-stmt
34190 # pragma omp atomic update new-line
34191 update-stmt
34193 # pragma omp atomic capture new-line
34194 capture-stmt
34196 # pragma omp atomic capture new-line
34197 capture-block
34199 read-stmt:
34200 v = x
34201 write-stmt:
34202 x = expr
34203 update-stmt:
34204 expression-stmt | x = x binop expr
34205 capture-stmt:
34206 v = expression-stmt
34207 capture-block:
34208 { v = x; update-stmt; } | { update-stmt; v = x; }
34210 OpenMP 4.0:
34211 update-stmt:
34212 expression-stmt | x = x binop expr | x = expr binop x
34213 capture-stmt:
34214 v = update-stmt
34215 capture-block:
34216 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34218 where x and v are lvalue expressions with scalar type. */
34220 static void
34221 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34223 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34224 tree rhs1 = NULL_TREE, orig_lhs;
34225 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34226 bool structured_block = false;
34227 bool seq_cst = false;
34229 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34231 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34232 const char *p = IDENTIFIER_POINTER (id);
34234 if (!strcmp (p, "seq_cst"))
34236 seq_cst = true;
34237 cp_lexer_consume_token (parser->lexer);
34238 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34239 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34240 cp_lexer_consume_token (parser->lexer);
34243 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34245 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34246 const char *p = IDENTIFIER_POINTER (id);
34248 if (!strcmp (p, "read"))
34249 code = OMP_ATOMIC_READ;
34250 else if (!strcmp (p, "write"))
34251 code = NOP_EXPR;
34252 else if (!strcmp (p, "update"))
34253 code = OMP_ATOMIC;
34254 else if (!strcmp (p, "capture"))
34255 code = OMP_ATOMIC_CAPTURE_NEW;
34256 else
34257 p = NULL;
34258 if (p)
34259 cp_lexer_consume_token (parser->lexer);
34261 if (!seq_cst)
34263 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34264 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34265 cp_lexer_consume_token (parser->lexer);
34267 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34269 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34270 const char *p = IDENTIFIER_POINTER (id);
34272 if (!strcmp (p, "seq_cst"))
34274 seq_cst = true;
34275 cp_lexer_consume_token (parser->lexer);
34279 cp_parser_require_pragma_eol (parser, pragma_tok);
34281 switch (code)
34283 case OMP_ATOMIC_READ:
34284 case NOP_EXPR: /* atomic write */
34285 v = cp_parser_unary_expression (parser);
34286 if (v == error_mark_node)
34287 goto saw_error;
34288 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34289 goto saw_error;
34290 if (code == NOP_EXPR)
34291 lhs = cp_parser_expression (parser);
34292 else
34293 lhs = cp_parser_unary_expression (parser);
34294 if (lhs == error_mark_node)
34295 goto saw_error;
34296 if (code == NOP_EXPR)
34298 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34299 opcode. */
34300 code = OMP_ATOMIC;
34301 rhs = lhs;
34302 lhs = v;
34303 v = NULL_TREE;
34305 goto done;
34306 case OMP_ATOMIC_CAPTURE_NEW:
34307 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34309 cp_lexer_consume_token (parser->lexer);
34310 structured_block = true;
34312 else
34314 v = cp_parser_unary_expression (parser);
34315 if (v == error_mark_node)
34316 goto saw_error;
34317 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34318 goto saw_error;
34320 default:
34321 break;
34324 restart:
34325 lhs = cp_parser_unary_expression (parser);
34326 orig_lhs = lhs;
34327 switch (TREE_CODE (lhs))
34329 case ERROR_MARK:
34330 goto saw_error;
34332 case POSTINCREMENT_EXPR:
34333 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34334 code = OMP_ATOMIC_CAPTURE_OLD;
34335 /* FALLTHROUGH */
34336 case PREINCREMENT_EXPR:
34337 lhs = TREE_OPERAND (lhs, 0);
34338 opcode = PLUS_EXPR;
34339 rhs = integer_one_node;
34340 break;
34342 case POSTDECREMENT_EXPR:
34343 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34344 code = OMP_ATOMIC_CAPTURE_OLD;
34345 /* FALLTHROUGH */
34346 case PREDECREMENT_EXPR:
34347 lhs = TREE_OPERAND (lhs, 0);
34348 opcode = MINUS_EXPR;
34349 rhs = integer_one_node;
34350 break;
34352 case COMPOUND_EXPR:
34353 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34354 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34355 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34356 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34357 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34358 (TREE_OPERAND (lhs, 1), 0), 0)))
34359 == BOOLEAN_TYPE)
34360 /* Undo effects of boolean_increment for post {in,de}crement. */
34361 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34362 /* FALLTHRU */
34363 case MODIFY_EXPR:
34364 if (TREE_CODE (lhs) == MODIFY_EXPR
34365 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34367 /* Undo effects of boolean_increment. */
34368 if (integer_onep (TREE_OPERAND (lhs, 1)))
34370 /* This is pre or post increment. */
34371 rhs = TREE_OPERAND (lhs, 1);
34372 lhs = TREE_OPERAND (lhs, 0);
34373 opcode = NOP_EXPR;
34374 if (code == OMP_ATOMIC_CAPTURE_NEW
34375 && !structured_block
34376 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34377 code = OMP_ATOMIC_CAPTURE_OLD;
34378 break;
34381 /* FALLTHRU */
34382 default:
34383 switch (cp_lexer_peek_token (parser->lexer)->type)
34385 case CPP_MULT_EQ:
34386 opcode = MULT_EXPR;
34387 break;
34388 case CPP_DIV_EQ:
34389 opcode = TRUNC_DIV_EXPR;
34390 break;
34391 case CPP_PLUS_EQ:
34392 opcode = PLUS_EXPR;
34393 break;
34394 case CPP_MINUS_EQ:
34395 opcode = MINUS_EXPR;
34396 break;
34397 case CPP_LSHIFT_EQ:
34398 opcode = LSHIFT_EXPR;
34399 break;
34400 case CPP_RSHIFT_EQ:
34401 opcode = RSHIFT_EXPR;
34402 break;
34403 case CPP_AND_EQ:
34404 opcode = BIT_AND_EXPR;
34405 break;
34406 case CPP_OR_EQ:
34407 opcode = BIT_IOR_EXPR;
34408 break;
34409 case CPP_XOR_EQ:
34410 opcode = BIT_XOR_EXPR;
34411 break;
34412 case CPP_EQ:
34413 enum cp_parser_prec oprec;
34414 cp_token *token;
34415 cp_lexer_consume_token (parser->lexer);
34416 cp_parser_parse_tentatively (parser);
34417 rhs1 = cp_parser_simple_cast_expression (parser);
34418 if (rhs1 == error_mark_node)
34420 cp_parser_abort_tentative_parse (parser);
34421 cp_parser_simple_cast_expression (parser);
34422 goto saw_error;
34424 token = cp_lexer_peek_token (parser->lexer);
34425 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34427 cp_parser_abort_tentative_parse (parser);
34428 cp_parser_parse_tentatively (parser);
34429 rhs = cp_parser_binary_expression (parser, false, true,
34430 PREC_NOT_OPERATOR, NULL);
34431 if (rhs == error_mark_node)
34433 cp_parser_abort_tentative_parse (parser);
34434 cp_parser_binary_expression (parser, false, true,
34435 PREC_NOT_OPERATOR, NULL);
34436 goto saw_error;
34438 switch (TREE_CODE (rhs))
34440 case MULT_EXPR:
34441 case TRUNC_DIV_EXPR:
34442 case RDIV_EXPR:
34443 case PLUS_EXPR:
34444 case MINUS_EXPR:
34445 case LSHIFT_EXPR:
34446 case RSHIFT_EXPR:
34447 case BIT_AND_EXPR:
34448 case BIT_IOR_EXPR:
34449 case BIT_XOR_EXPR:
34450 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34452 if (cp_parser_parse_definitely (parser))
34454 opcode = TREE_CODE (rhs);
34455 rhs1 = TREE_OPERAND (rhs, 0);
34456 rhs = TREE_OPERAND (rhs, 1);
34457 goto stmt_done;
34459 else
34460 goto saw_error;
34462 break;
34463 default:
34464 break;
34466 cp_parser_abort_tentative_parse (parser);
34467 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34469 rhs = cp_parser_expression (parser);
34470 if (rhs == error_mark_node)
34471 goto saw_error;
34472 opcode = NOP_EXPR;
34473 rhs1 = NULL_TREE;
34474 goto stmt_done;
34476 cp_parser_error (parser,
34477 "invalid form of %<#pragma omp atomic%>");
34478 goto saw_error;
34480 if (!cp_parser_parse_definitely (parser))
34481 goto saw_error;
34482 switch (token->type)
34484 case CPP_SEMICOLON:
34485 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34487 code = OMP_ATOMIC_CAPTURE_OLD;
34488 v = lhs;
34489 lhs = NULL_TREE;
34490 lhs1 = rhs1;
34491 rhs1 = NULL_TREE;
34492 cp_lexer_consume_token (parser->lexer);
34493 goto restart;
34495 else if (structured_block)
34497 opcode = NOP_EXPR;
34498 rhs = rhs1;
34499 rhs1 = NULL_TREE;
34500 goto stmt_done;
34502 cp_parser_error (parser,
34503 "invalid form of %<#pragma omp atomic%>");
34504 goto saw_error;
34505 case CPP_MULT:
34506 opcode = MULT_EXPR;
34507 break;
34508 case CPP_DIV:
34509 opcode = TRUNC_DIV_EXPR;
34510 break;
34511 case CPP_PLUS:
34512 opcode = PLUS_EXPR;
34513 break;
34514 case CPP_MINUS:
34515 opcode = MINUS_EXPR;
34516 break;
34517 case CPP_LSHIFT:
34518 opcode = LSHIFT_EXPR;
34519 break;
34520 case CPP_RSHIFT:
34521 opcode = RSHIFT_EXPR;
34522 break;
34523 case CPP_AND:
34524 opcode = BIT_AND_EXPR;
34525 break;
34526 case CPP_OR:
34527 opcode = BIT_IOR_EXPR;
34528 break;
34529 case CPP_XOR:
34530 opcode = BIT_XOR_EXPR;
34531 break;
34532 default:
34533 cp_parser_error (parser,
34534 "invalid operator for %<#pragma omp atomic%>");
34535 goto saw_error;
34537 oprec = TOKEN_PRECEDENCE (token);
34538 gcc_assert (oprec != PREC_NOT_OPERATOR);
34539 if (commutative_tree_code (opcode))
34540 oprec = (enum cp_parser_prec) (oprec - 1);
34541 cp_lexer_consume_token (parser->lexer);
34542 rhs = cp_parser_binary_expression (parser, false, false,
34543 oprec, NULL);
34544 if (rhs == error_mark_node)
34545 goto saw_error;
34546 goto stmt_done;
34547 /* FALLTHROUGH */
34548 default:
34549 cp_parser_error (parser,
34550 "invalid operator for %<#pragma omp atomic%>");
34551 goto saw_error;
34553 cp_lexer_consume_token (parser->lexer);
34555 rhs = cp_parser_expression (parser);
34556 if (rhs == error_mark_node)
34557 goto saw_error;
34558 break;
34560 stmt_done:
34561 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34563 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34564 goto saw_error;
34565 v = cp_parser_unary_expression (parser);
34566 if (v == error_mark_node)
34567 goto saw_error;
34568 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34569 goto saw_error;
34570 lhs1 = cp_parser_unary_expression (parser);
34571 if (lhs1 == error_mark_node)
34572 goto saw_error;
34574 if (structured_block)
34576 cp_parser_consume_semicolon_at_end_of_statement (parser);
34577 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34579 done:
34580 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34581 if (!structured_block)
34582 cp_parser_consume_semicolon_at_end_of_statement (parser);
34583 return;
34585 saw_error:
34586 cp_parser_skip_to_end_of_block_or_statement (parser);
34587 if (structured_block)
34589 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34590 cp_lexer_consume_token (parser->lexer);
34591 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34593 cp_parser_skip_to_end_of_block_or_statement (parser);
34594 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34595 cp_lexer_consume_token (parser->lexer);
34601 /* OpenMP 2.5:
34602 # pragma omp barrier new-line */
34604 static void
34605 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34607 cp_parser_require_pragma_eol (parser, pragma_tok);
34608 finish_omp_barrier ();
34611 /* OpenMP 2.5:
34612 # pragma omp critical [(name)] new-line
34613 structured-block
34615 OpenMP 4.5:
34616 # pragma omp critical [(name) [hint(expression)]] new-line
34617 structured-block */
34619 #define OMP_CRITICAL_CLAUSE_MASK \
34620 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34622 static tree
34623 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34625 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34627 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34629 matching_parens parens;
34630 parens.consume_open (parser);
34632 name = cp_parser_identifier (parser);
34634 if (name == error_mark_node
34635 || !parens.require_close (parser))
34636 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34637 /*or_comma=*/false,
34638 /*consume_paren=*/true);
34639 if (name == error_mark_node)
34640 name = NULL;
34642 clauses = cp_parser_omp_all_clauses (parser,
34643 OMP_CRITICAL_CLAUSE_MASK,
34644 "#pragma omp critical", pragma_tok);
34646 else
34647 cp_parser_require_pragma_eol (parser, pragma_tok);
34649 stmt = cp_parser_omp_structured_block (parser, if_p);
34650 return c_finish_omp_critical (input_location, stmt, name, clauses);
34653 /* OpenMP 2.5:
34654 # pragma omp flush flush-vars[opt] new-line
34656 flush-vars:
34657 ( variable-list ) */
34659 static void
34660 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34662 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34663 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34664 cp_parser_require_pragma_eol (parser, pragma_tok);
34666 finish_omp_flush ();
34669 /* Helper function, to parse omp for increment expression. */
34671 static tree
34672 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34674 tree cond = cp_parser_binary_expression (parser, false, true,
34675 PREC_NOT_OPERATOR, NULL);
34676 if (cond == error_mark_node
34677 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34679 cp_parser_skip_to_end_of_statement (parser);
34680 return error_mark_node;
34683 switch (TREE_CODE (cond))
34685 case GT_EXPR:
34686 case GE_EXPR:
34687 case LT_EXPR:
34688 case LE_EXPR:
34689 break;
34690 case NE_EXPR:
34691 /* Fall through: OpenMP disallows NE_EXPR. */
34692 gcc_fallthrough ();
34693 default:
34694 return error_mark_node;
34697 /* If decl is an iterator, preserve LHS and RHS of the relational
34698 expr until finish_omp_for. */
34699 if (decl
34700 && (type_dependent_expression_p (decl)
34701 || CLASS_TYPE_P (TREE_TYPE (decl))))
34702 return cond;
34704 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34705 TREE_CODE (cond),
34706 TREE_OPERAND (cond, 0), ERROR_MARK,
34707 TREE_OPERAND (cond, 1), ERROR_MARK,
34708 /*overload=*/NULL, tf_warning_or_error);
34711 /* Helper function, to parse omp for increment expression. */
34713 static tree
34714 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34716 cp_token *token = cp_lexer_peek_token (parser->lexer);
34717 enum tree_code op;
34718 tree lhs, rhs;
34719 cp_id_kind idk;
34720 bool decl_first;
34722 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34724 op = (token->type == CPP_PLUS_PLUS
34725 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34726 cp_lexer_consume_token (parser->lexer);
34727 lhs = cp_parser_simple_cast_expression (parser);
34728 if (lhs != decl
34729 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34730 return error_mark_node;
34731 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34734 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34735 if (lhs != decl
34736 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34737 return error_mark_node;
34739 token = cp_lexer_peek_token (parser->lexer);
34740 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34742 op = (token->type == CPP_PLUS_PLUS
34743 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34744 cp_lexer_consume_token (parser->lexer);
34745 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34748 op = cp_parser_assignment_operator_opt (parser);
34749 if (op == ERROR_MARK)
34750 return error_mark_node;
34752 if (op != NOP_EXPR)
34754 rhs = cp_parser_assignment_expression (parser);
34755 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34756 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34759 lhs = cp_parser_binary_expression (parser, false, false,
34760 PREC_ADDITIVE_EXPRESSION, NULL);
34761 token = cp_lexer_peek_token (parser->lexer);
34762 decl_first = (lhs == decl
34763 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34764 if (decl_first)
34765 lhs = NULL_TREE;
34766 if (token->type != CPP_PLUS
34767 && token->type != CPP_MINUS)
34768 return error_mark_node;
34772 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34773 cp_lexer_consume_token (parser->lexer);
34774 rhs = cp_parser_binary_expression (parser, false, false,
34775 PREC_ADDITIVE_EXPRESSION, NULL);
34776 token = cp_lexer_peek_token (parser->lexer);
34777 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34779 if (lhs == NULL_TREE)
34781 if (op == PLUS_EXPR)
34782 lhs = rhs;
34783 else
34784 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34785 tf_warning_or_error);
34787 else
34788 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34789 ERROR_MARK, NULL, tf_warning_or_error);
34792 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34794 if (!decl_first)
34796 if ((rhs != decl
34797 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34798 || op == MINUS_EXPR)
34799 return error_mark_node;
34800 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34802 else
34803 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34805 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34808 /* Parse the initialization statement of an OpenMP for loop.
34810 Return true if the resulting construct should have an
34811 OMP_CLAUSE_PRIVATE added to it. */
34813 static tree
34814 cp_parser_omp_for_loop_init (cp_parser *parser,
34815 tree &this_pre_body,
34816 vec<tree, va_gc> *for_block,
34817 tree &init,
34818 tree &orig_init,
34819 tree &decl,
34820 tree &real_decl)
34822 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34823 return NULL_TREE;
34825 tree add_private_clause = NULL_TREE;
34827 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34829 init-expr:
34830 var = lb
34831 integer-type var = lb
34832 random-access-iterator-type var = lb
34833 pointer-type var = lb
34835 cp_decl_specifier_seq type_specifiers;
34837 /* First, try to parse as an initialized declaration. See
34838 cp_parser_condition, from whence the bulk of this is copied. */
34840 cp_parser_parse_tentatively (parser);
34841 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34842 /*is_trailing_return=*/false,
34843 &type_specifiers);
34844 if (cp_parser_parse_definitely (parser))
34846 /* If parsing a type specifier seq succeeded, then this
34847 MUST be a initialized declaration. */
34848 tree asm_specification, attributes;
34849 cp_declarator *declarator;
34851 declarator = cp_parser_declarator (parser,
34852 CP_PARSER_DECLARATOR_NAMED,
34853 /*ctor_dtor_or_conv_p=*/NULL,
34854 /*parenthesized_p=*/NULL,
34855 /*member_p=*/false,
34856 /*friend_p=*/false);
34857 attributes = cp_parser_attributes_opt (parser);
34858 asm_specification = cp_parser_asm_specification_opt (parser);
34860 if (declarator == cp_error_declarator)
34861 cp_parser_skip_to_end_of_statement (parser);
34863 else
34865 tree pushed_scope, auto_node;
34867 decl = start_decl (declarator, &type_specifiers,
34868 SD_INITIALIZED, attributes,
34869 /*prefix_attributes=*/NULL_TREE,
34870 &pushed_scope);
34872 auto_node = type_uses_auto (TREE_TYPE (decl));
34873 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34875 if (cp_lexer_next_token_is (parser->lexer,
34876 CPP_OPEN_PAREN))
34877 error ("parenthesized initialization is not allowed in "
34878 "OpenMP %<for%> loop");
34879 else
34880 /* Trigger an error. */
34881 cp_parser_require (parser, CPP_EQ, RT_EQ);
34883 init = error_mark_node;
34884 cp_parser_skip_to_end_of_statement (parser);
34886 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34887 || type_dependent_expression_p (decl)
34888 || auto_node)
34890 bool is_direct_init, is_non_constant_init;
34892 init = cp_parser_initializer (parser,
34893 &is_direct_init,
34894 &is_non_constant_init);
34896 if (auto_node)
34898 TREE_TYPE (decl)
34899 = do_auto_deduction (TREE_TYPE (decl), init,
34900 auto_node);
34902 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34903 && !type_dependent_expression_p (decl))
34904 goto non_class;
34907 cp_finish_decl (decl, init, !is_non_constant_init,
34908 asm_specification,
34909 LOOKUP_ONLYCONVERTING);
34910 orig_init = init;
34911 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34913 vec_safe_push (for_block, this_pre_body);
34914 init = NULL_TREE;
34916 else
34918 init = pop_stmt_list (this_pre_body);
34919 if (init && TREE_CODE (init) == STATEMENT_LIST)
34921 tree_stmt_iterator i = tsi_start (init);
34922 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34923 while (!tsi_end_p (i))
34925 tree t = tsi_stmt (i);
34926 if (TREE_CODE (t) == DECL_EXPR
34927 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34929 tsi_delink (&i);
34930 vec_safe_push (for_block, t);
34931 continue;
34933 break;
34935 if (tsi_one_before_end_p (i))
34937 tree t = tsi_stmt (i);
34938 tsi_delink (&i);
34939 free_stmt_list (init);
34940 init = t;
34944 this_pre_body = NULL_TREE;
34946 else
34948 /* Consume '='. */
34949 cp_lexer_consume_token (parser->lexer);
34950 init = cp_parser_assignment_expression (parser);
34952 non_class:
34953 if (TYPE_REF_P (TREE_TYPE (decl)))
34954 init = error_mark_node;
34955 else
34956 cp_finish_decl (decl, NULL_TREE,
34957 /*init_const_expr_p=*/false,
34958 asm_specification,
34959 LOOKUP_ONLYCONVERTING);
34962 if (pushed_scope)
34963 pop_scope (pushed_scope);
34966 else
34968 cp_id_kind idk;
34969 /* If parsing a type specifier sequence failed, then
34970 this MUST be a simple expression. */
34971 cp_parser_parse_tentatively (parser);
34972 decl = cp_parser_primary_expression (parser, false, false,
34973 false, &idk);
34974 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34975 if (!cp_parser_error_occurred (parser)
34976 && decl
34977 && (TREE_CODE (decl) == COMPONENT_REF
34978 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34980 cp_parser_abort_tentative_parse (parser);
34981 cp_parser_parse_tentatively (parser);
34982 cp_token *token = cp_lexer_peek_token (parser->lexer);
34983 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34984 /*check_dependency_p=*/true,
34985 /*template_p=*/NULL,
34986 /*declarator_p=*/false,
34987 /*optional_p=*/false);
34988 if (name != error_mark_node
34989 && last_tok == cp_lexer_peek_token (parser->lexer))
34991 decl = cp_parser_lookup_name_simple (parser, name,
34992 token->location);
34993 if (TREE_CODE (decl) == FIELD_DECL)
34994 add_private_clause = omp_privatize_field (decl, false);
34996 cp_parser_abort_tentative_parse (parser);
34997 cp_parser_parse_tentatively (parser);
34998 decl = cp_parser_primary_expression (parser, false, false,
34999 false, &idk);
35001 if (!cp_parser_error_occurred (parser)
35002 && decl
35003 && DECL_P (decl)
35004 && CLASS_TYPE_P (TREE_TYPE (decl)))
35006 tree rhs;
35008 cp_parser_parse_definitely (parser);
35009 cp_parser_require (parser, CPP_EQ, RT_EQ);
35010 rhs = cp_parser_assignment_expression (parser);
35011 orig_init = rhs;
35012 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
35013 decl, NOP_EXPR,
35014 rhs,
35015 tf_warning_or_error));
35016 if (!add_private_clause)
35017 add_private_clause = decl;
35019 else
35021 decl = NULL;
35022 cp_parser_abort_tentative_parse (parser);
35023 init = cp_parser_expression (parser);
35024 if (init)
35026 if (TREE_CODE (init) == MODIFY_EXPR
35027 || TREE_CODE (init) == MODOP_EXPR)
35028 real_decl = TREE_OPERAND (init, 0);
35032 return add_private_clause;
35035 /* Parse the restricted form of the for statement allowed by OpenMP. */
35037 static tree
35038 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
35039 tree *cclauses, bool *if_p)
35041 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
35042 tree real_decl, initv, condv, incrv, declv;
35043 tree this_pre_body, cl, ordered_cl = NULL_TREE;
35044 location_t loc_first;
35045 bool collapse_err = false;
35046 int i, collapse = 1, ordered = 0, count, nbraces = 0;
35047 vec<tree, va_gc> *for_block = make_tree_vector ();
35048 auto_vec<tree, 4> orig_inits;
35049 bool tiling = false;
35051 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
35052 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
35053 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
35054 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
35056 tiling = true;
35057 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
35059 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
35060 && OMP_CLAUSE_ORDERED_EXPR (cl))
35062 ordered_cl = cl;
35063 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
35066 if (ordered && ordered < collapse)
35068 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
35069 "%<ordered%> clause parameter is less than %<collapse%>");
35070 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
35071 = build_int_cst (NULL_TREE, collapse);
35072 ordered = collapse;
35074 if (ordered)
35076 for (tree *pc = &clauses; *pc; )
35077 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
35079 error_at (OMP_CLAUSE_LOCATION (*pc),
35080 "%<linear%> clause may not be specified together "
35081 "with %<ordered%> clause with a parameter");
35082 *pc = OMP_CLAUSE_CHAIN (*pc);
35084 else
35085 pc = &OMP_CLAUSE_CHAIN (*pc);
35088 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
35089 count = ordered ? ordered : collapse;
35091 declv = make_tree_vec (count);
35092 initv = make_tree_vec (count);
35093 condv = make_tree_vec (count);
35094 incrv = make_tree_vec (count);
35096 loc_first = cp_lexer_peek_token (parser->lexer)->location;
35098 for (i = 0; i < count; i++)
35100 int bracecount = 0;
35101 tree add_private_clause = NULL_TREE;
35102 location_t loc;
35104 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35106 if (!collapse_err)
35107 cp_parser_error (parser, "for statement expected");
35108 return NULL;
35110 loc = cp_lexer_consume_token (parser->lexer)->location;
35112 matching_parens parens;
35113 if (!parens.require_open (parser))
35114 return NULL;
35116 init = orig_init = decl = real_decl = NULL;
35117 this_pre_body = push_stmt_list ();
35119 add_private_clause
35120 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
35121 init, orig_init, decl, real_decl);
35123 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35124 if (this_pre_body)
35126 this_pre_body = pop_stmt_list (this_pre_body);
35127 if (pre_body)
35129 tree t = pre_body;
35130 pre_body = push_stmt_list ();
35131 add_stmt (t);
35132 add_stmt (this_pre_body);
35133 pre_body = pop_stmt_list (pre_body);
35135 else
35136 pre_body = this_pre_body;
35139 if (decl)
35140 real_decl = decl;
35141 if (cclauses != NULL
35142 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
35143 && real_decl != NULL_TREE)
35145 tree *c;
35146 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
35147 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
35148 && OMP_CLAUSE_DECL (*c) == real_decl)
35150 error_at (loc, "iteration variable %qD"
35151 " should not be firstprivate", real_decl);
35152 *c = OMP_CLAUSE_CHAIN (*c);
35154 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35155 && OMP_CLAUSE_DECL (*c) == real_decl)
35157 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35158 tree l = *c;
35159 *c = OMP_CLAUSE_CHAIN (*c);
35160 if (code == OMP_SIMD)
35162 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35163 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35165 else
35167 OMP_CLAUSE_CHAIN (l) = clauses;
35168 clauses = l;
35170 add_private_clause = NULL_TREE;
35172 else
35174 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35175 && OMP_CLAUSE_DECL (*c) == real_decl)
35176 add_private_clause = NULL_TREE;
35177 c = &OMP_CLAUSE_CHAIN (*c);
35181 if (add_private_clause)
35183 tree c;
35184 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35186 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35187 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35188 && OMP_CLAUSE_DECL (c) == decl)
35189 break;
35190 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35191 && OMP_CLAUSE_DECL (c) == decl)
35192 error_at (loc, "iteration variable %qD "
35193 "should not be firstprivate",
35194 decl);
35195 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35196 && OMP_CLAUSE_DECL (c) == decl)
35197 error_at (loc, "iteration variable %qD should not be reduction",
35198 decl);
35200 if (c == NULL)
35202 if (code != OMP_SIMD)
35203 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35204 else if (collapse == 1)
35205 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35206 else
35207 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35208 OMP_CLAUSE_DECL (c) = add_private_clause;
35209 c = finish_omp_clauses (c, C_ORT_OMP);
35210 if (c)
35212 OMP_CLAUSE_CHAIN (c) = clauses;
35213 clauses = c;
35214 /* For linear, signal that we need to fill up
35215 the so far unknown linear step. */
35216 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35217 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35222 cond = NULL;
35223 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35224 cond = cp_parser_omp_for_cond (parser, decl);
35225 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35227 incr = NULL;
35228 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35230 /* If decl is an iterator, preserve the operator on decl
35231 until finish_omp_for. */
35232 if (real_decl
35233 && ((processing_template_decl
35234 && (TREE_TYPE (real_decl) == NULL_TREE
35235 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
35236 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35237 incr = cp_parser_omp_for_incr (parser, real_decl);
35238 else
35239 incr = cp_parser_expression (parser);
35240 if (!EXPR_HAS_LOCATION (incr))
35241 protected_set_expr_location (incr, input_location);
35244 if (!parens.require_close (parser))
35245 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35246 /*or_comma=*/false,
35247 /*consume_paren=*/true);
35249 TREE_VEC_ELT (declv, i) = decl;
35250 TREE_VEC_ELT (initv, i) = init;
35251 TREE_VEC_ELT (condv, i) = cond;
35252 TREE_VEC_ELT (incrv, i) = incr;
35253 if (orig_init)
35255 orig_inits.safe_grow_cleared (i + 1);
35256 orig_inits[i] = orig_init;
35259 if (i == count - 1)
35260 break;
35262 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35263 in between the collapsed for loops to be still considered perfectly
35264 nested. Hopefully the final version clarifies this.
35265 For now handle (multiple) {'s and empty statements. */
35266 cp_parser_parse_tentatively (parser);
35267 for (;;)
35269 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35270 break;
35271 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35273 cp_lexer_consume_token (parser->lexer);
35274 bracecount++;
35276 else if (bracecount
35277 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35278 cp_lexer_consume_token (parser->lexer);
35279 else
35281 loc = cp_lexer_peek_token (parser->lexer)->location;
35282 error_at (loc, "not enough for loops to collapse");
35283 collapse_err = true;
35284 cp_parser_abort_tentative_parse (parser);
35285 declv = NULL_TREE;
35286 break;
35290 if (declv)
35292 cp_parser_parse_definitely (parser);
35293 nbraces += bracecount;
35297 if (nbraces)
35298 if_p = NULL;
35300 /* Note that we saved the original contents of this flag when we entered
35301 the structured block, and so we don't need to re-save it here. */
35302 parser->in_statement = IN_OMP_FOR;
35304 /* Note that the grammar doesn't call for a structured block here,
35305 though the loop as a whole is a structured block. */
35306 body = push_stmt_list ();
35307 cp_parser_statement (parser, NULL_TREE, false, if_p);
35308 body = pop_stmt_list (body);
35310 if (declv == NULL_TREE)
35311 ret = NULL_TREE;
35312 else
35313 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35314 body, pre_body, &orig_inits, clauses);
35316 while (nbraces)
35318 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35320 cp_lexer_consume_token (parser->lexer);
35321 nbraces--;
35323 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35324 cp_lexer_consume_token (parser->lexer);
35325 else
35327 if (!collapse_err)
35329 error_at (cp_lexer_peek_token (parser->lexer)->location,
35330 "collapsed loops not perfectly nested");
35332 collapse_err = true;
35333 cp_parser_statement_seq_opt (parser, NULL);
35334 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35335 break;
35339 while (!for_block->is_empty ())
35341 tree t = for_block->pop ();
35342 if (TREE_CODE (t) == STATEMENT_LIST)
35343 add_stmt (pop_stmt_list (t));
35344 else
35345 add_stmt (t);
35347 release_tree_vector (for_block);
35349 return ret;
35352 /* Helper function for OpenMP parsing, split clauses and call
35353 finish_omp_clauses on each of the set of clauses afterwards. */
35355 static void
35356 cp_omp_split_clauses (location_t loc, enum tree_code code,
35357 omp_clause_mask mask, tree clauses, tree *cclauses)
35359 int i;
35360 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35361 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35362 if (cclauses[i])
35363 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35366 /* OpenMP 4.0:
35367 #pragma omp simd simd-clause[optseq] new-line
35368 for-loop */
35370 #define OMP_SIMD_CLAUSE_MASK \
35371 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35373 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35374 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35377 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35380 static tree
35381 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35382 char *p_name, omp_clause_mask mask, tree *cclauses,
35383 bool *if_p)
35385 tree clauses, sb, ret;
35386 unsigned int save;
35387 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35389 strcat (p_name, " simd");
35390 mask |= OMP_SIMD_CLAUSE_MASK;
35392 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35393 cclauses == NULL);
35394 if (cclauses)
35396 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35397 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35398 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35399 OMP_CLAUSE_ORDERED);
35400 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35402 error_at (OMP_CLAUSE_LOCATION (c),
35403 "%<ordered%> clause with parameter may not be specified "
35404 "on %qs construct", p_name);
35405 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35409 sb = begin_omp_structured_block ();
35410 save = cp_parser_begin_omp_structured_block (parser);
35412 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35414 cp_parser_end_omp_structured_block (parser, save);
35415 add_stmt (finish_omp_structured_block (sb));
35417 return ret;
35420 /* OpenMP 2.5:
35421 #pragma omp for for-clause[optseq] new-line
35422 for-loop
35424 OpenMP 4.0:
35425 #pragma omp for simd for-simd-clause[optseq] new-line
35426 for-loop */
35428 #define OMP_FOR_CLAUSE_MASK \
35429 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35439 static tree
35440 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35441 char *p_name, omp_clause_mask mask, tree *cclauses,
35442 bool *if_p)
35444 tree clauses, sb, ret;
35445 unsigned int save;
35446 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35448 strcat (p_name, " for");
35449 mask |= OMP_FOR_CLAUSE_MASK;
35450 /* parallel for{, simd} disallows nowait clause, but for
35451 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35452 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35453 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35454 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35455 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35456 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35458 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35460 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35461 const char *p = IDENTIFIER_POINTER (id);
35463 if (strcmp (p, "simd") == 0)
35465 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35466 if (cclauses == NULL)
35467 cclauses = cclauses_buf;
35469 cp_lexer_consume_token (parser->lexer);
35470 if (!flag_openmp) /* flag_openmp_simd */
35471 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35472 cclauses, if_p);
35473 sb = begin_omp_structured_block ();
35474 save = cp_parser_begin_omp_structured_block (parser);
35475 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35476 cclauses, if_p);
35477 cp_parser_end_omp_structured_block (parser, save);
35478 tree body = finish_omp_structured_block (sb);
35479 if (ret == NULL)
35480 return ret;
35481 ret = make_node (OMP_FOR);
35482 TREE_TYPE (ret) = void_type_node;
35483 OMP_FOR_BODY (ret) = body;
35484 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35485 SET_EXPR_LOCATION (ret, loc);
35486 add_stmt (ret);
35487 return ret;
35490 if (!flag_openmp) /* flag_openmp_simd */
35492 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35493 return NULL_TREE;
35496 /* Composite distribute parallel for disallows linear clause. */
35497 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35498 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35500 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35501 cclauses == NULL);
35502 if (cclauses)
35504 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35505 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35508 sb = begin_omp_structured_block ();
35509 save = cp_parser_begin_omp_structured_block (parser);
35511 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35513 cp_parser_end_omp_structured_block (parser, save);
35514 add_stmt (finish_omp_structured_block (sb));
35516 return ret;
35519 /* OpenMP 2.5:
35520 # pragma omp master new-line
35521 structured-block */
35523 static tree
35524 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35526 cp_parser_require_pragma_eol (parser, pragma_tok);
35527 return c_finish_omp_master (input_location,
35528 cp_parser_omp_structured_block (parser, if_p));
35531 /* OpenMP 2.5:
35532 # pragma omp ordered new-line
35533 structured-block
35535 OpenMP 4.5:
35536 # pragma omp ordered ordered-clauses new-line
35537 structured-block */
35539 #define OMP_ORDERED_CLAUSE_MASK \
35540 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35543 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35544 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35546 static bool
35547 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35548 enum pragma_context context, bool *if_p)
35550 location_t loc = pragma_tok->location;
35552 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35554 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35555 const char *p = IDENTIFIER_POINTER (id);
35557 if (strcmp (p, "depend") == 0)
35559 if (!flag_openmp) /* flag_openmp_simd */
35561 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35562 return false;
35564 if (context == pragma_stmt)
35566 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35567 "%<depend%> clause may only be used in compound "
35568 "statements");
35569 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35570 return false;
35572 tree clauses
35573 = cp_parser_omp_all_clauses (parser,
35574 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35575 "#pragma omp ordered", pragma_tok);
35576 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35577 return false;
35581 tree clauses
35582 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35583 "#pragma omp ordered", pragma_tok);
35585 if (!flag_openmp /* flag_openmp_simd */
35586 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35587 return false;
35589 c_finish_omp_ordered (loc, clauses,
35590 cp_parser_omp_structured_block (parser, if_p));
35591 return true;
35594 /* OpenMP 2.5:
35596 section-scope:
35597 { section-sequence }
35599 section-sequence:
35600 section-directive[opt] structured-block
35601 section-sequence section-directive structured-block */
35603 static tree
35604 cp_parser_omp_sections_scope (cp_parser *parser)
35606 tree stmt, substmt;
35607 bool error_suppress = false;
35608 cp_token *tok;
35610 matching_braces braces;
35611 if (!braces.require_open (parser))
35612 return NULL_TREE;
35614 stmt = push_stmt_list ();
35616 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35617 != PRAGMA_OMP_SECTION)
35619 substmt = cp_parser_omp_structured_block (parser, NULL);
35620 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35621 add_stmt (substmt);
35624 while (1)
35626 tok = cp_lexer_peek_token (parser->lexer);
35627 if (tok->type == CPP_CLOSE_BRACE)
35628 break;
35629 if (tok->type == CPP_EOF)
35630 break;
35632 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35634 cp_lexer_consume_token (parser->lexer);
35635 cp_parser_require_pragma_eol (parser, tok);
35636 error_suppress = false;
35638 else if (!error_suppress)
35640 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35641 error_suppress = true;
35644 substmt = cp_parser_omp_structured_block (parser, NULL);
35645 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35646 add_stmt (substmt);
35648 braces.require_close (parser);
35650 substmt = pop_stmt_list (stmt);
35652 stmt = make_node (OMP_SECTIONS);
35653 TREE_TYPE (stmt) = void_type_node;
35654 OMP_SECTIONS_BODY (stmt) = substmt;
35656 add_stmt (stmt);
35657 return stmt;
35660 /* OpenMP 2.5:
35661 # pragma omp sections sections-clause[optseq] newline
35662 sections-scope */
35664 #define OMP_SECTIONS_CLAUSE_MASK \
35665 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35671 static tree
35672 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35673 char *p_name, omp_clause_mask mask, tree *cclauses)
35675 tree clauses, ret;
35676 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35678 strcat (p_name, " sections");
35679 mask |= OMP_SECTIONS_CLAUSE_MASK;
35680 if (cclauses)
35681 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35683 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35684 cclauses == NULL);
35685 if (cclauses)
35687 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35688 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35691 ret = cp_parser_omp_sections_scope (parser);
35692 if (ret)
35693 OMP_SECTIONS_CLAUSES (ret) = clauses;
35695 return ret;
35698 /* OpenMP 2.5:
35699 # pragma omp parallel parallel-clause[optseq] new-line
35700 structured-block
35701 # pragma omp parallel for parallel-for-clause[optseq] new-line
35702 structured-block
35703 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35704 structured-block
35706 OpenMP 4.0:
35707 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35708 structured-block */
35710 #define OMP_PARALLEL_CLAUSE_MASK \
35711 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35721 static tree
35722 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35723 char *p_name, omp_clause_mask mask, tree *cclauses,
35724 bool *if_p)
35726 tree stmt, clauses, block;
35727 unsigned int save;
35728 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35730 strcat (p_name, " parallel");
35731 mask |= OMP_PARALLEL_CLAUSE_MASK;
35732 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35733 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35734 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35735 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35737 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35739 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35740 if (cclauses == NULL)
35741 cclauses = cclauses_buf;
35743 cp_lexer_consume_token (parser->lexer);
35744 if (!flag_openmp) /* flag_openmp_simd */
35745 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35746 if_p);
35747 block = begin_omp_parallel ();
35748 save = cp_parser_begin_omp_structured_block (parser);
35749 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35750 if_p);
35751 cp_parser_end_omp_structured_block (parser, save);
35752 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35753 block);
35754 if (ret == NULL_TREE)
35755 return ret;
35756 OMP_PARALLEL_COMBINED (stmt) = 1;
35757 return stmt;
35759 /* When combined with distribute, parallel has to be followed by for.
35760 #pragma omp target parallel is allowed though. */
35761 else if (cclauses
35762 && (mask & (OMP_CLAUSE_MASK_1
35763 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35765 error_at (loc, "expected %<for%> after %qs", p_name);
35766 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35767 return NULL_TREE;
35769 else if (!flag_openmp) /* flag_openmp_simd */
35771 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35772 return NULL_TREE;
35774 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35776 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35777 const char *p = IDENTIFIER_POINTER (id);
35778 if (strcmp (p, "sections") == 0)
35780 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35781 cclauses = cclauses_buf;
35783 cp_lexer_consume_token (parser->lexer);
35784 block = begin_omp_parallel ();
35785 save = cp_parser_begin_omp_structured_block (parser);
35786 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35787 cp_parser_end_omp_structured_block (parser, save);
35788 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35789 block);
35790 OMP_PARALLEL_COMBINED (stmt) = 1;
35791 return stmt;
35795 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35796 cclauses == NULL);
35797 if (cclauses)
35799 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35800 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35803 block = begin_omp_parallel ();
35804 save = cp_parser_begin_omp_structured_block (parser);
35805 cp_parser_statement (parser, NULL_TREE, false, if_p);
35806 cp_parser_end_omp_structured_block (parser, save);
35807 stmt = finish_omp_parallel (clauses, block);
35808 return stmt;
35811 /* OpenMP 2.5:
35812 # pragma omp single single-clause[optseq] new-line
35813 structured-block */
35815 #define OMP_SINGLE_CLAUSE_MASK \
35816 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35821 static tree
35822 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35824 tree stmt = make_node (OMP_SINGLE);
35825 TREE_TYPE (stmt) = void_type_node;
35827 OMP_SINGLE_CLAUSES (stmt)
35828 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35829 "#pragma omp single", pragma_tok);
35830 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35832 return add_stmt (stmt);
35835 /* OpenMP 3.0:
35836 # pragma omp task task-clause[optseq] new-line
35837 structured-block */
35839 #define OMP_TASK_CLAUSE_MASK \
35840 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35851 static tree
35852 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35854 tree clauses, block;
35855 unsigned int save;
35857 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35858 "#pragma omp task", pragma_tok);
35859 block = begin_omp_task ();
35860 save = cp_parser_begin_omp_structured_block (parser);
35861 cp_parser_statement (parser, NULL_TREE, false, if_p);
35862 cp_parser_end_omp_structured_block (parser, save);
35863 return finish_omp_task (clauses, block);
35866 /* OpenMP 3.0:
35867 # pragma omp taskwait new-line */
35869 static void
35870 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35872 cp_parser_require_pragma_eol (parser, pragma_tok);
35873 finish_omp_taskwait ();
35876 /* OpenMP 3.1:
35877 # pragma omp taskyield new-line */
35879 static void
35880 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35882 cp_parser_require_pragma_eol (parser, pragma_tok);
35883 finish_omp_taskyield ();
35886 /* OpenMP 4.0:
35887 # pragma omp taskgroup new-line
35888 structured-block */
35890 static tree
35891 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35893 cp_parser_require_pragma_eol (parser, pragma_tok);
35894 return c_finish_omp_taskgroup (input_location,
35895 cp_parser_omp_structured_block (parser,
35896 if_p));
35900 /* OpenMP 2.5:
35901 # pragma omp threadprivate (variable-list) */
35903 static void
35904 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35906 tree vars;
35908 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35909 cp_parser_require_pragma_eol (parser, pragma_tok);
35911 finish_omp_threadprivate (vars);
35914 /* OpenMP 4.0:
35915 # pragma omp cancel cancel-clause[optseq] new-line */
35917 #define OMP_CANCEL_CLAUSE_MASK \
35918 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35919 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35924 static void
35925 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35927 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35928 "#pragma omp cancel", pragma_tok);
35929 finish_omp_cancel (clauses);
35932 /* OpenMP 4.0:
35933 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35935 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35936 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35941 static void
35942 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35943 enum pragma_context context)
35945 tree clauses;
35946 bool point_seen = false;
35948 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35950 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35951 const char *p = IDENTIFIER_POINTER (id);
35953 if (strcmp (p, "point") == 0)
35955 cp_lexer_consume_token (parser->lexer);
35956 point_seen = true;
35959 if (!point_seen)
35961 cp_parser_error (parser, "expected %<point%>");
35962 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35963 return;
35966 if (context != pragma_compound)
35968 if (context == pragma_stmt)
35969 error_at (pragma_tok->location,
35970 "%<#pragma %s%> may only be used in compound statements",
35971 "omp cancellation point");
35972 else
35973 cp_parser_error (parser, "expected declaration specifiers");
35974 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35975 return;
35978 clauses = cp_parser_omp_all_clauses (parser,
35979 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35980 "#pragma omp cancellation point",
35981 pragma_tok);
35982 finish_omp_cancellation_point (clauses);
35985 /* OpenMP 4.0:
35986 #pragma omp distribute distribute-clause[optseq] new-line
35987 for-loop */
35989 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35990 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35996 static tree
35997 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35998 char *p_name, omp_clause_mask mask, tree *cclauses,
35999 bool *if_p)
36001 tree clauses, sb, ret;
36002 unsigned int save;
36003 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36005 strcat (p_name, " distribute");
36006 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
36008 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36010 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36011 const char *p = IDENTIFIER_POINTER (id);
36012 bool simd = false;
36013 bool parallel = false;
36015 if (strcmp (p, "simd") == 0)
36016 simd = true;
36017 else
36018 parallel = strcmp (p, "parallel") == 0;
36019 if (parallel || simd)
36021 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36022 if (cclauses == NULL)
36023 cclauses = cclauses_buf;
36024 cp_lexer_consume_token (parser->lexer);
36025 if (!flag_openmp) /* flag_openmp_simd */
36027 if (simd)
36028 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36029 cclauses, if_p);
36030 else
36031 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36032 cclauses, if_p);
36034 sb = begin_omp_structured_block ();
36035 save = cp_parser_begin_omp_structured_block (parser);
36036 if (simd)
36037 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36038 cclauses, if_p);
36039 else
36040 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36041 cclauses, if_p);
36042 cp_parser_end_omp_structured_block (parser, save);
36043 tree body = finish_omp_structured_block (sb);
36044 if (ret == NULL)
36045 return ret;
36046 ret = make_node (OMP_DISTRIBUTE);
36047 TREE_TYPE (ret) = void_type_node;
36048 OMP_FOR_BODY (ret) = body;
36049 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36050 SET_EXPR_LOCATION (ret, loc);
36051 add_stmt (ret);
36052 return ret;
36055 if (!flag_openmp) /* flag_openmp_simd */
36057 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36058 return NULL_TREE;
36061 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36062 cclauses == NULL);
36063 if (cclauses)
36065 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
36066 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36069 sb = begin_omp_structured_block ();
36070 save = cp_parser_begin_omp_structured_block (parser);
36072 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
36074 cp_parser_end_omp_structured_block (parser, save);
36075 add_stmt (finish_omp_structured_block (sb));
36077 return ret;
36080 /* OpenMP 4.0:
36081 # pragma omp teams teams-clause[optseq] new-line
36082 structured-block */
36084 #define OMP_TEAMS_CLAUSE_MASK \
36085 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36093 static tree
36094 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
36095 char *p_name, omp_clause_mask mask, tree *cclauses,
36096 bool *if_p)
36098 tree clauses, sb, ret;
36099 unsigned int save;
36100 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36102 strcat (p_name, " teams");
36103 mask |= OMP_TEAMS_CLAUSE_MASK;
36105 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36107 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36108 const char *p = IDENTIFIER_POINTER (id);
36109 if (strcmp (p, "distribute") == 0)
36111 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36112 if (cclauses == NULL)
36113 cclauses = cclauses_buf;
36115 cp_lexer_consume_token (parser->lexer);
36116 if (!flag_openmp) /* flag_openmp_simd */
36117 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36118 cclauses, if_p);
36119 sb = begin_omp_structured_block ();
36120 save = cp_parser_begin_omp_structured_block (parser);
36121 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36122 cclauses, if_p);
36123 cp_parser_end_omp_structured_block (parser, save);
36124 tree body = finish_omp_structured_block (sb);
36125 if (ret == NULL)
36126 return ret;
36127 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36128 ret = make_node (OMP_TEAMS);
36129 TREE_TYPE (ret) = void_type_node;
36130 OMP_TEAMS_CLAUSES (ret) = clauses;
36131 OMP_TEAMS_BODY (ret) = body;
36132 OMP_TEAMS_COMBINED (ret) = 1;
36133 SET_EXPR_LOCATION (ret, loc);
36134 return add_stmt (ret);
36137 if (!flag_openmp) /* flag_openmp_simd */
36139 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36140 return NULL_TREE;
36143 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36144 cclauses == NULL);
36145 if (cclauses)
36147 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
36148 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36151 tree stmt = make_node (OMP_TEAMS);
36152 TREE_TYPE (stmt) = void_type_node;
36153 OMP_TEAMS_CLAUSES (stmt) = clauses;
36154 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36155 SET_EXPR_LOCATION (stmt, loc);
36157 return add_stmt (stmt);
36160 /* OpenMP 4.0:
36161 # pragma omp target data target-data-clause[optseq] new-line
36162 structured-block */
36164 #define OMP_TARGET_DATA_CLAUSE_MASK \
36165 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36168 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36170 static tree
36171 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36173 tree clauses
36174 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36175 "#pragma omp target data", pragma_tok);
36176 int map_seen = 0;
36177 for (tree *pc = &clauses; *pc;)
36179 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36180 switch (OMP_CLAUSE_MAP_KIND (*pc))
36182 case GOMP_MAP_TO:
36183 case GOMP_MAP_ALWAYS_TO:
36184 case GOMP_MAP_FROM:
36185 case GOMP_MAP_ALWAYS_FROM:
36186 case GOMP_MAP_TOFROM:
36187 case GOMP_MAP_ALWAYS_TOFROM:
36188 case GOMP_MAP_ALLOC:
36189 map_seen = 3;
36190 break;
36191 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36192 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36193 case GOMP_MAP_ALWAYS_POINTER:
36194 break;
36195 default:
36196 map_seen |= 1;
36197 error_at (OMP_CLAUSE_LOCATION (*pc),
36198 "%<#pragma omp target data%> with map-type other "
36199 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36200 "on %<map%> clause");
36201 *pc = OMP_CLAUSE_CHAIN (*pc);
36202 continue;
36204 pc = &OMP_CLAUSE_CHAIN (*pc);
36207 if (map_seen != 3)
36209 if (map_seen == 0)
36210 error_at (pragma_tok->location,
36211 "%<#pragma omp target data%> must contain at least "
36212 "one %<map%> clause");
36213 return NULL_TREE;
36216 tree stmt = make_node (OMP_TARGET_DATA);
36217 TREE_TYPE (stmt) = void_type_node;
36218 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36220 keep_next_level (true);
36221 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36223 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36224 return add_stmt (stmt);
36227 /* OpenMP 4.5:
36228 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36229 structured-block */
36231 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36232 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36234 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36235 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36236 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36238 static tree
36239 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36240 enum pragma_context context)
36242 bool data_seen = false;
36243 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36245 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36246 const char *p = IDENTIFIER_POINTER (id);
36248 if (strcmp (p, "data") == 0)
36250 cp_lexer_consume_token (parser->lexer);
36251 data_seen = true;
36254 if (!data_seen)
36256 cp_parser_error (parser, "expected %<data%>");
36257 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36258 return NULL_TREE;
36261 if (context == pragma_stmt)
36263 error_at (pragma_tok->location,
36264 "%<#pragma %s%> may only be used in compound statements",
36265 "omp target enter data");
36266 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36267 return NULL_TREE;
36270 tree clauses
36271 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36272 "#pragma omp target enter data", pragma_tok);
36273 int map_seen = 0;
36274 for (tree *pc = &clauses; *pc;)
36276 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36277 switch (OMP_CLAUSE_MAP_KIND (*pc))
36279 case GOMP_MAP_TO:
36280 case GOMP_MAP_ALWAYS_TO:
36281 case GOMP_MAP_ALLOC:
36282 map_seen = 3;
36283 break;
36284 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36285 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36286 case GOMP_MAP_ALWAYS_POINTER:
36287 break;
36288 default:
36289 map_seen |= 1;
36290 error_at (OMP_CLAUSE_LOCATION (*pc),
36291 "%<#pragma omp target enter data%> with map-type other "
36292 "than %<to%> or %<alloc%> on %<map%> clause");
36293 *pc = OMP_CLAUSE_CHAIN (*pc);
36294 continue;
36296 pc = &OMP_CLAUSE_CHAIN (*pc);
36299 if (map_seen != 3)
36301 if (map_seen == 0)
36302 error_at (pragma_tok->location,
36303 "%<#pragma omp target enter data%> must contain at least "
36304 "one %<map%> clause");
36305 return NULL_TREE;
36308 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36309 TREE_TYPE (stmt) = void_type_node;
36310 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36311 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36312 return add_stmt (stmt);
36315 /* OpenMP 4.5:
36316 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36317 structured-block */
36319 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36320 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36326 static tree
36327 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36328 enum pragma_context context)
36330 bool data_seen = false;
36331 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36333 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36334 const char *p = IDENTIFIER_POINTER (id);
36336 if (strcmp (p, "data") == 0)
36338 cp_lexer_consume_token (parser->lexer);
36339 data_seen = true;
36342 if (!data_seen)
36344 cp_parser_error (parser, "expected %<data%>");
36345 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36346 return NULL_TREE;
36349 if (context == pragma_stmt)
36351 error_at (pragma_tok->location,
36352 "%<#pragma %s%> may only be used in compound statements",
36353 "omp target exit data");
36354 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36355 return NULL_TREE;
36358 tree clauses
36359 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36360 "#pragma omp target exit data", pragma_tok);
36361 int map_seen = 0;
36362 for (tree *pc = &clauses; *pc;)
36364 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36365 switch (OMP_CLAUSE_MAP_KIND (*pc))
36367 case GOMP_MAP_FROM:
36368 case GOMP_MAP_ALWAYS_FROM:
36369 case GOMP_MAP_RELEASE:
36370 case GOMP_MAP_DELETE:
36371 map_seen = 3;
36372 break;
36373 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36374 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36375 case GOMP_MAP_ALWAYS_POINTER:
36376 break;
36377 default:
36378 map_seen |= 1;
36379 error_at (OMP_CLAUSE_LOCATION (*pc),
36380 "%<#pragma omp target exit data%> with map-type other "
36381 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36382 " clause");
36383 *pc = OMP_CLAUSE_CHAIN (*pc);
36384 continue;
36386 pc = &OMP_CLAUSE_CHAIN (*pc);
36389 if (map_seen != 3)
36391 if (map_seen == 0)
36392 error_at (pragma_tok->location,
36393 "%<#pragma omp target exit data%> must contain at least "
36394 "one %<map%> clause");
36395 return NULL_TREE;
36398 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36399 TREE_TYPE (stmt) = void_type_node;
36400 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36401 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36402 return add_stmt (stmt);
36405 /* OpenMP 4.0:
36406 # pragma omp target update target-update-clause[optseq] new-line */
36408 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36409 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36416 static bool
36417 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36418 enum pragma_context context)
36420 if (context == pragma_stmt)
36422 error_at (pragma_tok->location,
36423 "%<#pragma %s%> may only be used in compound statements",
36424 "omp target update");
36425 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36426 return false;
36429 tree clauses
36430 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36431 "#pragma omp target update", pragma_tok);
36432 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36433 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36435 error_at (pragma_tok->location,
36436 "%<#pragma omp target update%> must contain at least one "
36437 "%<from%> or %<to%> clauses");
36438 return false;
36441 tree stmt = make_node (OMP_TARGET_UPDATE);
36442 TREE_TYPE (stmt) = void_type_node;
36443 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36444 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36445 add_stmt (stmt);
36446 return false;
36449 /* OpenMP 4.0:
36450 # pragma omp target target-clause[optseq] new-line
36451 structured-block */
36453 #define OMP_TARGET_CLAUSE_MASK \
36454 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36464 static bool
36465 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36466 enum pragma_context context, bool *if_p)
36468 tree *pc = NULL, stmt;
36470 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36472 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36473 const char *p = IDENTIFIER_POINTER (id);
36474 enum tree_code ccode = ERROR_MARK;
36476 if (strcmp (p, "teams") == 0)
36477 ccode = OMP_TEAMS;
36478 else if (strcmp (p, "parallel") == 0)
36479 ccode = OMP_PARALLEL;
36480 else if (strcmp (p, "simd") == 0)
36481 ccode = OMP_SIMD;
36482 if (ccode != ERROR_MARK)
36484 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36485 char p_name[sizeof ("#pragma omp target teams distribute "
36486 "parallel for simd")];
36488 cp_lexer_consume_token (parser->lexer);
36489 strcpy (p_name, "#pragma omp target");
36490 if (!flag_openmp) /* flag_openmp_simd */
36492 tree stmt;
36493 switch (ccode)
36495 case OMP_TEAMS:
36496 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36497 OMP_TARGET_CLAUSE_MASK,
36498 cclauses, if_p);
36499 break;
36500 case OMP_PARALLEL:
36501 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36502 OMP_TARGET_CLAUSE_MASK,
36503 cclauses, if_p);
36504 break;
36505 case OMP_SIMD:
36506 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36507 OMP_TARGET_CLAUSE_MASK,
36508 cclauses, if_p);
36509 break;
36510 default:
36511 gcc_unreachable ();
36513 return stmt != NULL_TREE;
36515 keep_next_level (true);
36516 tree sb = begin_omp_structured_block (), ret;
36517 unsigned save = cp_parser_begin_omp_structured_block (parser);
36518 switch (ccode)
36520 case OMP_TEAMS:
36521 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36522 OMP_TARGET_CLAUSE_MASK, cclauses,
36523 if_p);
36524 break;
36525 case OMP_PARALLEL:
36526 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36527 OMP_TARGET_CLAUSE_MASK, cclauses,
36528 if_p);
36529 break;
36530 case OMP_SIMD:
36531 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36532 OMP_TARGET_CLAUSE_MASK, cclauses,
36533 if_p);
36534 break;
36535 default:
36536 gcc_unreachable ();
36538 cp_parser_end_omp_structured_block (parser, save);
36539 tree body = finish_omp_structured_block (sb);
36540 if (ret == NULL_TREE)
36541 return false;
36542 if (ccode == OMP_TEAMS && !processing_template_decl)
36544 /* For combined target teams, ensure the num_teams and
36545 thread_limit clause expressions are evaluated on the host,
36546 before entering the target construct. */
36547 tree c;
36548 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36549 c; c = OMP_CLAUSE_CHAIN (c))
36550 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36551 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36552 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36554 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36555 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36556 if (expr == error_mark_node)
36557 continue;
36558 tree tmp = TARGET_EXPR_SLOT (expr);
36559 add_stmt (expr);
36560 OMP_CLAUSE_OPERAND (c, 0) = expr;
36561 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36562 OMP_CLAUSE_FIRSTPRIVATE);
36563 OMP_CLAUSE_DECL (tc) = tmp;
36564 OMP_CLAUSE_CHAIN (tc)
36565 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36566 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36569 tree stmt = make_node (OMP_TARGET);
36570 TREE_TYPE (stmt) = void_type_node;
36571 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36572 OMP_TARGET_BODY (stmt) = body;
36573 OMP_TARGET_COMBINED (stmt) = 1;
36574 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36575 add_stmt (stmt);
36576 pc = &OMP_TARGET_CLAUSES (stmt);
36577 goto check_clauses;
36579 else if (!flag_openmp) /* flag_openmp_simd */
36581 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36582 return false;
36584 else if (strcmp (p, "data") == 0)
36586 cp_lexer_consume_token (parser->lexer);
36587 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36588 return true;
36590 else if (strcmp (p, "enter") == 0)
36592 cp_lexer_consume_token (parser->lexer);
36593 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36594 return false;
36596 else if (strcmp (p, "exit") == 0)
36598 cp_lexer_consume_token (parser->lexer);
36599 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36600 return false;
36602 else if (strcmp (p, "update") == 0)
36604 cp_lexer_consume_token (parser->lexer);
36605 return cp_parser_omp_target_update (parser, pragma_tok, context);
36608 if (!flag_openmp) /* flag_openmp_simd */
36610 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36611 return false;
36614 stmt = make_node (OMP_TARGET);
36615 TREE_TYPE (stmt) = void_type_node;
36617 OMP_TARGET_CLAUSES (stmt)
36618 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36619 "#pragma omp target", pragma_tok);
36620 pc = &OMP_TARGET_CLAUSES (stmt);
36621 keep_next_level (true);
36622 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36624 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36625 add_stmt (stmt);
36627 check_clauses:
36628 while (*pc)
36630 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36631 switch (OMP_CLAUSE_MAP_KIND (*pc))
36633 case GOMP_MAP_TO:
36634 case GOMP_MAP_ALWAYS_TO:
36635 case GOMP_MAP_FROM:
36636 case GOMP_MAP_ALWAYS_FROM:
36637 case GOMP_MAP_TOFROM:
36638 case GOMP_MAP_ALWAYS_TOFROM:
36639 case GOMP_MAP_ALLOC:
36640 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36641 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36642 case GOMP_MAP_ALWAYS_POINTER:
36643 break;
36644 default:
36645 error_at (OMP_CLAUSE_LOCATION (*pc),
36646 "%<#pragma omp target%> with map-type other "
36647 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36648 "on %<map%> clause");
36649 *pc = OMP_CLAUSE_CHAIN (*pc);
36650 continue;
36652 pc = &OMP_CLAUSE_CHAIN (*pc);
36654 return true;
36657 /* OpenACC 2.0:
36658 # pragma acc cache (variable-list) new-line
36661 static tree
36662 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36664 tree stmt, clauses;
36666 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36667 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36669 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36671 stmt = make_node (OACC_CACHE);
36672 TREE_TYPE (stmt) = void_type_node;
36673 OACC_CACHE_CLAUSES (stmt) = clauses;
36674 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36675 add_stmt (stmt);
36677 return stmt;
36680 /* OpenACC 2.0:
36681 # pragma acc data oacc-data-clause[optseq] new-line
36682 structured-block */
36684 #define OACC_DATA_CLAUSE_MASK \
36685 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36690 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36697 static tree
36698 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36700 tree stmt, clauses, block;
36701 unsigned int save;
36703 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36704 "#pragma acc data", pragma_tok);
36706 block = begin_omp_parallel ();
36707 save = cp_parser_begin_omp_structured_block (parser);
36708 cp_parser_statement (parser, NULL_TREE, false, if_p);
36709 cp_parser_end_omp_structured_block (parser, save);
36710 stmt = finish_oacc_data (clauses, block);
36711 return stmt;
36714 /* OpenACC 2.0:
36715 # pragma acc host_data <clauses> new-line
36716 structured-block */
36718 #define OACC_HOST_DATA_CLAUSE_MASK \
36719 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36721 static tree
36722 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36724 tree stmt, clauses, block;
36725 unsigned int save;
36727 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36728 "#pragma acc host_data", pragma_tok);
36730 block = begin_omp_parallel ();
36731 save = cp_parser_begin_omp_structured_block (parser);
36732 cp_parser_statement (parser, NULL_TREE, false, if_p);
36733 cp_parser_end_omp_structured_block (parser, save);
36734 stmt = finish_oacc_host_data (clauses, block);
36735 return stmt;
36738 /* OpenACC 2.0:
36739 # pragma acc declare oacc-data-clause[optseq] new-line
36742 #define OACC_DECLARE_CLAUSE_MASK \
36743 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36756 static tree
36757 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36759 tree clauses, stmt;
36760 bool error = false;
36762 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36763 "#pragma acc declare", pragma_tok, true);
36766 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36768 error_at (pragma_tok->location,
36769 "no valid clauses specified in %<#pragma acc declare%>");
36770 return NULL_TREE;
36773 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36775 location_t loc = OMP_CLAUSE_LOCATION (t);
36776 tree decl = OMP_CLAUSE_DECL (t);
36777 if (!DECL_P (decl))
36779 error_at (loc, "array section in %<#pragma acc declare%>");
36780 error = true;
36781 continue;
36783 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36784 switch (OMP_CLAUSE_MAP_KIND (t))
36786 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36787 case GOMP_MAP_FORCE_ALLOC:
36788 case GOMP_MAP_FORCE_TO:
36789 case GOMP_MAP_FORCE_DEVICEPTR:
36790 case GOMP_MAP_DEVICE_RESIDENT:
36791 break;
36793 case GOMP_MAP_LINK:
36794 if (!global_bindings_p ()
36795 && (TREE_STATIC (decl)
36796 || !DECL_EXTERNAL (decl)))
36798 error_at (loc,
36799 "%qD must be a global variable in "
36800 "%<#pragma acc declare link%>",
36801 decl);
36802 error = true;
36803 continue;
36805 break;
36807 default:
36808 if (global_bindings_p ())
36810 error_at (loc, "invalid OpenACC clause at file scope");
36811 error = true;
36812 continue;
36814 if (DECL_EXTERNAL (decl))
36816 error_at (loc,
36817 "invalid use of %<extern%> variable %qD "
36818 "in %<#pragma acc declare%>", decl);
36819 error = true;
36820 continue;
36822 else if (TREE_PUBLIC (decl))
36824 error_at (loc,
36825 "invalid use of %<global%> variable %qD "
36826 "in %<#pragma acc declare%>", decl);
36827 error = true;
36828 continue;
36830 break;
36833 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36834 || lookup_attribute ("omp declare target link",
36835 DECL_ATTRIBUTES (decl)))
36837 error_at (loc, "variable %qD used more than once with "
36838 "%<#pragma acc declare%>", decl);
36839 error = true;
36840 continue;
36843 if (!error)
36845 tree id;
36847 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36848 id = get_identifier ("omp declare target link");
36849 else
36850 id = get_identifier ("omp declare target");
36852 DECL_ATTRIBUTES (decl)
36853 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36854 if (global_bindings_p ())
36856 symtab_node *node = symtab_node::get (decl);
36857 if (node != NULL)
36859 node->offloadable = 1;
36860 if (ENABLE_OFFLOADING)
36862 g->have_offload = true;
36863 if (is_a <varpool_node *> (node))
36864 vec_safe_push (offload_vars, decl);
36871 if (error || global_bindings_p ())
36872 return NULL_TREE;
36874 stmt = make_node (OACC_DECLARE);
36875 TREE_TYPE (stmt) = void_type_node;
36876 OACC_DECLARE_CLAUSES (stmt) = clauses;
36877 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36879 add_stmt (stmt);
36881 return NULL_TREE;
36884 /* OpenACC 2.0:
36885 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36889 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36891 LOC is the location of the #pragma token.
36894 #define OACC_ENTER_DATA_CLAUSE_MASK \
36895 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36898 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36899 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36900 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36901 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36903 #define OACC_EXIT_DATA_CLAUSE_MASK \
36904 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36910 static tree
36911 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36912 bool enter)
36914 location_t loc = pragma_tok->location;
36915 tree stmt, clauses;
36916 const char *p = "";
36918 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36919 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36921 if (strcmp (p, "data") != 0)
36923 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36924 enter ? "enter" : "exit");
36925 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36926 return NULL_TREE;
36929 cp_lexer_consume_token (parser->lexer);
36931 if (enter)
36932 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36933 "#pragma acc enter data", pragma_tok);
36934 else
36935 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36936 "#pragma acc exit data", pragma_tok);
36938 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36940 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36941 enter ? "enter" : "exit");
36942 return NULL_TREE;
36945 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36946 TREE_TYPE (stmt) = void_type_node;
36947 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36948 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36949 add_stmt (stmt);
36950 return stmt;
36953 /* OpenACC 2.0:
36954 # pragma acc loop oacc-loop-clause[optseq] new-line
36955 structured-block */
36957 #define OACC_LOOP_CLAUSE_MASK \
36958 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36969 static tree
36970 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36971 omp_clause_mask mask, tree *cclauses, bool *if_p)
36973 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36975 strcat (p_name, " loop");
36976 mask |= OACC_LOOP_CLAUSE_MASK;
36978 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36979 cclauses == NULL);
36980 if (cclauses)
36982 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36983 if (*cclauses)
36984 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36985 if (clauses)
36986 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36989 tree block = begin_omp_structured_block ();
36990 int save = cp_parser_begin_omp_structured_block (parser);
36991 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36992 cp_parser_end_omp_structured_block (parser, save);
36993 add_stmt (finish_omp_structured_block (block));
36995 return stmt;
36998 /* OpenACC 2.0:
36999 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37000 structured-block
37004 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37005 structured-block
37008 #define OACC_KERNELS_CLAUSE_MASK \
37009 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37027 #define OACC_PARALLEL_CLAUSE_MASK \
37028 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37049 static tree
37050 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
37051 char *p_name, bool *if_p)
37053 omp_clause_mask mask;
37054 enum tree_code code;
37055 switch (cp_parser_pragma_kind (pragma_tok))
37057 case PRAGMA_OACC_KERNELS:
37058 strcat (p_name, " kernels");
37059 mask = OACC_KERNELS_CLAUSE_MASK;
37060 code = OACC_KERNELS;
37061 break;
37062 case PRAGMA_OACC_PARALLEL:
37063 strcat (p_name, " parallel");
37064 mask = OACC_PARALLEL_CLAUSE_MASK;
37065 code = OACC_PARALLEL;
37066 break;
37067 default:
37068 gcc_unreachable ();
37071 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37073 const char *p
37074 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37075 if (strcmp (p, "loop") == 0)
37077 cp_lexer_consume_token (parser->lexer);
37078 tree block = begin_omp_parallel ();
37079 tree clauses;
37080 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
37081 if_p);
37082 return finish_omp_construct (code, block, clauses);
37086 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
37088 tree block = begin_omp_parallel ();
37089 unsigned int save = cp_parser_begin_omp_structured_block (parser);
37090 cp_parser_statement (parser, NULL_TREE, false, if_p);
37091 cp_parser_end_omp_structured_block (parser, save);
37092 return finish_omp_construct (code, block, clauses);
37095 /* OpenACC 2.0:
37096 # pragma acc update oacc-update-clause[optseq] new-line
37099 #define OACC_UPDATE_CLAUSE_MASK \
37100 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37103 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
37105 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37107 static tree
37108 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
37110 tree stmt, clauses;
37112 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
37113 "#pragma acc update", pragma_tok);
37115 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37117 error_at (pragma_tok->location,
37118 "%<#pragma acc update%> must contain at least one "
37119 "%<device%> or %<host%> or %<self%> clause");
37120 return NULL_TREE;
37123 stmt = make_node (OACC_UPDATE);
37124 TREE_TYPE (stmt) = void_type_node;
37125 OACC_UPDATE_CLAUSES (stmt) = clauses;
37126 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37127 add_stmt (stmt);
37128 return stmt;
37131 /* OpenACC 2.0:
37132 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37134 LOC is the location of the #pragma token.
37137 #define OACC_WAIT_CLAUSE_MASK \
37138 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37140 static tree
37141 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
37143 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
37144 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37146 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37147 list = cp_parser_oacc_wait_list (parser, loc, list);
37149 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
37150 "#pragma acc wait", pragma_tok);
37152 stmt = c_finish_oacc_wait (loc, list, clauses);
37153 stmt = finish_expr_stmt (stmt);
37155 return stmt;
37158 /* OpenMP 4.0:
37159 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37161 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37162 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37169 static void
37170 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37171 enum pragma_context context)
37173 bool first_p = parser->omp_declare_simd == NULL;
37174 cp_omp_declare_simd_data data;
37175 if (first_p)
37177 data.error_seen = false;
37178 data.fndecl_seen = false;
37179 data.tokens = vNULL;
37180 data.clauses = NULL_TREE;
37181 /* It is safe to take the address of a local variable; it will only be
37182 used while this scope is live. */
37183 parser->omp_declare_simd = &data;
37186 /* Store away all pragma tokens. */
37187 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37188 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37189 cp_lexer_consume_token (parser->lexer);
37190 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37191 parser->omp_declare_simd->error_seen = true;
37192 cp_parser_require_pragma_eol (parser, pragma_tok);
37193 struct cp_token_cache *cp
37194 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37195 parser->omp_declare_simd->tokens.safe_push (cp);
37197 if (first_p)
37199 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37200 cp_parser_pragma (parser, context, NULL);
37201 switch (context)
37203 case pragma_external:
37204 cp_parser_declaration (parser);
37205 break;
37206 case pragma_member:
37207 cp_parser_member_declaration (parser);
37208 break;
37209 case pragma_objc_icode:
37210 cp_parser_block_declaration (parser, /*statement_p=*/false);
37211 break;
37212 default:
37213 cp_parser_declaration_statement (parser);
37214 break;
37216 if (parser->omp_declare_simd
37217 && !parser->omp_declare_simd->error_seen
37218 && !parser->omp_declare_simd->fndecl_seen)
37219 error_at (pragma_tok->location,
37220 "%<#pragma omp declare simd%> not immediately followed by "
37221 "function declaration or definition");
37222 data.tokens.release ();
37223 parser->omp_declare_simd = NULL;
37227 /* Finalize #pragma omp declare simd clauses after direct declarator has
37228 been parsed, and put that into "omp declare simd" attribute. */
37230 static tree
37231 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37233 struct cp_token_cache *ce;
37234 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37235 int i;
37237 if (!data->error_seen && data->fndecl_seen)
37239 error ("%<#pragma omp declare simd%> not immediately followed by "
37240 "a single function declaration or definition");
37241 data->error_seen = true;
37243 if (data->error_seen)
37244 return attrs;
37246 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37248 tree c, cl;
37250 cp_parser_push_lexer_for_tokens (parser, ce);
37251 parser->lexer->in_pragma = true;
37252 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37253 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37254 cp_lexer_consume_token (parser->lexer);
37255 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37256 "#pragma omp declare simd", pragma_tok);
37257 cp_parser_pop_lexer (parser);
37258 if (cl)
37259 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37260 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37261 TREE_CHAIN (c) = attrs;
37262 if (processing_template_decl)
37263 ATTR_IS_DEPENDENT (c) = 1;
37264 attrs = c;
37267 data->fndecl_seen = true;
37268 return attrs;
37272 /* OpenMP 4.0:
37273 # pragma omp declare target new-line
37274 declarations and definitions
37275 # pragma omp end declare target new-line
37277 OpenMP 4.5:
37278 # pragma omp declare target ( extended-list ) new-line
37280 # pragma omp declare target declare-target-clauses[seq] new-line */
37282 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37283 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37286 static void
37287 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37289 tree clauses = NULL_TREE;
37290 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37291 clauses
37292 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37293 "#pragma omp declare target", pragma_tok);
37294 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37296 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37297 clauses);
37298 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37299 cp_parser_require_pragma_eol (parser, pragma_tok);
37301 else
37303 cp_parser_require_pragma_eol (parser, pragma_tok);
37304 scope_chain->omp_declare_target_attribute++;
37305 return;
37307 if (scope_chain->omp_declare_target_attribute)
37308 error_at (pragma_tok->location,
37309 "%<#pragma omp declare target%> with clauses in between "
37310 "%<#pragma omp declare target%> without clauses and "
37311 "%<#pragma omp end declare target%>");
37312 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37314 tree t = OMP_CLAUSE_DECL (c), id;
37315 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37316 tree at2 = lookup_attribute ("omp declare target link",
37317 DECL_ATTRIBUTES (t));
37318 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37320 id = get_identifier ("omp declare target link");
37321 std::swap (at1, at2);
37323 else
37324 id = get_identifier ("omp declare target");
37325 if (at2)
37327 error_at (OMP_CLAUSE_LOCATION (c),
37328 "%qD specified both in declare target %<link%> and %<to%>"
37329 " clauses", t);
37330 continue;
37332 if (!at1)
37334 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37335 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37336 continue;
37338 symtab_node *node = symtab_node::get (t);
37339 if (node != NULL)
37341 node->offloadable = 1;
37342 if (ENABLE_OFFLOADING)
37344 g->have_offload = true;
37345 if (is_a <varpool_node *> (node))
37346 vec_safe_push (offload_vars, t);
37353 static void
37354 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37356 const char *p = "";
37357 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37359 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37360 p = IDENTIFIER_POINTER (id);
37362 if (strcmp (p, "declare") == 0)
37364 cp_lexer_consume_token (parser->lexer);
37365 p = "";
37366 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37368 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37369 p = IDENTIFIER_POINTER (id);
37371 if (strcmp (p, "target") == 0)
37372 cp_lexer_consume_token (parser->lexer);
37373 else
37375 cp_parser_error (parser, "expected %<target%>");
37376 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37377 return;
37380 else
37382 cp_parser_error (parser, "expected %<declare%>");
37383 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37384 return;
37386 cp_parser_require_pragma_eol (parser, pragma_tok);
37387 if (!scope_chain->omp_declare_target_attribute)
37388 error_at (pragma_tok->location,
37389 "%<#pragma omp end declare target%> without corresponding "
37390 "%<#pragma omp declare target%>");
37391 else
37392 scope_chain->omp_declare_target_attribute--;
37395 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37396 expression and optional initializer clause of
37397 #pragma omp declare reduction. We store the expression(s) as
37398 either 3, 6 or 7 special statements inside of the artificial function's
37399 body. The first two statements are DECL_EXPRs for the artificial
37400 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37401 expression that uses those variables.
37402 If there was any INITIALIZER clause, this is followed by further statements,
37403 the fourth and fifth statements are DECL_EXPRs for the artificial
37404 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37405 constructor variant (first token after open paren is not omp_priv),
37406 then the sixth statement is a statement with the function call expression
37407 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37408 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37409 to initialize the OMP_PRIV artificial variable and there is seventh
37410 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37412 static bool
37413 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37415 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37416 gcc_assert (TYPE_REF_P (type));
37417 type = TREE_TYPE (type);
37418 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37419 DECL_ARTIFICIAL (omp_out) = 1;
37420 pushdecl (omp_out);
37421 add_decl_expr (omp_out);
37422 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37423 DECL_ARTIFICIAL (omp_in) = 1;
37424 pushdecl (omp_in);
37425 add_decl_expr (omp_in);
37426 tree combiner;
37427 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37429 keep_next_level (true);
37430 tree block = begin_omp_structured_block ();
37431 combiner = cp_parser_expression (parser);
37432 finish_expr_stmt (combiner);
37433 block = finish_omp_structured_block (block);
37434 add_stmt (block);
37436 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37437 return false;
37439 const char *p = "";
37440 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37442 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37443 p = IDENTIFIER_POINTER (id);
37446 if (strcmp (p, "initializer") == 0)
37448 cp_lexer_consume_token (parser->lexer);
37449 matching_parens parens;
37450 if (!parens.require_open (parser))
37451 return false;
37453 p = "";
37454 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37456 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37457 p = IDENTIFIER_POINTER (id);
37460 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37461 DECL_ARTIFICIAL (omp_priv) = 1;
37462 pushdecl (omp_priv);
37463 add_decl_expr (omp_priv);
37464 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37465 DECL_ARTIFICIAL (omp_orig) = 1;
37466 pushdecl (omp_orig);
37467 add_decl_expr (omp_orig);
37469 keep_next_level (true);
37470 block = begin_omp_structured_block ();
37472 bool ctor = false;
37473 if (strcmp (p, "omp_priv") == 0)
37475 bool is_direct_init, is_non_constant_init;
37476 ctor = true;
37477 cp_lexer_consume_token (parser->lexer);
37478 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37479 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37480 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37481 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37482 == CPP_CLOSE_PAREN
37483 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37484 == CPP_CLOSE_PAREN))
37486 finish_omp_structured_block (block);
37487 error ("invalid initializer clause");
37488 return false;
37490 initializer = cp_parser_initializer (parser, &is_direct_init,
37491 &is_non_constant_init);
37492 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37493 NULL_TREE, LOOKUP_ONLYCONVERTING);
37495 else
37497 cp_parser_parse_tentatively (parser);
37498 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37499 /*check_dependency_p=*/true,
37500 /*template_p=*/NULL,
37501 /*declarator_p=*/false,
37502 /*optional_p=*/false);
37503 vec<tree, va_gc> *args;
37504 if (fn_name == error_mark_node
37505 || cp_parser_error_occurred (parser)
37506 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37507 || ((args = cp_parser_parenthesized_expression_list
37508 (parser, non_attr, /*cast_p=*/false,
37509 /*allow_expansion_p=*/true,
37510 /*non_constant_p=*/NULL)),
37511 cp_parser_error_occurred (parser)))
37513 finish_omp_structured_block (block);
37514 cp_parser_abort_tentative_parse (parser);
37515 cp_parser_error (parser, "expected id-expression (arguments)");
37516 return false;
37518 unsigned int i;
37519 tree arg;
37520 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37521 if (arg == omp_priv
37522 || (TREE_CODE (arg) == ADDR_EXPR
37523 && TREE_OPERAND (arg, 0) == omp_priv))
37524 break;
37525 cp_parser_abort_tentative_parse (parser);
37526 if (arg == NULL_TREE)
37527 error ("one of the initializer call arguments should be %<omp_priv%>"
37528 " or %<&omp_priv%>");
37529 initializer = cp_parser_postfix_expression (parser, false, false, false,
37530 false, NULL);
37531 finish_expr_stmt (initializer);
37534 block = finish_omp_structured_block (block);
37535 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37536 add_stmt (block);
37538 if (ctor)
37539 add_decl_expr (omp_orig);
37541 if (!parens.require_close (parser))
37542 return false;
37545 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37546 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37547 UNKNOWN_LOCATION);
37549 return true;
37552 /* OpenMP 4.0
37553 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37554 initializer-clause[opt] new-line
37556 initializer-clause:
37557 initializer (omp_priv initializer)
37558 initializer (function-name (argument-list)) */
37560 static void
37561 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37562 enum pragma_context)
37564 auto_vec<tree> types;
37565 enum tree_code reduc_code = ERROR_MARK;
37566 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37567 unsigned int i;
37568 cp_token *first_token;
37569 cp_token_cache *cp;
37570 int errs;
37571 void *p;
37573 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37574 p = obstack_alloc (&declarator_obstack, 0);
37576 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37577 goto fail;
37579 switch (cp_lexer_peek_token (parser->lexer)->type)
37581 case CPP_PLUS:
37582 reduc_code = PLUS_EXPR;
37583 break;
37584 case CPP_MULT:
37585 reduc_code = MULT_EXPR;
37586 break;
37587 case CPP_MINUS:
37588 reduc_code = MINUS_EXPR;
37589 break;
37590 case CPP_AND:
37591 reduc_code = BIT_AND_EXPR;
37592 break;
37593 case CPP_XOR:
37594 reduc_code = BIT_XOR_EXPR;
37595 break;
37596 case CPP_OR:
37597 reduc_code = BIT_IOR_EXPR;
37598 break;
37599 case CPP_AND_AND:
37600 reduc_code = TRUTH_ANDIF_EXPR;
37601 break;
37602 case CPP_OR_OR:
37603 reduc_code = TRUTH_ORIF_EXPR;
37604 break;
37605 case CPP_NAME:
37606 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37607 break;
37608 default:
37609 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37610 "%<|%>, %<&&%>, %<||%> or identifier");
37611 goto fail;
37614 if (reduc_code != ERROR_MARK)
37615 cp_lexer_consume_token (parser->lexer);
37617 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37618 if (reduc_id == error_mark_node)
37619 goto fail;
37621 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37622 goto fail;
37624 /* Types may not be defined in declare reduction type list. */
37625 const char *saved_message;
37626 saved_message = parser->type_definition_forbidden_message;
37627 parser->type_definition_forbidden_message
37628 = G_("types may not be defined in declare reduction type list");
37629 bool saved_colon_corrects_to_scope_p;
37630 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37631 parser->colon_corrects_to_scope_p = false;
37632 bool saved_colon_doesnt_start_class_def_p;
37633 saved_colon_doesnt_start_class_def_p
37634 = parser->colon_doesnt_start_class_def_p;
37635 parser->colon_doesnt_start_class_def_p = true;
37637 while (true)
37639 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37640 type = cp_parser_type_id (parser);
37641 if (type == error_mark_node)
37643 else if (ARITHMETIC_TYPE_P (type)
37644 && (orig_reduc_id == NULL_TREE
37645 || (TREE_CODE (type) != COMPLEX_TYPE
37646 && (id_equal (orig_reduc_id, "min")
37647 || id_equal (orig_reduc_id, "max")))))
37648 error_at (loc, "predeclared arithmetic type %qT in "
37649 "%<#pragma omp declare reduction%>", type);
37650 else if (TREE_CODE (type) == FUNCTION_TYPE
37651 || TREE_CODE (type) == METHOD_TYPE
37652 || TREE_CODE (type) == ARRAY_TYPE)
37653 error_at (loc, "function or array type %qT in "
37654 "%<#pragma omp declare reduction%>", type);
37655 else if (TYPE_REF_P (type))
37656 error_at (loc, "reference type %qT in "
37657 "%<#pragma omp declare reduction%>", type);
37658 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37659 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37660 "%<#pragma omp declare reduction%>", type);
37661 else
37662 types.safe_push (type);
37664 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37665 cp_lexer_consume_token (parser->lexer);
37666 else
37667 break;
37670 /* Restore the saved message. */
37671 parser->type_definition_forbidden_message = saved_message;
37672 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37673 parser->colon_doesnt_start_class_def_p
37674 = saved_colon_doesnt_start_class_def_p;
37676 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37677 || types.is_empty ())
37679 fail:
37680 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37681 goto done;
37684 first_token = cp_lexer_peek_token (parser->lexer);
37685 cp = NULL;
37686 errs = errorcount;
37687 FOR_EACH_VEC_ELT (types, i, type)
37689 tree fntype
37690 = build_function_type_list (void_type_node,
37691 cp_build_reference_type (type, false),
37692 NULL_TREE);
37693 tree this_reduc_id = reduc_id;
37694 if (!dependent_type_p (type))
37695 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37696 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37697 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37698 DECL_ARTIFICIAL (fndecl) = 1;
37699 DECL_EXTERNAL (fndecl) = 1;
37700 DECL_DECLARED_INLINE_P (fndecl) = 1;
37701 DECL_IGNORED_P (fndecl) = 1;
37702 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37703 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37704 DECL_ATTRIBUTES (fndecl)
37705 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37706 DECL_ATTRIBUTES (fndecl));
37707 if (processing_template_decl)
37708 fndecl = push_template_decl (fndecl);
37709 bool block_scope = false;
37710 tree block = NULL_TREE;
37711 if (current_function_decl)
37713 block_scope = true;
37714 DECL_CONTEXT (fndecl) = global_namespace;
37715 if (!processing_template_decl)
37716 pushdecl (fndecl);
37718 else if (current_class_type)
37720 if (cp == NULL)
37722 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37723 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37724 cp_lexer_consume_token (parser->lexer);
37725 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37726 goto fail;
37727 cp = cp_token_cache_new (first_token,
37728 cp_lexer_peek_nth_token (parser->lexer,
37729 2));
37731 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37732 finish_member_declaration (fndecl);
37733 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37734 DECL_PENDING_INLINE_P (fndecl) = 1;
37735 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37736 continue;
37738 else
37740 DECL_CONTEXT (fndecl) = current_namespace;
37741 pushdecl (fndecl);
37743 if (!block_scope)
37744 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37745 else
37746 block = begin_omp_structured_block ();
37747 if (cp)
37749 cp_parser_push_lexer_for_tokens (parser, cp);
37750 parser->lexer->in_pragma = true;
37752 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37754 if (!block_scope)
37755 finish_function (/*inline_p=*/false);
37756 else
37757 DECL_CONTEXT (fndecl) = current_function_decl;
37758 if (cp)
37759 cp_parser_pop_lexer (parser);
37760 goto fail;
37762 if (cp)
37763 cp_parser_pop_lexer (parser);
37764 if (!block_scope)
37765 finish_function (/*inline_p=*/false);
37766 else
37768 DECL_CONTEXT (fndecl) = current_function_decl;
37769 block = finish_omp_structured_block (block);
37770 if (TREE_CODE (block) == BIND_EXPR)
37771 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37772 else if (TREE_CODE (block) == STATEMENT_LIST)
37773 DECL_SAVED_TREE (fndecl) = block;
37774 if (processing_template_decl)
37775 add_decl_expr (fndecl);
37777 cp_check_omp_declare_reduction (fndecl);
37778 if (cp == NULL && types.length () > 1)
37779 cp = cp_token_cache_new (first_token,
37780 cp_lexer_peek_nth_token (parser->lexer, 2));
37781 if (errs != errorcount)
37782 break;
37785 cp_parser_require_pragma_eol (parser, pragma_tok);
37787 done:
37788 /* Free any declarators allocated. */
37789 obstack_free (&declarator_obstack, p);
37792 /* OpenMP 4.0
37793 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37794 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37795 initializer-clause[opt] new-line
37796 #pragma omp declare target new-line */
37798 static bool
37799 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37800 enum pragma_context context)
37802 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37804 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37805 const char *p = IDENTIFIER_POINTER (id);
37807 if (strcmp (p, "simd") == 0)
37809 cp_lexer_consume_token (parser->lexer);
37810 cp_parser_omp_declare_simd (parser, pragma_tok,
37811 context);
37812 return true;
37814 cp_ensure_no_omp_declare_simd (parser);
37815 if (strcmp (p, "reduction") == 0)
37817 cp_lexer_consume_token (parser->lexer);
37818 cp_parser_omp_declare_reduction (parser, pragma_tok,
37819 context);
37820 return false;
37822 if (!flag_openmp) /* flag_openmp_simd */
37824 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37825 return false;
37827 if (strcmp (p, "target") == 0)
37829 cp_lexer_consume_token (parser->lexer);
37830 cp_parser_omp_declare_target (parser, pragma_tok);
37831 return false;
37834 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37835 "or %<target%>");
37836 cp_parser_require_pragma_eol (parser, pragma_tok);
37837 return false;
37840 /* OpenMP 4.5:
37841 #pragma omp taskloop taskloop-clause[optseq] new-line
37842 for-loop
37844 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37845 for-loop */
37847 #define OMP_TASKLOOP_CLAUSE_MASK \
37848 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37863 static tree
37864 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37865 char *p_name, omp_clause_mask mask, tree *cclauses,
37866 bool *if_p)
37868 tree clauses, sb, ret;
37869 unsigned int save;
37870 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37872 strcat (p_name, " taskloop");
37873 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37875 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37877 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37878 const char *p = IDENTIFIER_POINTER (id);
37880 if (strcmp (p, "simd") == 0)
37882 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37883 if (cclauses == NULL)
37884 cclauses = cclauses_buf;
37886 cp_lexer_consume_token (parser->lexer);
37887 if (!flag_openmp) /* flag_openmp_simd */
37888 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37889 cclauses, if_p);
37890 sb = begin_omp_structured_block ();
37891 save = cp_parser_begin_omp_structured_block (parser);
37892 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37893 cclauses, if_p);
37894 cp_parser_end_omp_structured_block (parser, save);
37895 tree body = finish_omp_structured_block (sb);
37896 if (ret == NULL)
37897 return ret;
37898 ret = make_node (OMP_TASKLOOP);
37899 TREE_TYPE (ret) = void_type_node;
37900 OMP_FOR_BODY (ret) = body;
37901 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37902 SET_EXPR_LOCATION (ret, loc);
37903 add_stmt (ret);
37904 return ret;
37907 if (!flag_openmp) /* flag_openmp_simd */
37909 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37910 return NULL_TREE;
37913 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37914 cclauses == NULL);
37915 if (cclauses)
37917 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37918 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37921 sb = begin_omp_structured_block ();
37922 save = cp_parser_begin_omp_structured_block (parser);
37924 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37925 if_p);
37927 cp_parser_end_omp_structured_block (parser, save);
37928 add_stmt (finish_omp_structured_block (sb));
37930 return ret;
37934 /* OpenACC 2.0:
37935 # pragma acc routine oacc-routine-clause[optseq] new-line
37936 function-definition
37938 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37941 #define OACC_ROUTINE_CLAUSE_MASK \
37942 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37948 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37949 component, which must resolve to a declared namespace-scope
37950 function. The clauses are either processed directly (for a named
37951 function), or defered until the immediatley following declaration
37952 is parsed. */
37954 static void
37955 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37956 enum pragma_context context)
37958 gcc_checking_assert (context == pragma_external);
37959 /* The checking for "another pragma following this one" in the "no optional
37960 '( name )'" case makes sure that we dont re-enter. */
37961 gcc_checking_assert (parser->oacc_routine == NULL);
37963 cp_oacc_routine_data data;
37964 data.error_seen = false;
37965 data.fndecl_seen = false;
37966 data.tokens = vNULL;
37967 data.clauses = NULL_TREE;
37968 data.loc = pragma_tok->location;
37969 /* It is safe to take the address of a local variable; it will only be
37970 used while this scope is live. */
37971 parser->oacc_routine = &data;
37973 /* Look for optional '( name )'. */
37974 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37976 matching_parens parens;
37977 parens.consume_open (parser); /* '(' */
37979 /* We parse the name as an id-expression. If it resolves to
37980 anything other than a non-overloaded function at namespace
37981 scope, it's an error. */
37982 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37983 tree name = cp_parser_id_expression (parser,
37984 /*template_keyword_p=*/false,
37985 /*check_dependency_p=*/false,
37986 /*template_p=*/NULL,
37987 /*declarator_p=*/false,
37988 /*optional_p=*/false);
37989 tree decl = (identifier_p (name)
37990 ? cp_parser_lookup_name_simple (parser, name, name_loc)
37991 : name);
37992 if (name != error_mark_node && decl == error_mark_node)
37993 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
37995 if (decl == error_mark_node
37996 || !parens.require_close (parser))
37998 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37999 parser->oacc_routine = NULL;
38000 return;
38003 data.clauses
38004 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38005 "#pragma acc routine",
38006 cp_lexer_peek_token (parser->lexer));
38008 if (decl && is_overloaded_fn (decl)
38009 && (TREE_CODE (decl) != FUNCTION_DECL
38010 || DECL_FUNCTION_TEMPLATE_P (decl)))
38012 error_at (name_loc,
38013 "%<#pragma acc routine%> names a set of overloads");
38014 parser->oacc_routine = NULL;
38015 return;
38018 /* Perhaps we should use the same rule as declarations in different
38019 namespaces? */
38020 if (!DECL_NAMESPACE_SCOPE_P (decl))
38022 error_at (name_loc,
38023 "%qD does not refer to a namespace scope function", decl);
38024 parser->oacc_routine = NULL;
38025 return;
38028 if (TREE_CODE (decl) != FUNCTION_DECL)
38030 error_at (name_loc, "%qD does not refer to a function", decl);
38031 parser->oacc_routine = NULL;
38032 return;
38035 cp_finalize_oacc_routine (parser, decl, false);
38036 parser->oacc_routine = NULL;
38038 else /* No optional '( name )'. */
38040 /* Store away all pragma tokens. */
38041 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38042 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38043 cp_lexer_consume_token (parser->lexer);
38044 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38045 parser->oacc_routine->error_seen = true;
38046 cp_parser_require_pragma_eol (parser, pragma_tok);
38047 struct cp_token_cache *cp
38048 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38049 parser->oacc_routine->tokens.safe_push (cp);
38051 /* Emit a helpful diagnostic if there's another pragma following this
38052 one. */
38053 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38055 cp_ensure_no_oacc_routine (parser);
38056 data.tokens.release ();
38057 /* ..., and then just keep going. */
38058 return;
38061 /* We only have to consider the pragma_external case here. */
38062 cp_parser_declaration (parser);
38063 if (parser->oacc_routine
38064 && !parser->oacc_routine->fndecl_seen)
38065 cp_ensure_no_oacc_routine (parser);
38066 else
38067 parser->oacc_routine = NULL;
38068 data.tokens.release ();
38072 /* Finalize #pragma acc routine clauses after direct declarator has
38073 been parsed. */
38075 static tree
38076 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
38078 struct cp_token_cache *ce;
38079 cp_oacc_routine_data *data = parser->oacc_routine;
38081 if (!data->error_seen && data->fndecl_seen)
38083 error_at (data->loc,
38084 "%<#pragma acc routine%> not immediately followed by "
38085 "a single function declaration or definition");
38086 data->error_seen = true;
38088 if (data->error_seen)
38089 return attrs;
38091 gcc_checking_assert (data->tokens.length () == 1);
38092 ce = data->tokens[0];
38094 cp_parser_push_lexer_for_tokens (parser, ce);
38095 parser->lexer->in_pragma = true;
38096 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38098 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38099 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
38100 parser->oacc_routine->clauses
38101 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38102 "#pragma acc routine", pragma_tok);
38103 cp_parser_pop_lexer (parser);
38104 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38105 fndecl_seen. */
38107 return attrs;
38110 /* Apply any saved OpenACC routine clauses to a just-parsed
38111 declaration. */
38113 static void
38114 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
38116 if (__builtin_expect (parser->oacc_routine != NULL, 0))
38118 /* Keep going if we're in error reporting mode. */
38119 if (parser->oacc_routine->error_seen
38120 || fndecl == error_mark_node)
38121 return;
38123 if (parser->oacc_routine->fndecl_seen)
38125 error_at (parser->oacc_routine->loc,
38126 "%<#pragma acc routine%> not immediately followed by"
38127 " a single function declaration or definition");
38128 parser->oacc_routine = NULL;
38129 return;
38131 if (TREE_CODE (fndecl) != FUNCTION_DECL)
38133 cp_ensure_no_oacc_routine (parser);
38134 return;
38137 if (oacc_get_fn_attrib (fndecl))
38139 error_at (parser->oacc_routine->loc,
38140 "%<#pragma acc routine%> already applied to %qD", fndecl);
38141 parser->oacc_routine = NULL;
38142 return;
38145 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
38147 error_at (parser->oacc_routine->loc,
38148 TREE_USED (fndecl)
38149 ? G_("%<#pragma acc routine%> must be applied before use")
38150 : G_("%<#pragma acc routine%> must be applied before "
38151 "definition"));
38152 parser->oacc_routine = NULL;
38153 return;
38156 /* Process the routine's dimension clauses. */
38157 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38158 oacc_replace_fn_attrib (fndecl, dims);
38160 /* Add an "omp declare target" attribute. */
38161 DECL_ATTRIBUTES (fndecl)
38162 = tree_cons (get_identifier ("omp declare target"),
38163 NULL_TREE, DECL_ATTRIBUTES (fndecl));
38165 /* Don't unset parser->oacc_routine here: we may still need it to
38166 diagnose wrong usage. But, remember that we've used this "#pragma acc
38167 routine". */
38168 parser->oacc_routine->fndecl_seen = true;
38172 /* Main entry point to OpenMP statement pragmas. */
38174 static void
38175 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38177 tree stmt;
38178 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38179 omp_clause_mask mask (0);
38181 switch (cp_parser_pragma_kind (pragma_tok))
38183 case PRAGMA_OACC_ATOMIC:
38184 cp_parser_omp_atomic (parser, pragma_tok);
38185 return;
38186 case PRAGMA_OACC_CACHE:
38187 stmt = cp_parser_oacc_cache (parser, pragma_tok);
38188 break;
38189 case PRAGMA_OACC_DATA:
38190 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38191 break;
38192 case PRAGMA_OACC_ENTER_DATA:
38193 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38194 break;
38195 case PRAGMA_OACC_EXIT_DATA:
38196 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38197 break;
38198 case PRAGMA_OACC_HOST_DATA:
38199 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38200 break;
38201 case PRAGMA_OACC_KERNELS:
38202 case PRAGMA_OACC_PARALLEL:
38203 strcpy (p_name, "#pragma acc");
38204 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38205 if_p);
38206 break;
38207 case PRAGMA_OACC_LOOP:
38208 strcpy (p_name, "#pragma acc");
38209 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38210 if_p);
38211 break;
38212 case PRAGMA_OACC_UPDATE:
38213 stmt = cp_parser_oacc_update (parser, pragma_tok);
38214 break;
38215 case PRAGMA_OACC_WAIT:
38216 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38217 break;
38218 case PRAGMA_OMP_ATOMIC:
38219 cp_parser_omp_atomic (parser, pragma_tok);
38220 return;
38221 case PRAGMA_OMP_CRITICAL:
38222 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38223 break;
38224 case PRAGMA_OMP_DISTRIBUTE:
38225 strcpy (p_name, "#pragma omp");
38226 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38227 if_p);
38228 break;
38229 case PRAGMA_OMP_FOR:
38230 strcpy (p_name, "#pragma omp");
38231 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38232 if_p);
38233 break;
38234 case PRAGMA_OMP_MASTER:
38235 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38236 break;
38237 case PRAGMA_OMP_PARALLEL:
38238 strcpy (p_name, "#pragma omp");
38239 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38240 if_p);
38241 break;
38242 case PRAGMA_OMP_SECTIONS:
38243 strcpy (p_name, "#pragma omp");
38244 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38245 break;
38246 case PRAGMA_OMP_SIMD:
38247 strcpy (p_name, "#pragma omp");
38248 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38249 if_p);
38250 break;
38251 case PRAGMA_OMP_SINGLE:
38252 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38253 break;
38254 case PRAGMA_OMP_TASK:
38255 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38256 break;
38257 case PRAGMA_OMP_TASKGROUP:
38258 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38259 break;
38260 case PRAGMA_OMP_TASKLOOP:
38261 strcpy (p_name, "#pragma omp");
38262 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38263 if_p);
38264 break;
38265 case PRAGMA_OMP_TEAMS:
38266 strcpy (p_name, "#pragma omp");
38267 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38268 if_p);
38269 break;
38270 default:
38271 gcc_unreachable ();
38274 protected_set_expr_location (stmt, pragma_tok->location);
38277 /* Transactional Memory parsing routines. */
38279 /* Parse a transaction attribute.
38281 txn-attribute:
38282 attribute
38283 [ [ identifier ] ]
38285 We use this instead of cp_parser_attributes_opt for transactions to avoid
38286 the pedwarn in C++98 mode. */
38288 static tree
38289 cp_parser_txn_attribute_opt (cp_parser *parser)
38291 cp_token *token;
38292 tree attr_name, attr = NULL;
38294 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38295 return cp_parser_attributes_opt (parser);
38297 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38298 return NULL_TREE;
38299 cp_lexer_consume_token (parser->lexer);
38300 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38301 goto error1;
38303 token = cp_lexer_peek_token (parser->lexer);
38304 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38306 token = cp_lexer_consume_token (parser->lexer);
38308 attr_name = (token->type == CPP_KEYWORD
38309 /* For keywords, use the canonical spelling,
38310 not the parsed identifier. */
38311 ? ridpointers[(int) token->keyword]
38312 : token->u.value);
38313 attr = build_tree_list (attr_name, NULL_TREE);
38315 else
38316 cp_parser_error (parser, "expected identifier");
38318 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38319 error1:
38320 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38321 return attr;
38324 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38326 transaction-statement:
38327 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38328 compound-statement
38329 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38332 static tree
38333 cp_parser_transaction (cp_parser *parser, cp_token *token)
38335 unsigned char old_in = parser->in_transaction;
38336 unsigned char this_in = 1, new_in;
38337 enum rid keyword = token->keyword;
38338 tree stmt, attrs, noex;
38340 cp_lexer_consume_token (parser->lexer);
38342 if (keyword == RID_TRANSACTION_RELAXED
38343 || keyword == RID_SYNCHRONIZED)
38344 this_in |= TM_STMT_ATTR_RELAXED;
38345 else
38347 attrs = cp_parser_txn_attribute_opt (parser);
38348 if (attrs)
38349 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38352 /* Parse a noexcept specification. */
38353 if (keyword == RID_ATOMIC_NOEXCEPT)
38354 noex = boolean_true_node;
38355 else if (keyword == RID_ATOMIC_CANCEL)
38357 /* cancel-and-throw is unimplemented. */
38358 sorry ("atomic_cancel");
38359 noex = NULL_TREE;
38361 else
38362 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38364 /* Keep track if we're in the lexical scope of an outer transaction. */
38365 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38367 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38369 parser->in_transaction = new_in;
38370 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38371 parser->in_transaction = old_in;
38373 finish_transaction_stmt (stmt, NULL, this_in, noex);
38375 return stmt;
38378 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38380 transaction-expression:
38381 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38382 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38385 static tree
38386 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38388 unsigned char old_in = parser->in_transaction;
38389 unsigned char this_in = 1;
38390 cp_token *token;
38391 tree expr, noex;
38392 bool noex_expr;
38393 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38395 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38396 || keyword == RID_TRANSACTION_RELAXED);
38398 if (!flag_tm)
38399 error_at (loc,
38400 keyword == RID_TRANSACTION_RELAXED
38401 ? G_("%<__transaction_relaxed%> without transactional memory "
38402 "support enabled")
38403 : G_("%<__transaction_atomic%> without transactional memory "
38404 "support enabled"));
38406 token = cp_parser_require_keyword (parser, keyword,
38407 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38408 : RT_TRANSACTION_RELAXED));
38409 gcc_assert (token != NULL);
38411 if (keyword == RID_TRANSACTION_RELAXED)
38412 this_in |= TM_STMT_ATTR_RELAXED;
38414 /* Set this early. This might mean that we allow transaction_cancel in
38415 an expression that we find out later actually has to be a constexpr.
38416 However, we expect that cxx_constant_value will be able to deal with
38417 this; also, if the noexcept has no constexpr, then what we parse next
38418 really is a transaction's body. */
38419 parser->in_transaction = this_in;
38421 /* Parse a noexcept specification. */
38422 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38423 true);
38425 if (!noex || !noex_expr
38426 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38428 matching_parens parens;
38429 parens.require_open (parser);
38431 expr = cp_parser_expression (parser);
38432 expr = finish_parenthesized_expr (expr);
38434 parens.require_close (parser);
38436 else
38438 /* The only expression that is available got parsed for the noexcept
38439 already. noexcept is true then. */
38440 expr = noex;
38441 noex = boolean_true_node;
38444 expr = build_transaction_expr (token->location, expr, this_in, noex);
38445 parser->in_transaction = old_in;
38447 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38448 return error_mark_node;
38450 return (flag_tm ? expr : error_mark_node);
38453 /* Parse a function-transaction-block.
38455 function-transaction-block:
38456 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38457 function-body
38458 __transaction_atomic txn-attribute[opt] function-try-block
38459 __transaction_relaxed ctor-initializer[opt] function-body
38460 __transaction_relaxed function-try-block
38463 static void
38464 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38466 unsigned char old_in = parser->in_transaction;
38467 unsigned char new_in = 1;
38468 tree compound_stmt, stmt, attrs;
38469 cp_token *token;
38471 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38472 || keyword == RID_TRANSACTION_RELAXED);
38473 token = cp_parser_require_keyword (parser, keyword,
38474 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38475 : RT_TRANSACTION_RELAXED));
38476 gcc_assert (token != NULL);
38478 if (keyword == RID_TRANSACTION_RELAXED)
38479 new_in |= TM_STMT_ATTR_RELAXED;
38480 else
38482 attrs = cp_parser_txn_attribute_opt (parser);
38483 if (attrs)
38484 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38487 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38489 parser->in_transaction = new_in;
38491 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38492 cp_parser_function_try_block (parser);
38493 else
38494 cp_parser_ctor_initializer_opt_and_function_body
38495 (parser, /*in_function_try_block=*/false);
38497 parser->in_transaction = old_in;
38499 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38502 /* Parse a __transaction_cancel statement.
38504 cancel-statement:
38505 __transaction_cancel txn-attribute[opt] ;
38506 __transaction_cancel txn-attribute[opt] throw-expression ;
38508 ??? Cancel and throw is not yet implemented. */
38510 static tree
38511 cp_parser_transaction_cancel (cp_parser *parser)
38513 cp_token *token;
38514 bool is_outer = false;
38515 tree stmt, attrs;
38517 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38518 RT_TRANSACTION_CANCEL);
38519 gcc_assert (token != NULL);
38521 attrs = cp_parser_txn_attribute_opt (parser);
38522 if (attrs)
38523 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38525 /* ??? Parse cancel-and-throw here. */
38527 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38529 if (!flag_tm)
38531 error_at (token->location, "%<__transaction_cancel%> without "
38532 "transactional memory support enabled");
38533 return error_mark_node;
38535 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38537 error_at (token->location, "%<__transaction_cancel%> within a "
38538 "%<__transaction_relaxed%>");
38539 return error_mark_node;
38541 else if (is_outer)
38543 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38544 && !is_tm_may_cancel_outer (current_function_decl))
38546 error_at (token->location, "outer %<__transaction_cancel%> not "
38547 "within outer %<__transaction_atomic%>");
38548 error_at (token->location,
38549 " or a %<transaction_may_cancel_outer%> function");
38550 return error_mark_node;
38553 else if (parser->in_transaction == 0)
38555 error_at (token->location, "%<__transaction_cancel%> not within "
38556 "%<__transaction_atomic%>");
38557 return error_mark_node;
38560 stmt = build_tm_abort_call (token->location, is_outer);
38561 add_stmt (stmt);
38563 return stmt;
38566 /* The parser. */
38568 static GTY (()) cp_parser *the_parser;
38571 /* Special handling for the first token or line in the file. The first
38572 thing in the file might be #pragma GCC pch_preprocess, which loads a
38573 PCH file, which is a GC collection point. So we need to handle this
38574 first pragma without benefit of an existing lexer structure.
38576 Always returns one token to the caller in *FIRST_TOKEN. This is
38577 either the true first token of the file, or the first token after
38578 the initial pragma. */
38580 static void
38581 cp_parser_initial_pragma (cp_token *first_token)
38583 tree name = NULL;
38585 cp_lexer_get_preprocessor_token (NULL, first_token);
38586 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38587 return;
38589 cp_lexer_get_preprocessor_token (NULL, first_token);
38590 if (first_token->type == CPP_STRING)
38592 name = first_token->u.value;
38594 cp_lexer_get_preprocessor_token (NULL, first_token);
38595 if (first_token->type != CPP_PRAGMA_EOL)
38596 error_at (first_token->location,
38597 "junk at end of %<#pragma GCC pch_preprocess%>");
38599 else
38600 error_at (first_token->location, "expected string literal");
38602 /* Skip to the end of the pragma. */
38603 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38604 cp_lexer_get_preprocessor_token (NULL, first_token);
38606 /* Now actually load the PCH file. */
38607 if (name)
38608 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38610 /* Read one more token to return to our caller. We have to do this
38611 after reading the PCH file in, since its pointers have to be
38612 live. */
38613 cp_lexer_get_preprocessor_token (NULL, first_token);
38616 /* Parse a pragma GCC ivdep. */
38618 static bool
38619 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
38621 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38622 return true;
38625 /* Parse a pragma GCC unroll. */
38627 static unsigned short
38628 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
38630 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38631 tree expr = cp_parser_constant_expression (parser);
38632 unsigned short unroll;
38633 expr = maybe_constant_value (expr);
38634 HOST_WIDE_INT lunroll = 0;
38635 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
38636 || TREE_CODE (expr) != INTEGER_CST
38637 || (lunroll = tree_to_shwi (expr)) < 0
38638 || lunroll >= USHRT_MAX)
38640 error_at (location, "%<#pragma GCC unroll%> requires an"
38641 " assignment-expression that evaluates to a non-negative"
38642 " integral constant less than %u", USHRT_MAX);
38643 unroll = 0;
38645 else
38647 unroll = (unsigned short)lunroll;
38648 if (unroll == 0)
38649 unroll = 1;
38651 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38652 return unroll;
38655 /* Normal parsing of a pragma token. Here we can (and must) use the
38656 regular lexer. */
38658 static bool
38659 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38661 cp_token *pragma_tok;
38662 unsigned int id;
38663 tree stmt;
38664 bool ret;
38666 pragma_tok = cp_lexer_consume_token (parser->lexer);
38667 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38668 parser->lexer->in_pragma = true;
38670 id = cp_parser_pragma_kind (pragma_tok);
38671 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38672 cp_ensure_no_omp_declare_simd (parser);
38673 switch (id)
38675 case PRAGMA_GCC_PCH_PREPROCESS:
38676 error_at (pragma_tok->location,
38677 "%<#pragma GCC pch_preprocess%> must be first");
38678 break;
38680 case PRAGMA_OMP_BARRIER:
38681 switch (context)
38683 case pragma_compound:
38684 cp_parser_omp_barrier (parser, pragma_tok);
38685 return false;
38686 case pragma_stmt:
38687 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38688 "used in compound statements", "omp barrier");
38689 break;
38690 default:
38691 goto bad_stmt;
38693 break;
38695 case PRAGMA_OMP_FLUSH:
38696 switch (context)
38698 case pragma_compound:
38699 cp_parser_omp_flush (parser, pragma_tok);
38700 return false;
38701 case pragma_stmt:
38702 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38703 "used in compound statements", "omp flush");
38704 break;
38705 default:
38706 goto bad_stmt;
38708 break;
38710 case PRAGMA_OMP_TASKWAIT:
38711 switch (context)
38713 case pragma_compound:
38714 cp_parser_omp_taskwait (parser, pragma_tok);
38715 return false;
38716 case pragma_stmt:
38717 error_at (pragma_tok->location,
38718 "%<#pragma %s%> may only be used in compound statements",
38719 "omp taskwait");
38720 break;
38721 default:
38722 goto bad_stmt;
38724 break;
38726 case PRAGMA_OMP_TASKYIELD:
38727 switch (context)
38729 case pragma_compound:
38730 cp_parser_omp_taskyield (parser, pragma_tok);
38731 return false;
38732 case pragma_stmt:
38733 error_at (pragma_tok->location,
38734 "%<#pragma %s%> may only be used in compound statements",
38735 "omp taskyield");
38736 break;
38737 default:
38738 goto bad_stmt;
38740 break;
38742 case PRAGMA_OMP_CANCEL:
38743 switch (context)
38745 case pragma_compound:
38746 cp_parser_omp_cancel (parser, pragma_tok);
38747 return false;
38748 case pragma_stmt:
38749 error_at (pragma_tok->location,
38750 "%<#pragma %s%> may only be used in compound statements",
38751 "omp cancel");
38752 break;
38753 default:
38754 goto bad_stmt;
38756 break;
38758 case PRAGMA_OMP_CANCELLATION_POINT:
38759 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38760 return false;
38762 case PRAGMA_OMP_THREADPRIVATE:
38763 cp_parser_omp_threadprivate (parser, pragma_tok);
38764 return false;
38766 case PRAGMA_OMP_DECLARE:
38767 return cp_parser_omp_declare (parser, pragma_tok, context);
38769 case PRAGMA_OACC_DECLARE:
38770 cp_parser_oacc_declare (parser, pragma_tok);
38771 return false;
38773 case PRAGMA_OACC_ENTER_DATA:
38774 if (context == pragma_stmt)
38776 error_at (pragma_tok->location,
38777 "%<#pragma %s%> may only be used in compound statements",
38778 "acc enter data");
38779 break;
38781 else if (context != pragma_compound)
38782 goto bad_stmt;
38783 cp_parser_omp_construct (parser, pragma_tok, if_p);
38784 return true;
38786 case PRAGMA_OACC_EXIT_DATA:
38787 if (context == pragma_stmt)
38789 error_at (pragma_tok->location,
38790 "%<#pragma %s%> may only be used in compound statements",
38791 "acc exit data");
38792 break;
38794 else if (context != pragma_compound)
38795 goto bad_stmt;
38796 cp_parser_omp_construct (parser, pragma_tok, if_p);
38797 return true;
38799 case PRAGMA_OACC_ROUTINE:
38800 if (context != pragma_external)
38802 error_at (pragma_tok->location,
38803 "%<#pragma acc routine%> must be at file scope");
38804 break;
38806 cp_parser_oacc_routine (parser, pragma_tok, context);
38807 return false;
38809 case PRAGMA_OACC_UPDATE:
38810 if (context == pragma_stmt)
38812 error_at (pragma_tok->location,
38813 "%<#pragma %s%> may only be used in compound statements",
38814 "acc update");
38815 break;
38817 else if (context != pragma_compound)
38818 goto bad_stmt;
38819 cp_parser_omp_construct (parser, pragma_tok, if_p);
38820 return true;
38822 case PRAGMA_OACC_WAIT:
38823 if (context == pragma_stmt)
38825 error_at (pragma_tok->location,
38826 "%<#pragma %s%> may only be used in compound statements",
38827 "acc wait");
38828 break;
38830 else if (context != pragma_compound)
38831 goto bad_stmt;
38832 cp_parser_omp_construct (parser, pragma_tok, if_p);
38833 return true;
38835 case PRAGMA_OACC_ATOMIC:
38836 case PRAGMA_OACC_CACHE:
38837 case PRAGMA_OACC_DATA:
38838 case PRAGMA_OACC_HOST_DATA:
38839 case PRAGMA_OACC_KERNELS:
38840 case PRAGMA_OACC_PARALLEL:
38841 case PRAGMA_OACC_LOOP:
38842 case PRAGMA_OMP_ATOMIC:
38843 case PRAGMA_OMP_CRITICAL:
38844 case PRAGMA_OMP_DISTRIBUTE:
38845 case PRAGMA_OMP_FOR:
38846 case PRAGMA_OMP_MASTER:
38847 case PRAGMA_OMP_PARALLEL:
38848 case PRAGMA_OMP_SECTIONS:
38849 case PRAGMA_OMP_SIMD:
38850 case PRAGMA_OMP_SINGLE:
38851 case PRAGMA_OMP_TASK:
38852 case PRAGMA_OMP_TASKGROUP:
38853 case PRAGMA_OMP_TASKLOOP:
38854 case PRAGMA_OMP_TEAMS:
38855 if (context != pragma_stmt && context != pragma_compound)
38856 goto bad_stmt;
38857 stmt = push_omp_privatization_clauses (false);
38858 cp_parser_omp_construct (parser, pragma_tok, if_p);
38859 pop_omp_privatization_clauses (stmt);
38860 return true;
38862 case PRAGMA_OMP_ORDERED:
38863 if (context != pragma_stmt && context != pragma_compound)
38864 goto bad_stmt;
38865 stmt = push_omp_privatization_clauses (false);
38866 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38867 pop_omp_privatization_clauses (stmt);
38868 return ret;
38870 case PRAGMA_OMP_TARGET:
38871 if (context != pragma_stmt && context != pragma_compound)
38872 goto bad_stmt;
38873 stmt = push_omp_privatization_clauses (false);
38874 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38875 pop_omp_privatization_clauses (stmt);
38876 return ret;
38878 case PRAGMA_OMP_END_DECLARE_TARGET:
38879 cp_parser_omp_end_declare_target (parser, pragma_tok);
38880 return false;
38882 case PRAGMA_OMP_SECTION:
38883 error_at (pragma_tok->location,
38884 "%<#pragma omp section%> may only be used in "
38885 "%<#pragma omp sections%> construct");
38886 break;
38888 case PRAGMA_IVDEP:
38890 if (context == pragma_external)
38892 error_at (pragma_tok->location,
38893 "%<#pragma GCC ivdep%> must be inside a function");
38894 break;
38896 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
38897 unsigned short unroll;
38898 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38899 if (tok->type == CPP_PRAGMA
38900 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
38902 tok = cp_lexer_consume_token (parser->lexer);
38903 unroll = cp_parser_pragma_unroll (parser, tok);
38904 tok = cp_lexer_peek_token (the_parser->lexer);
38906 else
38907 unroll = 0;
38908 if (tok->type != CPP_KEYWORD
38909 || (tok->keyword != RID_FOR
38910 && tok->keyword != RID_WHILE
38911 && tok->keyword != RID_DO))
38913 cp_parser_error (parser, "for, while or do statement expected");
38914 return false;
38916 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
38917 return true;
38920 case PRAGMA_UNROLL:
38922 if (context == pragma_external)
38924 error_at (pragma_tok->location,
38925 "%<#pragma GCC unroll%> must be inside a function");
38926 break;
38928 const unsigned short unroll
38929 = cp_parser_pragma_unroll (parser, pragma_tok);
38930 bool ivdep;
38931 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38932 if (tok->type == CPP_PRAGMA
38933 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
38935 tok = cp_lexer_consume_token (parser->lexer);
38936 ivdep = cp_parser_pragma_ivdep (parser, tok);
38937 tok = cp_lexer_peek_token (the_parser->lexer);
38939 else
38940 ivdep = false;
38941 if (tok->type != CPP_KEYWORD
38942 || (tok->keyword != RID_FOR
38943 && tok->keyword != RID_WHILE
38944 && tok->keyword != RID_DO))
38946 cp_parser_error (parser, "for, while or do statement expected");
38947 return false;
38949 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
38950 return true;
38953 default:
38954 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38955 c_invoke_pragma_handler (id);
38956 break;
38958 bad_stmt:
38959 cp_parser_error (parser, "expected declaration specifiers");
38960 break;
38963 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38964 return false;
38967 /* The interface the pragma parsers have to the lexer. */
38969 enum cpp_ttype
38970 pragma_lex (tree *value, location_t *loc)
38972 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38973 enum cpp_ttype ret = tok->type;
38975 *value = tok->u.value;
38976 if (loc)
38977 *loc = tok->location;
38979 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38980 ret = CPP_EOF;
38981 else if (ret == CPP_STRING)
38982 *value = cp_parser_string_literal (the_parser, false, false);
38983 else
38985 if (ret == CPP_KEYWORD)
38986 ret = CPP_NAME;
38987 cp_lexer_consume_token (the_parser->lexer);
38990 return ret;
38994 /* External interface. */
38996 /* Parse one entire translation unit. */
38998 void
38999 c_parse_file (void)
39001 static bool already_called = false;
39003 if (already_called)
39004 fatal_error (input_location,
39005 "inter-module optimizations not implemented for C++");
39006 already_called = true;
39008 the_parser = cp_parser_new ();
39009 push_deferring_access_checks (flag_access_control
39010 ? dk_no_deferred : dk_no_check);
39011 cp_parser_translation_unit (the_parser);
39012 the_parser = NULL;
39015 /* Create an identifier for a generic parameter type (a synthesized
39016 template parameter implied by `auto' or a concept identifier). */
39018 static GTY(()) int generic_parm_count;
39019 static tree
39020 make_generic_type_name ()
39022 char buf[32];
39023 sprintf (buf, "auto:%d", ++generic_parm_count);
39024 return get_identifier (buf);
39027 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39028 (creating a new template parameter list if necessary). Returns the newly
39029 created template type parm. */
39031 static tree
39032 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
39034 gcc_assert (current_binding_level->kind == sk_function_parms);
39036 /* Before committing to modifying any scope, if we're in an
39037 implicit template scope, and we're trying to synthesize a
39038 constrained parameter, try to find a previous parameter with
39039 the same name. This is the same-type rule for abbreviated
39040 function templates.
39042 NOTE: We can generate implicit parameters when tentatively
39043 parsing a nested name specifier, only to reject that parse
39044 later. However, matching the same template-id as part of a
39045 direct-declarator should generate an identical template
39046 parameter, so this rule will merge them. */
39047 if (parser->implicit_template_scope && constr)
39049 tree t = parser->implicit_template_parms;
39050 while (t)
39052 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39054 tree d = TREE_VALUE (t);
39055 if (TREE_CODE (d) == PARM_DECL)
39056 /* Return the TEMPLATE_PARM_INDEX. */
39057 d = DECL_INITIAL (d);
39058 return d;
39060 t = TREE_CHAIN (t);
39064 /* We are either continuing a function template that already contains implicit
39065 template parameters, creating a new fully-implicit function template, or
39066 extending an existing explicit function template with implicit template
39067 parameters. */
39069 cp_binding_level *const entry_scope = current_binding_level;
39071 bool become_template = false;
39072 cp_binding_level *parent_scope = 0;
39074 if (parser->implicit_template_scope)
39076 gcc_assert (parser->implicit_template_parms);
39078 current_binding_level = parser->implicit_template_scope;
39080 else
39082 /* Roll back to the existing template parameter scope (in the case of
39083 extending an explicit function template) or introduce a new template
39084 parameter scope ahead of the function parameter scope (or class scope
39085 in the case of out-of-line member definitions). The function scope is
39086 added back after template parameter synthesis below. */
39088 cp_binding_level *scope = entry_scope;
39090 while (scope->kind == sk_function_parms)
39092 parent_scope = scope;
39093 scope = scope->level_chain;
39095 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39097 /* If not defining a class, then any class scope is a scope level in
39098 an out-of-line member definition. In this case simply wind back
39099 beyond the first such scope to inject the template parameter list.
39100 Otherwise wind back to the class being defined. The latter can
39101 occur in class member friend declarations such as:
39103 class A {
39104 void foo (auto);
39106 class B {
39107 friend void A::foo (auto);
39110 The template parameter list synthesized for the friend declaration
39111 must be injected in the scope of 'B'. This can also occur in
39112 erroneous cases such as:
39114 struct A {
39115 struct B {
39116 void foo (auto);
39118 void B::foo (auto) {}
39121 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39122 but, nevertheless, the template parameter list synthesized for the
39123 declarator should be injected into the scope of 'A' as if the
39124 ill-formed template was specified explicitly. */
39126 while (scope->kind == sk_class && !scope->defining_class_p)
39128 parent_scope = scope;
39129 scope = scope->level_chain;
39133 current_binding_level = scope;
39135 if (scope->kind != sk_template_parms
39136 || !function_being_declared_is_template_p (parser))
39138 /* Introduce a new template parameter list for implicit template
39139 parameters. */
39141 become_template = true;
39143 parser->implicit_template_scope
39144 = begin_scope (sk_template_parms, NULL);
39146 ++processing_template_decl;
39148 parser->fully_implicit_function_template_p = true;
39149 ++parser->num_template_parameter_lists;
39151 else
39153 /* Synthesize implicit template parameters at the end of the explicit
39154 template parameter list. */
39156 gcc_assert (current_template_parms);
39158 parser->implicit_template_scope = scope;
39160 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39161 parser->implicit_template_parms
39162 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39166 /* Synthesize a new template parameter and track the current template
39167 parameter chain with implicit_template_parms. */
39169 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39170 tree synth_id = make_generic_type_name ();
39171 tree synth_tmpl_parm;
39172 bool non_type = false;
39174 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39175 synth_tmpl_parm
39176 = finish_template_type_parm (class_type_node, synth_id);
39177 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39178 synth_tmpl_parm
39179 = finish_constrained_template_template_parm (proto, synth_id);
39180 else
39182 synth_tmpl_parm = copy_decl (proto);
39183 DECL_NAME (synth_tmpl_parm) = synth_id;
39184 non_type = true;
39187 // Attach the constraint to the parm before processing.
39188 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39189 TREE_TYPE (node) = constr;
39190 tree new_parm
39191 = process_template_parm (parser->implicit_template_parms,
39192 input_location,
39193 node,
39194 /*non_type=*/non_type,
39195 /*param_pack=*/false);
39197 // Chain the new parameter to the list of implicit parameters.
39198 if (parser->implicit_template_parms)
39199 parser->implicit_template_parms
39200 = TREE_CHAIN (parser->implicit_template_parms);
39201 else
39202 parser->implicit_template_parms = new_parm;
39204 tree new_decl = get_local_decls ();
39205 if (non_type)
39206 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39207 new_decl = DECL_INITIAL (new_decl);
39209 /* If creating a fully implicit function template, start the new implicit
39210 template parameter list with this synthesized type, otherwise grow the
39211 current template parameter list. */
39213 if (become_template)
39215 parent_scope->level_chain = current_binding_level;
39217 tree new_parms = make_tree_vec (1);
39218 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39219 current_template_parms = tree_cons (size_int (processing_template_decl),
39220 new_parms, current_template_parms);
39222 else
39224 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39225 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39226 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39227 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39230 // If the new parameter was constrained, we need to add that to the
39231 // constraints in the template parameter list.
39232 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39234 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39235 reqs = conjoin_constraints (reqs, req);
39236 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39239 current_binding_level = entry_scope;
39241 return new_decl;
39244 /* Finish the declaration of a fully implicit function template. Such a
39245 template has no explicit template parameter list so has not been through the
39246 normal template head and tail processing. synthesize_implicit_template_parm
39247 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39248 provided if the declaration is a class member such that its template
39249 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39250 form is returned. Otherwise NULL_TREE is returned. */
39252 static tree
39253 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39255 gcc_assert (parser->fully_implicit_function_template_p);
39257 if (member_decl_opt && member_decl_opt != error_mark_node
39258 && DECL_VIRTUAL_P (member_decl_opt))
39260 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39261 "implicit templates may not be %<virtual%>");
39262 DECL_VIRTUAL_P (member_decl_opt) = false;
39265 if (member_decl_opt)
39266 member_decl_opt = finish_member_template_decl (member_decl_opt);
39267 end_template_decl ();
39269 parser->fully_implicit_function_template_p = false;
39270 parser->implicit_template_parms = 0;
39271 parser->implicit_template_scope = 0;
39272 --parser->num_template_parameter_lists;
39274 return member_decl_opt;
39277 /* Like finish_fully_implicit_template, but to be used in error
39278 recovery, rearranging scopes so that we restore the state we had
39279 before synthesize_implicit_template_parm inserted the implement
39280 template parms scope. */
39282 static void
39283 abort_fully_implicit_template (cp_parser *parser)
39285 cp_binding_level *return_to_scope = current_binding_level;
39287 if (parser->implicit_template_scope
39288 && return_to_scope != parser->implicit_template_scope)
39290 cp_binding_level *child = return_to_scope;
39291 for (cp_binding_level *scope = child->level_chain;
39292 scope != parser->implicit_template_scope;
39293 scope = child->level_chain)
39294 child = scope;
39295 child->level_chain = parser->implicit_template_scope->level_chain;
39296 parser->implicit_template_scope->level_chain = return_to_scope;
39297 current_binding_level = parser->implicit_template_scope;
39299 else
39300 return_to_scope = return_to_scope->level_chain;
39302 finish_fully_implicit_template (parser, NULL);
39304 gcc_assert (current_binding_level == return_to_scope);
39307 /* Helper function for diagnostics that have complained about things
39308 being used with 'extern "C"' linkage.
39310 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39312 void
39313 maybe_show_extern_c_location (void)
39315 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39316 inform (the_parser->innermost_linkage_specification_location,
39317 "%<extern \"C\"%> linkage started here");
39320 #include "gt-cp-parser.h"